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:
parent
90efca76fa
commit
8f519432b8
1 changed files with 43 additions and 6 deletions
|
@ -27,28 +27,65 @@ public class SunImageWrapper extends ImageWrapper {
|
||||||
|
|
||||||
public void reduceColors (int colors) {
|
public void reduceColors (int colors) {
|
||||||
try {
|
try {
|
||||||
ColorReducer redux = new ColorReducer (colors, true);
|
int pixels[][] = getPixels();
|
||||||
img = redux.getColorReducedImage (img);
|
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) {
|
} 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) {
|
public void saveAs (String filename) {
|
||||||
try {
|
try {
|
||||||
if (filename.toLowerCase().endsWith (".gif")) {
|
if (filename.toLowerCase().endsWith (".gif")) {
|
||||||
// sun's jimi package doesn't encode gifs, use Acme encoder
|
// sun's jimi package doesn't encode gifs, use Acme encoder
|
||||||
FileOutputStream fout = new FileOutputStream (filename);
|
FileOutputStream fout = new FileOutputStream (filename);
|
||||||
|
// Acme gif encoder
|
||||||
GifEncoder enc = new GifEncoder (img, fout);
|
GifEncoder enc = new GifEncoder (img, fout);
|
||||||
enc.encode ();
|
enc.encode ();
|
||||||
|
// new alternative gif encoder
|
||||||
|
// GIFEncoder enc = new GIFEncoder (img);
|
||||||
|
// enc.Write (fout);
|
||||||
fout.close ();
|
fout.close ();
|
||||||
} else {
|
} else {
|
||||||
Jimi.putImage (img, filename);
|
Jimi.putImage (img, filename);
|
||||||
}
|
}
|
||||||
} catch (JimiException x) {
|
} catch (Exception x) {
|
||||||
throw new RuntimeException (x.getMessage ());
|
throw new RuntimeException (x.getMessage ());
|
||||||
} catch (IOException iox) {
|
|
||||||
throw new RuntimeException (iox.getMessage ());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue