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 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 reduces the number of distinct colors used in an image (based on a specified or automatically-found color palette) without significantly affecting image quality. A quantized image contains fewer colors and results in a smaller file size than the original image while keeping the visual essence of the original true-colored image intact. Color quantization is a lossy process.
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. 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 determine the palette (Auto Palette) automatically.
Web Palette
The web palette contains 216 web-safe colors. Use this palette to create images guaranteed to display on any web page. These 216 colors are recognized by all web browsers and operating systems, ensuring these colors look similar 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 DynamicPDF Rasterizer uses to create indexed images. The auto palette uses a quantization algorithm to determine the best color palette based on existing image-specific colors. The auto palette can use either the Octree or Medium Cut algorithms to pick a color palette and has the option to specify the number of colors used in creating the output image. The quality of the output image is better when using an auto palette than other palettes. By default, the Auto Palette uses the Octree quantization algorithm and a maximum palette size 256.
Using an auto palette produces the best image quality.
The following example illustrates rasterizing a PDF document into an indexed PNG image using the windows color palette. It uses the PngIndexedColorFormat class along with the PngImageFormat class.
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)