Use new color quantizer for reduceColors() which requires a bit more code Ë"

but should be faster. Add code to use new GIFEncoder class to make some performance comparisons.
This commit is contained in:
hns 2002-10-02 09:41:40 +00:00
parent 90efca76fa
commit 8f519432b8

View file

@ -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 ());
}
}