/** * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance * penalty of BufferedImage.setRGB 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 of pixels to set * @see #getRGB */ public void setRGB(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) image.getRaster().setDataElements(x, y, width, height, pixels); else image.setRGB(x, y, width, height, pixels, 0, width); }
/** * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance * penalty of BufferedImage.setRGB 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 of pixels to set * @see #getRGB */ public void setRGB(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) { image.getRaster().setDataElements(x, y, width, height, pixels); } else { image.setRGB(x, y, width, height, pixels, 0, width); } } }
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; }
for (int y = 0; y < height; y++) { buffer.get(row); raster.setDataElements(0, y, width, 1, row);
for (int y = 0; y < height; y++) { buffer.get(row); raster.setDataElements(0, y, width, 1, row);
@Override public void writeRow(int y, WritableRaster destination) { destination.setDataElements(0, y, raster.getWidth(), 1, pixels); } }
@Override public void writeRow(int y, WritableRaster destination) { destination.setDataElements(0, y, raster.getWidth(), 1, pixels); } }
@Override public void writeRow(int y, WritableRaster destination) { destination.setDataElements(0, y, raster.getWidth(), 1, pixels); } }
/** * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance * penalty of BufferedImage.setRGB unmanaging the image. */ public void setRGB( 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 ) image.getRaster().setDataElements( x, y, width, height, pixels ); else image.setRGB( x, y, width, height, pixels, 0, width ); } }
/** * Sets ARGB pixels in image. Solves the performance * issue of BufferedImage.setRGB method. */ public void setRGB(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) { image.getRaster().setDataElements(x, y, width, height, pixels); } else { image.setRGB(x, y, width, height, pixels, 0, width); } } }
public void compose(Raster src, Raster dstIn, WritableRaster dstOut) { int width = min(src.getWidth(), dstIn.getWidth()); // We always work in RGB, using DataBuffer.TYPE_INT transfer type. int[] srcData = null; int[] dstData = null; int[] resData = new int[width - src.getMinX()]; for (int y = src.getMinY(); y < src.getHeight(); y++) { srcData = (int[]) src.getDataElements(src.getMinX(), y, width, 1, srcData); dstData = (int[]) dstIn.getDataElements(src.getMinX(), y, width, 1, dstData); for (int x = src.getMinX(); x < width; x++) { // TODO: Decide how to handle alpha (if at all) resData[x] = 0xff000000 | ((~srcData[x] ^ dstData[x])) & 0xffffff; } dstOut.setDataElements(src.getMinX(), y, width, 1, resData); } } };
BufferedImage createPaletteImage(final BMHDChunk header, boolean isEHB) throws IIOException { // Create a 1 x colors.length image IndexColorModel cm = getIndexColorModel(header, isEHB); WritableRaster raster = cm.createCompatibleWritableRaster(cm.getMapSize(), 1); byte[] pixel = null; for (int x = 0; x < cm.getMapSize(); x++) { pixel = (byte[]) cm.getDataElements(cm.getRGB(x), pixel); raster.setDataElements(x, 0, pixel); } return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null); }
@Test public void testConvertByteABGRColors() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR).getRaster(); byte[] pixel = null; for (int i = 0; i < 255; i++) { input.setDataElements(0, 0, new byte[] {(byte) i, (byte) (255 - i), (byte) (128 + i), 0}); result = convert.filter(input, result); pixel = (byte[]) result.getDataElements(0, 0, pixel); assertNotNull(pixel); assertEquals(4, pixel.length); byte[] expected = {(byte) (255 - i), (byte) i, (byte) (127 - i), (byte) 0xff}; assertTrue(String.format("Was: %s, expected: %s", Arrays.toString(pixel), Arrays.toString(expected)), Arrays.equals(expected, pixel)); } }
@Test public void testConvertByteRGBBlack() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = null; byte[] pixel = null; for (int i = 0; i < 255; i++) { input.setDataElements(0, 0, new byte[] {(byte) i, (byte) (255 - i), (byte) (127 + i), (byte) 255}); result = convert.filter(input, result); pixel = (byte[]) result.getDataElements(0, 0, pixel); assertNotNull(pixel); assertEquals(3, pixel.length); byte[] expected = {(byte) 0, (byte) 0, (byte) 0}; assertTrue(String.format("Was: %s, expected: %s", Arrays.toString(pixel), Arrays.toString(expected)), Arrays.equals(expected, pixel)); } }
@Test public void testConvertByteBGRColors() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = null; byte[] pixel = null; for (int i = 0; i < 255; i++) { input.setDataElements(0, 0, new byte[] {(byte) i, (byte) (255 - i), (byte) (128 + i), 0}); result = convert.filter(input, result); pixel = (byte[]) result.getDataElements(0, 0, pixel); assertNotNull(pixel); assertEquals(3, pixel.length); byte[] expected = {(byte) (255 - i), (byte) i, (byte) (127 - i)}; assertTrue(String.format("Was: %s, expected: %s", Arrays.toString(pixel), Arrays.toString(expected)), Arrays.equals(expected, pixel)); } }
@Test public void testConvertByteRGBColors() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = null; byte[] pixel = null; for (int i = 0; i < 255; i++) { input.setDataElements(0, 0, new byte[] {(byte) i, (byte) (255 - i), (byte) (128 + i), 0}); result = convert.filter(input, result); pixel = (byte[]) result.getDataElements(0, 0, pixel); assertNotNull(pixel); assertEquals(3, pixel.length); byte[] expected = {(byte) (255 - i), (byte) i, (byte) (127 - i)}; assertTrue(String.format("Was: %s, expected: %s", Arrays.toString(pixel), Arrays.toString(expected)), Arrays.equals(expected, pixel)); } }
@Test public void testConvertIntRGBColors() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB).getRaster(); int[] pixel = null; for (int i = 0; i < 255; i++) { input.setDataElements(0, 0, new byte[] {(byte) i, (byte) (255 - i), (byte) (128 + i), 0}); result = convert.filter(input, result); pixel = (int[]) result.getDataElements(0, 0, pixel); assertNotNull(pixel); assertEquals(1, pixel.length); int expected = (((byte) (255 - i)) & 0xFF) << 16 | (((byte) i) & 0xFF) << 8 | ((byte) (127 - i)) & 0xFF; int rgb = pixel[0] & 0xFFFFFF; assertEquals(String.format("Was: 0x%08x, expected: 0x%08x", rgb, expected), expected, rgb); } }
@Test public void testConvertIntARGBColors() { FastCMYKToRGB convert = new FastCMYKToRGB(); WritableRaster input = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 1, 1, 4, null); WritableRaster result = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB).getRaster(); int[] pixel = null; for (int i = 0; i < 255; i++) { input.setDataElements(0, 0, new byte[]{(byte) i, (byte) (255 - i), (byte) (128 + i), 0}); result = convert.filter(input, result); pixel = (int[]) result.getDataElements(0, 0, pixel); assertNotNull(pixel); assertEquals(1, pixel.length); int expected = 0xFF << 24 | (((byte) (255 - i)) & 0xFF) << 16 | (((byte) i) & 0xFF) << 8 | ((byte) (127 - i)) & 0xFF; assertEquals(String.format("Was: 0x%08x, expected: 0x%08x", pixel[0], expected), expected, pixel[0]); } } }