use of com.sun.media.jai.opimage.CopyOpImage in project imageio-ext by geosolutions-it.
the class TurboJpegImageWriter method refineImage.
/**
* Performs a few check in order to make sure to provide the proper data bytes to the
* incoming encoding phase. When calling getData(Rectangle).getDataBuffer() on an image having size
* smaller than the tile size, the underlying data buffer will be made of the data contained in the
* full tile (Even having used the getData(Rectangle) call. As an instance, a Rectangle(0,0,64,64)
* extracted from a 64x64 image with tiling 128x128 will result into a ByteBuffer filled with
* 128x128xBands bytes. (Therefore a lot of zeros). The encoded image will have a lot of scattered
* black stripes.
* This method do a copy of the only needed part of data when such a condition is met, or return the
* original image otherwise.
*
* @param srcImage The source image to be refined.
* @return
*/
private RenderedImage refineImage(RenderedImage srcImage) {
final int w = srcImage.getWidth();
final int h = srcImage.getHeight();
final int minX = srcImage.getMinX();
final int minY = srcImage.getMinY();
final int tw = srcImage.getTileWidth();
final int th = srcImage.getTileHeight();
// portion of the original data
if ((tw > w) || (th > h)) {
RenderingHints hints = null;
ImageLayout layout = null;
if (srcImage instanceof RenderedOp) {
hints = ((RenderedOp) srcImage).getRenderingHints();
if ((hints != null) && hints.containsKey(JAI.KEY_IMAGE_LAYOUT)) {
layout = (ImageLayout) hints.get(JAI.KEY_IMAGE_LAYOUT);
} else {
layout = new ImageLayout(srcImage);
}
} else {
layout = new ImageLayout(srcImage);
hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
}
// Imposing the layout of the requested image
// as well as reducing the tile layout which was
// bigger than the image
layout.setTileHeight(h);
layout.setTileWidth(w);
layout.setTileGridXOffset(minX);
layout.setTileGridYOffset(minY);
layout.setMinX(minX);
layout.setMinY(minY);
layout.setWidth(w);
layout.setHeight(h);
srcImage = new CopyOpImage(srcImage, hints, layout);
}
return srcImage;
}
Aggregations