public CalculatePalette ( |
||
image | 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 );
}
ColorImageQuantizer::CalculatePalette ( |
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; }