} else {
icm = new IndexColorModel(depth, numColors, colorMap[0], colorMap[1], colorMap[2], colorMap[3]);
}
// create the indexed BufferedImage:
BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED, icm);
if (dither)
new DiffusionFilterOp().filter(image, dest);
else {
ClosestColor closest = new ClosestColor();
// convert to indexed color
byte[] dst = ((DataBufferByte) dest.getRaster().getDataBuffer()).getData();
// create a BufferedImage of only 1 pixel height for fetching
// the rows of the image in the correct format (ARGB)
// This speeds up things by more than factor 2, compared to the
// standard BufferedImage.getRGB solution
BufferedImage row = new BufferedImage(width, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = row.createGraphics();
int pixels[] = ((DataBufferInt) row.getRaster().getDataBuffer()).getData();
// make sure alpha values do not add up for each row:
g2d.setComposite(AlphaComposite.Src);
// calculate scanline by scanline in order to safe memory.
// It also seems to run faster like that
Node node;