Rectangle bounds = new Rectangle(image.getMinX(), image.getMinY(), image.getWidth(), image.getHeight());
visibleRect = bounds.intersection(visibleRect);
if (bounds.contains(visibleRect)) {
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
pb.add((float) visibleRect.x);
pb.add((float) visibleRect.y);
pb.add((float) visibleRect.width);
pb.add((float) visibleRect.height);
image = JAI.create("Crop", pb, JAIContext.noCacheHint);
}
Dimension previewSize = getSize();
float scale = 1;
if (visibleRect.width > previewSize.width || visibleRect.height > previewSize.height) {
scale = Math.min(previewSize.width / (float) visibleRect.width, previewSize.height / (float) visibleRect.height);
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
pb.add(scale);
pb.add(scale);
image = JAI.create("Scale", pb, JAIContext.noCacheHint);
}
// avoid keeping references to the input image
if (image instanceof RenderedOp) {
RenderedOp ropImage = (RenderedOp) image;
SampleModel sm = ropImage.getSampleModel().createCompatibleSampleModel(image.getWidth(), image.getHeight());
WritableRaster wr = Raster.createWritableRaster(sm, new Point(ropImage.getMinX(), ropImage.getMinY()));
ropImage.copyData(wr);
image = new BufferedImage(ropImage.getColorModel(), wr.createWritableTranslatedChild(0, 0), false, null);
ropImage.dispose();
}
/* image = Functions.toColorSpace(image, JAIContext.sRGBColorSpace, null);
if (((PlanarImage) image).getSampleModel().getDataType() == DataBuffer.TYPE_USHORT)
image = Functions.fromUShortToByte(image, null); */
if (!colorMode && image.getColorModel().getNumColorComponents() == 3) {
ICC_Profile profile = ((ICC_ColorSpace) (image.getColorModel().getColorSpace())).getProfile();
if (!(profile instanceof ICC_ProfileRGB)) {
image = Functions.toColorSpace(image, JAIContext.sRGBColorSpace, null);
profile = ((ICC_ColorSpace) (image.getColorModel().getColorSpace())).getProfile();
}
ICC_ProfileRGB rgb_profile = (ICC_ProfileRGB) profile;
ColorScience.ICC_ProfileParameters pp = new ColorScience.ICC_ProfileParameters(rgb_profile);
double[][] transform = {
{pp.W[0], pp.W[1], pp.W[2], 0}
};
ParameterBlock pb = new ParameterBlock();
pb.addSource(image);
pb.add(transform);
image = JAI.create("BandCombine", pb, JAIContext.noCacheHint); // Desaturate, single banded
}
return image;
}