Ejemplo n.º 1
0
 private int GetLastPuzzleId()
 {
     using (var db = new PuzzleContext())
     {
         return(db.PuzzleCells.OrderByDescending(p => p.puzzleId).FirstOrDefault().puzzleId);
     }
 }
Ejemplo n.º 2
0
 private List <Puzzle> GetById(int id)
 {
     using (var db = new PuzzleContext())
     {
         return(db.PuzzleCells.Where(p => p.puzzleId == id).ToList());
     }
 }
Ejemplo n.º 3
0
        public void Regress1()
        {
            var context = new PuzzleContext();

            var cvec1 = context.FromPath("pics/luxmarket_tshirt01.jpg");
            var cvec2 = context.FromPath("pics/luxmarket_tshirt01_black.jpg");
            var cvec3 = context.FromPath("pics/luxmarket_tshirt01_sal.jpg");
            var cvec4 = context.FromPath("pics/luxmarket_tshirt01_sheum.jpg");
            var cvec5 = context.FromPath("pics/duck.gif");
            var cvec6 = context.FromPath("pics/pic-a-0.jpg");

            var cvec1c = cvec1.ToCompressedPuzzle();
            var cvec1u = cvec1c.ToUncompressedPuzzle();

            Assert.AreEqual(cvec1,  cvec1c);
            Assert.AreEqual(cvec1,  cvec1u);
            Assert.AreEqual(cvec1c, cvec1u);

            Assert.AreEqual(786947673,   cvec1.GetHashCode());
            Assert.AreEqual(-1292227431, cvec2.GetHashCode());
            Assert.AreEqual(556554137,   cvec3.GetHashCode());
            Assert.AreEqual(-1389008455, cvec4.GetHashCode());
            Assert.AreEqual(-304630951,  cvec5.GetHashCode());
            Assert.AreEqual(739909369,   cvec6.GetHashCode());
        }
