Exemple #1
0
        /// <summary>
        /// Model Deserialization
        /// </summary>
        public static GlyphModel Deserialize(string fileName)
        {
            CheckValidPath(fileName);
            if (File.Exists(fileName) == false)
            {
                throw new FileNotFoundException("The specified file cannot be found (File deleted ? Wrong location ? Local file ? Network issue ? Localization problem ?)", fileName);
            }

            GlyphModel retVal     = null;
            FileStream fileStream = null;

            try
            {
                fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.AssemblyFormat = 0;        // System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple;
                // formatter.Binder = syntheticalBinder
                retVal = (GlyphModel)formatter.Deserialize(fileStream);
            }
            finally
            {
                if (fileStream != null)
                {
                    fileStream.Close(); fileStream = null;
                }
            }
            return(retVal);
        }
        private ImageAdapter GetTransformedContent(IImageAdapter imageAdapter, double[] transformMatrix, int width, int height)
        {
            // validate the variation
            //extract the popup from the original
            GlyphModel glmodel = new GlyphModel();

            //todo work with the minsize bounding box - add the transfrom from XTC
            glmodel.Size = new System.Drawing.Size(width, height);

            GlyphImage glimage = new GlyphImage(glmodel);

            glmodel.Glyphs.Add(glimage);
            ImageUtility.ToImageFile(imageAdapter, "foo.png", ImageFormat.Png);
            glimage.Path                   = "foo.png";
            transformMatrix[1]             = -transformMatrix[1];
            transformMatrix[2]             = -transformMatrix[2];
            glimage.Panel.Transform.Matrix = transformMatrix;

            double[] ltrans = glimage.Panel.Transform.Matrix;
            double   minX   = double.MaxValue;
            double   minY   = double.MaxValue;
            double   maxX   = double.MinValue;
            double   maxY   = double.MinValue;

            double[,] tp = new double[, ] {
                { 0, 0 }, { imageAdapter.Width, 0 }, { 0, imageAdapter.Height }, { imageAdapter.Width, imageAdapter.Height }
            };

            for (int j = 0; j < 4; j++)
            {
                double lxp = ltrans[0] * tp[j, 0] + ltrans[1] * tp[j, 1] + ltrans[4];
                double lyp = ltrans[2] * tp[j, 0] + ltrans[3] * tp[j, 1] + ltrans[5];

                _contentLocation[j, 0] = lxp;
                _contentLocation[j, 1] = lyp;
                Console.WriteLine(lxp + "," + lyp);
                if (lxp > maxX)
                {
                    maxX = lxp;
                }

                if (lyp > maxY)
                {
                    maxY = lyp;
                }

                if (lxp < minX)
                {
                    minX = lxp;
                }

                if (lyp < minY)
                {
                    minY = lyp;
                }
            }

            _imageAdapterXmin = minX;
            _imageAdapterYmin = minY;
            Console.WriteLine(minX + " " + minY + "    " + maxX + " " + maxY);
            transformMatrix[1]             = -transformMatrix[1];
            transformMatrix[2]             = -transformMatrix[2];
            glimage.Panel.Transform.Matrix = transformMatrix;

            ImageAdapter res    = new ImageAdapter((int)(maxX - minX), (int)(maxY - minY));
            ImageAdapter buffer = new ImageAdapter(glmodel.Size.Width, glmodel.Size.Height);

            glimage.Render();
            Point pt = new Point((int)(glimage.Position.X + .5), (int)(glimage.Position.Y + .5));

            ImageUtility.CopyImageAdapter(buffer, glimage.GeneratedImage, pt, glimage.Size, true);

            int mixx = int.MaxValue;
            int mixy = int.MaxValue;

            for (int j = 0; j < buffer.Width; j++)
            {
                for (int i = 0; i < buffer.Height; i++)
                {
                    if (buffer[j, i].Alpha != 0)
                    {
                        if (j < mixx)
                        {
                            mixx = j;
                        }

                        if (i < mixy)
                        {
                            mixy = i;
                        }
                    }
                }
            }

            Console.WriteLine("comp " + mixx + " " + mixy);
            for (int j = 0; j < res.Width; j++)
            {
                for (int i = 0; i < res.Height; i++)
                {
                    if (j + mixx >= 0 && j + mixx < buffer.Width && i + mixy >= 0 && i + mixy < buffer.Height)
                    {
                        res[j, i] = buffer[j + mixx, i + mixy];
                    }
                }
            }
            string path = "TransPost.png";

#if DEBUG
            ImageUtility.ToImageFile(res, path);
#endif
            glimage.Path = path;
            return(res);
        }
