From be94b98311b35cf83e66d156a3f48cb33eecae6f Mon Sep 17 00:00:00 2001 From: hns Date: Thu, 7 Aug 2003 13:04:05 +0000 Subject: [PATCH] New reduceColors(): try JIMI ColorReducer first, which preserves transparency, but throws an exception for some images. If that fails, use the alternative color reduction code from helma 1.2. --- src/helma/image/SunImageWrapper.java | 40 +++++++++++++++++----------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/helma/image/SunImageWrapper.java b/src/helma/image/SunImageWrapper.java index 30db9dfb..b1e0dd5c 100644 --- a/src/helma/image/SunImageWrapper.java +++ b/src/helma/image/SunImageWrapper.java @@ -44,28 +44,38 @@ public class SunImageWrapper extends ImageWrapper { } /** + * Reduce the colors used in this image. Useful and necessary before saving + * the image as GIF file. * - * - * @param colors ... + * @param colors the number of colors to use, usually <= 256. */ public void reduceColors(int colors) { try { - int[][] pixels = getPixels(); - int[] palette = Quantize.quantizeImage(pixels, colors); - int w = pixels.length; - int h = pixels[0].length; - int[] pix = new int[w * h]; + // first, try to use JIMI's ColorReducer class. It is able to + // preserve transparency on GIF files, but does throw exceptions on some GIFs. + img = new ColorReducer(colors, false).getColorReducedImage(img); + } catch (Exception excpt) { + // JIMI sometimes fails to reduce colors, throwing an exception. + // Use our alternative Quantizer in this case. + System.err.println("Using alternative color reducer ("+excpt+")"); + try { + int[][] pixels = getPixels(); + int[] palette = Quantize.quantizeImage(pixels, colors); + int w = pixels.length; + int h = pixels[0].length; + int[] pix = new int[w * h]; - // convert to RGB - for (int x = w; x-- > 0;) { - for (int y = h; y-- > 0;) { - pix[(y * w) + x] = palette[pixels[x][y]]; + // convert to RGB + for (int x = w; x-- > 0;) { + for (int y = h; y-- > 0;) { + pix[(y * w) + x] = palette[pixels[x][y]]; + } } - } - img = imggen.createImage(new MemoryImageSource(w, h, pix, 0, w)); - } catch (Exception x) { - // throw new RuntimeException (x.getMessage ()); + img = imggen.createImage(new MemoryImageSource(w, h, pix, 0, w)); + } catch (IOException ioxcpt) { + System.err.println("Error in reduceColors(): "+ioxcpt); + } } }