コード例 #1
0
        public int GetNearestColor(Color colorTarget, ColorSpace colorSpace)
        {
            if (colorTarget.ToArgb() == Colors[0].ToArgb())
            {
                //. color key.
                return(0);
            }

            ColorDistance[] distances = new ColorDistance[Colors.Length - 1];
            for (int i = 0; i < distances.Length; i++)
            {
                distances[i] = new ColorDistance(i + 1, Colors[i + 1], colorSpace.Distance(Colors[i + 1], colorTarget));
            }

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < distances.Length - i - 1; j++)
                {
                    if (distances[j].Distance < distances[j + 1].Distance)
                    {
                        ColorDistance swap = distances[j];
                        distances[j]     = distances[j + 1];
                        distances[j + 1] = swap;
                    }
                }
            }

            return(distances[distances.Length - 1].Index);
        }
コード例 #2
0
        public void TestColorCoeffs()
        {
            Assert.AreEqual(30, ColorDistance.CoeffRed);
            Assert.AreEqual(59, ColorDistance.CoeffGreen);
            Assert.AreEqual(11, ColorDistance.CoeffBlue);
            Assert.AreEqual(0, ColorDistance.CoeffHue);
            Assert.AreEqual(0, ColorDistance.CoeffSaturation);
            Assert.AreEqual(0, ColorDistance.CoeffBrightness);

            using (ColorDistance.UseCoeffs(1, 2, 3, 4, 5, 6))
            {
                Assert.AreEqual(1, ColorDistance.CoeffRed);
                Assert.AreEqual(2, ColorDistance.CoeffGreen);
                Assert.AreEqual(3, ColorDistance.CoeffBlue);
                Assert.AreEqual(4, ColorDistance.CoeffHue);
                Assert.AreEqual(5, ColorDistance.CoeffSaturation);
                Assert.AreEqual(6, ColorDistance.CoeffBrightness);
            }

            Assert.AreEqual(30, ColorDistance.CoeffRed);
            Assert.AreEqual(59, ColorDistance.CoeffGreen);
            Assert.AreEqual(11, ColorDistance.CoeffBlue);
            Assert.AreEqual(0, ColorDistance.CoeffHue);
            Assert.AreEqual(0, ColorDistance.CoeffSaturation);
            Assert.AreEqual(0, ColorDistance.CoeffBrightness);
        }
コード例 #3
0
        public void TestSearchSubstiture()
        {
            var palette = new Palette
            {
                new Color(0),
                new Color(250, 250, 250),
                new Color(100, 0, 0),
                new Color(0, 100, 0),
                new Color(0, 0, 100),
                new Color(0, 0, 101)
            };

            var quickColorSearcher = new PaletteQuickColorSearcher(palette);

            using (ColorDistance.UseCoeffs(1, 1, 1))
            {
                Assert.AreEqual(new Color(0), quickColorSearcher.SearchSubstitute(new Color(0)));
                Assert.AreEqual(new Color(250, 250, 250), quickColorSearcher.SearchSubstitute(new Color(254, 254, 254)));
                Assert.AreEqual(new Color(0, 0, 100), quickColorSearcher.SearchSubstitute(new Color(98, 98, 99)));
                Assert.AreEqual(new Color(0, 100, 0), quickColorSearcher.SearchSubstitute(new Color(98, 99, 98)));
                Assert.AreEqual(new Color(100, 0, 0), quickColorSearcher.SearchSubstitute(new Color(99, 98, 98)));

                Assert.AreEqual(new Color(0, 0, 100), quickColorSearcher.SearchSubstitute(new Color(0, 0, 99), true));
                Assert.AreEqual(new Color(0, 0, 101), quickColorSearcher.SearchSubstitute(new Color(0, 0, 99), true));
            }
        }
コード例 #4
0
 public void TestSquareDistance()
 {
     Assert.AreEqual(0, ColorDistance.GetSquareDistance(new Color(1, 2, 3), new Color(1, 2, 3)));
     Assert.AreEqual(9 * 9 + 18 * 18 + 27 * 27, ColorDistance.GetSquareDistance(new Color(1, 2, 3), new Color(10, 20, 30)));
     Assert.AreEqual(1 * 1 + 2 * 2 + 3 * 3, ColorDistance.GetSquareDistance(new Color(1, 2, 3), new Color(0)));
     Assert.AreEqual(new Color(123).GetSquareDistance(new Color(456)), new Color(456).GetSquareDistance(new Color(123)));
 }
