AreaAveragingScaleFilterpublic class AreaAveragingScaleFilter extends ReplicateScaleFilter The AreaAveragingScaleFilter class scales the source image using area
averaging algorithm. This algorithm provides a source image with a new image
containing the resampled image. |
Fields Summary |
---|
private static final ColorModel | rgbCMThe Constant rgbCM. | private static final int | averagingFlagsThe Constant averagingFlags. | private boolean | resetThe reset. | private boolean | initedThe inited. | private int[] | sum_rThe sum_r. | private int[] | sum_gThe sum_g. | private int[] | sum_bThe sum_b. | private int[] | sum_aThe sum_a. | private int[] | buffThe buff. | private int | avgFactorThe avg factor. | private int | cachedDYThe cached dy. | private int | cachedDVRestThe cached dv rest. |
Constructors Summary |
---|
public AreaAveragingScaleFilter(int width, int height)Instantiates a new AreaAveragingScaleFilter object which scales a source
image with the specified width and height. // Cached value of rest src scanlines for sum
// pixel samples
// Because data if transferring by whole scanlines
// we are caching only Y coordinate values
super(width, height);
|
Methods Summary |
---|
private void | initialize()Initialization of the auxiliary data.
sum_a = new int[destWidth];
sum_r = new int[destWidth];
sum_g = new int[destWidth];
sum_b = new int[destWidth];
buff = new int[destWidth];
outpixbuf = buff;
avgFactor = srcWidth * srcHeight;
inited = true;
| private void | setFilteredPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, java.lang.Object pixels, int off, int scansize)This method implements the Area Averaging Scale filter. The description
of algorithm is presented in Java API Specification. Arrays sum_r, sum_g,
sum_b, sum_a have length equals width of destination image. In each
array's element is accumulating pixel's component values, proportional to
the area which source pixels will occupy in destination image. Then that
values will divide by Global averaging factor (area of the destination
image) for receiving average values of destination pixels.
if (!inited) {
initialize();
}
int srcX, srcY, dx, dy;
int svRest, dvRest, shRest, dhRest, vDif, hDif;
if (y == 0) {
dy = 0;
dvRest = srcHeight;
} else {
dy = cachedDY;
dvRest = cachedDVRest;
}
srcY = y;
svRest = destHeight;
int srcOff = off;
while (srcY < y + h) {
if (svRest < dvRest) {
vDif = svRest;
} else {
vDif = dvRest;
}
srcX = 0;
dx = 0;
shRest = destWidth;
dhRest = srcWidth;
while (srcX < w) {
if (shRest < dhRest) {
hDif = shRest;
} else {
hDif = dhRest;
}
int avg = hDif * vDif; // calculation of contribution factor
int rgb, pix;
if (pixels instanceof int[]) {
pix = ((int[])pixels)[srcOff + srcX];
} else {
pix = ((byte[])pixels)[srcOff + srcX] & 0xff;
}
rgb = model.getRGB(pix);
int a = rgb >>> 24;
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
// accumulating pixel's component values
sum_a[dx] += a * avg;
sum_r[dx] += r * avg;
sum_g[dx] += g * avg;
sum_b[dx] += b * avg;
shRest -= hDif;
dhRest -= hDif;
if (shRest == 0) {
srcX++;
shRest = destWidth;
}
if (dhRest == 0) {
dx++;
dhRest = srcWidth;
}
}
svRest -= vDif;
dvRest -= vDif;
if (svRest == 0) {
svRest = destHeight;
srcY++;
srcOff += scansize;
}
if (dvRest == 0) {
// averaging destination pixel's values
for (int i = 0; i < destWidth; i++) {
int a = (sum_a[i] / avgFactor) & 0xff;
int r = (sum_r[i] / avgFactor) & 0xff;
int g = (sum_g[i] / avgFactor) & 0xff;
int b = (sum_b[i] / avgFactor) & 0xff;
int frgb = (a << 24) | (r << 16) | (g << 8) | b;
buff[i] = frgb;
}
consumer.setPixels(0, dy, destWidth, 1, rgbCM, buff, 0, destWidth);
dy++;
dvRest = srcHeight;
Arrays.fill(sum_a, 0);
Arrays.fill(sum_r, 0);
Arrays.fill(sum_g, 0);
Arrays.fill(sum_b, 0);
}
}
cachedDY = dy;
cachedDVRest = dvRest;
| public void | setHints(int hints)
super.setHints(hints);
reset = ((hints & averagingFlags) != averagingFlags);
| public void | setPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, int[] pixels, int off, int scansize)
if (reset) {
super.setPixels(x, y, w, h, model, pixels, off, scansize);
} else {
setFilteredPixels(x, y, w, h, model, pixels, off, scansize);
}
| public void | setPixels(int x, int y, int w, int h, java.awt.image.ColorModel model, byte[] pixels, int off, int scansize)
if (reset) {
super.setPixels(x, y, w, h, model, pixels, off, scansize);
} else {
setFilteredPixels(x, y, w, h, model, pixels, off, scansize);
}
|
|