use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class ImageReadOpImageMT method layoutHelper.
/**
* Derive the image layout based on the user-supplied layout, reading
* parameters, and image index.
*/
private static ImageLayout layoutHelper(ImageLayout il, ImageReadParam param, ImageReader reader, int imageIndex) throws IOException {
ImageLayout layout = (il == null) ? new ImageLayout() : (ImageLayout) il.clone();
// and ColorModel from the ImageReadParam, if supplied.
if (!layout.isValid(ImageLayout.SAMPLE_MODEL_MASK) && !layout.isValid(ImageLayout.COLOR_MODEL_MASK)) {
// If an ImageReadParam has been supplied and has its
// destinationType set then use it. Otherwise default to
// the raw image type.
ImageTypeSpecifier imageType = (param != null && param.getDestinationType() != null) ? param.getDestinationType() : reader.getRawImageType(imageIndex);
// returns null for earth.jpg, Bas-noir.jpg, etc.
if (imageType == null) {
Iterator imageTypes = reader.getImageTypes(imageIndex);
while (imageType == null && imageTypes.hasNext()) {
imageType = (ImageTypeSpecifier) imageTypes.next();
}
}
// XXX Should an exception be thrown if imageType is null?
if (imageType != null) {
// Set the SampleModel and ColorModel.
layout.setSampleModel(imageType.getSampleModel());
layout.setColorModel(imageType.getColorModel());
}
}
// --- Set up the destination bounds. ---
// Calculate the computable destination bounds.
Dimension sourceSize = getSourceSize(param, reader, imageIndex);
Rectangle srcRegion = new Rectangle();
Rectangle destRegion = new Rectangle();
computeRegions(param, sourceSize.width, sourceSize.height, layout.getMinX(// valid value or 0
null), // valid value or 0
layout.getMinY(null), false, srcRegion, destRegion);
if (!destRegion.isEmpty()) {
// Backup layout image bounds with computable bounds.
if (!layout.isValid(ImageLayout.WIDTH_MASK)) {
layout.setWidth(destRegion.width);
}
if (!layout.isValid(ImageLayout.HEIGHT_MASK)) {
layout.setHeight(destRegion.height);
}
if (!layout.isValid(ImageLayout.MIN_X_MASK)) {
layout.setMinX(destRegion.x);
}
if (!layout.isValid(ImageLayout.MIN_Y_MASK)) {
layout.setMinY(destRegion.y);
}
// Ensure the layout bounds intersect computable bounds.
Rectangle destBounds = new Rectangle(layout.getMinX(null), layout.getMinY(null), layout.getWidth(null), layout.getHeight(null));
if (destRegion.intersection(destBounds).isEmpty()) {
throw new IllegalArgumentException(I18N.getString("ImageReadOpImage0"));
}
}
if (!layout.isValid(ImageLayout.TILE_GRID_X_OFFSET_MASK)) {
layout.setTileGridXOffset(reader.getTileGridXOffset(imageIndex));
}
if (!layout.isValid(ImageLayout.TILE_GRID_Y_OFFSET_MASK)) {
layout.setTileGridYOffset(reader.getTileGridYOffset(imageIndex));
}
if (!layout.isValid(ImageLayout.TILE_WIDTH_MASK)) {
layout.setTileWidth(reader.getTileWidth(imageIndex));
}
if (!layout.isValid(ImageLayout.TILE_HEIGHT_MASK)) {
layout.setTileHeight(reader.getTileHeight(imageIndex));
}
return layout;
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class JP2KakaduReadTest method jaiReadFromFile.
@org.junit.Test
public void jaiReadFromFile() throws IOException {
if (!runTests)
return;
final File file = TestData.file(this, "CB_TM432.jp2");
ImageReadDescriptorMT.register(JAI.getDefaultInstance());
final ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageReadMT");
ImageLayout l = new ImageLayout();
l.setTileHeight(256);
l.setTileWidth(256);
JP2KKakaduImageReadParam rp = new JP2KKakaduImageReadParam();
rp.setSourceSubsampling(1, 1, 0, 0);
rp.setSourceRegion(new Rectangle(10, 10, 200, 200));
rp.setInterpolationType(JP2KKakaduImageReadParam.INTERPOLATION_BILINEAR);
rp.setQualityLayers(2);
pbjImageRead.setParameter("ReadParam", rp);
pbjImageRead.setParameter("Input", file);
pbjImageRead.setParameter("imageChoice", 0);
RenderedOp image = JAI.create("ImageReadMT", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest())
ImageIOUtilities.visualize(image);
else
Assert.assertNotNull(image.getTiles());
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class GrayAlpha8bitTest method testGrayAlpha8Bit.
@Test
public void testGrayAlpha8Bit() throws Exception {
BufferedImage bi = new BufferedImage(50, 50, BufferedImage.TYPE_BYTE_GRAY);
Graphics2D graphics = bi.createGraphics();
graphics.setColor(Color.BLACK);
graphics.fillRect(0, 0, 16, 32);
graphics.setColor(Color.WHITE);
graphics.fillRect(16, 0, 16, 32);
graphics.dispose();
final ImageLayout tempLayout = new ImageLayout(bi);
tempLayout.unsetValid(ImageLayout.COLOR_MODEL_MASK).unsetValid(ImageLayout.SAMPLE_MODEL_MASK);
RenderedImage alpha = ConstantDescriptor.create(Float.valueOf(bi.getWidth()), Float.valueOf(bi.getHeight()), new Byte[] { Byte.valueOf((byte) 255) }, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, tempLayout));
RenderedImage grayAlpha = BandMergeDescriptor.create(bi, alpha, null);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
float quality = 5f / 9 - 1;
new PNGWriter().writePNG(grayAlpha, bos, -quality, FilterType.FILTER_NONE);
BufferedImage read = ImageIO.read(new ByteArrayInputStream(bos.toByteArray()));
BufferedImage gaBuffered = PlanarImage.wrapRenderedImage(grayAlpha).getAsBufferedImage();
ImageAssert.assertImagesEqual(gaBuffered, read);
// now using imagewriter interface
ImageWriter writer = new PNGImageWriterSPI().createWriterInstance();
writer.setOutput(bos);
ImageWriteParam wp = writer.getDefaultWriteParam();
wp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
wp.setCompressionQuality(-quality);
writer.write(null, new IIOImage(bi, null, null), wp);
writer.dispose();
ImageAssert.assertImagesEqual(bi, ImageIO.read(new ByteArrayInputStream(bos.toByteArray())));
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class PngSuiteImagesTest method testRoundTripTiledImage.
@Test
public void testRoundTripTiledImage() throws Exception {
BufferedImage input = ImageIO.read(sourceFile);
// prepare a tiled image layout
ImageLayout il = new ImageLayout(input);
il.setTileWidth(8);
il.setTileHeight(8);
RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, il);
RenderedOp tiled = FormatDescriptor.create(input, input.getSampleModel().getDataType(), hints);
assertEquals(8, tiled.getTileWidth());
assertEquals(8, tiled.getTileHeight());
roundTripPNGJ(input, tiled);
}
use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.
the class JPEGReadTest method multithreadedJAIRead.
/**
* Simple test read
*
* @throws FileNotFoundException
* @throws IOException
*/
public void multithreadedJAIRead() throws FileNotFoundException, IOException {
if (!isJmagickAvailable) {
LOGGER.warning("JMagick Library is not Available; Skipping tests");
return;
}
// register the image read mt operation
ImageReadDescriptorMT.register(JAI.getDefaultInstance());
// get the file we are going to read
final String fileName = "001140.jpg";
final File file = TestData.file(this, fileName);
// acquire a reader for it but check that it is the right one
final Iterator readersIt = ImageIO.getImageReaders(file);
ImageReader reader = null;
while (readersIt.hasNext()) {
reader = (ImageReader) readersIt.next();
if (reader instanceof JpegJMagickImageReader)
break;
reader = null;
}
assertNotNull(reader);
// do an image read with jai
final ParameterBlockJAI pbjImageRead;
final ImageReadParam irp = reader.getDefaultReadParam();
irp.setSourceSubsampling(4, 4, 0, 0);
pbjImageRead = new ParameterBlockJAI("ImageReadMT");
pbjImageRead.setParameter("Input", file);
pbjImageRead.setParameter("Reader", reader);
pbjImageRead.setParameter("readParam", irp);
// set the layout so that we shrink the amount of memory needed to load
// this image
final ImageLayout l = new ImageLayout();
l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512);
RenderedOp image = JAI.create("ImageReadMT", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
if (TestData.isInteractiveTest()) {
final JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(new ScrollingImagePanel(image, 1024, 768));
jf.pack();
jf.show();
} else {
assertNotNull(image.getTiles());
// remember that if we do not explictly provide an Imagereader to
// the ImageReadMT operation it consistently dispose the one it
// creates once we dispose the ImageReadOpImage
image.dispose();
}
}
Aggregations