コード例 #5
0
        private void UpdateImages()
        {
            activeQuantizer.Clear();

            try
            {
                if (listMethod.SelectedIndex <= 1)
                {
                    Image targetImage = GetQuantizedImage(SourceImage);
                    pictureSource.Image = SourceImage;
                    ImageSizeLabel.Text = SourceImage.Width + " x " + SourceImage.Height;
                    pictureTarget.Image = targetImage;
                }
                else
                {
                    Bitmap b = new Bitmap(Ditherhelper.Dithering(SourceImage));
                    ColorDistanceBox.Text = ColorDistance.GetColorDistance(this.SourceImage, b).ToString();
                    pictureTarget.Image   = b;
                    pictureSource.Image   = SourceImage;
                    ImageSizeLabel.Text   = SourceImage.Width + " x " + SourceImage.Height;
                    //GetPaletteBitmap(palette);
                }
            }
            catch (NotSupportedException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
コード例 #6
0
        public int GetNearestColorIndexDither(Color colorTarget, int x, int y, ColorSpace colorSpace, Dither dither)
        {
            ColorDistance[] distances = new ColorDistance[Colors.Length];
            for (int i = 0; i < distances.Length; i++)
            {
                distances[i] = new ColorDistance(i, Colors[i], colorSpace.Distance(Colors[i], colorTarget));
            }

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < distances.Length - i - 1; j++)
                {
                    if (distances[j].Distance < distances[j + 1].Distance)
                    {
                        ColorDistance swap = distances[j];
                        distances[j]     = distances[j + 1];
                        distances[j + 1] = swap;
                    }
                }
            }

            Color color1 = distances[Colors.Length - 1].Color;
            Color color2 = distances[Colors.Length - 2].Color;
            float f      = distances[Colors.Length - 1].Distance + distances[13].Distance;
            float fLerp  = 0.0f;

            if (f > 0)
            {
                fLerp = FMath.Powf(distances[Colors.Length - 1].Distance / f, 1.0f);
            }

            return(dither.SelectDither(x, y, fLerp) == 0 ? distances[Colors.Length - 1].Index : distances[Colors.Length - 2].Index);
        }
コード例 #7
0
        public void TestGetVisualDistance()
        {
            Assert.AreEqual(0, ColorDistance.GetVisualDistance(new Color(1, 2, 3), new Color(1, 2, 3)));
            Assert.AreEqual(30 * 9 * 9 + 59 * 18 * 18 + 11 * 27 * 27, ColorDistance.GetVisualDistance(new Color(1, 2, 3), new Color(10, 20, 30)));

            Assert.AreEqual(30 * 1 * 1 + 59 * 2 * 2 + 11 * 3 * 3, ColorDistance.GetVisualDistance(new Color(1, 2, 3), new Color(0)));
            using (ColorDistance.UseCoeffs(5, 7, 13))
            {
                Assert.AreEqual(5 * 1 * 1 + 7 * 2 * 2 + 13 * 3 * 3, ColorDistance.GetVisualDistance(new Color(1, 2, 3), new Color(0)));
            }
            Assert.AreEqual(30 * 1 * 1 + 59 * 2 * 2 + 11 * 3 * 3, ColorDistance.GetVisualDistance(new Color(1, 2, 3), new Color(0)));

            Assert.AreEqual(new Color(123).GetVisualDistance(new Color(456)), new Color(456).GetVisualDistance(new Color(123)));
        }
コード例 #8
0
        private Image GetQuantizedImage(Image image)
        {
            if (image == null)
            {
                const String message = "Cannot quantize your file. Please choose a new file.";
                throw new ArgumentNullException(message);
            }

            Bitmap     bitmap     = (Bitmap)image;
            Rectangle  bounds     = Rectangle.FromLTRB(0, 0, bitmap.Width, bitmap.Height);
            BitmapData sourceData = bitmap.LockBits(bounds, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            try
            {
                int[] sourceBuffer = new int[image.Width];

                Int64 sourceOffset = sourceData.Scan0.ToInt64();

                for (int i = 0; i < image.Height; i++)
                {
                    Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width);

                    foreach (Color color in sourceBuffer.Select(argb => Color.FromArgb(argb)))
                    {
                        activeQuantizer.AddColor(color);
                    }

                    sourceOffset += sourceData.Stride;
                }
            }
            catch
            {
                bitmap.UnlockBits(sourceData);
                throw;
            }

            Bitmap result = new Bitmap(image.Width, image.Height, PixelFormat.Format8bppIndexed);

            try
            {
                List <Color> palette      = activeQuantizer.GetPalette(256);
                ColorPalette imagePalette = result.Palette;

                for (Int32 index = 0; index < palette.Count; index++)
                {
                    imagePalette.Entries[index] = palette[index];
                }

                PaletteBox.Image = GetPalette.GetPaletteBitmap(palette);
                result.Palette   = imagePalette;
            }
            catch (Exception)
            {
                bitmap.UnlockBits(sourceData);
                throw;
            }

            BitmapData targetData = result.LockBits(bounds, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

            try
            {
                Byte[] targetBuffer = new Byte[result.Width];
                int[]  sourceBuffer = new int[image.Width];

                Int64 sourceOffset = sourceData.Scan0.ToInt64();
                Int64 targetOffset = targetData.Scan0.ToInt64();

                for (int i = 0; i < image.Height; i++)
                {
                    Marshal.Copy(new IntPtr(sourceOffset), sourceBuffer, 0, image.Width);

                    for (int j = 0; j < image.Width; j++)
                    {
                        Color color = Color.FromArgb(sourceBuffer[j]);
                        targetBuffer[j] = (Byte)activeQuantizer.GetPaletteIndex(color);
                    }

                    Marshal.Copy(targetBuffer, 0, new IntPtr(targetOffset), result.Width);

                    sourceOffset += sourceData.Stride;
                    targetOffset += targetData.Stride;
                }
            }
            finally
            {
                bitmap.UnlockBits(sourceData);
                result.UnlockBits(targetData);
            }

            this.Result           = result;
            ColorDistanceBox.Text = ColorDistance.GetColorDistance(this.SourceImage, result).ToString();
            return(result);
        }