Class ArbitraryPaletteQuantizer

  • All Implemented Interfaces:
    RGBQuantizer, RGBIndex

    public class ArbitraryPaletteQuantizer
    extends ImageToImageOperation
    implements RGBIndex, RGBQuantizer
    A color quantizer that maps an RGBImage to any given palette. This operation is restricted to RGB24Image and palettes with up to 256 colors. It picks the color from the palette which is closest to the color to be quantized (with the minimum distance). This is a rather naive implementation which, for any given color to be quantized, computes the distance between it and each color in the palette (read: this operation is rather slow with a large palette and input image).

    It uses Manhattan distance (L1) instead of Euclidean distance (L2). This saves a square root operation per distance computation.

    There are more sophisticated nearest neighbor algorithms available, left for future extensions.

    Usage example

    This example maps an RGB truecolor image to some palette we create.

     RGB24Image image = ...; // initialize this
     // create some Palette object that you want to map the image to
     Palette palette = new Palette(3); // create palette with three entries
     palette.put(0, 33, 00, 244); // set first color
     palette.put(1, 0, 240, 193); // set second color
     palette.put(2, 245, 126, 136); // set third color
     ArbitraryPaletteQuantizer quantizer = new ArbitraryPaletteQuantizer(palette);
     quantizer.setInputImage(image);
     quantizer.process();
     PixelImage quantizedImage = quantizer.getOutputImage();
     
    Since:
    0.5.0
    Author:
    Marco Schmidt
    • Field Detail

      • RED

        private final int[] RED
      • GREEN

        private final int[] GREEN
      • BLUE

        private final int[] BLUE
      • numEntries

        private int numEntries
    • Constructor Detail

      • ArbitraryPaletteQuantizer

        public ArbitraryPaletteQuantizer​(Palette palette)
        Creates a quantizer that will be able to map pixels (or a complete image) to the argument palette.
    • Method Detail

      • createPalette

        public Palette createPalette()
        Returns a copy of the palette that was given to the constructor of this class.
        Specified by:
        createPalette in interface RGBQuantizer
        Returns:
        new copy of the palette this quantizer works on
      • map

        public int map​(int[] origRgb,
                       int[] quantizedRgb)
        Description copied from interface: RGBQuantizer
        This method maps a triplet of intensity values to its quantized counterpart and returns the palette index of that quantized color. The index values for the two arrays are taken from RGBIndex.
        Specified by:
        map in interface RGBQuantizer
        Parameters:
        origRgb - the three samples red, green and blue for which a good match is searched in the palette
        quantizedRgb - will hold the three samples found to be closest to origRgb after the call to this method
        Returns:
        int index in the palette of the match quantizedRgb
      • map

        public int map​(int red,
                       int green,
                       int blue)
        Finds the best match for the argument color in the palette and returns its index. Similar to map(int[], int[]), the quantized color is not required and thus a few assignemnts are saved by this method.
        Parameters:
        red - red sample of the pixel to be quantized
        green - green sample of the pixel to be quantized
        blue - blue sample of the pixel to be quantized
        Returns:
        index of the color in the palette that is closest to the argument color