예제 #1
0
        public static Bitmap Quantize(Bitmap bitmap, int colourCount)
        {
            var quantizer = new PaletteQuantizer();

            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    var colour = bitmap.GetPixel(x, y);
                    quantizer.AddColour(colour);
                }
            }
            quantizer.Quantize(colourCount);
            var ret = new Bitmap(bitmap.Width, bitmap.Height);

            for (int x = 0; x < bitmap.Width; x++)
            {
                for (int y = 0; y < bitmap.Height; y++)
                {
                    var colour = quantizer.GetQuantizedColour(bitmap.GetPixel(x, y));
                    ret.SetPixel(x, y, colour);
                }
            }
            return(ret);
        }
예제 #2
0
파일: BLP.cs 프로젝트: chipzz/libwarcraft
        /// <summary>
        /// Generates an indexed 256-color palette from the specified image and overwrites the current palette with it.
        /// Ordinarily, this would be the original mipmap.
        /// </summary>
        /// <param name="inImage">Image.</param>
        private void GeneratePalette(Image inImage)
        {
            // TODO: Replace with an algorithm that produces a better result. For now, it works.
            PaletteQuantizer quantizer = new PaletteQuantizer(new ArrayList());

            using (Bitmap quantizedMap = quantizer.Quantize(inImage))
            {
                this.Palette.Clear();
                this.Palette.AddRange(quantizedMap.Palette.Entries);
            }
        }
예제 #3
0
        public void PaletteQuantizerYieldsCorrectTransparentPixel <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)));

                IQuantizer <TPixel> quantizer = new PaletteQuantizer <TPixel> {
                    Dither = dither
                };

                foreach (ImageFrame <TPixel> frame in image.Frames)
                {
                    QuantizedImage <TPixel> quantized = quantizer.Quantize(frame, 256);

                    int index = this.GetTransparentIndex(quantized);
                    Assert.Equal(index, quantized.Pixels[0]);
                }
            }
        }