Ejemplo n.º 4
0
        public void Regress2()
        {
            var context = new PuzzleContext();

            var cvec1 = context.FromPath("pics/luxmarket_tshirt01.jpg");
            var cvec2 = context.FromPath("pics/luxmarket_tshirt01_black.jpg");
            var cvec3 = context.FromPath("pics/luxmarket_tshirt01_sal.jpg");
            var cvec4 = context.FromPath("pics/luxmarket_tshirt01_sheum.jpg");
            var cvec5 = context.FromPath("pics/duck.gif");
            var cvec6 = context.FromPath("pics/pic-a-0.jpg");

            var d1 = cvec2.GetDistanceFrom(cvec1);
            var d2 = cvec1.GetDistanceFrom(cvec2);
            var d3 = cvec1.GetDistanceFrom(cvec3);
            var d4 = cvec1.GetDistanceFrom(cvec4);
            var d5 = cvec1.GetDistanceFrom(cvec5);
            var d6 = cvec1.GetDistanceFrom(cvec6);

            Assert.AreEqual((int)d1, (int)d2);

            Assert.IsFalse(d1 > IPuzzle.DefaultSimilarityThreshold);
            Assert.IsFalse(d3 > IPuzzle.DefaultSimilarityThreshold);
            Assert.IsFalse(d4 > IPuzzle.DefaultSimilarityThreshold);
            Assert.IsFalse(d5 < IPuzzle.DefaultSimilarityThreshold);
            Assert.IsFalse(d6 < IPuzzle.DefaultSimilarityThreshold);
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            var context = new PuzzleContext();

            var cvec1 = context.FromPath("pics/luxmarket_tshirt01.jpg");
            var cvec2 = context.FromPath("pics/luxmarket_tshirt01_black.jpg");
            var cvec3 = context.FromPath("pics/luxmarket_tshirt01_sal.jpg");
            var cvec4 = context.FromPath("pics/luxmarket_tshirt01_sheum.jpg");
            var cvec5 = context.FromPath("pics/duck.gif");
            var cvec6 = context.FromPath("pics/pic-a-0.jpg");

            var cvec1c = cvec1.ToCompressedPuzzle();
            var cvec1u = cvec1c.ToUncompressedPuzzle();

            Debug.Assert(cvec1.Equals(cvec1c));
            Debug.Assert(cvec1.Equals(cvec1u));
            Debug.Assert(cvec1c.Equals(cvec1u));

            Debug.Assert(786947673   == cvec1.GetHashCode());
            Debug.Assert(-1292227431 == cvec1.GetHashCode());
            Debug.Assert(556554137   == cvec1.GetHashCode());
            Debug.Assert(-1389008455 == cvec1.GetHashCode());
            Debug.Assert(-304630951  == cvec1.GetHashCode());
            Debug.Assert(739909369   == cvec1.GetHashCode());
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            var context = new PuzzleContext();

            var cvec1 = context.FromPath("pics/luxmarket_tshirt01.jpg");
            var cvec2 = context.FromPath("pics/luxmarket_tshirt01_black.jpg");
            var cvec3 = context.FromPath("pics/luxmarket_tshirt01_sal.jpg");
            var cvec4 = context.FromPath("pics/luxmarket_tshirt01_sheum.jpg");
            var cvec5 = context.FromPath("pics/duck.gif");
            var cvec6 = context.FromPath("pics/pic-a-0.jpg");

            var d1 = cvec2.GetDistanceFrom(cvec1);
            var d2 = cvec1.GetDistanceFrom(cvec2);
            var d3 = cvec1.GetDistanceFrom(cvec3);
            var d4 = cvec1.GetDistanceFrom(cvec4);
            var d5 = cvec1.GetDistanceFrom(cvec5);
            var d6 = cvec1.GetDistanceFrom(cvec6);

            Debug.Assert((int)d1 == (int)d2);

            Debug.Assert(d1 < IPuzzle.DefaultSimilarityThreshold);
            Debug.Assert(d3 < IPuzzle.DefaultSimilarityThreshold);
            Debug.Assert(d4 < IPuzzle.DefaultSimilarityThreshold);
            Debug.Assert(d5 > IPuzzle.DefaultSimilarityThreshold);
            Debug.Assert(d6 > IPuzzle.DefaultSimilarityThreshold);
        }
Ejemplo n.º 7
0
 private void Show(object sender, EventArgs e)
 {
     using (var db = new PuzzleContext())
     {
         List <int> ids = db.PuzzleCells.Select(p => p.puzzleId).Distinct().OrderBy(p => p).ToList();
         puzzleView.puzzleIds = ids;
     }
 }
Ejemplo n.º 8
0
 private void UpdatePuzzleIds()
 {
     using (var db = new PuzzleContext())
     {
         List <int> ids = db.PuzzleCells.Select(p => p.puzzleId).Distinct().OrderBy(p => p).ToList();
         puzzleView.puzzleIds = ids;
     }
 }
