code: public static RenderedImage createIndexedImage(RenderedImage src) {
          PaletteBuilder pb = new PaletteBuilder(src);
          pb.buildPalette();
          return pb.getIndexedImage();
      }
docs: Creates an image representing given image <code>src</code> using <code>IndexColorModel</code>. Lossless
  conversion is not always possible (e.g. if number of colors in the given image exceeds maximum palette size).
  Result image then is an approximation constructed by octree quantization method.
  
  @exception IllegalArgumentException if <code>src</code> is <code>null</code>.
  @exception UnsupportedOperationException if implemented method is unable to create approximation of
  <code>src</code> and <code>canCreatePalette</code> returns <code>false</code>.
  @see createIndexColorModel
  @see canCreatePalette