code formating, slight speed improvments.

This commit is contained in:
lehni 2005-09-20 19:20:01 +00:00
parent 6385a1e0d1
commit f752601c16

View file

@ -26,6 +26,7 @@ import java.awt.image.IndexColorModel;
/* /*
* Modifications by Juerg Lehni: * Modifications by Juerg Lehni:
* *
* - Ported to Java from C
* - Support for alpha-channels. * - Support for alpha-channels.
* - Returns a BufferedImage of TYPE_BYTE_INDEXED with a IndexColorModel. * - Returns a BufferedImage of TYPE_BYTE_INDEXED with a IndexColorModel.
* - Dithering of images through helma.image.DiffusionFilterOp by setting * - Dithering of images through helma.image.DiffusionFilterOp by setting
@ -332,26 +333,17 @@ public class ColorQuantizer {
children[id].findClosestColor(red, green, blue, alpha, closest); children[id].findClosestColor(red, green, blue, alpha, closest);
if (uniqueCount != 0) { if (uniqueCount != 0) {
// Determine if this color is "closest". // Determine if this color is "closest".
int r = (cube.colorMap[0][colorIndex] & 0xff) - red; int dr = (cube.colorMap[0][colorIndex] & 0xff) - red;
int distance = r * r; int dg = (cube.colorMap[1][colorIndex] & 0xff) - green;
if (distance < closest.distance) { int db = (cube.colorMap[2][colorIndex] & 0xff) - blue;
int g = (cube.colorMap[1][colorIndex] & 0xff) - green; int da = (cube.colorMap[3][colorIndex] & 0xff) - alpha;
distance += g * g; int distance = da * da + dr * dr + dg * dg + db * db;
if (distance < closest.distance) {
int b = (cube.colorMap[2][colorIndex] & 0xff) - blue;
distance += b * b;
if (distance < closest.distance) {
int a = (cube.colorMap[3][colorIndex] & 0xff) - alpha;
distance += a * a;
if (distance < closest.distance) { if (distance < closest.distance) {
closest.distance = distance; closest.distance = distance;
closest.colorIndex = colorIndex; closest.colorIndex = colorIndex;
} }
} }
} }
}
}
}
int fillColorMap(byte colorMap[][], int index) { int fillColorMap(byte colorMap[][], int index) {
// Traverse any children. // Traverse any children.
@ -417,11 +409,9 @@ public class ColorQuantizer {
// Classify the first 256 colors to a tree depth of MAX_TREE_DEPTH. // Classify the first 256 colors to a tree depth of MAX_TREE_DEPTH.
int levelThreshold = MAX_TREE_DEPTH; int levelThreshold = MAX_TREE_DEPTH;
// create a BufferedImage of only 1 pixel height for fetching the // create a BufferedImage of only 1 pixel height for fetching the rows
// rows
// of the image in the correct format (ARGB) // of the image in the correct format (ARGB)
// This speeds up things by more than factor 2, compared to the // This speeds up things by more than factor 2, compared to the standard
// standard
// BufferedImage.getRGB solution // BufferedImage.getRGB solution
BufferedImage row = new BufferedImage(width, 1, BufferedImage.TYPE_INT_ARGB); BufferedImage row = new BufferedImage(width, 1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = row.createGraphics(); Graphics2D g2d = row.createGraphics();