A row of mostly cosmetic changes to imaging code.

This commit is contained in:
lehni 2009-08-10 14:28:10 +00:00
parent f0ebbdb6a4
commit cac4b7d891
3 changed files with 16 additions and 17 deletions

View file

@ -17,13 +17,13 @@
/* /*
* The GIF encoding routines are based on the Acme libary * The GIF encoding routines are based on the Acme libary
* *
* The following addaptions and extensions were added by Juerg Lehni: * The following changes and extensions were added by Juerg Lehni:
* *
* - encode now directly works on BufferedImage objects, the ImageEncoder * - encode now directly works on BufferedImage objects, the ImageEncoder
* and ImageConsumer oriented frameworks has been removed. * and ImageConsumer oriented frameworks has been removed.
* - Only BufferedImages with IndexColorModel are taken, so no more * - Only BufferedImages with IndexColorModel are taken, so no more
* palette optimization with IntHashtable objects are necessary. If the * palette optimization with IntHashtable objects are necessary. If the
* BufferedImage is in wrong format, helma.image.Quantizie is used to * BufferedImage is in wrong format, helma.image.ColorQuantizer is used to
* convert it into a index based image. * convert it into a index based image.
* - This version is much less space consuming as it only takes one sample * - This version is much less space consuming as it only takes one sample
* row of the rastered image at a time into memory during compression. * row of the rastered image at a time into memory during compression.

View file

@ -81,11 +81,11 @@ public class ImageFilterOp implements BufferedImageOp {
} }
*/ */
// allways work in integer mode. this is more effective, and most // Always work in integer mode. this is more effective, and most
// filters convert to integer internally anyhow // filters convert to integer internally anyhow
ColorModel cm = new SimpleColorModel(); ColorModel cm = new SimpleColorModel();
// create a BufferedImage of only 1 pixel height for fetching the rows of the image in the correct format (ARGB) // Create a BufferedImage of only 1 pixel height for fetching the rows of the image in the correct format (ARGB)
// This speeds up things by more than factor 2, compared to the standard BufferedImage.getRGB solution, // This speeds up things by more than factor 2, compared to the standard BufferedImage.getRGB solution,
// which is supposed to be fast too. This is probably the case because drawing to BufferedImages uses // which is supposed to be fast too. This is probably the case because drawing to BufferedImages uses
// very optimized code which may even be hardware accelerated. // very optimized code which may even be hardware accelerated.
@ -93,19 +93,18 @@ public class ImageFilterOp implements BufferedImageOp {
Graphics2D g2d = row.createGraphics(); Graphics2D g2d = row.createGraphics();
int pixels[] = ((DataBufferInt)row.getRaster().getDataBuffer()).getData(); int pixels[] = ((DataBufferInt)row.getRaster().getDataBuffer()).getData();
// make sure alpha values do not add up for each row: // Make sure alpha values do not add up for each row:
g2d.setComposite(AlphaComposite.Src); g2d.setComposite(AlphaComposite.Src);
// calculate scanline by scanline in order to safe memory. // Calculate scanline by scanline in order to safe memory.
// It also seems to run faster like that // It also seems to run faster like that
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
g2d.drawImage(src, null, 0, -y); g2d.drawImage(src, null, 0, -y);
// now pixels contains the rgb values of the row y! // Now pixels contains the rgb values of the row y!
// filter this row now: // filter this row now:
fltr.setPixels(0, y, width, 1, cm, pixels, 0, width); fltr.setPixels(0, y, width, 1, cm, pixels, 0, width);
} }
g2d.dispose(); g2d.dispose();
// The consumer now contains the filtered image, return it.
// the consumer now contains the filtered image, return it.
return consumer.getImage(); return consumer.getImage();
} }
@ -140,7 +139,7 @@ public class ImageFilterOp implements BufferedImageOp {
} }
public int getRGB(int rgb) { public int getRGB(int rgb) {
// this is the part that speeds up most. // This is the part that speeds up most.
// java.awt.image.ColorModel would return the same value, but with // java.awt.image.ColorModel would return the same value, but with
// 4 function calls and a lot of shifts and ors per color! // 4 function calls and a lot of shifts and ors per color!
return rgb; return rgb;

View file

@ -364,7 +364,7 @@ public class ImageWrapper {
*/ */
public void trim(int x, int y, boolean trimLeft, boolean trimTop, boolean trimRight, boolean trimBottom) { public void trim(int x, int y, boolean trimLeft, boolean trimTop, boolean trimRight, boolean trimBottom) {
BufferedImage bi = this.getBufferedImage(); BufferedImage bi = this.getBufferedImage();
int color = bi.getRGB(x, y), pixel; int color = bi.getRGB(x, y);
int left = 0, top = 0, right = width - 1, bottom = height - 1; int left = 0, top = 0, right = width - 1, bottom = height - 1;
// create a BufferedImage of only 1 pixel height for fetching the rows of the image in the correct format (ARGB) // create a BufferedImage of only 1 pixel height for fetching the rows of the image in the correct format (ARGB)
@ -447,7 +447,7 @@ public class ImageWrapper {
} }
/** /**
* resizes the image using the Graphics2D approach * Resizes the image using the Graphics2D approach
*/ */
protected void resize(int w, int h, boolean smooth) { protected void resize(int w, int h, boolean smooth) {
BufferedImage buffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); BufferedImage buffered = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
@ -473,7 +473,7 @@ public class ImageWrapper {
} }
/** /**
* Resize the image * Resizes the image
* *
* @param w ... * @param w ...
* @param h ... * @param h ...
@ -483,19 +483,19 @@ public class ImageWrapper {
(double) w / width, (double) w / width,
(double) h / height (double) h / height
); );
// if the image is scaled, used the Graphcis2D method, otherwise use AWT: // If the image is scaled, used the Graphcis2D method, otherwise use AWT:
if (factor > 1f) { if (factor > 1f) {
// scale it with the Graphics2D approach for supperiour quality. // Scale it with the Graphics2D approach for superior quality.
resize(w, h, true); resize(w, h, true);
} else { } else {
// Area averaging has the best results for shrinking of images: // Area averaging has the best results for shrinking of images:
// as getScaledInstance is asynchronous, the ImageWaiter is needed here too: // As getScaledInstance is asynchronous, the ImageWaiter is needed here too:
// Image scaled = ImageWaiter.waitForImage(image.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING)); // Image scaled = ImageWaiter.waitForImage(image.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING));
// if (scaled == null) // if (scaled == null)
// throw new RuntimeException("Image cannot be resized."); // throw new RuntimeException("Image cannot be resized.");
// this version is up to 4 times faster than getScaledInstance: // This version is up to 4 times faster than getScaledInstance:
ImageFilterOp filter = new ImageFilterOp(new AreaAveragingScaleFilter(w, h)); ImageFilterOp filter = new ImageFilterOp(new AreaAveragingScaleFilter(w, h));
setImage(filter.filter(getBufferedImage(), null)); setImage(filter.filter(getBufferedImage(), null));
} }