/** * A convenience method for getting ARGB pixels from an image. This tries to avoid the performance * penalty of BufferedImage.getRGB unmanaging the image. * @param image a BufferedImage object * @param x the left edge of the pixel block * @param y the right edge of the pixel block * @param width the width of the pixel array * @param height the height of the pixel array * @param pixels the array to hold the returned pixels. May be null. * @return the pixels * @see #setRGB */ public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) { int type = image.getType(); if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels); return image.getRGB(x, y, width, height, pixels, 0, width); }
static private String hash (BufferedImage image) { try { MessageDigest digest = MessageDigest.getInstance("SHA1"); // Ensure image is the correct format. int width = image.getWidth(); int height = image.getHeight(); if (image.getType() != BufferedImage.TYPE_INT_ARGB) { BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); newImage.getGraphics().drawImage(image, 0, 0, null); image = newImage; } WritableRaster raster = image.getRaster(); int[] pixels = new int[width]; for (int y = 0; y < height; y++) { raster.getDataElements(0, y, width, 1, pixels); for (int x = 0; x < width; x++) hash(digest, pixels[x]); } hash(digest, width); hash(digest, height); return new BigInteger(1, digest.digest()).toString(16); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException(ex); } }
/** * A convenience method for getting ARGB pixels from an image. This tries to avoid the * performance penalty of BufferedImage.getRGB unmanaging the image. * * @param image a BufferedImage object * @param x the left edge of the pixel block * @param y the right edge of the pixel block * @param width the width of the pixel array * @param height the height of the pixel array * @param pixels the array to hold the returned pixels. May be null. * @return the pixels * @see #setRGB */ public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) { int type = image.getType(); if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) { return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels); } return image.getRGB(x, y, width, height, pixels, 0, width); }
static private String hash (BufferedImage image) { try { MessageDigest digest = MessageDigest.getInstance("SHA1"); // Ensure image is the correct format. int width = image.getWidth(); int height = image.getHeight(); if (image.getType() != BufferedImage.TYPE_INT_ARGB) { BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); newImage.getGraphics().drawImage(image, 0, 0, null); image = newImage; } WritableRaster raster = image.getRaster(); int[] pixels = new int[width]; for (int y = 0; y < height; y++) { raster.getDataElements(0, y, width, 1, pixels); for (int x = 0; x < width; x++) hash(digest, pixels[x]); } hash(digest, width); hash(digest, height); return new BigInteger(1, digest.digest()).toString(16); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException(ex); } }
@Override public byte[] getRow(int y, byte[] row) { if (y < 0 || y >= getHeight()) { throw new IllegalArgumentException("Requested row is outside the image: " + y); } int width = getWidth(); if (row == null || row.length < width) { row = new byte[width]; } image.getRaster().getDataElements(left, top + y, width, 1, row); return row; }
@Override public byte[] getRow(int y, byte[] row) { if (y < 0 || y >= getHeight()) { throw new IllegalArgumentException("Requested row is outside the image: " + y); } int width = getWidth(); if (row == null || row.length < width) { row = new byte[width]; } image.getRaster().getDataElements(left, top + y, width, 1, row); return row; }
for (int y = 0; y < source.getHeight(); y++) { for (int x = 0; x < source.getWidth(); x++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256; for (int y = source.getHeight(); --y >= top;) { for (int x = 0; x < source.getWidth(); x++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256; for (int x = 0; x < source.getWidth(); x++) { for (int y = top; y < bottom; y++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256; for (int x = source.getWidth(); --x >= left;) { for (int y = top; y < bottom; y++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256;
@Override public byte[] getMatrix() { int width = getWidth(); int height = getHeight(); int area = width * height; byte[] matrix = new byte[area]; image.getRaster().getDataElements(left, top, width, height, matrix); return matrix; }
@Override public byte[] getMatrix() { int width = getWidth(); int height = getHeight(); int area = width * height; byte[] matrix = new byte[area]; image.getRaster().getDataElements(left, top, width, height, matrix); return matrix; }
for (int y = 0; y < source.getHeight(); y++) { for (int x = 0; x < source.getWidth(); x++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256; for (int y = source.getHeight(); --y >= top;) { for (int x = 0; x < source.getWidth(); x++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256; for (int x = 0; x < source.getWidth(); x++) { for (int y = top; y < bottom; y++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256; for (int x = source.getWidth(); --x >= left;) { for (int y = top; y < bottom; y++) { alphaRaster.getDataElements(x, y, a); int alpha = a[0]; if (alpha < 0) alpha += 256;
@Override public byte[] getMatrix() { int width = getWidth(); int height = getHeight(); int area = width * height; byte[] matrix = new byte[area]; // The underlying raster of image consists of area bytes with the luminance values image.getRaster().getDataElements(left, top, width, height, matrix); return matrix; }
@Override public byte[] getRow(int y, byte[] row) { if (y < 0 || y >= getHeight()) { throw new IllegalArgumentException("Requested row is outside the image: " + y); } int width = getWidth(); if (row == null || row.length < width) { row = new byte[width]; } // The underlying raster of image consists of bytes with the luminance values image.getRaster().getDataElements(left, top + y, width, 1, row); return row; }
public BufferedImage filter(BufferedImage src, BufferedImage dest) { if (dest == null) { dest = createCompatibleDestImage(src, null); } int width = src.getWidth(); int height = src.getHeight(); int[] inPixels = new int[width * height]; int[] outPixels = new int[width * height]; src.getRaster().getDataElements(0, 0, width, height, inPixels); filter(inPixels, outPixels, width, height); dest.getRaster().setDataElements(0, 0, width, height, outPixels); return dest; }
@Override public BufferedImage filter(BufferedImage src, BufferedImage dst) { int width = src.getWidth(); int height = src.getHeight(); int type = src.getType(); WritableRaster srcRaster = src.getRaster(); if (dst == null) { dst = createCompatibleDestImage(src, null); } WritableRaster dstRaster = dst.getRaster(); int[] inPixels = new int[width]; for (int y = 0; y < height; y++) { if (type == BufferedImage.TYPE_INT_ARGB) { srcRaster.getDataElements(0, y, width, 1, inPixels); for (int x = 0; x < width; x++) { inPixels[x] = filterRGB(inPixels[x]); } dstRaster.setDataElements(0, y, width, 1, inPixels); } else { src.getRGB(0, y, width, 1, inPixels, 0, width); for (int x = 0; x < width; x++) { inPixels[x] = filterRGB(inPixels[x]); } dst.setRGB(0, y, width, 1, inPixels, 0, width); } } return dst; }
public Raster getRaster(int xOffset, int yOffset, int w, int h) { ColorModel cm = getColorModel(); if (raster == null) createRaster(); // TODO: eventually use caching here WritableRaster childRaster = cm.createCompatibleWritableRaster(w, h); Rectangle2D childRect = new Rectangle2D.Double(xOffset, yOffset, w, h); if (!childRect.intersects(deviceBounds)) { // usually doesn't happen ... return childRaster; } Rectangle2D destRect = new Rectangle2D.Double(); Rectangle2D.intersect(childRect, deviceBounds, destRect); int dx = (int)(destRect.getX()-deviceBounds.getX()); int dy = (int)(destRect.getY()-deviceBounds.getY()); int dw = (int)destRect.getWidth(); int dh = (int)destRect.getHeight(); Object data = raster.getDataElements(dx, dy, dw, dh, null); dx = (int)(destRect.getX()-childRect.getX()); dy = (int)(destRect.getY()-childRect.getY()); childRaster.setDataElements(dx, dy, dw, dh, data); return childRaster; }
int[] row = new int[width]; for (int y = 0; y < height; y++) { raster.getDataElements(0, y, width, 1, row); scratchIntBuffer.put(row);
int[] row = new int[width]; for (int y = 0; y < height; y++) { raster.getDataElements(0, y, width, 1, row); scratchIntBuffer.put(row);
/** * Gets ARGB pixels from image. Solves the performance * issue of BufferedImage.getRGB method. */ public int[] getRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) { int type = image.getType(); if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) { return (int[]) image.getRaster().getDataElements(x, y, width, height, pixels); } return image.getRGB(x, y, width, height, pixels, 0, width); }
@Test public void testConvertByteRGBWhite() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = convert.filter(input, null); byte[] pixel = (byte[]) result.getDataElements(0, 0, null); assertNotNull(pixel); assertEquals(3, pixel.length); byte[] expected = {(byte) 255, (byte) 255, (byte) 255}; assertTrue(String.format("Was: %s, expected: %s", Arrays.toString(pixel), Arrays.toString(expected)), Arrays.equals(expected, pixel)); }
@Test public void testConvertIntRGBWhite() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = convert.filter(input, new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getRaster()); int[] pixel = (int[]) result.getDataElements(0, 0, null); assertNotNull(pixel); assertEquals(1, pixel.length); int expected = 0xFFFFFF; int rgb = pixel[0] & 0xFFFFFF; assertEquals(String.format("Was: 0x%08x, expected: 0x%08x", rgb, expected), expected, rgb); }