Search in sources :

Example 1 with ImageLayout

use of javax.media.jai.ImageLayout 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;
}
Also used : CopyOpImage(com.sun.media.jai.opimage.CopyOpImage) RenderedOp(javax.media.jai.RenderedOp) ImageLayout(javax.media.jai.ImageLayout) RenderingHints(java.awt.RenderingHints)

Example 2 with ImageLayout

use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.

the class JPEGWriterTest method writerTestRefineLayout.

@Test
public void writerTestRefineLayout() throws IOException {
    if (SKIP_TESTS) {
        LOGGER.warning(ERROR_LIB_MESSAGE);
        assumeTrue(!SKIP_TESTS);
        return;
    }
    // test-data
    final File input = TestData.file(this, "testme.jpg");
    FileImageInputStream stream = null;
    ImageReader reader = null;
    try {
        stream = new FileImageInputStream(input);
        ImageLayout layout = new ImageLayout();
        layout.setTileGridXOffset(-2);
        layout.setTileGridYOffset(-2);
        layout.setTileWidth(228);
        layout.setTileHeight(104);
        reader = ImageIO.getImageReaders(stream).next();
        RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
        BufferedImage sourceImage = ImageIO.read(input);
        sourceImage.getWidth();
        RenderedImage inputImage = BandSelectDescriptor.create(sourceImage, new int[] { 0 }, hints);
        // get the SPI for writer\
        final Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName(TurboJpegImageWriterSpi.formatNames[0]);
        assertTrue(it.hasNext());
        TurboJpegImageWriter writer = null;
        while (it.hasNext()) {
            ImageWriterSpi writer_ = it.next().getOriginatingProvider();
            if (writer_ instanceof TurboJpegImageWriterSpi) {
                writer = (TurboJpegImageWriter) writer_.createWriterInstance();
                break;
            }
        }
        assertNotNull("Unable to find TurboJpegImageWriter", writer);
        IIOImage image = new IIOImage(inputImage, null, null);
        // create write param
        ImageWriteParam wParam_ = writer.getDefaultWriteParam();
        assertTrue(wParam_ instanceof TurboJpegImageWriteParam);
        TurboJpegImageWriteParam wParam = (TurboJpegImageWriteParam) wParam_;
        wParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        wParam.setCompressionType("JPEG");
        wParam.setCompressionQuality(.75f);
        // create output file
        final File output = TestData.temp(this, "output.jpeg", false);
        LOGGER.info("output file is " + output);
        writer.setOutput(output);
        writer.write(null, image, wParam);
        writer.dispose();
        assertTrue("Unable to create output file", output.exists() && output.isFile());
    } catch (Throwable t) {
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (Throwable t) {
            }
        }
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
            }
        }
    }
}
Also used : ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) RenderingHints(java.awt.RenderingHints) BufferedImage(java.awt.image.BufferedImage) IIOImage(javax.imageio.IIOImage) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReader(javax.imageio.ImageReader) RenderedImage(java.awt.image.RenderedImage) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) Test(org.junit.Test)

Example 3 with ImageLayout

use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.

the class GeoTiffVrtTest method write.

/**
 * Test Writing capabilities.
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void write() throws IOException, FileNotFoundException {
    if (!isGDALAvailable) {
        return;
    }
    final File outputFile = TestData.temp(this, "writetest.tif", false);
    outputFile.deleteOnExit();
    final File inputFile = TestData.file(this, "utmByte.tif.vrt");
    ImageReadParam rparam = new ImageReadParam();
    rparam.setSourceRegion(new Rectangle(1, 1, 300, 500));
    rparam.setSourceSubsampling(1, 2, 0, 0);
    ImageReader reader = new VRTImageReaderSpi().createReaderInstance();
    reader.setInput(inputFile);
    final IIOMetadata metadata = reader.getImageMetadata(0);
    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(256).setTileWidth(256);
    RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
    if (TestData.isInteractiveTest())
        Viewer.visualizeAllInformation(image, "geotiff");
    // ////////////////////////////////////////////////////////////////
    // preparing to write
    // ////////////////////////////////////////////////////////////////
    final ParameterBlockJAI pbjImageWrite = new ParameterBlockJAI("ImageWrite");
    ImageWriter writer = new GeoTiffImageWriterSpi().createWriterInstance();
    pbjImageWrite.setParameter("Output", outputFile);
    pbjImageWrite.setParameter("writer", writer);
    pbjImageWrite.setParameter("ImageMetadata", metadata);
    pbjImageWrite.setParameter("Transcode", false);
    ImageWriteParam param = new ImageWriteParam(Locale.getDefault());
    param.setSourceRegion(new Rectangle(10, 10, 100, 100));
    param.setSourceSubsampling(2, 1, 0, 0);
    pbjImageWrite.setParameter("writeParam", param);
    pbjImageWrite.addSource(image);
    final RenderedOp op = JAI.create("ImageWrite", pbjImageWrite);
    final ImageWriter writer2 = (ImageWriter) op.getProperty(ImageWriteDescriptor.PROPERTY_NAME_IMAGE_WRITER);
    writer2.dispose();
    // ////////////////////////////////////////////////////////////////
    // preparing to read again
    // ////////////////////////////////////////////////////////////////
    final ParameterBlockJAI pbjImageReRead = new ParameterBlockJAI("ImageRead");
    pbjImageReRead.setParameter("Input", outputFile);
    pbjImageReRead.setParameter("Reader", new GeoTiffImageReaderSpi().createReaderInstance());
    final RenderedOp image2 = JAI.create("ImageRead", pbjImageReRead);
    if (TestData.isInteractiveTest())
        Viewer.visualizeAllInformation(image2, "geotif2");
    else
        Assert.assertNotNull(image2.getTiles());
}
Also used : ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ImageWriter(javax.imageio.ImageWriter) ImageWriteParam(javax.imageio.ImageWriteParam) GeoTiffImageReaderSpi(it.geosolutions.imageio.plugins.geotiff.GeoTiffImageReaderSpi) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReadParam(javax.imageio.ImageReadParam) RenderedOp(javax.media.jai.RenderedOp) GeoTiffImageWriterSpi(it.geosolutions.imageio.plugins.geotiff.GeoTiffImageWriterSpi) ImageReader(javax.imageio.ImageReader) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest) Test(org.junit.Test)

Example 4 with ImageLayout

use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.

the class Doq2VrtTest method imageRead.

/**
 * Test read exploiting common JAI operations (Crop-Translate-Rotate)
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void imageRead() throws FileNotFoundException, IOException {
    if (!isGDALAvailable) {
        return;
    }
    File file = TestData.file(this, fileName);
    // ////////////////////////////////////////////////////////////////
    // preparing to read
    // ////////////////////////////////////////////////////////////////
    final ParameterBlockJAI pbjImageRead;
    final ImageReadParam irp = new ImageReadParam();
    pbjImageRead = new ParameterBlockJAI("ImageRead");
    pbjImageRead.setParameter("Input", file);
    pbjImageRead.setParameter("readParam", irp);
    // NOTE that the actual sample data (fakedoq1.doq) only contains a row.
    // Therefore, we need to force the read on that reduced area.
    // Requesting a bigger image height will result in a GDAL ReadBlock error.
    irp.setSourceRegion(new Rectangle(0, 0, 500, 1));
    final ImageLayout l = new ImageLayout();
    l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512);
    // get a RenderedImage
    RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
    if (TestData.isInteractiveTest()) {
        Viewer.visualizeAllInformation(image, "test");
    } else {
        Assert.assertNotNull(image.getTiles());
    }
    Assert.assertEquals(500, image.getWidth());
    Assert.assertEquals(1, image.getHeight());
    ImageIOUtilities.disposeImage(image);
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) RenderedOp(javax.media.jai.RenderedOp) ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) Test(org.junit.Test) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest)

Example 5 with ImageLayout

use of javax.media.jai.ImageLayout in project imageio-ext by geosolutions-it.

the class MrSIDTest method subBandsRead.

/**
 * Test read exploiting the setSourceBands and setDestinationType on
 * imageReadParam
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void subBandsRead() throws IOException {
    if (!isMrSidAvailable) {
        return;
    }
    try {
        ImageReader reader = new MrSIDImageReaderSpi().createReaderInstance();
        final File file = TestData.file(this, fileName);
        reader.setInput(file);
        // //
        // 
        // Getting image properties
        // 
        // //
        ImageTypeSpecifier spec = (ImageTypeSpecifier) reader.getImageTypes(0).next();
        SampleModel sm = spec.getSampleModel();
        final int width = reader.getWidth(0);
        final int height = reader.getHeight(0);
        // //
        // 
        // Setting a ColorModel
        // 
        // //
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorModel cm = RasterFactory.createComponentColorModel(sm.getDataType(), // color space
        cs, // has alpha
        false, // is alphaPremultiplied
        false, // transparency
        Transparency.OPAQUE);
        // //
        // 
        // Setting Image Read Parameters
        // 
        // //
        final ImageReadParam param = new ImageReadParam();
        final int ssx = 2;
        final int ssy = 2;
        param.setSourceSubsampling(ssx, ssy, 0, 0);
        final Rectangle sourceRegion = new Rectangle(50, 50, 300, 300);
        param.setSourceRegion(sourceRegion);
        param.setSourceBands(new int[] { 0 });
        Rectangle intersRegion = new Rectangle(0, 0, width, height);
        intersRegion = intersRegion.intersection(sourceRegion);
        int subsampledWidth = (intersRegion.width + ssx - 1) / ssx;
        int subsampledHeight = (intersRegion.height + ssy - 1) / ssy;
        param.setDestinationType(new ImageTypeSpecifier(cm, sm.createCompatibleSampleModel(subsampledWidth, subsampledHeight).createSubsetSampleModel(new int[] { 0 })));
        // //
        // 
        // Preparing the ImageRead operation
        // 
        // //
        ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
        pbjImageRead.setParameter("Input", file);
        pbjImageRead.setParameter("readParam", param);
        pbjImageRead.setParameter("reader", reader);
        // //
        // 
        // Setting a Layout
        // 
        // //
        final ImageLayout l = new ImageLayout();
        l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(256).setTileWidth(256);
        RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
        if (TestData.isInteractiveTest())
            Viewer.visualizeAllInformation(image, "SourceBand selection");
        else {
            Assert.assertNotNull(image.getTiles());
            ImageIOUtilities.disposeImage(image);
        }
    } catch (FileNotFoundException fnfe) {
        warningMessage();
    }
}
Also used : ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) ColorSpace(java.awt.color.ColorSpace) Rectangle(java.awt.Rectangle) FileNotFoundException(java.io.FileNotFoundException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Point(java.awt.Point) RenderingHints(java.awt.RenderingHints) ImageReadParam(javax.imageio.ImageReadParam) SampleModel(java.awt.image.SampleModel) RenderedOp(javax.media.jai.RenderedOp) ColorModel(java.awt.image.ColorModel) ImageReader(javax.imageio.ImageReader) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest) Test(org.junit.Test)

Aggregations

ImageLayout (javax.media.jai.ImageLayout)36 File (java.io.File)29 RenderedOp (javax.media.jai.RenderedOp)28 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)26 RenderingHints (java.awt.RenderingHints)23 Test (org.junit.Test)23 ImageReadParam (javax.imageio.ImageReadParam)20 AbstractGDALTest (it.geosolutions.imageio.gdalframework.AbstractGDALTest)18 ImageReader (javax.imageio.ImageReader)12 Rectangle (java.awt.Rectangle)8 FileNotFoundException (java.io.FileNotFoundException)7 ImageWriter (javax.imageio.ImageWriter)6 RenderedImage (java.awt.image.RenderedImage)5 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)5 ImageWriteParam (javax.imageio.ImageWriteParam)4 IIOMetadata (javax.imageio.metadata.IIOMetadata)4 ColorSpace (java.awt.color.ColorSpace)3 BufferedImage (java.awt.image.BufferedImage)3 ColorModel (java.awt.image.ColorModel)3 SampleModel (java.awt.image.SampleModel)3