use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class JpegJMagickImageReader method read.
/**
* Read the imageMagick and returns it as a complete
* <code>BufferedImage</code>, using a supplied
* <code>ImageReadParam</code>.
*
* @param imageIndex
* the index of the image to be retrieved.
* @param param
* an <code>ImageReadParam</code> used to control the reading
* process, or null.
*/
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
synchronized (imagesLayouts) {
checkImageIndex(imageIndex);
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine("Requesting imageMagick at index " + imageIndex + " with ImageReadParam=" + param.toString());
// ///////////////////////////////////////////////////////////
//
// STEP 1.
// -------
// local variables initialization
//
// ///////////////////////////////////////////////////////////
// width and height for this imageMagick
int width = 0;
int height = 0;
final MagickImageAdapter im = ((MagickImageAdapter) imagesLayouts.get(imageIndex));
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine("Selected imageMagick adapter " + im.toString());
final ImageLayout layout = im.getLayout();
width = layout.getWidth(null);
height = layout.getHeight(null);
// get a default set of ImageReadParam if needed.
if (param == null)
param = getDefaultReadParam();
// The destination imageMagick properties
int dstWidth = -1;
int dstHeight = -1;
// int dstXOffset = 0;
// int dstYOffset = 0;
// The source region imageMagick properties
int srcRegionWidth = -1;
int srcRegionHeight = -1;
int srcRegionXOffset = 0;
int srcRegionYOffset = 0;
// Subsampling Factors */
int xSubsamplingFactor = -1;
int ySubsamplingFactor = -1;
// ///////////////////////////////////////////////////////////
//
// STEP 2.
// -------
// parameters management (retrieve user defined readParam and
// futher initializations)
//
// ///////////////////////////////////////////////////////////
// //
// Retrieving Information about Source Region and doing additional
// intialization operations.
// //
Rectangle srcRegion = param.getSourceRegion();
if (srcRegion != null) {
srcRegionWidth = (int) srcRegion.getWidth();
srcRegionHeight = (int) srcRegion.getHeight();
srcRegionXOffset = (int) srcRegion.getX();
srcRegionYOffset = (int) srcRegion.getY();
// ////////////////////////////////////////////////////////////////
if (srcRegionXOffset < 0)
srcRegionXOffset = 0;
if (srcRegionYOffset < 0)
srcRegionYOffset = 0;
// initializing destination imageMagick properties
dstWidth = srcRegionWidth;
if ((srcRegionXOffset + srcRegionWidth) > width) {
srcRegionWidth = width - srcRegionXOffset;
}
dstHeight = srcRegionHeight;
if ((srcRegionYOffset + srcRegionHeight) > height) {
srcRegionHeight = height - srcRegionYOffset;
}
// creating a correct source region
srcRegion = new Rectangle(srcRegionXOffset, srcRegionYOffset, srcRegionWidth, srcRegionHeight);
} else {
// Source Region not specified.
// Assuming Source Region Dimension equal to Source Image
// Dimension
dstWidth = width;
dstHeight = height;
// dstXOffset = dstYOffset = 0;
srcRegionWidth = width;
srcRegionHeight = height;
srcRegionXOffset = srcRegionYOffset = 0;
}
// SubSampling variables initialization
xSubsamplingFactor = param.getSourceXSubsampling();
ySubsamplingFactor = param.getSourceYSubsampling();
// ////////////////////////////////////////////////////////////////////
//
// Updating the destination size in compliance with the subSampling
// parameters
//
// ////////////////////////////////////////////////////////////////////
dstWidth = ((dstWidth - 1) / xSubsamplingFactor) + 1;
dstHeight = ((dstHeight - 1) / ySubsamplingFactor) + 1;
// ////////////////////////////////////////////////////////////////
return MagickImageAdapter.magickImageToBufferedImage(im, srcRegion, dstWidth, dstHeight);
}
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class JPEGReadTest method sourceBands.
/**
* Test sourceBands management capabilities.
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void sourceBands() throws IOException, FileNotFoundException {
if (!isGDALAvailable) {
return;
}
final File inputFile = TestData.file(this, "small_world.jpg");
// //
//
// Preparing srcRegion constants
//
// //
final int srcRegionX = 40;
final int srcRegionY = 50;
final int srcRegionWidth = 400;
final int srcRegionHeight = 300;
final int subSamplingX = 2;
final int subSamplingY = 1;
// //
//
// Setting source settings parameters
//
// //
ImageReadParam rparam = new ImageReadParam();
rparam.setSourceRegion(new Rectangle(srcRegionX, srcRegionY, srcRegionWidth, srcRegionHeight));
rparam.setSourceSubsampling(subSamplingX, subSamplingY, 0, 0);
rparam.setSourceBands(new int[] { 0 });
// //
//
// Setting destination settings parameters
//
// //
rparam.setDestinationBands(new int[] { 0 });
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = RasterFactory.createComponentColorModel(// dataType
DataBuffer.TYPE_BYTE, // color space
cs, // has alpha
false, // is alphaPremultiplied
false, // transparency
Transparency.OPAQUE);
final int destWidth = srcRegionWidth / subSamplingX;
final int destHeight = srcRegionHeight / subSamplingY;
Assert.assertEquals(destWidth, 200);
Assert.assertEquals(destHeight, 300);
final SampleModel sm = cm.createCompatibleSampleModel(destWidth, destHeight);
rparam.setDestinationType(new ImageTypeSpecifier(cm, sm));
// //
//
// Preparing for image read operation
//
// //
ImageReader reader = new JpegGDALImageReaderSpi().createReaderInstance();
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
pbjImageRead.setParameter("reader", reader);
pbjImageRead.setParameter("readParam", rparam);
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(128).setTileWidth(128);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, "imageread");
else
Assert.assertNotNull(image.getTiles());
ImageIOUtilities.disposeImage(image);
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class JPEGReadTest method read.
/**
* Simple test read
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void read() throws FileNotFoundException, IOException {
if (!isGDALAvailable) {
return;
}
final ParameterBlockJAI pbjImageRead;
final ImageReadParam irp = new ImageReadParam();
final String fileName = "bw_sample.jpg";
final File file = TestData.file(this, fileName);
irp.setSourceSubsampling(1, 2, 0, 0);
pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", file);
pbjImageRead.setParameter("Reader", new JpegGDALImageReaderSpi().createReaderInstance());
pbjImageRead.setParameter("readParam", irp);
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(image, fileName);
else
Assert.assertNotNull(image.getTiles());
ImageIOUtilities.disposeImage(image);
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class JP2KReadTest method jaiOperations.
/**
* Test read exploiting common JAI operations (Crop-Translate-Rotate)
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void jaiOperations() throws IOException {
if (!isJp2KakDriverAvailable) {
return;
}
final File inputFile = TestData.file(this, fileName);
JP2GDALKakaduImageReaderSpi.setKakaduInputErrorManagement(KakaduErrorManagementType.FAST);
// ////////////////////////////////////////////////////////////////
// preparing to read
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjImageRead;
final ImageReadParam irp = new ImageReadParam();
Integer xSubSampling = new Integer(2);
Integer ySubSampling = new Integer(1);
Integer xSubSamplingOffset = new Integer(0);
Integer ySubSamplingOffset = new Integer(0);
irp.setSourceSubsampling(xSubSampling.intValue(), ySubSampling.intValue(), xSubSamplingOffset.intValue(), ySubSamplingOffset.intValue());
pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", inputFile);
pbjImageRead.setParameter("readParam", irp);
pbjImageRead.setParameter("Reader", new JP2GDALKakaduImageReaderSpi().createReaderInstance());
final ImageLayout layout = new ImageLayout();
layout.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout));
if (TestData.isInteractiveTest())
ImageIOUtilities.visualize(image, "subsampled");
// ////////////////////////////////////////////////////////////////
// preparing to crop
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjCrop = new ParameterBlockJAI("Crop");
pbjCrop.addSource(image);
// Setting a square crop to avoid blanks zone when rotating.
Float xCrop = new Float(image.getMinX() + image.getWidth() / 4);
Float yCrop = new Float(image.getMinX() + image.getWidth() / 4);
Float cropWidth = new Float(image.getWidth() / 4);
Float cropHeigth = new Float(image.getWidth() / 4);
pbjCrop.setParameter("x", xCrop);
pbjCrop.setParameter("y", yCrop);
pbjCrop.setParameter("width", cropWidth);
pbjCrop.setParameter("height", cropHeigth);
final RenderedOp croppedImage = JAI.create("Crop", pbjCrop);
if (TestData.isInteractiveTest())
ImageIOUtilities.visualize(croppedImage, "cropped");
// ////////////////////////////////////////////////////////////////
// preparing to translate
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjTranslate = new ParameterBlockJAI("Translate");
pbjTranslate.addSource(croppedImage);
Float xTrans = new Float(xCrop.floatValue() * (-1));
Float yTrans = new Float(yCrop.floatValue() * (-1));
pbjTranslate.setParameter("xTrans", xTrans);
pbjTranslate.setParameter("yTrans", yTrans);
final RenderedOp translatedImage = JAI.create("Translate", pbjTranslate);
if (TestData.isInteractiveTest())
ImageIOUtilities.visualize(translatedImage, "translated");
// ////////////////////////////////////////////////////////////////
// preparing to rotate
// ////////////////////////////////////////////////////////////////
final ParameterBlockJAI pbjRotate = new ParameterBlockJAI("Rotate");
pbjRotate.addSource(translatedImage);
Float xOrigin = new Float(cropWidth.floatValue() / 2);
Float yOrigin = new Float(cropHeigth.floatValue() / 2);
Float angle = new Float(java.lang.Math.PI / 2);
pbjRotate.setParameter("xOrigin", xOrigin);
pbjRotate.setParameter("yOrigin", yOrigin);
pbjRotate.setParameter("angle", angle);
final RenderedOp rotatedImage = JAI.create("Rotate", pbjRotate);
StringBuffer title = new StringBuffer("SUBSAMP:").append("X[").append(xSubSampling.toString()).append("]-Y[").append(ySubSampling.toString()).append("]-Xof[").append(xSubSamplingOffset.toString()).append("]-Yof[").append(ySubSamplingOffset).append("]CROP:X[").append(xCrop.toString()).append("]-Y[").append(yCrop.toString()).append("]-W[").append(cropWidth.toString()).append("]-H[").append(cropHeigth.toString()).append("]TRANS:X[").append(xTrans.toString()).append("]-Y[").append(yTrans.toString()).append("]ROTATE:xOrig[").append(xOrigin.toString()).append("]-yOrig[").append(yOrigin.toString()).append("]-ang[").append(angle.toString()).append("]");
if (TestData.isInteractiveTest())
Viewer.visualizeAllInformation(rotatedImage, title.toString());
else
Assert.assertNotNull(rotatedImage.getTiles());
ImageIOUtilities.disposeImage(rotatedImage);
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class JP2KReadTest method read.
/**
* Simple test read
*
* @throws FileNotFoundException
* @throws IOException
*/
@Test
public void read() throws FileNotFoundException, IOException {
if (!isJp2KakDriverAvailable) {
return;
}
final ParameterBlockJAI pbjImageRead;
final File file = TestData.file(this, fileName);
JP2GDALKakaduImageReaderSpi.setKakaduInputErrorManagement(KakaduErrorManagementType.FAST);
pbjImageRead = new ParameterBlockJAI("ImageRead");
pbjImageRead.setParameter("Input", file);
pbjImageRead.setParameter("Reader", new JP2GDALKakaduImageReaderSpi().createReaderInstance());
final ImageLayout layout = new ImageLayout();
layout.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout));
if (TestData.isInteractiveTest())
Viewer.visualizeBothMetadata(image, "");
else
Assert.assertNotNull(image.getTiles());
ImageIOUtilities.disposeImage(image);
}
Aggregations