/// <summary> /// Places one image on top of the other. The top image should fit within the bottom /// image. The top image completely cover the bottom image. /// </summary> /// <param name="bmpTop"></param> /// <param name="xstart">The x value of the point in the bottom image at which the left /// hand top corner of the top image is placed</param> /// <param name="ystart">The y value of the point in the bottom image at which the left /// hand top corner of the top image is placed</param> /// <returns>Returns an interface to the image</returns> IImageAdapterUnmanaged IImageAdapterUnmanaged.Overlay( IImageAdapterUnmanaged bmpTop, int xstart, int ystart) { System.Diagnostics.Debug.WriteLine("Overlay() started"); Rectangle rBg = new Rectangle(0, 0, this.Width, this.Height); Rectangle rTop = new Rectangle(xstart, ystart, bmpTop.Width, bmpTop.Height); System.Diagnostics.Debug.WriteLine("Overlay() \n\nrBg " + rBg.ToString() + "\nrTop " + rTop.ToString()); if (Rectangle.Intersect(rBg, rTop) != rTop) { throw new ArgumentOutOfRangeException("Top BMP must be inside Bg one"); } // Copy Color from background image into this (same as cloning bg image into 'this') ImageAdapter temp = new ImageAdapter((IImageAdapter)this); System.Diagnostics.Debug.WriteLine("Overlay() : Before the loop"); for (int x = rBg.Left; x < rBg.Right; x++) { for (int y = rBg.Top; y < rBg.Bottom; y++) { if (rTop.Contains(x, y)) { IColor colorTop = bmpTop[x - rTop.Left, y - rTop.Top]; temp[x, y] = colorTop; } } } return((IImageAdapterUnmanaged)temp); }
/// <summary> /// Merges two images taking into consideration their alpha values. /// </summary> /// <param name="bmpBg">The background Image</param> /// <param name="bmpTop">The image that goes on top of the background</param> /// <param name="xBg">Left corner position of background</param> /// <param name="yBg">Top corner position of the background</param> /// <param name="xTop">Left corner position of the top image</param> /// <param name="yTop">Top corner position of the top image</param> /// <returns>Returns an interface to the image</returns> IImageAdapterUnmanaged IImageAdapterUnmanaged.Merge(IImageAdapterUnmanaged bmpBg, IImageAdapterUnmanaged bmpTop, int xBg, int yBg, int xTop, int yTop) { System.Diagnostics.Debug.WriteLine("Merge() started"); #if DEBUG ImageUtility.ToImageFile(bmpBg, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Before_Merge_Bg.png")); ImageUtility.ToImageFile(bmpTop, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Before_Merge_Top.png")); #endif // debug #if DEBUG ImageUtility.ToImageFile((IImageAdapter)bmpBg, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Merge_BgImage.bmp")); ImageUtility.ToImageFile((IImageAdapter)bmpTop, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Merge_TopImage.bmp")); #endif // DEBUG Rectangle rBg = new Rectangle(xBg, yBg, bmpBg.Width, bmpBg.Height); Rectangle rTop = new Rectangle(xTop, yTop, bmpTop.Width, bmpTop.Height); System.Diagnostics.Debug.WriteLine("Merge() \n\nrBg " + rBg.ToString() + "\nrTop " + rTop.ToString()); if (Rectangle.Intersect(rBg, rTop) != rTop) { throw new ArgumentOutOfRangeException("Top BMP must be inside Bg one"); } int maxAlpha = 0; float bgAlpha = 0; float topAlpha = 0; // Copy Color from background image into this (same as cloning bg image into 'this') ImageAdapter temp = new ImageAdapter((IImageAdapter)bmpBg); System.Diagnostics.Debug.WriteLine("Merge1() : Before the loop"); for (int x = rBg.Left; x < rBg.Right; x++) { for (int y = rBg.Top; y < rBg.Bottom; y++) { if (rTop.Contains(x, y)) { IColor colorBg = bmpBg[x - rBg.Left, y - rBg.Top]; IColor colorTop = bmpTop[x - rTop.Left, y - rTop.Top]; maxAlpha = Math.Max(colorBg.A, colorTop.A); topAlpha = (float)colorTop.Alpha; bgAlpha = (float)((1f - colorTop.Alpha) * colorBg.Alpha); IColor color = new ColorByte((byte)maxAlpha, (byte)(bgAlpha * colorBg.R + topAlpha * colorTop.R), (byte)(bgAlpha * colorBg.G + topAlpha * colorTop.G), (byte)(bgAlpha * colorBg.B + topAlpha * colorTop.B)); temp[x - rBg.Left, y - rBg.Top] = color; } } } #if DEBUG ImageUtility.ToImageFile(temp, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "After_Merge_This.png")); #endif return((IImageAdapterUnmanaged)temp); }
/// <summary> /// Multiply all the pixels with an alpha value. /// </summary> /// <param name="alpha">Alpha value(between 0 and 1)</param> /// <param name="image">BitmapResources to have Alpha applied on</param> /// <returns>Returns an interface to the image</returns> IImageAdapterUnmanaged IImageAdapterUnmanaged.ApplySingleAlpha( double alpha, IImageAdapterUnmanaged image) { #if DEBUG ImageUtility.ToImageFile(this, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Before_ApplyAlpha_this.png")); ImageUtility.ToImageFile(image, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "Before_ApplyAlpha_image.png")); #endif if (alpha > 1.0 || alpha < 0.0) { throw new ArgumentOutOfRangeException("Alpha must be between 0.0 and 1.0 (this is percentage)"); } System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() started"); IColor argb = ColorByte.Empty; if ((ImageAdapter)this != (ImageAdapter)image) { System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() this != image"); System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() Before for-loop"); for (int x = 0; x < image.Width; x++) { for (int y = 0; y < image.Height; y++) { argb = (IColor)image[x, y].Clone(); argb.ExtendedAlpha *= alpha; this[x, y] = argb; } } System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() After for-loop"); } else { System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() this == image"); System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() Before for-loop"); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { this[x, y].ExtendedAlpha = this[x, y].ExtendedAlpha * alpha; } } System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() After for-loop"); } #if DEBUG ImageUtility.ToImageFile(this, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "After_ApplyAlpha_Result.png")); ImageUtility.ToImageFile(image, System.IO.Path.Combine(System.IO.Path.GetTempPath(), "After_ApplyAlpha_Operand.png")); #endif System.Diagnostics.Debug.WriteLine("ApplySingleAlpha() ended \n"); return((IImageAdapterUnmanaged)this); }
/// <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); }