TMC-XXXX: Code cleaun-up and fixed spelling errors.

This commit is contained in:
Harald Kuhr 2013-02-28 13:08:19 +01:00
parent 0319a6f84c
commit dd0f382d3c
3 changed files with 59 additions and 57 deletions

View File

@ -292,20 +292,20 @@ public class DiffusionDither implements BufferedImageOp, RasterOp {
// When reference for column, add 1 to reference as this buffer is // When reference for column, add 1 to reference as this buffer is
// offset from actual column position by one to allow FS to not check // offset from actual column position by one to allow FS to not check
// left/right edge conditions // left/right edge conditions
int[][] mCurrErr = new int[width + 2][3]; int[][] currErr = new int[width + 2][3];
int[][] mNextErr = new int[width + 2][3]; int[][] nextErr = new int[width + 2][3];
// Random errors in [-1 .. 1] - for first row // Random errors in [-1 .. 1] - for first row
for (int i = 0; i < width + 2; i++) { for (int i = 0; i < width + 2; i++) {
// Note: This is broken for the strange cases where nextInt returns Integer.MIN_VALUE // Note: This is broken for the strange cases where nextInt returns Integer.MIN_VALUE
/* /*
mCurrErr[i][0] = (Math.abs(RANDOM.nextInt()) % (FS_SCALE * 2)) - FS_SCALE; currErr[i][0] = (Math.abs(RANDOM.nextInt()) % (FS_SCALE * 2)) - FS_SCALE;
mCurrErr[i][1] = (Math.abs(RANDOM.nextInt()) % (FS_SCALE * 2)) - FS_SCALE; currErr[i][1] = (Math.abs(RANDOM.nextInt()) % (FS_SCALE * 2)) - FS_SCALE;
mCurrErr[i][2] = (Math.abs(RANDOM.nextInt()) % (FS_SCALE * 2)) - FS_SCALE; currErr[i][2] = (Math.abs(RANDOM.nextInt()) % (FS_SCALE * 2)) - FS_SCALE;
*/ */
mCurrErr[i][0] = RANDOM.nextInt(FS_SCALE * 2) - FS_SCALE; currErr[i][0] = RANDOM.nextInt(FS_SCALE * 2) - FS_SCALE;
mCurrErr[i][1] = RANDOM.nextInt(FS_SCALE * 2) - FS_SCALE; currErr[i][1] = RANDOM.nextInt(FS_SCALE * 2) - FS_SCALE;
mCurrErr[i][2] = RANDOM.nextInt(FS_SCALE * 2) - FS_SCALE; currErr[i][2] = RANDOM.nextInt(FS_SCALE * 2) - FS_SCALE;
} }
// Temp buffers // Temp buffers
@ -318,10 +318,10 @@ public class DiffusionDither implements BufferedImageOp, RasterOp {
// Loop through image data // Loop through image data
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
// Clear out next error rows for colour errors // Clear out next error rows for colour errors
for (int i = mNextErr.length; --i >= 0;) { for (int i = nextErr.length; --i >= 0;) {
mNextErr[i][0] = 0; nextErr[i][0] = 0;
mNextErr[i][1] = 0; nextErr[i][1] = 0;
mNextErr[i][2] = 0; nextErr[i][2] = 0;
} }
// Set up start column and limit // Set up start column and limit
@ -348,7 +348,7 @@ public class DiffusionDither implements BufferedImageOp, RasterOp {
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
// Make a 28.4 FP number, add Error (with fraction), // Make a 28.4 FP number, add Error (with fraction),
// rounding and truncate to int // rounding and truncate to int
inRGB[i] = ((inRGB[i] << 4) + mCurrErr[x + 1][i] + 0x08) >> 4; inRGB[i] = ((inRGB[i] << 4) + currErr[x + 1][i] + 0x08) >> 4;
// Clamp // Clamp
if (inRGB[i] > 255) { if (inRGB[i] > 255) {
@ -384,26 +384,26 @@ public class DiffusionDither implements BufferedImageOp, RasterOp {
if (forward) { if (forward) {
// Row 1 (y) // Row 1 (y)
// Update error in this pixel (x + 1) // Update error in this pixel (x + 1)
mCurrErr[x + 2][0] += diff[0] * 7; currErr[x + 2][0] += diff[0] * 7;
mCurrErr[x + 2][1] += diff[1] * 7; currErr[x + 2][1] += diff[1] * 7;
mCurrErr[x + 2][2] += diff[2] * 7; currErr[x + 2][2] += diff[2] * 7;
// Row 2 (y + 1) // Row 2 (y + 1)
// Update error in this pixel (x - 1) // Update error in this pixel (x - 1)
mNextErr[x][0] += diff[0] * 3; nextErr[x][0] += diff[0] * 3;
mNextErr[x][1] += diff[1] * 3; nextErr[x][1] += diff[1] * 3;
mNextErr[x][2] += diff[2] * 3; nextErr[x][2] += diff[2] * 3;
// Update error in this pixel (x) // Update error in this pixel (x)
mNextErr[x + 1][0] += diff[0] * 5; nextErr[x + 1][0] += diff[0] * 5;
mNextErr[x + 1][1] += diff[1] * 5; nextErr[x + 1][1] += diff[1] * 5;
mNextErr[x + 1][2] += diff[2] * 5; nextErr[x + 1][2] += diff[2] * 5;
// Update error in this pixel (x + 1) // Update error in this pixel (x + 1)
// TODO: Consider calculating this using // TODO: Consider calculating this using
// error term = error - sum(error terms 1, 2 and 3) // error term = error - sum(error terms 1, 2 and 3)
// See Computer Graphics (Foley et al.), p. 573 // See Computer Graphics (Foley et al.), p. 573
mNextErr[x + 2][0] += diff[0]; // * 1; nextErr[x + 2][0] += diff[0]; // * 1;
mNextErr[x + 2][1] += diff[1]; // * 1; nextErr[x + 2][1] += diff[1]; // * 1;
mNextErr[x + 2][2] += diff[2]; // * 1; nextErr[x + 2][2] += diff[2]; // * 1;
// Next // Next
x++; x++;
@ -417,26 +417,26 @@ public class DiffusionDither implements BufferedImageOp, RasterOp {
else { else {
// Row 1 (y) // Row 1 (y)
// Update error in this pixel (x - 1) // Update error in this pixel (x - 1)
mCurrErr[x][0] += diff[0] * 7; currErr[x][0] += diff[0] * 7;
mCurrErr[x][1] += diff[1] * 7; currErr[x][1] += diff[1] * 7;
mCurrErr[x][2] += diff[2] * 7; currErr[x][2] += diff[2] * 7;
// Row 2 (y + 1) // Row 2 (y + 1)
// Update error in this pixel (x + 1) // Update error in this pixel (x + 1)
mNextErr[x + 2][0] += diff[0] * 3; nextErr[x + 2][0] += diff[0] * 3;
mNextErr[x + 2][1] += diff[1] * 3; nextErr[x + 2][1] += diff[1] * 3;
mNextErr[x + 2][2] += diff[2] * 3; nextErr[x + 2][2] += diff[2] * 3;
// Update error in this pixel (x) // Update error in this pixel (x)
mNextErr[x + 1][0] += diff[0] * 5; nextErr[x + 1][0] += diff[0] * 5;
mNextErr[x + 1][1] += diff[1] * 5; nextErr[x + 1][1] += diff[1] * 5;
mNextErr[x + 1][2] += diff[2] * 5; nextErr[x + 1][2] += diff[2] * 5;
// Update error in this pixel (x - 1) // Update error in this pixel (x - 1)
// TODO: Consider calculating this using // TODO: Consider calculating this using
// error term = error - sum(error terms 1, 2 and 3) // error term = error - sum(error terms 1, 2 and 3)
// See Computer Graphics (Foley et al.), p. 573 // See Computer Graphics (Foley et al.), p. 573
mNextErr[x][0] += diff[0]; // * 1; nextErr[x][0] += diff[0]; // * 1;
mNextErr[x][1] += diff[1]; // * 1; nextErr[x][1] += diff[1]; // * 1;
mNextErr[x][2] += diff[2]; // * 1; nextErr[x][2] += diff[2]; // * 1;
// Previous // Previous
x--; x--;
@ -450,9 +450,9 @@ public class DiffusionDither implements BufferedImageOp, RasterOp {
// Make next error info current for next iteration // Make next error info current for next iteration
int[][] temperr; int[][] temperr;
temperr = mCurrErr; temperr = currErr;
mCurrErr = mNextErr; currErr = nextErr;
mNextErr = temperr; nextErr = temperr;
// Toggle direction // Toggle direction
if (alternateScans) { if (alternateScans) {

View File

@ -39,7 +39,7 @@ import java.util.Hashtable;
* *
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a> * @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haku $ * @author last modified by $Author: haku $
* @version $Id: //depot/branches/personal/haraldk/twelvemonkeys/release-2/twelvemonkeys-core/src/main/java/com/twelvemonkeys/image/ImageUtil.java#3 $ * @version $Id: common/common-image/src/main/java/com/twelvemonkeys/image/ImageUtil.java#3 $
*/ */
public final class ImageUtil { public final class ImageUtil {
// TODO: Split palette generation out, into ColorModel classes (?) // TODO: Split palette generation out, into ColorModel classes (?)
@ -845,11 +845,13 @@ public final class ImageUtil {
BufferedImage temp = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); BufferedImage temp = new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
if (cm instanceof IndexColorModel && pHints == Image.SCALE_SMOOTH) { if (cm instanceof IndexColorModel && pHints == Image.SCALE_SMOOTH) {
// TODO: DiffusionDither does not support transparency at the moment, this will create bad results
new DiffusionDither((IndexColorModel) cm).filter(scaled, temp); new DiffusionDither((IndexColorModel) cm).filter(scaled, temp);
} }
else { else {
drawOnto(temp, scaled); drawOnto(temp, scaled);
} }
scaled = temp; scaled = temp;
//long end = System.currentTimeMillis(); //long end = System.currentTimeMillis();
//System.out.println("Time: " + (end - start) + " ms"); //System.out.println("Time: " + (end - start) + " ms");

View File

@ -96,7 +96,7 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
* This class implements an adaptive pallete generator to reduce images * This class implements an adaptive palette generator to reduce images
* to a variable number of colors. * to a variable number of colors.
* It can also render images into fixed color pallettes. * It can also render images into fixed color pallettes.
* <p/> * <p/>
@ -589,7 +589,7 @@ class IndexImage {
/** /**
* Gets an {@code IndexColorModel} from the given image. If the image has an * Gets an {@code IndexColorModel} from the given image. If the image has an
* {@code IndexColorModel}, this will be returned. Otherwise, an {@code IndexColorModel} * {@code IndexColorModel}, this will be returned. Otherwise, an {@code IndexColorModel}
* is created, using an adaptive pallete. * is created, using an adaptive palette.
* *
* @param pImage the image to get {@code IndexColorModel} from * @param pImage the image to get {@code IndexColorModel} from
* @param pNumberOfColors the number of colors for the {@code IndexColorModel} * @param pNumberOfColors the number of colors for the {@code IndexColorModel}
@ -637,7 +637,7 @@ class IndexImage {
// We now have at least a buffered image, create model from it // We now have at least a buffered image, create model from it
if (icm == null) { if (icm == null) {
icm = createIndexColorModel(ImageUtil.toBuffered(image), pNumberOfColors, pHints); icm = createIndexColorModel(ImageUtil.toBuffered(image), pNumberOfColors, pHints);
} }
else if (!(icm instanceof InverseColorMapIndexColorModel)) { else if (!(icm instanceof InverseColorMapIndexColorModel)) {
// If possible, use faster code // If possible, use faster code
icm = new InverseColorMapIndexColorModel(icm); icm = new InverseColorMapIndexColorModel(icm);
@ -648,7 +648,7 @@ class IndexImage {
/** /**
* Creates an {@code IndexColorModel} from the given image, using an adaptive * Creates an {@code IndexColorModel} from the given image, using an adaptive
* pallete. * palette.
* *
* @param pImage the image to get {@code IndexColorModel} from * @param pImage the image to get {@code IndexColorModel} from
* @param pNumberOfColors the number of colors for the {@code IndexColorModel} * @param pNumberOfColors the number of colors for the {@code IndexColorModel}
@ -821,7 +821,7 @@ class IndexImage {
/** /**
* Converts the input image (must be {@code TYPE_INT_RGB} or * Converts the input image (must be {@code TYPE_INT_RGB} or
* {@code TYPE_INT_ARGB}) to an indexed image. Generating an adaptive * {@code TYPE_INT_ARGB}) to an indexed image. Generating an adaptive
* pallete (8 bit) from the color data in the image, and uses default * palette (8 bit) from the color data in the image, and uses default
* dither. * dither.
* <p/> * <p/>
* The image returned is a new image, the input image is not modified. * The image returned is a new image, the input image is not modified.
@ -865,7 +865,7 @@ class IndexImage {
* Converts the input image (must be {@code TYPE_INT_RGB} or * Converts the input image (must be {@code TYPE_INT_RGB} or
* {@code TYPE_INT_ARGB}) to an indexed image. If the palette image * {@code TYPE_INT_ARGB}) to an indexed image. If the palette image
* uses an {@code IndexColorModel}, this will be used. Otherwise, generating an * uses an {@code IndexColorModel}, this will be used. Otherwise, generating an
* adaptive pallete (8 bit) from the given palette image. * adaptive palette (8 bit) from the given palette image.
* Dithering, transparency and color selection is controlled with the * Dithering, transparency and color selection is controlled with the
* {@code pHints}parameter. * {@code pHints}parameter.
* <p/> * <p/>
@ -875,7 +875,7 @@ class IndexImage {
* @param pPalette the Image to read color information from * @param pPalette the Image to read color information from
* @param pMatte the background color, used where the original image was * @param pMatte the background color, used where the original image was
* transparent * transparent
* @param pHints mHints that control output quality and speed. * @param pHints hints that control output quality and speed.
* @return the indexed BufferedImage. The image will be of type * @return the indexed BufferedImage. The image will be of type
* {@code BufferedImage.TYPE_BYTE_INDEXED} or * {@code BufferedImage.TYPE_BYTE_INDEXED} or
* {@code BufferedImage.TYPE_BYTE_BINARY}, and use an * {@code BufferedImage.TYPE_BYTE_BINARY}, and use an
@ -900,7 +900,7 @@ class IndexImage {
/** /**
* Converts the input image (must be {@code TYPE_INT_RGB} or * Converts the input image (must be {@code TYPE_INT_RGB} or
* {@code TYPE_INT_ARGB}) to an indexed image. Generating an adaptive * {@code TYPE_INT_ARGB}) to an indexed image. Generating an adaptive
* pallete with the given number of colors. * palette with the given number of colors.
* Dithering, transparency and color selection is controlled with the * Dithering, transparency and color selection is controlled with the
* {@code pHints}parameter. * {@code pHints}parameter.
* <p/> * <p/>
@ -910,7 +910,7 @@ class IndexImage {
* @param pNumberOfColors the number of colors for the image * @param pNumberOfColors the number of colors for the image
* @param pMatte the background color, used where the original image was * @param pMatte the background color, used where the original image was
* transparent * transparent
* @param pHints mHints that control output quality and speed. * @param pHints hints that control output quality and speed.
* @return the indexed BufferedImage. The image will be of type * @return the indexed BufferedImage. The image will be of type
* {@code BufferedImage.TYPE_BYTE_INDEXED} or * {@code BufferedImage.TYPE_BYTE_INDEXED} or
* {@code BufferedImage.TYPE_BYTE_BINARY}, and use an * {@code BufferedImage.TYPE_BYTE_BINARY}, and use an
@ -947,7 +947,7 @@ class IndexImage {
/** /**
* Converts the input image (must be {@code TYPE_INT_RGB} or * Converts the input image (must be {@code TYPE_INT_RGB} or
* {@code TYPE_INT_ARGB}) to an indexed image. Using the supplied * {@code TYPE_INT_ARGB}) to an indexed image. Using the supplied
* {@code IndexColorModel}'s pallete. * {@code IndexColorModel}'s palette.
* Dithering, transparency and color selection is controlled with the * Dithering, transparency and color selection is controlled with the
* {@code pHints} parameter. * {@code pHints} parameter.
* <p/> * <p/>
@ -1064,7 +1064,7 @@ class IndexImage {
/** /**
* Converts the input image (must be {@code TYPE_INT_RGB} or * Converts the input image (must be {@code TYPE_INT_RGB} or
* {@code TYPE_INT_ARGB}) to an indexed image. Generating an adaptive * {@code TYPE_INT_ARGB}) to an indexed image. Generating an adaptive
* pallete with the given number of colors. * palette with the given number of colors.
* Dithering, transparency and color selection is controlled with the * Dithering, transparency and color selection is controlled with the
* {@code pHints}parameter. * {@code pHints}parameter.
* <p/> * <p/>
@ -1072,7 +1072,7 @@ class IndexImage {
* *
* @param pImage the BufferedImage to index * @param pImage the BufferedImage to index
* @param pNumberOfColors the number of colors for the image * @param pNumberOfColors the number of colors for the image
* @param pHints mHints that control output quality and speed. * @param pHints hints that control output quality and speed.
* @return the indexed BufferedImage. The image will be of type * @return the indexed BufferedImage. The image will be of type
* {@code BufferedImage.TYPE_BYTE_INDEXED} or * {@code BufferedImage.TYPE_BYTE_INDEXED} or
* {@code BufferedImage.TYPE_BYTE_BINARY}, and use an * {@code BufferedImage.TYPE_BYTE_BINARY}, and use an
@ -1094,7 +1094,7 @@ class IndexImage {
/** /**
* Converts the input image (must be {@code TYPE_INT_RGB} or * Converts the input image (must be {@code TYPE_INT_RGB} or
* {@code TYPE_INT_ARGB}) to an indexed image. Using the supplied * {@code TYPE_INT_ARGB}) to an indexed image. Using the supplied
* {@code IndexColorModel}'s pallete. * {@code IndexColorModel}'s palette.
* Dithering, transparency and color selection is controlled with the * Dithering, transparency and color selection is controlled with the
* {@code pHints}parameter. * {@code pHints}parameter.
* <p/> * <p/>
@ -1125,7 +1125,7 @@ class IndexImage {
* Converts the input image (must be {@code TYPE_INT_RGB} or * Converts the input image (must be {@code TYPE_INT_RGB} or
* {@code TYPE_INT_ARGB}) to an indexed image. If the palette image * {@code TYPE_INT_ARGB}) to an indexed image. If the palette image
* uses an {@code IndexColorModel}, this will be used. Otherwise, generating an * uses an {@code IndexColorModel}, this will be used. Otherwise, generating an
* adaptive pallete (8 bit) from the given palette image. * adaptive palette (8 bit) from the given palette image.
* Dithering, transparency and color selection is controlled with the * Dithering, transparency and color selection is controlled with the
* {@code pHints}parameter. * {@code pHints}parameter.
* <p/> * <p/>
@ -1133,7 +1133,7 @@ class IndexImage {
* *
* @param pImage the BufferedImage to index * @param pImage the BufferedImage to index
* @param pPalette the Image to read color information from * @param pPalette the Image to read color information from
* @param pHints mHints that control output quality and speed. * @param pHints hints that control output quality and speed.
* @return the indexed BufferedImage. The image will be of type * @return the indexed BufferedImage. The image will be of type
* {@code BufferedImage.TYPE_BYTE_INDEXED} or * {@code BufferedImage.TYPE_BYTE_INDEXED} or
* {@code BufferedImage.TYPE_BYTE_BINARY}, and use an * {@code BufferedImage.TYPE_BYTE_BINARY}, and use an
@ -1393,7 +1393,7 @@ class IndexImage {
System.exit(5); System.exit(5);
} }
// Create mHints // Create hints
int hints = DITHER_DEFAULT; int hints = DITHER_DEFAULT;
if ("DIFFUSION".equalsIgnoreCase(dither)) { if ("DIFFUSION".equalsIgnoreCase(dither)) {