Exemple #1
0
        /// <summary>
        /// Tests the <see cref="Roi.Contains(System.Drawing.PointF)"/> method for a given shape. The image is used to provide a basis for the coordinate space.
        /// </summary>
        protected void TestRoiContains(ImageKey key, T shapeData, string name)
        {
            using (IPresentationImage image = GetImage(key))
            {
                IImageSopProvider provider = (IImageSopProvider)image;
                using (Bitmap bmp = new Bitmap(provider.Frame.Columns, provider.Frame.Rows))
                {
                    using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
                    {
                        g.Clear(Color.Black);

                        // baseline ROI using GDI's GraphicsPath
                        using (GraphicsPath graphicsPath = new GraphicsPath())
                        {
                            AddShapeToGraphicsPath(graphicsPath, shapeData);
                            g.FillPath(Brushes.Blue, graphicsPath);
                        }
                    }

                    // simulates ROIs that an end-user would create using graphics constructed by the tools
                    Roi userRoi = CreateRoiFromGraphic((IOverlayGraphicsProvider)image, shapeData);
                    userRoi.PixelData.ForEachPixel(delegate(int i, int x, int y, int pixelIndex)
                    {
                        if (userRoi.Contains(x, y))
                        {
                            bmp.SetPixel(x, y, ShiftColor(bmp.GetPixel(x, y), true, false, false));
                        }
                    });

                    // simulates ROIs that a programmer might create using the SDK directly, rather than via graphics
                    Roi sdkRoi = CreateRoiFromImage(image, shapeData);
                    sdkRoi.PixelData.ForEachPixel(delegate(int i, int x, int y, int pixelIndex)
                    {
                        if (sdkRoi.Contains(x, y))
                        {
                            bmp.SetPixel(x, y, ShiftColor(bmp.GetPixel(x, y), false, true, false));
                        }
                    });

                    string filename = string.Format("{0}.{1}.containment.test.png", name, this.ShapeName).TrimStart('.');
                    bmp.Save(filename);
                    Trace.WriteLine(string.Format("Pixel containment map has been dumped to {0}.", filename));

                    int totalCount = 0;
                    int errorCount = 0;
                    for (int y = 0; y < bmp.Height; y++)
                    {
                        for (int x = 0; x < bmp.Width; x++)
                        {
                            Color c = bmp.GetPixel(x, y);
                            if (c.R > 0 || c.G > 0 || c.B > 0)
                            {
                                totalCount++;
                                if (c.R < 255 || c.G < 255 || c.B < 255)
                                {
                                    errorCount++;
                                }
                            }
                        }
                    }

                    if (errorCount > 0)
                    {
                        WriteLine("The pixel containment test results are not perfect. {0} differences were found out of {1} pixels.", errorCount, totalCount);
                        WriteLine("The image should be mostly a white shape on a black background.");
                        WriteLine("Any coloured areas indicate locations where Roi.Contains(...) did not return perfectly coincident results.");
                        WriteLine("There will invariably be such areas along shape boundaries, but the shape should be white overall.");
                        WriteLine("Red channel is painted by user mode ROI. Green channel is painted by SDK. Blue channel is painted by GDI+.");
                    }
                    Assert.AreEqual(0, errorCount, 0.01 * totalCount, "Automated pixel containment test failed. Please review the test output manually.");
                }
            }
        }