diff --git a/src/helma/image/SunImageWrapper.java b/src/helma/image/SunImageWrapper.java index 7947fc35..4e1f59fd 100644 --- a/src/helma/image/SunImageWrapper.java +++ b/src/helma/image/SunImageWrapper.java @@ -27,28 +27,65 @@ public class SunImageWrapper extends ImageWrapper { public void reduceColors (int colors) { try { - ColorReducer redux = new ColorReducer (colors, true); - img = redux.getColorReducedImage (img); + 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]]; + } + } + img = imggen.createImage (new MemoryImageSource(w, h, pix, 0, w)); + // ColorReducer redux = new ColorReducer (colors, true); + // img = redux.getColorReducedImage (img); } catch (Exception x) { - throw new RuntimeException (x.getMessage ()); + // throw new RuntimeException (x.getMessage ()); } } + /** + * Snag the pixels from an image. + */ + int[][] getPixels () throws IOException { + int pix[] = new int[width * height]; + PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pix, 0, width); + try { + if (grabber.grabPixels() != true) { + throw new IOException("Grabber returned false: " + grabber.status()); + } + } catch (InterruptedException e) { + e.printStackTrace(); + } + int pixels[][] = new int[width][height]; + for (int x = width; x-- > 0; ) { + for (int y = height; y-- > 0; ) { + pixels[x][y] = pix[y * width + x]; + } + } + return pixels; + } + + public void saveAs (String filename) { try { if (filename.toLowerCase().endsWith (".gif")) { // sun's jimi package doesn't encode gifs, use Acme encoder FileOutputStream fout = new FileOutputStream (filename); + // Acme gif encoder GifEncoder enc = new GifEncoder (img, fout); enc.encode (); + // new alternative gif encoder + // GIFEncoder enc = new GIFEncoder (img); + // enc.Write (fout); fout.close (); } else { Jimi.putImage (img, filename); } - } catch (JimiException x) { + } catch (Exception x) { throw new RuntimeException (x.getMessage ()); - } catch (IOException iox) { - throw new RuntimeException (iox.getMessage ()); } }