private static WritableRaster buildRaster(final Bitmap bitmap, final FilterType filterType, final double scaleX, final double scaleY) { final Rectangle dstBounds = new Rectangle(0, 0, // (int) Math.round(bitmap.getWidth() * scaleX), // (int) Math.round(bitmap.getHeight() * scaleY)); final WritableRaster dst = WritableRaster.createInterleavedRaster(DataBuffer.TYPE_BYTE, dstBounds.width, dstBounds.height, 1, new Point()); if (scaleX != 1 || scaleY != 1) { // scaling required final Resizer resizer = new Resizer(scaleX, scaleY); final Filter filter = Filter.byType(filterType); resizer.resize(bitmap, bitmap.getBounds() /* sourceRegion */, dst, dstBounds, filter, filter); } else { // scaling not required, paste bitmap into raster pixel per pixel int byteIndex = 0; for (int y = 0; y < bitmap.getHeight(); y++) { for (int x = 0; x < bitmap.getWidth(); byteIndex++) { final int pixels = (~bitmap.getByte(byteIndex)) & 0xFF; final int relevantPixels = bitmap.getWidth() - x > 8 ? 8 : bitmap.getWidth() - x; final int endIdx = 7 - relevantPixels; for (int bytePosition = 7; bytePosition > endIdx; bytePosition--, x++) { dst.setSample(x, y, 0, (pixels >> bytePosition) & 0x1); } } } } return dst; }
/** * Adapt a rank 2 array into a java.awt.image.BufferedImage. * If passed a rank 3 array, take first 2D slice. * @param ma rank 2 or 3 array. * @return BufferedImage */ public static java.awt.image.BufferedImage makeGrayscaleImage( Array ma, IsMissingEvaluator missEval) { if (ma.getRank() < 2) return null; if (ma.getRank() == 3) ma = ma.reduce(); if (ma.getRank() == 3) ma = ma.slice( 0, 0); // we need 2D int h = ma.getShape()[0]; int w = ma.getShape()[1]; DataBuffer dataBuffer = makeDataBuffer(ma, missEval); WritableRaster raster = WritableRaster.createInterleavedRaster(dataBuffer, w, h, // int w, int h, w, // int scanlineStride, 1, // int pixelStride, new int[]{0}, // int bandOffsets[], null); // Point location) ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); ComponentColorModel colorModel = new ComponentColorModel(cs,new int[] {8}, false,false,Transparency.OPAQUE, DataBuffer.TYPE_BYTE); return new BufferedImage( colorModel, raster, false, null); }
private static WritableRaster buildRaster(final Bitmap bitmap, final FilterType filterType, final double scaleX, final double scaleY) { final Rectangle dstBounds = new Rectangle(0, 0, // (int) Math.round(bitmap.getWidth() * scaleX), // (int) Math.round(bitmap.getHeight() * scaleY)); final WritableRaster dst = WritableRaster.createInterleavedRaster(DataBuffer.TYPE_BYTE, dstBounds.width, dstBounds.height, 1, new Point()); if (scaleX != 1 || scaleY != 1) { // scaling required final Resizer resizer = new Resizer(scaleX, scaleY); final Filter filter = Filter.byType(filterType); resizer.resize(bitmap, bitmap.getBounds() /* sourceRegion */, dst, dstBounds, filter, filter); } else { // scaling not required, paste bitmap into raster pixel per pixel int byteIndex = 0; for (int y = 0; y < bitmap.getHeight(); y++) { for (int x = 0; x < bitmap.getWidth(); byteIndex++) { final int pixels = (~bitmap.getByte(byteIndex)) & 0xFF; final int relevantPixels = bitmap.getWidth() - x > 8 ? 8 : bitmap.getWidth() - x; final int endIdx = 7 - relevantPixels; for (int bytePosition = 7; bytePosition > endIdx; bytePosition--, x++) { dst.setSample(x, y, 0, (pixels >> bytePosition) & 0x1); } } } } return dst; }
/** * Adapt a rank 2 array into a java.awt.image.BufferedImage. * If passed a rank 3 array, take first 2D slice. * @param ma rank 2 or 3 array. * @return BufferedImage */ public static java.awt.image.BufferedImage makeGrayscaleImage( Array ma) { if (ma.getRank() < 2) return null; if (ma.getRank() == 3) ma = ma.reduce(); if (ma.getRank() == 3) ma = ma.slice( 0, 0); // we need 2D int h = ma.getShape()[0]; int w = ma.getShape()[1]; DataBuffer dataBuffer = makeDataBuffer( ma); WritableRaster raster = WritableRaster.createInterleavedRaster(dataBuffer, w, h, // int w, int h, w, // int scanlineStride, 1, // int pixelStride, new int[]{0}, // int bandOffsets[], null); // Point location) ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY); ComponentColorModel colorModel = new ComponentColorModel(cs,new int[] {8}, false,false,Transparency.OPAQUE, DataBuffer.TYPE_BYTE); return new BufferedImage( colorModel, raster, false, null); }
(int) Math.round(bitmap.getHeight() * scaleY)); final WritableRaster dst = WritableRaster.createInterleavedRaster(DataBuffer.TYPE_BYTE, dstBounds.width, dstBounds.height, 1, new Point());
raster = WritableRaster.createInterleavedRaster(dataType, width, height, nbBand, upperLeft); }else{