use of javax.media.jai.ROI in project imageio-ext by geosolutions-it.
the class ImageIOUtilities method disposeImage.
/**
* Allow to dispose this image, as well as the related image sources.
*
* @param rOp
* the image to be disposed.
*/
public static void disposeImage(RenderedImage rOp) {
if (rOp != null) {
if (rOp instanceof RenderedOp) {
RenderedOp renderedOp = (RenderedOp) rOp;
final int nSources = renderedOp.getNumSources();
if (nSources > 0) {
for (int k = 0; k < nSources; k++) {
Object source = null;
try {
source = renderedOp.getSourceObject(k);
} catch (ArrayIndexOutOfBoundsException e) {
// Ignore
}
if (source != null) {
if (source instanceof RenderedOp) {
disposeImage((RenderedOp) source);
} else if (source instanceof BufferedImage) {
((BufferedImage) source).flush();
source = null;
}
}
}
} else {
// get the reader
Object imageReader = rOp.getProperty(ImageReadDescriptor.PROPERTY_NAME_IMAGE_READER);
if (imageReader != null && imageReader instanceof ImageReader) {
final ImageReader reader = (ImageReader) imageReader;
final ImageInputStream stream = (ImageInputStream) reader.getInput();
try {
stream.close();
} catch (Throwable e) {
// swallow this
}
try {
reader.dispose();
} catch (Throwable e) {
// swallow this
}
}
}
final Object roi = rOp.getProperty("ROI");
if (roi != null && (roi instanceof ROI || roi instanceof RenderedImage)) {
ROI roiImage = (ROI) roi;
PlanarImage image = roiImage.getAsImage();
if (image != null) {
image.dispose();
image = null;
roiImage = null;
}
}
if (rOp instanceof PlanarImage) {
((PlanarImage) rOp).dispose();
} else if (rOp instanceof BufferedImage) {
((BufferedImage) rOp).flush();
rOp = null;
}
}
}
}
use of javax.media.jai.ROI in project imageio-ext by geosolutions-it.
the class ImageIOUtilities method visualizeRescaled.
/**
* Visualize the image, after rescaling its values, given a threshold for
* the ROI. This is useful to rescale an image coming from a source which
* may contain noDataValues. The ROI threshold allows to set a minimal value
* to be included in the computation of the future JAI extrema operation
* used before the JAI rescale operation.
*
* @param image
* RenderedImage to visualize
* @param title
* title for the frame
* @param roiThreshold
* the threshold for the inner ROI
*/
static void visualizeRescaled(RenderedImage image, String title, int roiThreshold) {
ROI roi = new ROI(image, roiThreshold);
ParameterBlock pb = new ParameterBlock();
// The source image
pb.addSource(image);
if (roi != null)
// The region of the image to scan
pb.add(roi);
// Perform the extrema operation on the source image
RenderedOp op = JAI.create("extrema", pb);
// Retrieve both the maximum and minimum pixel value
double[][] extrema = (double[][]) op.getProperty("extrema");
final double[] scale = new double[] { (255) / (extrema[1][0] - extrema[0][0]) };
final double[] offset = new double[] { ((255) * extrema[0][0]) / (extrema[0][0] - extrema[1][0]) };
// Preparing to rescaling values
ParameterBlock pbRescale = new ParameterBlock();
pbRescale.add(scale);
pbRescale.add(offset);
pbRescale.addSource(image);
RenderedOp rescaledImage = JAI.create("Rescale", pbRescale);
ParameterBlock pbConvert = new ParameterBlock();
pbConvert.addSource(rescaledImage);
pbConvert.add(DataBuffer.TYPE_BYTE);
RenderedOp destImage = JAI.create("format", pbConvert);
visualize(destImage, title);
}
Aggregations