code: public static IndexColorModel createIndexColorModel(RenderedImage img) {
          PaletteBuilder pb = new PaletteBuilder(img);
          pb.buildPalette();
          return pb.getIndexColorModel();
      }
docs: Creates an palette representing colors from given image <code>img</code>. If number of colors in the given image
  exceeds maximum palette size closest colors would be merged.
  
  @exception IllegalArgumentException if <code>img</code> is <code>null</code>.
  @exception UnsupportedOperationException if implemented method is unable to create approximation of
  <code>img</code> and <code>canCreatePalette</code> returns <code>false</code>.
  @see createIndexedImage
  @see canCreatePalette