Search in sources :

Example 21 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readFromFileDirect.

@Test
public void readFromFileDirect() throws IOException {
    final File file = TestData.file(this, "test.tif");
    final ImageReadParam param = new ImageReadParam();
    param.setSourceRegion(new Rectangle(0, 0, 2, 2));
    // double sum=0;
    // final long num = 10000l;
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    FileImageInputStream inputStream = new FileImageInputStream(file);
    try {
        reader.setInput(inputStream);
        // System.out.println(new IIOMetadataDumper(
        // reader.getImageMetadata(0),TIFFImageMetadata.nativeMetadataFormatName).getMetadata());
        // for(long i=0;i<num;i++){
        // final double time= System.nanoTime();
        // IMAGE 0
        BufferedImage image = reader.read(0, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(1, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(2, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(1, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(3, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        image = reader.read(0, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
    // sum+=System.nanoTime()-time;
    // Assert.assertEquals(120, image.getWidth());
    // Assert.assertEquals(107, image.getHeight());
    // System.out.println("test "+i);
    // 
    // }
    // System.out.println(sum/num);
    } catch (Exception e) {
        // If an exception occurred the logger catch the exception and print
        // the message
        logger.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        // and the input stream are closed
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReadParam(javax.imageio.ImageReadParam) Rectangle(java.awt.Rectangle) File(java.io.File) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Test(org.junit.Test)

Example 22 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readWithEmptyTiles.

@Test
public void readWithEmptyTiles() throws IOException {
    // This input image is a 1440x720 image. However, the right half of the image
    // is made of empty tiles filled with nodata
    final File file = TestData.file(this, "emptyTiles.tif");
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    FileImageInputStream inputStream = new FileImageInputStream(file);
    try {
        reader.setInput(inputStream);
        ImageReadParam param = new ImageReadParam();
        // Setting up a region to fall in the half of the image containing empty tiles
        param.setSourceRegion(new Rectangle(360, 0, 720, 720));
        BufferedImage image = reader.read(0, param);
        Assert.assertEquals(720, image.getWidth());
        Assert.assertEquals(720, image.getHeight());
        IIOMetadata metadata = reader.getImageMetadata(0);
        Node rootNode = metadata.getAsTree(metadata.getNativeMetadataFormatName());
        double noDataValue = getNoDataValue(rootNode);
        // get it from the core common metadata too
        CoreCommonImageMetadata ccm = (CoreCommonImageMetadata) metadata;
        double[] noDataArray = ccm.getNoData();
        assertNotNull(noDataArray);
        assertEquals(noDataArray[0], noDataValue, 0d);
        assertEquals(noDataArray[1], noDataValue, 0d);
        // Check that the value is noData (the empty Tiles are filled with NoData)
        double val = image.getData().getSampleDouble(719, 0, 0);
        assertEquals(Double.toString(noDataValue), Double.toString(val));
        image.flush();
        image = null;
    } catch (Exception e) {
        // If an exception occurred the logger catch the exception and print
        // the message
        logger.log(Level.SEVERE, e.getMessage(), e);
    } finally {
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : CoreCommonImageMetadata(it.geosolutions.imageio.core.CoreCommonImageMetadata) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) Rectangle(java.awt.Rectangle) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) FileImageInputStream(javax.imageio.stream.FileImageInputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReadParam(javax.imageio.ImageReadParam) File(java.io.File) Test(org.junit.Test)

Example 23 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project imageio-ext by geosolutions-it.

the class TIFFReadTest method readDeflateWithHorizontalDifferencingPredictorOn16Bits.

@Test
public void readDeflateWithHorizontalDifferencingPredictorOn16Bits() throws IOException {
    // This image has been created from test.tif using the command:
    // gdal_translate -OT UInt16 -co COMPRESS=DEFLATE -co PREDICTOR=2 test.tif deflatetest.tif
    final File file = TestData.file(this, "deflatetest.tif");
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    FileImageInputStream inputStream = new FileImageInputStream(file);
    try {
        reader.setInput(inputStream);
        BufferedImage image = reader.read(0);
        image.flush();
        image = null;
    } finally {
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) File(java.io.File) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 24 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project imageio-ext by geosolutions-it.

the class TIFFWriteTest method readWriteFromFileDirect.

@Test
public void readWriteFromFileDirect() throws IOException {
    // JAI.getDefaultInstance().getTileCache().setMemoryCapacity(512*1024*1024);
    // final TCTool tc= new TCTool((SunTileCache)JAI.getDefaultInstance().getTileCache());
    // new File("c:\\work\\dem30_final.tiff");
    final File inputFile = TestData.file(this, "test.tif");
    final File outputFile = TestData.temp(this, "testw.tif", true);
    final ImageReadParam param = new ImageReadParam();
    param.setSourceRegion(new Rectangle(0, 0, 10, 10));
    TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    reader.setInput(new FileImageInputStream(inputFile));
    BufferedImage image = reader.read(0, param);
    // RenderedImage image = ImageReadDescriptor.create(new FileImageInputStream(inputFile),
    // Integer.valueOf(0), false, false, false, null, null, null,
    // reader, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, new ImageLayout().setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512)));
    final TIFFImageWriter writer = (TIFFImageWriter) new TIFFImageWriterSpi().createWriterInstance();
    final ImageWriteParam writeParam = new TIFFImageWriteParam(Locale.getDefault());
    writeParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
    writeParam.setTiling(512, 512, 0, 0);
    // for( int i=0;i<1;i++){
    writer.setOutput(new FileImageOutputStream(outputFile));
    // BUG
    // writer.write(metadata, new IIOImage(image, null, metadata), null);
    writer.addIIOWriteProgressListener(new IIOWriteProgressListener() {

        public void writeAborted(ImageWriter source) {
        // TODO Auto-generated method stub
        }

        public void thumbnailStarted(ImageWriter source, int imageIndex, int thumbnailIndex) {
            assertTrue(imageIndex >= 0);
            assertTrue(thumbnailIndex == 0);
        }

        public void thumbnailProgress(ImageWriter source, float percentageDone) {
            assertTrue(percentageDone >= 0 && percentageDone <= 100);
        }

        public void thumbnailComplete(ImageWriter source) {
        // TODO Auto-generated method stub
        }

        public void imageStarted(ImageWriter source, int imageIndex) {
            assertTrue(imageIndex >= 0);
            LOGGER.info("imageStarted");
        }

        public void imageProgress(ImageWriter source, float percentageDone) {
            assertTrue(percentageDone >= 0 && percentageDone <= 100);
            LOGGER.info(percentageDone + "%");
        }

        public void imageComplete(ImageWriter source) {
            LOGGER.info("imageComplete");
        }
    });
    writer.write(null, new IIOImage(image, null, null), writeParam);
    // }
    writer.dispose();
    reader.reset();
    reader.setInput(new FileImageInputStream(outputFile));
    image = reader.read(0);
    Assert.assertEquals(10, image.getWidth());
    Assert.assertEquals(10, image.getHeight());
    image.flush();
    image = null;
    reader.dispose();
}
Also used : TIFFImageWriterSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageWriterSpi) FileImageOutputStream(javax.imageio.stream.FileImageOutputStream) Rectangle(java.awt.Rectangle) TIFFImageWriteParam(it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam) TIFFImageWriter(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageWriter) ImageWriter(javax.imageio.ImageWriter) TIFFImageWriteParam(it.geosolutions.imageio.plugins.tiff.TIFFImageWriteParam) ImageWriteParam(javax.imageio.ImageWriteParam) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) BufferedImage(java.awt.image.BufferedImage) IIOWriteProgressListener(javax.imageio.event.IIOWriteProgressListener) IIOImage(javax.imageio.IIOImage) FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReadParam(javax.imageio.ImageReadParam) TIFFImageWriter(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageWriter) File(java.io.File) Test(org.junit.Test)

Example 25 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream in project imageio-ext by geosolutions-it.

the class JP2KKakaduQualityLayersWriteTest method testWriteCompressionProfiles.

@Test
@Ignore
public void testWriteCompressionProfiles() throws IOException {
    final File file = new File(inputFileName);
    FileImageInputStream fis = null;
    new FileImageInputStream(file);
    ImageReader reader = null;
    try {
        fis = new FileImageInputStream(file);
        reader = JP2K_RSPI.createReaderInstance();
        RenderedImage ri = ImageReadDescriptor.create(fis, 0, false, false, false, null, null, null, reader, null);
        // ImageIO.write(ri, "TIFF", new File("c:\\readback.tif"));
        write(inputFileName, ri, Compression.NUMERICALLY_LOSSLESS, CompressionProfile.NPJE);
        write(inputFileName, ri, Compression.NUMERICALLY_LOSSLESS, CompressionProfile.EPJE);
        write(inputFileName, ri, Compression.LOSSY, CompressionProfile.NPJE);
        write(inputFileName, ri, Compression.LOSSY, CompressionProfile.EPJE);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (Throwable t) {
            }
        }
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
            }
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReader(javax.imageio.ImageReader) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

FileImageInputStream (javax.imageio.stream.FileImageInputStream)36 File (java.io.File)22 BufferedImage (java.awt.image.BufferedImage)19 IOException (java.io.IOException)16 ImageInputStream (javax.imageio.stream.ImageInputStream)15 ImageReader (javax.imageio.ImageReader)14 Test (org.junit.Test)14 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)10 TIFFImageReaderSpi (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi)9 TIFFImageReader (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader)7 RenderedImage (java.awt.image.RenderedImage)7 ImageReadParam (javax.imageio.ImageReadParam)7 Rectangle (java.awt.Rectangle)6 FileImageOutputStream (javax.imageio.stream.FileImageOutputStream)4 CMMException (java.awt.color.CMMException)3 InputStream (java.io.InputStream)3 RandomAccessFile (java.io.RandomAccessFile)3 IIOImage (javax.imageio.IIOImage)3 ImageOutputStream (javax.imageio.stream.ImageOutputStream)3 JPEGImageReaderSpi (com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi)2