Exemplo n.º 1
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 (IFrameQuantizer <Rgba32> quantizer = werner.CreateFrameQuantizer <Rgba32>(this.Configuration))
            {
                Assert.NotNull(quantizer.Options.Dither);
            }

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

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

            using (IFrameQuantizer <Rgba32> quantizer = wu.CreateFrameQuantizer <Rgba32>(this.Configuration))
            {
                Assert.NotNull(quantizer.Options.Dither);
            }
        }
Exemplo n.º 2
0
        // 이미지를 팔레트화시킨다.
        // 팔레트에는 반드시 검은색과 흰색은 빠지도록 한다.
        private string ExecuteQuantize(string sourceFileName, int maxColor = 30)
        {
            var targetFileName = AppendToFileName(sourceFileName, "-Q");
            var quantizer      = new WuQuantizer(null, maxColor);
            var config         = Configuration.Default;

            using (var image = Image.Load <Rgba32>(sourceFileName))
                using (var quantizedResult = quantizer.CreateFrameQuantizer <Rgba32>(config).QuantizeFrame(image.Frames[0]))
                {
                    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 SixLabors.ImageSharp.Formats.Png.PngEncoder
                        {
                            Quantizer = new PaletteQuantizer(quantizedPalette.ToArray(), false),
                            ColorType = SixLabors.ImageSharp.Formats.Png.PngColorType.Palette,
                        };
                        image.SaveAsPng(stream, encoder);
                        stream.Close();
                    }

                    return(targetFileName);
                }
        }
Exemplo n.º 3
0
        public void WuQuantizerCanCreateFrameQuantizer()
        {
            var quantizer = new WuQuantizer();
            IFrameQuantizer <Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer <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.CreateFrameQuantizer <Rgba32>(Configuration.Default);

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

            quantizer = new WuQuantizer(new QuantizerOptions {
                Dither = KnownDitherings.Atkinson
            });
            frameQuantizer = quantizer.CreateFrameQuantizer <Rgba32>(Configuration.Default);
            Assert.NotNull(frameQuantizer);
            Assert.Equal(KnownDitherings.Atkinson, frameQuantizer.Options.Dither);
            frameQuantizer.Dispose();
        }
