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.

PdfRasterizer rasterizer = new PdfRasterizer("DocumentA.pdf");
GifImageFormat GifImageFormat = new GifImageFormat();
GifImageFormat.DitheringAlgorithm = DitheringAlgorithm.FloydSteinberg;
GifImageFormat.DitheringPercent = 100;
GifImageFormat.ColorPalette = Palette.WebPalette;
GifImageFormat.AntiAliasing = true;
FixedImageSize fixedImageSize = new FixedImageSize(595, 841);
rasterizer.Draw("gif-web-palette-output.gif", GifImageFormat, fixedImageSize);
Dim rasterizer As New PdfRasterizer("DocumentA.pdf")
Dim gifImageFormat As New GifImageFormat()
gifImageFormat.DitheringAlgorithm = DitheringAlgorithm.FloydSteinberg gifImageFormat.DitheringPercent = 100
gifImageFormat.ColorPalette = Palette.WebPalette
gifImageFormat.AntiAliasing = True
Dim fixedImageSize As New FixedImageSize(595, 841)
rasterizer.Draw("gif-web-palette-output.gif", gifImageFormat, fixedImageSize)

Windows Palette

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

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)

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.

The following example illustrates using a user palette.

PdfRasterizer rasterizer = new PdfRasterizer("DocumentA.pdf");
Color[] color = new Color[] { Color.FromArgb(255,0,0,0),
      Color.FromArgb(255,255,255,255),
      Color.FromArgb(255,255,0,0),
      Color.FromArgb(255,0,255,0),
      Color.FromArgb(255,0,0,255),
      Color.FromArgb(255,255,255,0),
      Color.FromArgb(255,0,255,255),
      Color.FromArgb(255,255,0,255) };

UserPalette userPalette = new UserPalette(color);
PngIndexedColorFormat pngIndexedColorFormat = new PngIndexedColorFormat(userPalette, 100, DitheringAlgorithm.FloydSteinberg);
PngImageFormat pngImageFormat = new PngImageFormat(pngIndexedColorFormat);
FixedImageSize fixedImageSize = new FixedImageSize(595, 841);
rasterizer.Draw("PngImageWithUserPalette.png", pngImageFormat, fixedImageSize);
Dim rasterizer As PdfRasterizer = New PdfRasterizer("DocumentA.pdf")
Dim pngIndexedColorFormat As PngIndexedColorFormat = New PngIndexedColorFormat(ImageFormat.DefaultColorPalette, 100, DitheringAlgorithm.FloydSteinberg)
Dim color() As ceTe.DynamicPDF.Rasterizer.Color = New ceTe.DynamicPDF.Rasterizer.Color() {
             ceTe.DynamicPDF.Rasterizer.Color.FromArgb(255, 0, 0, 0),
           ceTe.DynamicPDF.Rasterizer.Color.FromArgb(255, 255, 255, 255),
              ceTe.DynamicPDF.Rasterizer.Color.FromArgb(255, 255, 0, 0),
              ceTe.DynamicPDF.Rasterizer.Color.FromArgb(255, 0, 255, 0),
              ceTe.DynamicPDF.Rasterizer.Color.FromArgb(255, 0, 0, 255),
              ceTe.DynamicPDF.Rasterizer.Color.FromArgb(255, 255, 255, 0),
              ceTe.DynamicPDF.Rasterizer.Color.FromArgb(255, 0, 255, 255)}
Dim userpalette As UserPalette = New UserPalette(color)
pngIndexedColorFormat.ColorPalette = userpalette
Dim pngImageFormat As PngImageFormat = New PngImageFormat(pngIndexedColorFormat)
rasterizer.Draw("user-palette-output.png", pngImageFormat, ImageSize.Dpi72)

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.

Monochrome

The DynamicPDF Rasterizer also supports rasterizing PDFs to monochrome images.

PdfRasterizer rasterizer = new PdfRasterizer("DocumentA.pdf");
BmpMonochromeColorFormat bmpMonochromeColorFormat = new BmpMonochromeColorFormat(DitheringAlgorithm.FloydSteinberg, 100);
BmpImageFormat bmpImageFormat = new BmpImageFormat(bmpMonochromeColorFormat);
FixedImageSize fixedImageSize = new FixedImageSize(595, 841);
rasterizer.Draw("monochrom-image-output.bmp", bmpImageFormat, fixedImageSize);
Dim rasterizer As New PdfRasterizer("DocumentA.pdf")
Dim bmpMonochromeColorFormat As New BmpMonochromeColorFormat(DitheringAlgorithm.FloydSteinberg, 100)
Dim bmpImageFormat As New BmpImageFormat(bmpMonochromeColorFormat)
Dim fixedImageSize As New FixedImageSize(595, 841)
            rasterizer.Draw("monochrom-image-output.bmp", bmpImageFormat, fixedImageSize)

In this topic