public void CI_InsertPairTest_08_ThickLines() { string testObject = "ContourImage.ScanObject"; Color backgroundColor = Color.White; Color foregroundColor = Color.Black; Brush fgBrush = new SolidBrush(foregroundColor); Pen fgPen = new Pen(fgBrush, 8); fgPen.StartCap = fgPen.EndCap = System.Drawing.Drawing2D.LineCap.Round; // When painting into the image, leave an outer frame of 16 pixels free. // Only use the range 50..250. int width = 300, height = 300; Bitmap image = new Bitmap(width, height); Graphics g = Graphics.FromImage(image); int contourDist = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.ContourDistance; int blurDist = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.BlurDistanceMax; SWA_Ariadne_Gui_Mazes_ContourImageAccessor.PrepareInfluenceRegions(contourDist + blurDist); int[,] alpha = new int[width, height]; List <int>[] objectXs, contourXs, borderXs; SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXs, out contourXs, out borderXs); #region Create a complicated pattern of border scan lines. Point[] points = { new Point(50, 50), new Point(250, 250), new Point(200, 100), new Point(100, 200), }; g.FillRectangle(new SolidBrush(backgroundColor), 0, 0, image.Width, image.Height); for (int i = 0, j = i + 1; j < points.Length; i++, j++) { g.DrawLine(fgPen, points[i], points[j]); } #endregion ContourImage target = new ContourImage(image); SWA_Ariadne_Gui_Mazes_ContourImageAccessor accessor = new SWA_Ariadne_Gui_Mazes_ContourImageAccessor(target); accessor.image = image; int y0 = image.Height / 2; #region Find the leftmost object pixel on the scan line at y0. int x0 = 0; int fuzziness = (int)(0.1F * SWA_Ariadne_Gui_Mazes_ContourImageAccessor.MaxColorDistance); while (accessor.ColorDistance(image.GetPixel(x0, y0)) <= fuzziness) { x0++; } #endregion accessor.ScanObject(x0, y0, fuzziness, alpha, objectXs, contourXs, borderXs); TestBorderScanlines(testObject, borderXs); }
private static void TestScan(string testObject, Bitmap image, Color backgroundColor, int y0, int maxContourScanRegions) { testObject += " @ " + y0.ToString(); int fuzziness = (int)(0.1F * SWA_Ariadne_Gui_Mazes_ContourImageAccessor.MaxColorDistance); int frameWidth = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.ContourDistance; SWA_Ariadne_Gui_Mazes_ContourImageAccessor.PrepareInfluenceRegions(frameWidth); ContourImage target = new ContourImage(image); SWA_Ariadne_Gui_Mazes_ContourImageAccessor accessor = new SWA_Ariadne_Gui_Mazes_ContourImageAccessor(target); accessor.image = image; #region Find the leftmost object pixel on the scan line at y0. int x0 = 0; while (accessor.ColorDistance(image.GetPixel(x0, y0)) <= fuzziness) { x0++; if (x0 >= image.Width) { return; } } #endregion #region Prepare required data structures. int width = image.Width, height = image.Height; int[,] alpha = new int[width, height]; List <int>[] objectXs, contourXs, borderXs; SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXs, out contourXs, out borderXs); #endregion accessor.ScanObject(x0, y0, fuzziness, alpha, objectXs, contourXs, borderXs); #region Test if the object map is well formed. for (int i = 0; i < height; i++) { int nEntries = objectXs[i].Count; int m = nEntries % 2; Assert.AreEqual(1, m, testObject + string.Format(" - objectXs[{0}] must be an odd number: {1}", i, nEntries)); int nRegions = (nEntries - 1) / 2; Assert.IsTrue(nRegions <= maxContourScanRegions, testObject + string.Format(" - objectXs[{0}] regions = {1} must be less than {2}", i, nRegions, maxContourScanRegions)); } #endregion #region Test if the object map is complete. int imageArea = ImageArea(image, backgroundColor, (float)fuzziness / SWA_Ariadne_Gui_Mazes_ContourImageAccessor.MaxColorDistance); int objectArea = ObjectArea(objectXs); Assert.AreEqual(imageArea, objectArea, testObject + string.Format(" - object area and image area must be equal")); #endregion // Test if the contour map is well formed. TestBorderScanlines(testObject + " - contourXs", contourXs); // Test if the border map is well formed. TestBorderScanlines(testObject + " - borderXs", borderXs); }