Color Quantization and Color Palettes

Reducing the number of distinct colors used in an image is called color quantization. It is a technique for optimally reducing the number of colors used to create an image. Images with color quantization applied are smaller in size and contain fewer colors than non-color quantized images. Color palettes are applied to indexed images. DynamicPDF Rasterizer supports both color quantization and color palettes.

Color Quantization

Color Quantization is a process that reduces the number of distinct colors used in an image (based on a specified or automatically-found color palette) without greatly affecting image quality. A quantized image contains fewer colors and results in a smaller file size when compared to the original image, while keeping the visual essence of the original true-colored image intact. Color quantization is a lossy process.

NOTE: You can also apply dithering to quantized images to improve the quality of the final image.

Color Palettes

When creating an indexed image using DynamicPDF Rasterizer, the number of distinct colors is reduced to 256 or less. You reduce your document's colors to 256 by using one of the predefined color palettes (Web or Windows Palette), a custom palette (User Palette), or by using an algorithm to automatically determine the palette (Auto Palette).

Web Palette

The web palette contains 216 web safe colors . You can use this palette to create images guaranteed to display in any web page. These 216 colors are recognized by all web browsers and operating systems, ensuring these colors look similarly in any browser.

Windows Palette

The windows palette contains 216 web safe colors and 20 windows specific colors .This palette is suitable for creating ordinary images.

User Palette

The user palette, or custom palette, provides complete freedom to users to select the colors and the number of colors present in an image. When applying a user palette, the resulting image only contains the user colors specified.

Auto Palette

The auto palette is the default color palette used within DynamicPDF Rasterizer to create indexed images. The auto palette uses a quantization algorithm to automatically determine the best color palette to use based on existing image specific colors. The auto palette can use either the Octree or Medium Cut algorithms to determine a color palette and has an option to specify the number of colors used in creating the output image. The quality of the output image is better when using auto palette as compared to using other palettes. By default, the Auto Palette uses the Octree quantization algorithm and a maximum palette size of 256.

The following example illustrates rasterizing a PDF document into an indexed PNG image using the windows color palette.

PdfRasterizer rasterizer = new PdfRasterizer(pdfFilePath);
PngIndexedColorFormat pngIndexedColorFormat = new PngIndexedColorFormat(Palette.WindowsPalette);
PngImageFormat pngIndexed = new PngImageFormat(pngIndexedColorFormat);
rasterizer.Draw(pngFilePath, pngIndexed, ImageSize.Dpi72);        
Dim rasterizer As New PdfRasterizer(pdfFilePath)
Dim pngIndexedColorFormat As New PngIndexedColorFormat(Palette.WindowsPalette)
Dim pngIndexed As New PngImageFormat(pngIndexedColorFormat)
rasterizer.Draw(pngFilePath, pngIndexed, ImageSize.Dpi72)

In this topic