示例#1
0
        public void WuQuantizerCanCreateFrameQuantizer()
        {
            var quantizer = new WuQuantizer();
            IQuantizer <Rgba32> frameQuantizer = quantizer.CreatePixelSpecificQuantizer <Rgba32>(Configuration.Default);

            Assert.NotNull(frameQuantizer);
            Assert.NotNull(frameQuantizer.Options);
            Assert.Equal(QuantizerConstants.DefaultDither, frameQuantizer.Options.Dither);
            frameQuantizer.Dispose();

            quantizer = new WuQuantizer(new QuantizerOptions {
                Dither = null
            });
            frameQuantizer = quantizer.CreatePixelSpecificQuantizer <Rgba32>(Configuration.Default);

            Assert.NotNull(frameQuantizer);
            Assert.Null(frameQuantizer.Options.Dither);
            frameQuantizer.Dispose();

            quantizer = new WuQuantizer(new QuantizerOptions {
                Dither = KnownDitherings.Atkinson
            });
            frameQuantizer = quantizer.CreatePixelSpecificQuantizer <Rgba32>(Configuration.Default);
            Assert.NotNull(frameQuantizer);
            Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither);
            frameQuantizer.Dispose();
        }
示例#2
0
        public void QuantizersDitherByDefault()
        {
            var werner  = new WernerPaletteQuantizer();
            var webSafe = new WebSafePaletteQuantizer();
            var octree  = new OctreeQuantizer();
            var wu      = new WuQuantizer();

            Assert.NotNull(werner.Options.Dither);
            Assert.NotNull(webSafe.Options.Dither);
            Assert.NotNull(octree.Options.Dither);
            Assert.NotNull(wu.Options.Dither);

            using (IQuantizer <Rgba32> quantizer = werner.CreatePixelSpecificQuantizer <Rgba32>(this.Configuration))
            {
                Assert.NotNull(quantizer.Options.Dither);
            }

            using (IQuantizer <Rgba32> quantizer = webSafe.CreatePixelSpecificQuantizer <Rgba32>(this.Configuration))
            {
                Assert.NotNull(quantizer.Options.Dither);
            }

            using (IQuantizer <Rgba32> quantizer = octree.CreatePixelSpecificQuantizer <Rgba32>(this.Configuration))
            {
                Assert.NotNull(quantizer.Options.Dither);
            }

            using (IQuantizer <Rgba32> quantizer = wu.CreatePixelSpecificQuantizer <Rgba32>(this.Configuration))
            {
                Assert.NotNull(quantizer.Options.Dither);
            }
        }
示例#3
0
        // 이미지를 팔레트화시킨다.
        // 팔레트에는 반드시 검은색과 흰색은 빠지도록 한다.
        static string ExecuteQuantize(string sourceFileName, int maxColor = 30)
        {
            Logger.WriteLine($"Running {nameof(ExecuteQuantize)}");

            var targetFileName = AppendToFileName(sourceFileName, "-Q");

            var quOpts = new QuantizerOptions {
                MaxColors = maxColor
            };
            //var quantizer = new OctreeQuantizer(quOpts);
            var quantizer      = new WuQuantizer(quOpts);
            var config         = Configuration.Default;
            var pixelQuantizer = quantizer.CreatePixelSpecificQuantizer <Rgba32>(config);

            using (var image = Image.Load <Rgba32>(sourceFileName))
                using (var quantizedResult =
                           pixelQuantizer.BuildPaletteAndQuantizeFrame(image.Frames[0], image.Frames[0].Bounds()))
                {
                    var quantizedPalette = new List <Color>();
                    foreach (var p in quantizedResult.Palette.Span)
                    {
                        var c = new Color(p);
                        // 팔레트에 흰색과 검은색은 반드시 빠지도록 한다.
                        if (c != Color.White && c != Color.Black)
                        {
                            quantizedPalette.Add(c);
                        }
                    }

                    using (var stream = new FileStream(targetFileName, FileMode.Create))
                    {
                        var encoder = new PngEncoder
                        {
                            Quantizer = new PaletteQuantizer(quantizedPalette.ToArray()),
                            ColorType = PngColorType.Palette
                        };
                        image.SaveAsPng(stream, encoder);
                        stream.Close();
                    }

                    return(targetFileName);
                }
        }
示例#4
0
        public static ImageColors ProcessImage(Stream imageStream, string filename)
        {
            var image = Image.Load <Rgba32>(imageStream);

            try {
                if (image.Width > 800 || image.Height > 800)
                {
                    image.Mutate(i => i.Resize(800, 800));
                }

                var imageFrame = image.Frames[0];
                var quantizer  = new WuQuantizer(new QuantizerOptions {
                    MaxColors = 12
                });
                var frameQuantizer = quantizer.CreatePixelSpecificQuantizer <Rgba32>(Configuration.Default);

                frameQuantizer.BuildPaletteAndQuantizeFrame(imageFrame, image.Bounds());
                var quantizedColors = frameQuantizer.QuantizeFrame(imageFrame, image.Bounds());

                var paletteColors = quantizedColors.Palette;

                var frame       = image.Frames[0];
                var totalPixels = frame.Width * frame.Height;
                frame.TryGetSinglePixelSpan(out var pixels);

                var counter = new double[paletteColors.Length];

                foreach (var pixel in pixels)
                {
                    var closestDistance   = double.MaxValue;
                    var nearestColorIndex = -1;

                    for (var index = 0; index < paletteColors.Length; index++)
                    {
                        var paletteColor = paletteColors.Span[index];
                        var distance     = EuclideanDistance(paletteColor, pixel);

                        if (distance < closestDistance)
                        {
                            closestDistance   = distance;
                            nearestColorIndex = index;
                        }
                    }

                    counter[nearestColorIndex]++;
                }

                var colors = paletteColors.Span.ToArray().Select((color, idx) => {
                    var(hue, saturation, value) = ColorToHsv(color);

                    return(new ExtractedColor {
                        Hsv = new HsvColor(hue, saturation, value),
                        Hex = color.ToHex().Substring(0, 6),
                        Fraction = counter[idx] / totalPixels
                    });
                });

                return(new ImageColors {
                    Filename = filename,
                    Colors = colors.OrderByDescending(c => c.Fraction).ToList()
                });
            } finally {
                image?.Dispose();
            }
        }