*) cleaned up code

*) added new filter 'FILTER_INVERT' and new method ymageMatrix.invert() to use it (does not work where characters have been written with ymageToolPrint.print(), haven't found the reason yet)
*) fixed a possible arrayOutOfBoundsExceptions in filter() if y-value of area to use filter in was larger than height of image filter is used with


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@4279 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
low012 17 years ago
parent b0aefcf2f3
commit 6fbda9ef4f

@ -61,6 +61,7 @@ public class ymageMatrix {
public static final byte FILTER_ANTIALIASING = 0; public static final byte FILTER_ANTIALIASING = 0;
public static final byte FILTER_BLUR = 1; public static final byte FILTER_BLUR = 1;
public static final byte FILTER_INVERT = 2;
protected int width, height; protected int width, height;
private BufferedImage image; private BufferedImage image;
@ -391,7 +392,46 @@ public class ymageMatrix {
*/ */
public void insertBitmap(BufferedImage bitmap, int x, int y, int transRGB, byte filter) { public void insertBitmap(BufferedImage bitmap, int x, int y, int transRGB, byte filter) {
insertBitmap(bitmap, x, y, transRGB); insertBitmap(bitmap, x, y, transRGB);
filter(x-1, y-1, x + bitmap.getWidth(), y + bitmap.getHeight(), filter, image.getRGB(x, y));
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
if (filter == FILTER_ANTIALIASING) {
int transX = -1;
int transY = -1;
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// find first pixel in bitmap that equals transRGB
// and also lies in area of image that will be covered by bitmap
int i = 0;
int j = 0;
boolean found = false;
while ((i < bitmapWidth) && (i + x < imageWidth) && !found) {
while ((j < bitmapHeight) && (j + y < imageHeight) && !found) {
if (bitmap.getRGB(i, j) == transRGB) {
transX = i;
transY = j;
}
j++;
}
i++;
}
// if there is a transparent pixel in the bitmap that covers an area
// of the image, the fiter will be used. If no such pixel has been found that
// means that there either is no transparent pixel in the bitmap or part
// of the bitmap that covers part of tha image is not within the borders of
// the image (i.e. bitmap is larger than image)
if (transX != -1) {
filter(x - 1, y - 1, x + bitmapWidth, y + bitmapHeight, filter, image.getRGB(transX + x, transY + y));
}
} else {
filter(x - 1, y - 1, x + bitmapWidth, y + bitmapHeight, filter, -1);
}
} }
/** /**
@ -419,6 +459,18 @@ public class ymageMatrix {
filter(lox, loy, rux, ruy, FILTER_BLUR, -1); filter(lox, loy, rux, ruy, FILTER_BLUR, -1);
} }
/**
* invert filter for a square part of the ymageMatrix
* @param lox x value for left upper coordinate
* @param loy y value for left upper coordinate
* @param rux x value for right lower coordinate
* @param ruy y value for right lower coordinate
* @author Marc Nause
*/
public void invert(int lox, int loy, int rux, int ruy) {
filter(lox, loy, rux, ruy, FILTER_INVERT, -1);
}
/** /**
* filter for a square part of the ymageMatrix * filter for a square part of the ymageMatrix
* @param lox x value for left upper coordinate * @param lox x value for left upper coordinate
@ -438,7 +490,7 @@ public class ymageMatrix {
if (lox > width) { lox = width - 1; } if (lox > width) { lox = width - 1; }
if (loy > height){ loy = height - 1; } if (loy > height){ loy = height - 1; }
if (rux > width) { rux = width - 1; } if (rux > width) { rux = width - 1; }
if (ruy > height){ ruy = width - 1; } if (ruy > height){ ruy = height - 1; }
if (lox > rux) { if (lox > rux) {
int tmp = lox; int tmp = lox;
lox = rux; lox = rux;
@ -468,77 +520,80 @@ public class ymageMatrix {
rgbG = 0; rgbG = 0;
rgbB = 0; rgbB = 0;
// taking samples from neighbours of pixel if (filter == FILTER_ANTIALIASING || filter == FILTER_BLUR) {
if (i > lox) { // taking samples from neighbours of pixel
rgb = image.getRGB(i-1, j); if (i > lox) {
if (rgb == bgcolor) { rgb = image.getRGB(i - 1, j);
border = true; if (rgb == bgcolor) {
border = true;
}
rgbR += rgb >> 16 & 0xff;
rgbG += rgb >> 8 & 0xff;
rgbB += rgb & 0xff;
numberOfNeighbours++;
} }
rgbR += rgb >> 16 & 0xff; if (j > loy) {
rgbG += rgb >> 8& 0xff; rgb = image.getRGB(i, j - 1);
rgbB += rgb & 0xff; if (rgb == bgcolor) {
numberOfNeighbours++; border = true;
} }
if (j > loy) { rgbR += rgb >> 16 & 0xff;
rgb = image.getRGB(i, j-1); rgbG += rgb >> 8 & 0xff;
if (rgb == bgcolor) { rgbB += rgb & 0xff;
border = true; numberOfNeighbours++;
} }
rgbR += rgb >> 16 & 0xff; if (i < width - 1) {
rgbG += rgb >> 8& 0xff; rgb = image.getRGB(i + 1, j);
rgbB += rgb & 0xff; if (rgb == bgcolor) {
numberOfNeighbours++; border = true;
} }
if (i < width- 1) { rgbR += rgb >> 16 & 0xff;
rgb = image.getRGB(i+1, j); rgbG += rgb >> 8 & 0xff;
if (rgb == bgcolor) { rgbB += rgb & 0xff;
border = true; numberOfNeighbours++;
} }
rgbR += rgb >> 16 & 0xff; if (i < height - 1) {
rgbG += rgb >> 8& 0xff; rgb = image.getRGB(i, j + 1);
rgbB += rgb & 0xff; if (rgb == bgcolor) {
numberOfNeighbours++; border = true;
} }
if (i < height - 1) { rgbR += rgb >> 16 & 0xff;
rgb = image.getRGB(i, j+1); rgbG += rgb >> 8 & 0xff;
if (rgb == bgcolor) { rgbB += rgb & 0xff;
border = true; numberOfNeighbours++;
} }
rgbR += rgb >> 16 & 0xff;
rgbG += rgb >> 8& 0xff;
rgbB += rgb & 0xff;
numberOfNeighbours++;
} }
rgb = image.getRGB(i, j); rgb = image.getRGB(i, j);
// put more weight on pixle than on neighbours // add value of pixel
if (filter == FILTER_ANTIALIASING && border) { // in case filter is used for antialiasing this will only be done if
// rgbR += (rgb >> 16 & 0xff) * numberOfNeighbours; // the pixel is on the edge to the background color
// rgbG += (rgb >> 8 & 0xff) * numberOfNeighbours; if ((filter == FILTER_ANTIALIASING && border) || (filter == FILTER_BLUR)) {
// rgbB += (rgb & 0xff) * numberOfNeighbours;
// numberOfNeighbours += numberOfNeighbours;
rgbR += (rgb >> 16 & 0xff); rgbR += (rgb >> 16 & 0xff);
rgbG += (rgb >> 8 & 0xff); rgbG += (rgb >> 8 & 0xff);
rgbB += (rgb & 0xff); rgbB += (rgb & 0xff);
numberOfNeighbours++; numberOfNeighbours++;
border = false; border = false;
} }
// set to value of pixel => keep value
else if (filter == FILTER_ANTIALIASING) { else if (filter == FILTER_ANTIALIASING) {
rgbR = (rgb >> 16 & 0xff); rgbR = (rgb >> 16 & 0xff);
rgbG = (rgb >> 8 & 0xff); rgbG = (rgb >> 8 & 0xff);
rgbB = (rgb & 0xff); rgbB = (rgb & 0xff);
numberOfNeighbours = 1; numberOfNeighbours = 1;
} }
// put same weight in neighbours as on pixel // set value of pixel to inverted value (using XOR)
else if (filter == FILTER_BLUR) { else if (filter == FILTER_INVERT) {
rgbR += (rgb >> 16 & 0xff); rgb = rgb ^ 0xffffff;
rgbG += (rgb >> 8 & 0xff); rgbR = (rgb >> 16 & 0xff);
rgbB += (rgb & 0xff); rgbG = (rgb >> 8 & 0xff);
numberOfNeighbours++; rgbB = (rgb & 0xff);
numberOfNeighbours = 1;
} }
// calculatinc the average // calculating the average
rgbR = (int) (rgbR / numberOfNeighbours); rgbR = (int) (rgbR / numberOfNeighbours);
rgbG = (int) (rgbG / numberOfNeighbours); rgbG = (int) (rgbG / numberOfNeighbours);
rgbB = (int) (rgbB / numberOfNeighbours); rgbB = (int) (rgbB / numberOfNeighbours);

Loading…
Cancel
Save