/// <summary> /// Compares this(Rendered img) to model created for masterFileName /// </summary> /// <param name="masterImg">Contains master img</param> /// <param name="vscanFileName">.vscan file name - cab which will package failures</param> /// <param name="silhouetteTolerance">Silhouette tolerance</param> /// <param name="xTolerance">x shift tolerance</param> /// <param name="yTolerance">y shift tolerance</param> /// <param name="imgTolerance">image tolerance</param> /// <param name="a">A part of ARGB tolerance</param> /// <param name="r">R part of ARGB tolerance</param> /// <param name="g">G part of ARGB tolerance</param> /// <param name="b">B part of ARGB tolerance</param> /// <param name="rcToCompareLeft">Rectangle-part of the image which will be compared. 0 if whole image will be compared</param> /// <param name="rcToCompareTop">Rectangle-part of the image which will be compared. 0 if whole image will be compared</param> /// <param name="rcToCompareRight">Rectangle-part of the image which will be compared. 0 if whole image will be compared</param> /// <param name="rcToCompareBottom">Rectangle-part of the image which will be compared. 0 if whole image will be compared</param> /// <returns>Returns true if every descriptor in the model was found (within tolerance), false otherwise</returns> bool IModelManager2Unmanaged.CompareModelsSavePackage(IImageAdapterUnmanaged masterImg, string vscanFileName, double silhouetteTolerance, double xTolerance, double yTolerance, double imgTolerance, byte a, byte r, byte g, byte b, int rcToCompareLeft, int rcToCompareTop, int rcToCompareRight, int rcToCompareBottom ) { System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Started"); #if DEBUG ImageUtility.ToImageFile(masterImg, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Before_CMSP_masterImage.png")); ImageUtility.ToImageFile(Image, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Before_CMSP_thisImage.png")); #endif // debug //logging bool comparisonResult = false; VScan master = null; VScan rendered = new VScan((IImageAdapter)Image.Clone()); XmlNode xmlDiff = null; if (!(rcToCompareLeft == 0 && rcToCompareTop == 0 && rcToCompareRight == 0 && rcToCompareBottom == 0)) { Rectangle rcToCompare = new Rectangle(rcToCompareLeft, rcToCompareTop, rcToCompareRight - rcToCompareLeft, rcToCompareBottom - rcToCompareTop); IImageAdapter imgAdapter = ImageUtility.ClipImageAdapter(rendered.OriginalData.Image, rcToCompare); rendered.OriginalData.Image = imgAdapter; master = new VScan(ImageUtility.ClipImageAdapter((ImageAdapter)masterImg, rcToCompare)); } else { master = new VScan((IImageAdapter)masterImg); } #if DEBUG ImageUtility.ToImageFile(master.OriginalData.Image, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "CMSP_master.png")); ImageUtility.ToImageFile(rendered.OriginalData.Image, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "CMSP_rendered.png")); #endif // debug System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage(): images created"); System.Diagnostics.Debug.WriteLine(string.Format("color to be created a:{0} r:{1} g:{2} b:{3}", a, r, g, b)); IColor color = new ColorByte(a, r, g, b); System.Diagnostics.Debug.WriteLine("color created"); if (color.ARGB == 0) { color.IsEmpty = true; } System.Diagnostics.Debug.WriteLine("coor just reset to empty (if necessary)"); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage(): color created"); ((IModelManager2Unmanaged)master.OriginalData).CreateModel(silhouetteTolerance, xTolerance, yTolerance, imgTolerance, color); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage(): CreateModel() completed"); System.Diagnostics.Debug.WriteLine("Comparing..."); comparisonResult = rendered.OriginalData.CompareModels(master.OriginalData); System.Diagnostics.Debug.WriteLine("Compare model returned " + comparisonResult.ToString()); // Workaround for the if (comparisonResult == false) { System.Diagnostics.Debug.WriteLine("\nChanging color threshlod..."); master.OriginalData.Descriptors.ColorThreshold = master.OriginalData.Descriptors.ColorThreshold + 1; rendered.OriginalData.Descriptors.ColorThreshold = rendered.OriginalData.Descriptors.ColorThreshold + 1; System.Diagnostics.Debug.WriteLine("Analyzing master..."); master.OriginalData.Analyze(); System.Diagnostics.Debug.WriteLine("Analyzing rendered..."); rendered.OriginalData.Analyze(); System.Diagnostics.Debug.WriteLine("Comparing again..."); comparisonResult = rendered.OriginalData.CompareModels(master.OriginalData); System.Diagnostics.Debug.WriteLine("Getting xml diff..."); xmlDiff = rendered.OriginalData.ModelDifferences; System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : CompareToModel() returned " + comparisonResult.ToString() + " ColorThreshold = " + master.OriginalData.Descriptors.ColorThreshold.ToString()); if (comparisonResult == false) { System.Diagnostics.Debug.WriteLine("\nFailed again..."); master.OriginalData.Descriptors.ColorThreshold = master.OriginalData.Descriptors.ColorThreshold - 2; rendered.OriginalData.Descriptors.ColorThreshold = this.Descriptors.ColorThreshold - 2; master.OriginalData.Analyze(); rendered.OriginalData.Analyze(); comparisonResult = rendered.OriginalData.CompareModels(master.OriginalData); xmlDiff = rendered.OriginalData.ModelDifferences; System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : CompareToModel() returned " + comparisonResult.ToString() + " ColorThreshold = " + master.OriginalData.Descriptors.ColorThreshold.ToString()); if (comparisonResult == false) { System.Diagnostics.Debug.WriteLine("\nFailed one more time ..."); master.OriginalData.Descriptors.ColorThreshold = master.OriginalData.Descriptors.ColorThreshold + 1; rendered.OriginalData.Descriptors.ColorThreshold = this.Descriptors.ColorThreshold + 1; // return original to be saved master.OriginalData.Analyze(); rendered.OriginalData.Analyze(); comparisonResult = rendered.OriginalData.CompareModels(master.OriginalData); xmlDiff = rendered.OriginalData.ModelDifferences; System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : CompareToModel() returned " + comparisonResult.ToString() + " ColorThreshold = " + master.OriginalData.Descriptors.ColorThreshold.ToString()); } } } if (comparisonResult == false && vscanFileName != null && vscanFileName != string.Empty) { System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : before <new Package>. vscanFileName=" + vscanFileName); Package package = Package.Create(vscanFileName, true); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 1"); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 2"); package.MasterBitmap = ImageUtility.ToBitmap(master.OriginalData.Image); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 3"); package.CapturedBitmap = ImageUtility.ToBitmap(rendered.OriginalData.Image); //System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 4"); // byte[] buffer = System.Text.ASCIIEncoding.ASCII.GetBytes(xmlDoc.DocumentElement.OuterXml); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 5"); Stream memoryStream = master.OriginalData.Descriptors.Serialize(); package.MasterModel = memoryStream; System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 6"); package.CapturedBitmap = ImageUtility.ToBitmap(rendered.OriginalData.Image); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 7"); package.XmlDiff = xmlDiff; System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 8"); package.PackageCompare = PackageCompareTypes.ImageCompare | PackageCompareTypes.ModelAnalytical; System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : Saving package 9"); package.Save(); System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : PackageModel() completed"); } System.Diagnostics.Debug.WriteLine("CompareModelsSavePackage() : completed"); return(comparisonResult); }
/// <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); }