Exemple #3
0
        /// <summary>
        /// Find the all the GlyphBase specified in the Array within the GlyphModel::Target
        /// </summary>
        /// <param name="glyphsToFind">The GlyphBase to find</param>
        /// <param name="glyphModel">The GlyphModel to use</param>
        /// <returns>The number of match found (max of one per glyph found)</returns>
        public int FindGlyphs(GlyphBase[] glyphsToFind, GlyphModel glyphModel)
        {
            int retVal = 0;

            _glyphSearched   = glyphModel;
            MatchFoundEvent += new MatchFoundEventHandler(GlyphComparator_MatchFoundInternal);

            IImageAdapter targetFiltered = ImageUtility.ClipImageAdapter(glyphModel.Target, new Rectangle((int)glyphModel.Panel.Position.X, (int)glyphModel.Panel.Position.Y, (int)glyphModel.Panel.Size.Width, (int)glyphModel.Panel.Size.Height));
//                    IImageAdapter targetFiltered = ImageUtility.ClipImageAdapter(glyphModel.Target, new Rectangle((int)glyphModel.Layout.Position.X, (int)glyphModel.Layout.Position.Y, (int)glyphModel.Layout.Size.Width, (int)glyphModel.Layout.Size.Height));

            // Filter Image (Only needed if one or more "GlyphBase::EdgesOnly" property is true)
            SaturateEdgeFilter edgeFilter = new SaturateEdgeFilter();

            edgeFilter.ColorBelowThresold = (ColorByte)Color.White;
            edgeFilter.ColorAboveThresold = (ColorByte)Color.Red;
            edgeFilter.Threshold          = 0;
            if (GlyphUseEdgeOnly(glyphsToFind))
            {
                targetFiltered = edgeFilter.Process(targetFiltered);
            }

            // Render every glyphs (needed because Glyph overlap would mess up the resulting bmp otherwise)
            IImageAdapter renderSurface = null;

            if (glyphModel.GeneratedImage != null)
            {
                renderSurface = glyphModel.GeneratedImage;
            }
            else
            {
                renderSurface = new ImageAdapter(glyphModel.Size.Width, glyphModel.Size.Height, ColorByte.Empty);
                renderSurface = RenderGlyphs(glyphsToFind, renderSurface);
            }

            // Capture images
            Hashtable glyphBitmapHash = new Hashtable(glyphModel.Glyphs.Count);

            for (int t = 0; t < glyphModel.Glyphs.Count; t++)
            {
                GlyphBase glyph = (GlyphBase)glyphModel.Glyphs[t];

                Rectangle     area         = new Rectangle(glyph.ComputedLayout.Position.X, glyph.ComputedLayout.Position.Y, (int)Math.Min(glyph.ComputedLayout.Size.Width, glyphModel.Panel.Size.Width - glyph.ComputedLayout.Position.X + .5), (int)Math.Min(glyph.ComputedLayout.Size.Height, glyphModel.Panel.Size.Height - glyph.ComputedLayout.Position.Y + .5));
                IImageAdapter clippedImage = ImageUtility.ClipImageAdapter(renderSurface, area);
                if (glyphBitmapHash.Contains(glyph) == false)
                {
                    glyphBitmapHash.Add(glyph, clippedImage);
                }
                else
                {
                    // Duplicate, remove it from the collection
                    glyphModel.Glyphs.RemoveAt(t);
                    t--;
                }
            }

            // Try to match ever Glyph in the collection
            IDictionaryEnumerator iter = glyphBitmapHash.GetEnumerator();

            while (iter.MoveNext())
            {
                GlyphBase glyph = (GlyphBase)iter.Key;
                _glyphToFind = glyph;
                IImageAdapter imageAdapter = (ImageAdapter)iter.Value;

                if (glyph.CompareInfo.EdgesOnly)
                {
                    imageAdapter = edgeFilter.Process(imageAdapter);
                }
                bool found = FindImageAdapter(targetFiltered, imageAdapter, glyph.CompareInfo.ErrorMax);
                if (found)
                {
                    retVal++;
                }
            }

            return(retVal);
        }
Exemple #4
0
 /// <summary>
 /// Find the all the GlyphBase specified in the GlyphModel::Glyphs property within the GlyphModel::Target
 /// </summary>
 /// <param name="glyphModel">The GlyphModel to use</param>
 /// <returns>The number of match found (max of one per glyph found)</returns>
 public int FindGlyphs(GlyphModel glyphModel)
 {
     return(FindGlyphs((GlyphBase[])glyphModel.Glyphs.ToArray(typeof(GlyphBase)), glyphModel));
 }
Exemple #5
0
 /// <summary>
 /// 'Ctor
 /// </summary>
 /// <param name="glyphModel"></param>
 /// <param name="glyphBase"></param>
 /// <param name="matchInfo"></param>
 public GlyphMatchEventArgs(GlyphModel glyphModel, GlyphBase glyphBase, MatchingInfo matchInfo)
 {
     ModelGlyph = glyphModel;
     BaseGlyph  = glyphBase;
     MatchInfo  = matchInfo;
 }
Exemple #6
0
 /// <summary>
 /// Constructor
 /// </summary>
 public LayoutContainer(GlyphModel model) : base(model)
 {
 }