Class QuantizeFilter

java.lang.Object
bdsup2sub.tools.QuantizeFilter

public class QuantizeFilter extends Object
A filter which quantizes an image to a set number of colors - useful for producing images which are to be encoded using an index color model. The filter can perform Floyd-Steinberg error-diffusion dithering if required. At present, the quantization is done using an octtree algorithm but I eventually hope to add more quantization methods such as median cut. Note: at present, the filter produces an image which uses the RGB color model (because the application it was written for required it). I hope to extend it to produce an IndexColorModel by request.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
     
    private static final int[]
    Floyd-Steinberg dithering matrix.
    private int
     
    private boolean
     
    private static final int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static int
    clamp(int c)
    Clamp a value to the range 0..255
    boolean
    Return the dithering setting
    int
    Get the number of colors to quantize to.
    boolean
    Return the serpentine setting
    int[]
    quantize(int[] inPixels, byte[] outPixels, int width, int height, int numColors, boolean dither, boolean serpentine)
    Quantize picture
    void
    setDither(boolean dither)
    Set whether to use dithering or not.
    void
    setNumColors(int numColors)
    Set the number of colors to quantize to.
    void
    setSerpentine(boolean serpentine)
    Set whether to use a serpentine pattern for return or not.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FS_MATRIX

      private static final int[] FS_MATRIX
      Floyd-Steinberg dithering matrix.
    • SUM

      private static final int SUM
      See Also:
    • dither

      private boolean dither
    • numColors

      private int numColors
    • serpentine

      private boolean serpentine
  • Constructor Details

    • QuantizeFilter

      public QuantizeFilter()
  • Method Details

    • setNumColors

      public void setNumColors(int numColors)
      Set the number of colors to quantize to.
      Parameters:
      numColors - Number of colors. The default is 256.
    • clamp

      private static int clamp(int c)
      Clamp a value to the range 0..255
      Parameters:
      c - Value to clamp
      Returns:
      Clamped value
    • getNumColors

      public int getNumColors()
      Get the number of colors to quantize to.
      Returns:
      Number of colors.
    • setDither

      public void setDither(boolean dither)
      Set whether to use dithering or not. If not, the image is posterized.
      Parameters:
      dither - True to use dithering
    • getDither

      public boolean getDither()
      Return the dithering setting
      Returns:
      Current dithering setting
    • setSerpentine

      public void setSerpentine(boolean serpentine)
      Set whether to use a serpentine pattern for return or not. This can reduce 'avalanche' artifacts in the output.
      Parameters:
      serpentine - True to use serpentine pattern
    • getSerpentine

      public boolean getSerpentine()
      Return the serpentine setting
      Returns:
      Current serpentine setting
    • quantize

      public int[] quantize(int[] inPixels, byte[] outPixels, int width, int height, int numColors, boolean dither, boolean serpentine)
      Quantize picture
      Parameters:
      inPixels - Array of RGBA pixels to quantize
      outPixels - Array with quantized palette entries
      width - Width of image
      height - Height of image
      numColors - Number of colors used
      dither - Use dithering?
      serpentine - Use serpentine for dithering?
      Returns:
      Integer array containing palette information