Example #1
0
            private void ReturnsExpectedValueForKnownInput(double expectedValue, double ratio, IColorSpace a, IColorSpace b)
            {
                var target      = new CmcComparison(ratio);
                var actualValue = a.Compare(b, target);

                Assert.IsTrue(expectedValue.BasicallyEqualTo(actualValue), expectedValue + " != " + actualValue);
            }
        public void UpdateDominoes()
        {
            IColorSpaceComparison comp;

            switch (m_regression_mode)
            {
            case 0: comp = new CmcComparison(); break;

            case 1: comp = new Cie1976Comparison(); break;

            case 2: comp = new Cie94Comparison(); break;

            default: comp = new CieDe2000Comparison(); break;
            }

            float scaling_x = (SourceImage.Width - 1) / size_x;
            float scaling_y = (SourceImage.Height - 1) / size_y;

            if (!m_allow_stretch)
            {
                if (scaling_x > scaling_y)
                {
                    scaling_x = scaling_y;
                }
                else
                {
                    scaling_y = scaling_x;
                }
            }
            // fix transparency: if image is transparent, background appears black
            Bitmap   notransparency = new Bitmap(SourceImage.Width, SourceImage.Height);
            Graphics temp           = Graphics.FromImage(notransparency);

            temp.Clear(Color.White);
            System.Drawing.Imaging.ImageAttributes Att = new System.Drawing.Imaging.ImageAttributes();
            Att.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
            temp.DrawImage(SourceImage, new Rectangle(0, 0, SourceImage.Width, SourceImage.Height), 0, 0, SourceImage.Width, SourceImage.Height, GraphicsUnit.Pixel, Att);
            // image to read from
            WriteableBitmap pixelsource = BitmapFactory.ConvertToPbgra32Format(ImageHelper.BitmapToBitmapSource(notransparency));

            pixelsource.Lock();

            System.Windows.Media.Color[] sourceColors = new System.Windows.Media.Color[shapes.Length];
            dominoes = new int[shapes.Length];
            if (!calculation_mode)
            {
                // if source pixel = top left pixel of each domino
                for (int i = 0; i < shapes.Length; i++)
                {
                    RectangleF container = shapes[i].GetContainer(scaling_x, scaling_y);
                    try
                    {
                        sourceColors[i] = pixelsource.GetPixel((int)container.X, (int)container.Y);
                    }
                    catch (Exception) { }

                    dominoes[i] = Dithering.Compare(new Rgb()
                    {
                        R = sourceColors[i].R, G = sourceColors[i].G, B = sourceColors[i].B
                    }.To <Lab>(), comp, lab_colors, 0);
                }
            }
            else
            {
                // if source pixel is average of region
                for (int i = 0; i < shapes.Length; i++)
                {
                    RectangleF container = shapes[i].GetContainer(scaling_x, scaling_y);

                    int r = 0, g = 0, b = 0;
                    int counter = 0;
                    // for loop: each container
                    for (float x_iterator = container.X; x_iterator < container.X + container.Width; x_iterator++)
                    {
                        for (float y_iterator = container.Y; y_iterator < container.Y + container.Width; y_iterator++)
                        {
                            if (shapes[i].IsInside(new PointF(x_iterator, y_iterator), true, scaling_x, scaling_y))
                            {
                                System.Windows.Media.Color c = pixelsource.GetPixel((int)x_iterator, (int)y_iterator);
                                r += c.R;
                                g += c.G;
                                b += c.B;
                                counter++;
                            }
                        }
                    }
                    sourceColors[i] = System.Windows.Media.Color.FromRgb((byte)(r / counter), (byte)(g / counter), (byte)(b / counter));
                    // calculates the color of the domino
                    dominoes[i] = Dithering.Compare(new Rgb()
                    {
                        R = sourceColors[i].R, G = sourceColors[i].G, B = sourceColors[i].B
                    }.To <Lab>(), comp, lab_colors, 0);
                }
            }
        }
Example #3
0
        public void GenerateField(bool update_filters = false)
        {
            dominoes = new int[length, height];
            System.Drawing.Image i;
            if (!update_filters)
            {
                using (var bmpTemp = new System.Drawing.Bitmap(SourcePath))
                {
                    i = new System.Drawing.Bitmap(bmpTemp);
                }
            }
            else
            {
                i = ImageHelper.BitmapImageToBitmap(filters.preview);
            }
            if (length < 2)
            {
                m_length = 2;
            }
            if (height < 2)
            {
                m_height = 2;
            }
            System.Drawing.Bitmap   thumb    = new System.Drawing.Bitmap(length, height);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(thumb);
            graphics.Clear(System.Drawing.Color.White);
            if (ResizeMode == 0)
            {
                graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
            }
            if (ResizeMode == 1)
            {
                graphics.InterpolationMode = InterpolationMode.Bicubic;
            }
            if (ResizeMode == 2)
            {
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            }
            System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
            System.Drawing.Imaging.ImageAttributes Att  = new System.Drawing.Imaging.ImageAttributes();
            Att.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
            graphics.DrawImage(i, new System.Drawing.Rectangle(0, 0, length, height), 0, 0, i.Width, i.Height, System.Drawing.GraphicsUnit.Pixel, Att);
            //graphics.DrawImage(i, 0, 0, length, height);
            BitmapImage     bitmapImage = ImageHelper.BitmapToBitmapImage(thumb);
            BitmapSource    bitm        = new FormatConvertedBitmap(bitmapImage, PixelFormats.Bgr24, null, 0);
            WriteableBitmap b           = BitmapFactory.ConvertToPbgra32Format(bitm);

            IColorSpaceComparison comp;

            switch (ColorRegressionMode)
            {
            case 0: comp = new CmcComparison(); break;

            case 1: comp = new Cie1976Comparison(); break;

            case 2: comp = new Cie94Comparison(); break;

            default: comp = new CieDe2000Comparison(); break;
            }

            Dithering d;

            switch (DiffusionMode)
            {
            case 0: d = new NoDithering(comp, Colors); break;

            case 1: d = new FloydSteinbergDithering(comp, Colors); break;

            case 2: d = new JarvisJudiceNinkeDithering(comp, Colors); break;

            default: d = new StuckiDithering(comp, Colors); break;
            }
            dominoes = d.Dither(b, comp);

            b.Lock();
        }