Exemplo n.º 4
0
        public void SinglePixelTransparent()
        {
            Configuration config    = Configuration.Default;
            var           quantizer = new WuQuantizer(false);

            using (var image = new Image <Rgba32>(config, 1, 1, default(Rgba32)))
                using (IQuantizedFrame <Rgba32> result = quantizer.CreateFrameQuantizer <Rgba32>(config).QuantizeFrame(image.Frames[0]))
                {
                    Assert.Equal(1, result.Palette.Length);
                    Assert.Equal(1, result.GetPixelSpan().Length);

                    Assert.Equal(default, result.Palette.Span[0]);
Exemplo n.º 5
0
        public void QuantizersDitherByDefault()
        {
            var palette = new PaletteQuantizer();
            var octree  = new OctreeQuantizer();
            var wu      = new WuQuantizer();

            Assert.NotNull(palette.Diffuser);
            Assert.NotNull(octree.Diffuser);
            Assert.NotNull(wu.Diffuser);

            Assert.True(palette.CreateFrameQuantizer <Rgba32>().Dither);
            Assert.True(octree.CreateFrameQuantizer <Rgba32>().Dither);
            Assert.True(wu.CreateFrameQuantizer <Rgba32>().Dither);
        }
Exemplo n.º 6
0
        public void SinglePixelOpaque()
        {
            Configuration config    = Configuration.Default;
            var           quantizer = new WuQuantizer(false);

            using (var image = new Image <Rgba32>(config, 1, 1, Color.Black))
                using (IQuantizedFrame <Rgba32> result = quantizer.CreateFrameQuantizer <Rgba32>(config).QuantizeFrame(image.Frames[0]))
                {
                    Assert.Equal(1, result.Palette.Length);
                    Assert.Equal(1, result.GetPixelSpan().Length);

                    Assert.Equal(Color.Black, (Color)result.Palette.Span[0]);
                    Assert.Equal(0, result.GetPixelSpan()[0]);
                }
        }
Exemplo n.º 7
0
        public void QuantizersDitherByDefault()
        {
            var werner  = new WernerPaletteQuantizer();
            var webSafe = new WebSafePaletteQuantizer();
            var octree  = new OctreeQuantizer();
            var wu      = new WuQuantizer();

            Assert.NotNull(werner.Diffuser);
            Assert.NotNull(webSafe.Diffuser);
            Assert.NotNull(octree.Diffuser);
            Assert.NotNull(wu.Diffuser);

            Assert.True(werner.CreateFrameQuantizer <Rgba32>(this.Configuration).Dither);
            Assert.True(webSafe.CreateFrameQuantizer <Rgba32>(this.Configuration).Dither);
            Assert.True(octree.CreateFrameQuantizer <Rgba32>(this.Configuration).Dither);
            Assert.True(wu.CreateFrameQuantizer <Rgba32>(this.Configuration).Dither);
        }
Exemplo n.º 8
0
        public void WuQuantizerYieldsCorrectTransparentPixel <TPixel>(TestImageProvider <TPixel> provider, bool dither)
            where TPixel : struct, IPixel <TPixel>
        {
            using (Image <TPixel> image = provider.GetImage())
            {
                Assert.True(image[0, 0].Equals(default(TPixel)));

                var quantizer = new WuQuantizer(dither);

                foreach (ImageFrame <TPixel> frame in image.Frames)
                {
                    QuantizedFrame <TPixel> quantized = quantizer.CreateFrameQuantizer <TPixel>().QuantizeFrame(frame);

                    int index = this.GetTransparentIndex(quantized);
                    Assert.Equal(index, quantized.GetPixelSpan()[0]);
                }
            }
        }
Exemplo n.º 9
0
        public void WuQuantizerCanCreateFrameQuantizer()
        {
            var quantizer = new WuQuantizer();
            IFrameQuantizer <Rgba32> frameQuantizer = quantizer.CreateFrameQuantizer <Rgba32>(Configuration.Default);

            Assert.NotNull(frameQuantizer);
            Assert.True(frameQuantizer.Dither);
            Assert.Equal(KnownDiffusers.FloydSteinberg, frameQuantizer.Diffuser);

            quantizer      = new WuQuantizer(false);
            frameQuantizer = quantizer.CreateFrameQuantizer <Rgba32>(Configuration.Default);

            Assert.NotNull(frameQuantizer);
            Assert.False(frameQuantizer.Dither);
            Assert.Null(frameQuantizer.Diffuser);

            quantizer      = new WuQuantizer(KnownDiffusers.Atkinson);
            frameQuantizer = quantizer.CreateFrameQuantizer <Rgba32>(Configuration.Default);
            Assert.NotNull(frameQuantizer);
            Assert.True(frameQuantizer.Dither);
            Assert.Equal(KnownDiffusers.Atkinson, frameQuantizer.Diffuser);
        }
Exemplo n.º 10
0
        public static byte[] GetXorImageBytesIndexedColor(this Image <Rgba32> image, IconEntryDescription description, bool asPng = false)
        {
            int bitCount  = description.BitCount;
            int numColors = 1 << bitCount;

            if (bitCount == 1)
            {
                image = image.Clone(x => x.BlackWhite());
            }

            if (asPng)
            {
                PngEncoder encoder = new PngEncoder
                {
                    BitDepth  = (PngBitDepth)bitCount,
                    ColorType = (PngColorType.Palette)
                };
                using (MemoryStream stream = new MemoryStream())
                {
                    image.SaveAsPng(stream, encoder);
                    stream.Seek(0, SeekOrigin.Begin);
                    return(stream.ToArray());
                }
            }

            WuQuantizer quantizer      = new WuQuantizer(Math.Min(255, numColors));
            var         frameQuantizer = quantizer.CreateFrameQuantizer <Rgba32>(new Configuration());
            var         quantizedFrame = frameQuantizer.QuantizeFrame(image.Frames[0]);

            Console.WriteLine($"quantizedFrame.Palette.Length = {quantizedFrame.Palette.Length}");
            int bitsInRow = quantizedFrame.Width * bitCount;
            // rows are always a multiple of 32 bits long
            // (padded with 0's if necessary)
            int remainder = bitsInRow % 32;
            int padding   = 0;

            if (remainder > 0)
            {
                Console.WriteLine($"remainder = {remainder}");
                padding    = 32 - remainder;
                bitsInRow += padding;
            }
            Console.WriteLine($"bitsInRow = {bitsInRow}");
            int bytesInRow = bitsInRow / 8;

            Console.WriteLine($"bytesInRow = {bytesInRow}");
            using (MemoryStream stream = new MemoryStream())
                using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream))
                {
                    foreach (Rgba32 pixel in quantizedFrame.Palette.ToArray())
                    {
                        writer.Write(pixel.B);
                        writer.Write(pixel.G);
                        writer.Write(pixel.R);
                        writer.Write(pixel.A);
                    }
                    for (int i = quantizedFrame.Palette.Length; i < numColors; i++)
                    {
                        writer.Write(0);
                    }
                    Console.WriteLine($"quantizedFrame.Height: {quantizedFrame.Height}");
                    Console.WriteLine($"quantizedFrame.Width: {quantizedFrame.Width}");

                    int valsPerByte = 8 / bitCount;
                    for (int r = 0; r < quantizedFrame.Height; r++)
                    {
                        var  rowVals      = quantizedFrame.GetRowSpan(quantizedFrame.Height - 1 - r);
                        byte b            = 0;
                        int  position     = valsPerByte - 1;
                        int  bytesWritten = 0;
                        foreach (byte val in rowVals)
                        {
                            b |= (byte)(val << (position * bitCount));
                            if (--position < 0)
                            {
                                writer.Write(b);
                                bytesWritten++;
                                b        = 0;
                                position = valsPerByte - 1;
                            }
                        }
                        Console.WriteLine($"bytesWritten = {bytesWritten}");
                        int extra = bytesInRow - bytesWritten;
                        Console.WriteLine($"extra = {extra}");
                        for (int i = 0; i < extra; i++)
                        {
                            writer.Write((byte)0);
                        }
                    }
                    stream.Seek(0, SeekOrigin.Begin);
                    return(stream.ToArray());
                };
        }