ERRDIFF

Compress Image with Error Diffusion


EnvironmentsPYTHON :: EASI :: MODELER
Batch ModeYes
Quick linksDescription :: Parameters :: Parameter descriptions :: Details :: Algorithm :: Related

Back to top

Description


ERRDIFF compresses a 24-bit RGB image into a single 8-bit image, based on a pseudocolor table (PCT) that you provide. An error diffusion algorithm improves the quality of the output image. ERRDIFF can be used when color images must be exported to other hardware or software systems without 24-bit color support.
Back to top

Parameters


Name Type Length Value range
FILE * String 1 -    
Input: Input channels or layers  * Raster port 3 - 3  
Output: Output channel or layer  * Raster port 1 - 1  
InputPCT: Input pseudocolor segment or layer  * PCT port 1 - 1  

* Required parameter
Back to top

Parameter descriptions

FILE

Specifies the name of the PCIDSK image file that contains the imagery to be compressed.

Input: Input channels or layers

Specifies the three 8-bit channels or layers that contain the RGB image to be compressed.

Output: Output channel or layer

Specifies the single 8-bit black and white channel or layer that receives the compressed image.

InputPCT: Input pseudocolor segment or layer

Specifies the PCT segment or layer that supplies the set of available colors to which the image is compressed.

Back to top

Details

ERRDIFF compresses a 24-bit color (RGB) image into a single 8-bit channel, based on a PCT (segment type: 171) that you provide. The PCT is used to color a compressed image so that it looks similar to the original 24-bit color image. This capability can be used when preparing color images for exporting to other third-party software (for example, a GIS system with limited color capability).

ERRDIFF can be used when color images must be exported to other hardware or software systems without 24-bit color support.

The error diffusion algorithm used by ERRDIFF attempts to select a nearby color in the PCT, (but not always the nearest one) in proportions such that, at a distance (as the pixels merge visually), a better approximation of the color of a region is achieved.

The input file contains the input RGB channels and the channel to which to write the compressed result. If the imagery is to be enhanced with a lookup table (LUT), apply it to the RGB channels ahead of time with the LUT function.

The input RGB channels and the output compressed channel must be 8-bit because of the nature of a PCT. Although any type of image channel can be used, values are internally converted to 8-bit data. Using non 8-bit data can result in unexpected or poor results.

Back to top

Algorithm

The error diffusion algorithm used by ERRDIFF is based on the concept of remembering the cumulative error in approximating the error for each pixel and attempting to err in the opposite direction in red, green, and blue image space for the next pixel.

For each pixel, the following process takes place.

  1. Subtract the RGB error values previously accumulated to the RGB input value for this pixel (P1) to give a modified target color (MP1).
  2. Find the nearest color in the PCT table to this modified RGB value (MP1) and designate the nearest color AP1.
  3. Subtract the RGB value of the selected entry in PCT (AP1) from the RGB value (MP1) to give a red, green, and blue error value.
  4. Add one sixth of the error to the error accumulations for the bottom-left (P3) pixel and the bottom-right (P5) pixel from this pixel. Add one third of the error to the pixel immediately below the current pixel (P4). Add the remainder of the error (approximately one third, depending on rounding) to the pixel to the right (P2) of the current pixel.

            P1 -- P2
          / | \
         /  |  \
       P3   P4  P5

Finding the nearest color in the PCT to an RGB color is performed by building a large three-dimensional table with red, green, and blue values as the indices. This table is populated in advance with the nearest color for each possible RGB value to avoid computing by exhaustive comparison for each pixel in the image.

© PCI Geomatics Enterprises, Inc.®, 2026. All rights reserved.