List of usage examples for java.awt.image WritableRaster getMinY
public final int getMinY()
From source file:GraphicsUtil.java
protected static void mult_BYTE_COMP_Data(WritableRaster wr) { // System.out.println("Multiply Int: " + wr); ComponentSampleModel csm;// w ww .j a va2 s.c o m csm = (ComponentSampleModel) wr.getSampleModel(); final int width = wr.getWidth(); final int scanStride = csm.getScanlineStride(); final int pixStride = csm.getPixelStride(); final int[] bandOff = csm.getBandOffsets(); DataBufferByte db = (DataBufferByte) wr.getDataBuffer(); final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(), wr.getMinY() - wr.getSampleModelTranslateY())); int aOff = bandOff[bandOff.length - 1]; int bands = bandOff.length - 1; // Access the pixel data array final byte[] pixels = db.getBankData()[0]; for (int y = 0; y < wr.getHeight(); y++) { int sp = base + y * scanStride; final int end = sp + width * pixStride; while (sp < end) { int a = pixels[sp + aOff] & 0xFF; if (a != 0xFF) for (int b = 0; b < bands; b++) { int i = sp + bandOff[b]; pixels[i] = (byte) (((pixels[i] & 0xFF) * a) >> 8); } sp += pixStride; } } }
From source file:GraphicsUtil.java
protected static void divide_BYTE_COMP_Data(WritableRaster wr) { // System.out.println("Multiply Int: " + wr); ComponentSampleModel csm;/*from w w w . j a v a 2s .c o m*/ csm = (ComponentSampleModel) wr.getSampleModel(); final int width = wr.getWidth(); final int scanStride = csm.getScanlineStride(); final int pixStride = csm.getPixelStride(); final int[] bandOff = csm.getBandOffsets(); DataBufferByte db = (DataBufferByte) wr.getDataBuffer(); final int base = (db.getOffset() + csm.getOffset(wr.getMinX() - wr.getSampleModelTranslateX(), wr.getMinY() - wr.getSampleModelTranslateY())); int aOff = bandOff[bandOff.length - 1]; int bands = bandOff.length - 1; // Access the pixel data array final byte[] pixels = db.getBankData()[0]; for (int y = 0; y < wr.getHeight(); y++) { int sp = base + y * scanStride; final int end = sp + width * pixStride; while (sp < end) { int a = pixels[sp + aOff] & 0xFF; if (a == 0) { for (int b = 0; b < bands; b++) pixels[sp + bandOff[b]] = (byte) 0xFF; } else if (a < 255) { // this does NOT include a == 255 (0xff) ! int aFP = (0x00FF0000 / a); for (int b = 0; b < bands; b++) { int i = sp + bandOff[b]; pixels[i] = (byte) (((pixels[i] & 0xFF) * aFP) >>> 16); } } sp += pixStride; } } }
From source file:nitf.imageio.NITFReader.java
/** * Reads image data as bytes for the given region, and writes it to the * given writable raster/* www. java 2 s. co m*/ * * @param sourceRegion * @param sourceXSubsampling * @param sourceYSubsampling * @param bandOffsets * @param destinationOffset * @param imRas * @return Raster * @throws IOException */ protected void readRaster(int imageIndex, Rectangle sourceRegion, Rectangle destRegion, int sourceXSubsampling, int sourceYSubsampling, int[] bandOffsets, int pixelSize, Point destinationOffset, WritableRaster imRas) throws IOException { checkIndex(imageIndex); try { ImageSubheader subheader = record.getImages()[imageIndex].getSubheader(); int numCols = subheader.getNumCols().getIntData(); int numRows = subheader.getNumRows().getIntData(); // try to optimize the read call by reading in the entire // image at once if ((destRegion.height * sourceYSubsampling) == numRows && (destRegion.width * sourceXSubsampling) == numCols) { readFullImage(imageIndex, destRegion, sourceXSubsampling, sourceYSubsampling, bandOffsets, pixelSize, imRas); return; } // the general purpose case else { int colBytes = destRegion.width * pixelSize; int dstMinX = imRas.getMinX(); int dstMaxX = dstMinX + imRas.getWidth() - 1; int dstMinY = imRas.getMinY(); int dstMaxY = dstMinY + imRas.getHeight() - 1; // int swap = 0; int nBands = subheader.getBandCount(); /* * NOTE: This is a "fix" that will be removed once the * underlying NITRO library gets patched. Currently, if you make * a request of a single band, it doesn't matter which band you * request - the data from the first band will be returned * regardless. This is obviously wrong. To thwart this, we will * read all bands, then scale down what we return to the user * based on their actual request. */ int[] requestBands = new int[nBands]; for (int i = 0; i < nBands; ++i) requestBands[i] = i; byte[][] rowBuf = new byte[requestBands.length][colBytes]; // make a SubWindow from the params // TODO may want to read by blocks or rows to make faster and // more // memory efficient SubWindow window; window = new SubWindow(); window.setNumBands(requestBands.length); window.setBandList(requestBands); window.setNumCols(destRegion.width); window.setNumRows(1); window.setStartCol(sourceRegion.x); window.setStartRow(sourceRegion.y); // the NITRO library can do the subsampling for us if (sourceYSubsampling != 1 || sourceXSubsampling != 1) { DownSampler downSampler = new PixelSkipDownSampler(sourceYSubsampling, sourceXSubsampling); window.setDownSampler(downSampler); } // String pixelJustification = record.getImages()[imageIndex] // .getSubheader().getPixelJustification().getStringData() // .trim(); // swap = pixelJustification.equals("R") ? 1 : 0; List<ByteBuffer> bandBufs = new ArrayList<ByteBuffer>(); for (int i = 0; i < requestBands.length; ++i) { ByteBuffer bandBuf = null; bandBuf = ByteBuffer.wrap(rowBuf[i]); // bandBuf.order(ByteOrder.nativeOrder()); // bandBuf.order(swap == 0 ? ByteOrder.BIG_ENDIAN // : ByteOrder.LITTLE_ENDIAN); bandBufs.add(bandBuf); } nitf.ImageReader imageReader = getImageReader(imageIndex); for (int srcY = 0; srcY < sourceRegion.height; srcY++) { if (sourceYSubsampling != 1 && (srcY % sourceYSubsampling) != 0) continue; window.setStartRow(sourceRegion.y + srcY); // Read the row try { imageReader.read(window, rowBuf); } catch (NITFException e) { throw new IIOException("Error reading line " + srcY, e); } // Determine where the row will go in the destination int dstY = destinationOffset.y + srcY / sourceYSubsampling; if (dstY < dstMinY) { continue; // The row is above imRas } if (dstY > dstMaxY) { break; // We're done with the image } // Copy each (subsampled) source pixel into imRas for (int srcX = 0, dstX = destinationOffset.x; srcX < colBytes; srcX += pixelSize, dstX++) { if (dstX < dstMinX) { continue; } if (dstX > dstMaxX) { break; } for (int i = 0; i < bandOffsets.length; ++i) { ByteBuffer bandBuf = bandBufs.get(bandOffsets[i]); switch (pixelSize) { case 1: imRas.setSample(dstX, dstY, i, bandBuf.get(srcX)); break; case 2: imRas.setSample(dstX, dstY, i, bandBuf.getShort(srcX)); break; case 4: imRas.setSample(dstX, dstY, i, bandBuf.getFloat(srcX)); break; case 8: imRas.setSample(dstX, dstY, i, bandBuf.getDouble(srcX)); break; } } } } } } catch (NITFException e1) { throw new IOException(ExceptionUtils.getStackTrace(e1)); } }
From source file:org.esa.s2tbx.dataio.jp2.internal.JP2TileOpImage.java
private void computeRectIndirect(WritableRaster dest, Rectangle destRect) { try {//from www . ja v a2 s .co m Path tile = decompressTile(tileIndex, getLevel()); RenderedImage readTileImage = null; if (tile != null) { try (ImageReader imageReader = new ImageReader(tile)) { final DataBuffer dataBuffer = dest.getDataBuffer(); int tileWidth = this.getTileWidth(); int tileHeight = this.getTileHeight(); final int fileTileX = destRect.x / tileLayout.tileWidth; final int fileTileY = destRect.y / tileLayout.tileHeight; int fileTileOriginX = destRect.x - fileTileX * tileLayout.tileWidth; int fileTileOriginY = destRect.y - fileTileY * tileLayout.tileHeight; Rectangle fileTileRect = tileDims.get(tile); if (fileTileRect == null) { fileTileRect = new Rectangle(0, 0, imageReader.getImageWidth(), imageReader.getImageHeight()); tileDims.put(tile, fileTileRect); } if (fileTileOriginX == 0 && tileLayout.tileWidth == tileWidth && fileTileOriginY == 0 && tileLayout.tileHeight == tileHeight && tileWidth * tileHeight == dataBuffer.getSize()) { readTileImage = imageReader.read(); } else { final Rectangle tileRect = new Rectangle(fileTileOriginX, fileTileOriginY, tileWidth, tileHeight); final Rectangle intersection = fileTileRect.intersection(tileRect); if (!intersection.isEmpty()) { readTileImage = imageReader.read(intersection); } } if (readTileImage != null) { Raster readBandRaster = readTileImage.getData().createChild(0, 0, readTileImage.getWidth(), readTileImage.getHeight(), 0, 0, new int[] { bandIndex }); dest.setDataElements(dest.getMinX(), dest.getMinY(), readBandRaster); } } catch (IOException e) { logger.severe(e.getMessage()); } } } catch (IOException e) { logger.severe(e.getMessage()); } }
From source file:org.esa.s2tbx.dataio.jp2.internal.JP2TileOpImage.java
private void computeRectDirect(WritableRaster dest, Rectangle destRect) { try (OpenJP2Decoder decoder = new OpenJP2Decoder(this.cacheDir, this.imageFile, this.bandIndex, this.dataType, getLevel(), 20, tileIndex)) { Raster readTileImage = null; final DataBuffer dataBuffer = dest.getDataBuffer(); int tileWidth = this.getTileWidth(); int tileHeight = this.getTileHeight(); final int fileTileX = destRect.x / tileLayout.tileWidth; final int fileTileY = destRect.y / tileLayout.tileHeight; int fileTileOriginX = destRect.x - fileTileX * tileLayout.tileWidth; int fileTileOriginY = destRect.y - fileTileY * tileLayout.tileHeight; Dimension dimensions = decoder.getImageDimensions(); Rectangle fileTileRect = new Rectangle(0, 0, dimensions.width, dimensions.height); if (fileTileOriginX == 0 && tileLayout.tileWidth == tileWidth && fileTileOriginY == 0 && tileLayout.tileHeight == tileHeight && tileWidth * tileHeight == dataBuffer.getSize()) { readTileImage = decoder.read(null); } else {//from ww w . j av a 2 s . c o m final Rectangle tileRect = new Rectangle(fileTileOriginX, fileTileOriginY, tileWidth, tileHeight); final Rectangle intersection = fileTileRect.intersection(tileRect); if (!intersection.isEmpty()) { readTileImage = decoder.read(intersection); } } if (readTileImage != null) { Raster readBandRaster = readTileImage.createChild(0, 0, readTileImage.getWidth(), readTileImage.getHeight(), 0, 0, bands); dest.setDataElements(dest.getMinX(), dest.getMinY(), readBandRaster); } } catch (IOException e) { logger.severe(e.getMessage()); } }