Ejemplo n.º 9
0
        public double ComparePhoto(string pathToFolderDownload, string originalPhoto, string namePhotoDownloadedFromVk)
        {
            var context             = new PuzzleContext();
            var imageOriginal       = context.FromPath(Environment.CurrentDirectory + pathToFolderDownload + originalPhoto);
            var imageDownloadedToVk = context.FromPath(Environment.CurrentDirectory + pathToFolderDownload + SeparatorPathToFolder + namePhotoDownloadedFromVk);
            var similarity          = imageOriginal.GetDistanceFrom(imageDownloadedToVk);

            return(similarity);
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="tableData"></param>
        /// <param name="srcBitmap"></param>
        /// <param name="exampleBitmap"></param>
        /// <param name="rowCount"></param>
        /// <param name="colCount"></param>
        /// <returns></returns>
        public static TableData UpdateRestoreTable(TableData tableData, Bitmap srcBitmap, Bitmap exampleBitmap, int tileWidth, int tileHeight, int rowCount, int colCount)
        {
            if (tableData == null)
            {
                tableData            = new TableData();
                tableData.Row        = rowCount;
                tableData.Col        = colCount;
                tableData.TileWidth  = tileWidth;
                tableData.TileHeight = tileHeight;
            }
            else
            {
                if (!(tableData.Row == rowCount && tableData.Col == colCount) && !(tableData.TileWidth == tileWidth && tableData.TileHeight == tileHeight))
                {
                    return(null);
                }
            }

            var srcTiles     = createTileImage(srcBitmap, tileWidth, tileHeight);
            var exampleTiles = createTileImage(exampleBitmap, tileWidth, tileHeight);

            var context = new PuzzleContext();

            var restoredTable = new int[rowCount, colCount];

            for (int exampleIndex = 0; exampleIndex < exampleTiles.Count; ++exampleIndex)
            {
                var exampleVec = context.FromImage(exampleTiles[exampleIndex]);
                for (int srcIndex = 0; srcIndex < srcTiles.Count; ++srcIndex)
                {
                    var    srcVec = context.FromImage(srcTiles[srcIndex]);
                    double dist   = exampleVec.GetDistanceFrom(srcVec);

                    if (tableData.Table.Count <= exampleIndex)
                    {
                        tableData.Table.Add(new Tuple <int, double>(-1, 1000.0));
                    }

                    if (tableData.Table[exampleIndex].Item2 > dist)
                    {
                        tableData.Table[exampleIndex] = new Tuple <int, double>(srcIndex, dist);
                    }
                }
            }

            foreach (var bmp in srcTiles)
            {
                bmp.Dispose();
            }
            foreach (var bmp in exampleTiles)
            {
                bmp.Dispose();
            }

            return(tableData);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            var context = new PuzzleContext();

            var cvec1 = context.FromPath("pics/pic-a-0.jpg");
            var cvec2 = context.FromPath("pics/pic-a-1.jpg");

            var d1 = cvec2.GetDistanceFrom(cvec1);
            var d2 = cvec1.GetDistanceFrom(cvec2);

            Debug.Assert(d1 < IPuzzle.DefaultSimilarityThreshold);
            Debug.Assert(d2 < IPuzzle.DefaultSimilarityThreshold);
        }
Ejemplo n.º 12
0
        public void Regress3()
        {
            var context = new PuzzleContext();

            var cvec1 = context.FromPath("pics/pic-a-0.jpg");
            var cvec2 = context.FromPath("pics/pic-a-1.jpg");

            var d1 = cvec2.GetDistanceFrom(cvec1);
            var d2 = cvec1.GetDistanceFrom(cvec2);

            Assert.IsFalse(d1 > IPuzzle.DefaultSimilarityThreshold);
            Assert.IsFalse(d2 > IPuzzle.DefaultSimilarityThreshold);
        }
Ejemplo n.º 13
0
        private void Save(object sender, EventArgs e)
        {
            int lastPuzzleId = GetLastPuzzleId();
            int newPuzzleId  = ++lastPuzzleId;

            using (var db = new PuzzleContext())
            {
                List <Puzzle> cells = new List <Puzzle>();
                for (int r = 0; r < 9; r++)
                {
                    for (int c = 0; c < 9; c++)
                    {
                        cells.Add(new Puzzle()
                        {
                            row = r, col = c, puzzleId = newPuzzleId, value = puzzleView.cellValues[r, c]
                        });
                    }
                }
                db.PuzzleCells.AddRange(cells);
                db.SaveChanges();
            }

            UpdatePuzzleIds();
        }
Ejemplo n.º 14
0
        public static sbyte[] puzzle_fill_cvec_from_dvec(PuzzleContext context, PuzzleDvec dvec)
        {
            if (dvec.sizeof_compressed_vec == 0)
                throw new Exception();

            int s;

            int sizeof_vec = dvec.sizeof_compressed_vec;
            sbyte[] cvec = new sbyte[sizeof_vec];

            int sizeof_lights = sizeof_vec;
            int sizeof_darks  = sizeof_vec;

            double[] lights = new double[sizeof_lights];
            double[] darks  = new double[sizeof_darks];

            int dvecptr = 0;
            int pos_lights = 0, pos_darks = 0;

            double dv;
            for (s = 0; s < sizeof_vec; ++s)
            {
                dv = dvec.vec[dvecptr++];
                if (dv >= -context.puzzle_noise_cutoff &&
                    dv <=  context.puzzle_noise_cutoff)
                    continue;

                if (dv < context.puzzle_noise_cutoff)
                {
                    darks[pos_darks++] = dv;
                    if (pos_darks > sizeof_darks)
                        throw new Exception();
                }
                else if (dv > context.puzzle_noise_cutoff)
                {
                    lights[pos_lights++] = dv;
                    if (pos_lights > sizeof_lights)
                        throw new Exception();
                }
            }
            double lighter_cutoff = puzzle_median(lights, pos_lights);
            double darker_cutoff  = puzzle_median(darks,  pos_darks);

            dvecptr = 0;
            int cvecptr = 0;
            for (s = 0; s < sizeof_vec; ++s)
            {
                dv = dvec.vec[dvecptr++];
                if (dv >= -context.puzzle_noise_cutoff &&
                    dv <=  context.puzzle_noise_cutoff)
                    cvec[cvecptr++] = 0;
                else if (dv < 0.0)
                    cvec[cvecptr++] = (sbyte)(dv < darker_cutoff  ? -2 : -1);
                else
                    cvec[cvecptr++] = (sbyte)(dv > lighter_cutoff ?  2 :  1);
            };

            if (cvecptr != sizeof_vec)
                throw new Exception();

            return cvec;
        }
Ejemplo n.º 15
0
 public PuzzlesController(PuzzleContext context, UserManager <ApplicationUser> userManager, RoleManager <ApplicationRole> roleManager)
 {
     _context     = context;
     _userManager = userManager;
     _roleManager = roleManager;
 }
Ejemplo n.º 16
0
        private static int Main(string[] args)
        {
            PuzzleContext context = new PuzzleContext();

            string file1 = null, file2 = null;
            bool useExitCode = false;
            double threshold = IPuzzle.DefaultSimilarityThreshold;
            bool fixForTexts = true;

            try
            {
                int i = 0;
                while (i < args.Length)
                {
                    switch (args[i])
                    {
                        case "-b":
                            context.ContrastBarrierForCropping = int.Parse(args[++i]);
                            break;

                        case "-c":
                            context.AutoCrop = false;
                            break;

                        case "-C":
                            context.MaxCroppingRatio = int.Parse(args[++i]);
                            break;

                        case "-e":
                            useExitCode = true;
                            break;

                        case "-E":
                            threshold = double.Parse(args[++i]);
                            break;

                        case "-h":
                            Usage();
                            return 0;

                        case "-H":
                            context.MaxHeight = int.Parse(args[++i]);
                            break;

                        case "-W":
                            context.MaxHeight = int.Parse(args[++i]);
                            break;

                        case "-l":
                            context.Lambdas = int.Parse(args[++i]);
                            break;

                        case "-n":
                            context.NoiseCutOff = double.Parse(args[++i]);
                            break;

                        case "-p":
                            context.PRatio = double.Parse(args[++i]);
                            break;

                        case "-t":
                            fixForTexts = false;
                            break;

                        default:
                            if (file1 == null)
                                file1 = args[i];
                            else if (file2 == null)
                                file2 = args[i];
                            else
                            {
                                Usage();
                                return 0;
                            }
                            break;
                    }

                    ++i;
                }

                if (file1 == null || file2 == null)
                {
                    Usage();
                    return 0;
                }

                Puzzle cvec1, cvec2;
                try
                {
                    cvec1 = context.FromPath(file1);
                }
                catch
                {
                    Console.Error.WriteLine("Unable to read [{0}]\n", file1);
                    return 1;
                }
                try
                {
                    cvec2 = context.FromPath(file2);
                }
                catch
                {
                    Console.Error.WriteLine("Unable to read [{0}]\n", file2);
                    return 1;
                }

                double d = cvec1.GetDistanceFrom(cvec2, fixForTexts);

                if (!useExitCode)
                {
                    Console.WriteLine(d);
                    return 0;
                }

                return d >= threshold ? 20 : 10;
            }
            catch
            {
                Usage();
            }

            return 0;
        }
Ejemplo n.º 17
0
        public static void puzzle_autocrop_view(PuzzleContext context, ref PuzzleView view)
        {
            int cropx0, cropx1;
            int cropy0, cropy1;

            puzzle_autocrop_axis(context, view, out cropx0, out cropx1, view.width,  view.height, (int)view.width, 1 - (int)(view.width * view.height));
            puzzle_autocrop_axis(context, view, out cropy0, out cropy1, view.height, view.width,  1,               0);

            if (cropx0 > cropx1 || cropy0 > cropy1)
                throw new Exception();

            int maptr = 0;
            int x;
            int y = cropy0;
            do
            {
                x = cropx0;
                do
                {
                    view.map[maptr++] = view.map[view.width * y + x];
                } while (x++ != cropx1);
            } while (y++ != cropy1);
            view.width  = cropx1 - cropx0 + 1;
            view.height = cropy1 - cropy0 + 1;

            byte[] newArr = new byte[view.width * view.height];
            Array.Copy(view.map, newArr, newArr.Length);
            view.map = newArr;
            if (view.width <= 0 || view.height <= 0)
                throw new Exception();
        }
Ejemplo n.º 18
0
        public static PuzzleAvgLvls puzzle_fill_avglgls(PuzzleContext context, PuzzleView view, int lambdas)
        {
            double width = (double)view.width;
            double height = (double)view.height;

            PuzzleAvgLvls avglvls = new PuzzleAvgLvls();
            avglvls.lambdas = lambdas;
            avglvls.lvls = new double[lambdas * lambdas];
            double xshift = (width  - width  * lambdas / (lambdas + 1)) / 2;
            double yshift = (height - height * lambdas / (lambdas + 1)) / 2;
            int p = (int)Math.Round(Math.Min(width, height) / ((lambdas + 1) * context.puzzle_p_ratio));
            if (p < PuzzleNative.PUZZLE_MIN_P)
                p = PuzzleNative.PUZZLE_MIN_P;

            double x, y;
            int xd, yd;
            int px, py;
            int lwidth, lheight;
            int lx = 0;
            int ly;
            do
            {
                ly = 0;
                do
                {
                    x = xshift + (double) lx * (width  - 1) / (lambdas + 1);
                    y = yshift + (double) ly * (height - 1) / (lambdas + 1);
                    lwidth  = (int)Math.Round(xshift + (lx + 1) * (width  - 1) / (lambdas + 1) - x);
                    lheight = (int)Math.Round(yshift + (ly + 1) * (height - 1) / (lambdas + 1) - y);

                    xd = p < lwidth  ? (int)Math.Round(x + (lwidth  - p) / 2.0) : (int)Math.Round(x);
                    yd = p < lheight ? (int)Math.Round(y + (lheight - p) / 2.0) : (int)Math.Round(y);

                    px = view.width  - xd < p ? 1 : p;
                    py = view.height - yd < p ? 1 : p;

                    avglvls.lvls[avglvls.lambdas * ly + lx] = (px > 0 && py > 0) ? puzzle_get_avglvl(ref view, xd, yd, px, py) : 0;
                } while (++ly < lambdas);
            } while (++lx < lambdas);

            return avglvls;
        }
Ejemplo n.º 19
0
        public static PuzzleDvec puzzle_fill_dvec_from_file(PuzzleContext context, Bitmap bitmap)
        {
            PuzzleView view = puzzle_getview_from_gdimage(context, bitmap);

            if (context.puzzle_enable_autocrop)
                puzzle_autocrop_view(context, ref view);

            return puzzle_fill_dvec(puzzle_fill_avglgls(context, view, context.puzzle_lambdas));
        }
Ejemplo n.º 20
0
        public static PuzzleView puzzle_getview_from_gdimage(PuzzleContext context, Bitmap bitmap)
        {
            PuzzleView view = new PuzzleView();
            view.map = null;
            view.width = bitmap.Width;
            view.height = bitmap.Height;
            if (view.width > context.puzzle_max_width || view.height > context.puzzle_max_height)
                throw new Exception();
            if (view.width == 0 || view.height == 0)
                throw new Exception();

            int x1 = view.width - 1;
            int y1 = view.height - 1;
            if (view.width <= 0 || view.height <= 0)
                throw new Exception();
            if (x1 > int.MaxValue || y1 > int.MaxValue)
                throw new Exception();

            view.map = new byte[view.width * view.height];

            Color pixel;
            int maptr = 0;// view.map;
            int x = (int)x1;
            int y;
            do
            {
                y = (int)y1;
                do
                {
                    pixel = bitmap.GetPixel(x, y);

                    view.map[maptr++] = (byte)((pixel.R * 77 + pixel.G * 151 + pixel.B * 28 + 128) / 256);
                } while (y-- != 0);
            } while (x-- != 0);

            return view;
        }
Ejemplo n.º 21
0
 public static sbyte[] puzzle_fill_cvec_from_file(PuzzleContext context, Bitmap bitmap)
 {
     return puzzle_fill_cvec_from_dvec(context, puzzle_fill_dvec_from_file(context, bitmap));
 }
Ejemplo n.º 22
0
        public static void puzzle_autocrop_axis(PuzzleContext context, PuzzleView view, out int crop0, out int crop1, int axisn, int axiso, int omaptrinc, int nmaptrinc)
        {
            int chunk_n1 = axisn - 1;
            int chunk_o1 = axiso - 1;
            crop0 = 0;
            crop1 = chunk_n1;
            if (axisn < PuzzleNative.PUZZLE_MIN_SIZE_FOR_CROPPING ||
                axiso < PuzzleNative.PUZZLE_MIN_SIZE_FOR_CROPPING)
                return;

            double[] chunk_contrasts = new double[chunk_n1 + 1];
            if (axisn >= int.MaxValue || axiso >= int.MaxValue)
                throw new Exception();

            double chunk_contrast = 0.0, total_contrast = 0.0, barrier_contrast;
            int chunk_n = chunk_n1;
            byte level = 0;
            byte previous_level = 0;
            int chunk_o;
            int maptr = 0;
            do
            {
                chunk_contrast = 0.0;
                chunk_o = chunk_o1;
                do
                {
                    level = view.map[maptr];
                    if (previous_level > level)
                        chunk_contrast += previous_level - level;
                    else
                        chunk_contrast += level - previous_level;
                    maptr += omaptrinc;
                } while (chunk_o-- != 0);
                chunk_contrasts[chunk_n] = chunk_contrast;
                total_contrast += chunk_contrast;
                maptr += nmaptrinc;
            } while (chunk_n-- != 0);
            barrier_contrast = total_contrast * context.puzzle_contrast_barrier_for_cropping;
            total_contrast = 0.0;
            crop0 = 0;
            do
            {
                total_contrast += chunk_contrasts[crop0];
                if (total_contrast >= barrier_contrast)
                    break;
            } while (crop0++ < chunk_n1);
            total_contrast = 0.0;
            crop1 = chunk_n1;
            do
            {
                total_contrast += chunk_contrasts[crop1];
                if (total_contrast >= barrier_contrast)
                    break;
            } while (crop1-- > 0);
            chunk_contrasts = null;
            if (crop0 > chunk_n1 || crop1 > chunk_n1)
                throw new Exception();

            int max_crop = (int)Math.Round(chunk_n1 * context.puzzle_max_cropping_ratio);
            if (max_crop > chunk_n1)
                throw new Exception();
            crop0 = Math.Min(crop0, max_crop);
            crop1 = Math.Max(crop1, chunk_n1 - max_crop);
        }