AForge.Imaging.ColorReduction.ColorImageQuantizer.CalculatePalette C# (CSharp) Méthode

CalculatePalette() public méthode

Calculate reduced color palette for the specified image.

The method processes the specified image and feeds color value of each pixel to the specified color quantization algorithm. Finally it returns color palette built by that algorithm.

Unsupported format of the source image - it must 24 or 32 bpp color image.
public CalculatePalette ( UnmanagedImage image, int paletteSize ) : Color[]
image AForge.Imaging.UnmanagedImage Image to calculate palette for.
paletteSize int Palette size to calculate.
Résultat Color[]
        public Color[] CalculatePalette( UnmanagedImage image, int paletteSize )
        {
            if ( ( image.PixelFormat != PixelFormat.Format24bppRgb ) &&
                 ( image.PixelFormat != PixelFormat.Format32bppRgb ) &&
                 ( image.PixelFormat != PixelFormat.Format32bppArgb ) &&
                 ( image.PixelFormat != PixelFormat.Format32bppPArgb ) )
            {
                throw new UnsupportedImageFormatException( "Unsupported format of the source image." );
            }

            quantizer.Clear( );

            int width = image.Width;
            int height = image.Height;

            int pixelSize = Bitmap.GetPixelFormatSize( image.PixelFormat ) / 8;

            unsafe
            {
                byte* ptr = (byte*) image.ImageData.ToPointer( );
                int offset = image.Stride - width * pixelSize;

                for ( int y = 0; y < height; y++ )
                {
                    for ( int x = 0; x < width; x++, ptr += pixelSize )
                    {
                        quantizer.AddColor( Color.FromArgb( ptr[RGB.R], ptr[RGB.G], ptr[RGB.B] ) );
                    }

                    ptr += offset;
                }
            }

            return quantizer.GetPalette( paletteSize );
        }

Same methods

ColorImageQuantizer::CalculatePalette ( Bitmap image, int paletteSize ) : Color[]

Usage Example

        private Bitmap reducedColor(Bitmap image, int numColors)
        {
            // create color image quantization routine
            ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer());
            // create 16 colors table
            Color[] colorTable = ciq.CalculatePalette(image, numColors);
            // create dithering routine
            FloydSteinbergColorDithering dithering = new FloydSteinbergColorDithering();
            dithering.ColorTable = colorTable;
            // apply the dithering routine
            Bitmap newImage = dithering.Apply(image);

            return newImage;
        }
All Usage Examples Of AForge.Imaging.ColorReduction.ColorImageQuantizer::CalculatePalette