Search in sources :

Example 6 with FileImageInputStream

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

the class JPEGReaderTest method readManual.

@Test
public void readManual() throws Exception {
    final File file = TestData.file(this, FILENAME);
    final File fileGray = TestData.file(this, FILENAMEGRAY);
    if (!TurboJpegUtilities.isTurboJpegAvailable()) {
        LOGGER.warning("Unable to find native libs. Tests are skipped");
        assumeTrue(false);
        return;
    }
    final ImageReader reader = new TurboJpegImageReaderSpi().createReaderInstance();
    FileImageInputStream fis = null;
    BufferedImage image = null;
    // //
    try {
        fis = new FileImageInputStream(file);
        reader.setInput(fis);
        image = reader.read(0, null);
        assertEquals(227, image.getWidth());
        assertEquals(103, image.getHeight());
        assertEquals(3, image.getSampleModel().getNumBands());
        if (TestData.isInteractiveTest()) {
            ImageIOUtilities.visualize(image, "testManualRead");
            Thread.sleep(1000);
        } else {
            assertNotNull(image.getData());
        }
        image.flush();
        image = null;
    } finally {
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
            // Does nothing
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (Throwable t) {
            // Does nothing
            }
        }
    }
    // //
    try {
        fis = new FileImageInputStream(fileGray);
        reader.setInput(fis);
        image = reader.read(0, null);
        assertEquals(227, image.getWidth());
        assertEquals(103, image.getHeight());
        assertEquals(1, image.getSampleModel().getNumBands());
        if (TestData.isInteractiveTest()) {
            ImageIOUtilities.visualize(image, "testManualRead");
            Thread.sleep(1000);
        } else {
            assertNotNull(image.getData());
        }
        image.flush();
        image = null;
    } finally {
        if (reader != null) {
            try {
                reader.dispose();
            } catch (Throwable t) {
            // Does nothing
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (Throwable t) {
            // Does nothing
            }
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 7 with FileImageInputStream

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

the class JPEGWriterCompareTest method write.

private static int write(final int loop, final long delayMs, final BufferedImage buffered, final boolean useNative) throws Exception {
    int differences = 0;
    for (int i = 0; i < loop; i++) {
        ByteArrayOutputStream out1 = encodeImageAsJpeg(buffered, 0.75f, useNative);
        Thread.sleep(delayMs);
        ByteArrayOutputStream out2 = encodeImageAsJpeg(buffered, 0.75f, useNative);
        final File file1 = new File(OUTPUT_FOLDER + "outA" + i + ".jpg");
        file1.delete();
        final File file2 = new File(OUTPUT_FOLDER + "outB" + i + ".jpg");
        file2.delete();
        FileOutputStream fos1 = new FileOutputStream(file1);
        FileOutputStream fos2 = new FileOutputStream(file2);
        out1.writeTo(fos1);
        out2.writeTo(fos2);
        dispose(out2, fos2);
        dispose(out1, fos1);
        ImageReaderSpi spi = new JPEGImageReaderSpi();
        ImageReader reader1 = spi.createReaderInstance();
        ImageReader reader2 = spi.createReaderInstance();
        FileImageInputStream fis1 = new FileImageInputStream(file1);
        reader1.setInput(fis1);
        FileImageInputStream fis2 = new FileImageInputStream(file2);
        reader2.setInput(fis2);
        BufferedImage bi1 = reader1.read(0);
        BufferedImage bi2 = reader2.read(0);
        if (!imagesAreEquals(bi1, bi2)) {
            differences++;
        }
        fis1.close();
        fis2.close();
        file1.delete();
        file2.delete();
    }
    return differences;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) PNGImageReaderSpi(com.sun.imageio.plugins.png.PNGImageReaderSpi) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) JPEGImageReaderSpi(com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) JPEGImageReaderSpi(com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi)

Example 8 with FileImageInputStream

use of javax.imageio.stream.FileImageInputStream 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 9 with FileImageInputStream

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

the class TIFFReadTest method readMasks.

@Test
public void readMasks() throws IOException {
    // Reading a File with internal masks
    final File file = TestData.file(this, "masks.tif");
    // Setting Read parameters
    final ImageReadParam param = new ImageReadParam();
    param.setSourceRegion(new Rectangle(0, 0, 2, 2));
    // Reader creation
    final TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    // Stream creation
    FileImageInputStream inputStream = new FileImageInputStream(file);
    try {
        // Setting input
        reader.setInput(inputStream);
        // IMAGE 0
        BufferedImage image = reader.read(0, param);
        Assert.assertEquals(2, image.getWidth());
        Assert.assertEquals(2, image.getHeight());
        image.flush();
        image = null;
        // Getting Stream Metadata
        IIOMetadata metadata = reader.getStreamMetadata();
        Node tree = metadata.getAsTree("com_sun_media_imageio_plugins_tiff_stream_1.0");
        // Ensuring not null
        Assert.assertNotNull(tree);
        // Checking Childs
        NodeList list = tree.getChildNodes();
        int len = list.getLength();
        // Loop on the list
        for (int i = 0; i < len; i++) {
            // Node i-th
            Node node = list.item(i);
            // Ensuring not null
            Assert.assertNotNull(node);
            // Getting the name
            String nodeName = node.getNodeName();
            // Checking attributes
            Assert.assertTrue(node.hasAttributes());
            // Getting Attribute Value
            String value = node.getAttributes().item(0).getNodeValue();
            // Getting related enum
            MetadataNode mnode = MetadataNode.getFromName(nodeName);
            // Checking Attribute value
            switch(mnode) {
                case B_ORDER:
                    Assert.assertTrue(value.equalsIgnoreCase(ByteOrder.LITTLE_ENDIAN.toString()));
                    break;
                case N_INT_MASK:
                    Assert.assertEquals(5, Integer.parseInt(value));
                    break;
                case N_EXT_MASK:
                    Assert.assertEquals(-1, Integer.parseInt(value));
                    break;
                case N_INT_OVR:
                    Assert.assertEquals(4, Integer.parseInt(value));
                    break;
                case N_EXT_OVR:
                    Assert.assertEquals(-1, Integer.parseInt(value));
                    break;
                case N_EXT_OVR_MASK:
                    Assert.assertEquals(-1, Integer.parseInt(value));
                    break;
                case EXT_MASK_FILE:
                    Assert.assertTrue(value.isEmpty());
                    break;
                case EXT_OVR_FILE:
                    Assert.assertTrue(value.isEmpty());
                    break;
                case EXT_OVR_MASK_FILE:
                    Assert.assertTrue(value.isEmpty());
                    break;
                default:
                    // Wrong element
                    Assert.assertTrue(false);
            }
        }
    } catch (Exception e) {
        // If an exception occurred the logger catch the exception and print
        // the message
        logger.log(Level.SEVERE, e.getMessage(), e);
        Assert.assertTrue(false);
    } finally {
        // and the input stream are closed
        if (inputStream != null) {
            inputStream.flush();
            inputStream.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) NodeList(org.w3c.dom.NodeList) 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) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) MetadataNode(it.geosolutions.imageioimpl.plugins.tiff.TIFFStreamMetadata.MetadataNode) FileImageInputStream(javax.imageio.stream.FileImageInputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageReadParam(javax.imageio.ImageReadParam) File(java.io.File) Test(org.junit.Test)

Example 10 with FileImageInputStream

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

the class TIFFReadTest method readFromFileJAI.

@Test
public void readFromFileJAI() throws IOException {
    final File file = TestData.file(this, "test.tif");
    // double sum = 0;
    // final long num = 10000l;
    // for (long i = 0; i < num; i++) {
    // final double time = System.nanoTime();
    // IMAGE 0
    RenderedImage image = ImageReadDescriptor.create(new FileImageInputStream(file), Integer.valueOf(0), false, false, false, null, null, null, new TIFFImageReaderSpi().createReaderInstance(), null);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, "testManualRead");
    else
        Assert.assertNotNull(PlanarImage.wrapRenderedImage(image).getTiles());
    // sum += System.nanoTime() - time;
    Assert.assertEquals(30, image.getWidth());
    Assert.assertEquals(26, image.getHeight());
    PlanarImage.wrapRenderedImage(image).dispose();
    image = null;
    // }
    // IMAGE 2
    final ImageReadParam readParam = new ImageReadParam();
    readParam.setSourceRegion(new Rectangle(0, 0, 10, 10));
    image = ImageReadDescriptor.create(new FileImageInputStream(file), Integer.valueOf(2), false, false, false, null, null, readParam, new TIFFImageReaderSpi().createReaderInstance(), null);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, "testManualRead");
    else
        Assert.assertNotNull(image.getData());
    // sum+=System.nanoTime()-time;
    Assert.assertEquals(8, image.getWidth());
    Assert.assertEquals(7, image.getHeight());
    PlanarImage.wrapRenderedImage(image).dispose();
    image = null;
    // IMAGE 4
    image = ImageReadDescriptor.create(new FileImageInputStream(file), Integer.valueOf(4), false, false, false, null, null, null, new TIFFImageReaderSpi().createReaderInstance(), null);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, "testManualRead");
    else
        Assert.assertNotNull(image.getData());
    // sum+=System.nanoTime()-time;
    Assert.assertEquals(2, image.getWidth());
    Assert.assertEquals(2, image.getHeight());
    PlanarImage.wrapRenderedImage(image).dispose();
    image = null;
    // IMAGE 5
    image = ImageReadDescriptor.create(new FileImageInputStream(file), Integer.valueOf(5), false, false, false, null, null, null, new TIFFImageReaderSpi().createReaderInstance(), null);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, "testManualRead");
    else
        Assert.assertNotNull(image.getData());
    // sum+=System.nanoTime()-time;
    Assert.assertEquals(1, image.getWidth());
    Assert.assertEquals(1, image.getHeight());
    PlanarImage.wrapRenderedImage(image).dispose();
    image = null;
    // IMAGE 1
    image = ImageReadDescriptor.create(new FileImageInputStream(file), Integer.valueOf(1), false, false, false, null, null, null, new TIFFImageReaderSpi().createReaderInstance(), null);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, "testManualRead");
    else
        Assert.assertNotNull(image.getData());
    // sum+=System.nanoTime()-time;
    Assert.assertEquals(15, image.getWidth());
    Assert.assertEquals(13, image.getHeight());
    PlanarImage.wrapRenderedImage(image).dispose();
    image = null;
    // IMAGE 3
    image = ImageReadDescriptor.create(new FileImageInputStream(file), Integer.valueOf(3), false, false, false, null, null, null, new TIFFImageReaderSpi().createReaderInstance(), null);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, "testManualRead");
    else
        Assert.assertNotNull(image.getData());
    // sum+=System.nanoTime()-time;
    Assert.assertEquals(4, image.getWidth());
    Assert.assertEquals(4, image.getHeight());
    PlanarImage.wrapRenderedImage(image).dispose();
    image = null;
    // IMAGE 5
    image = ImageReadDescriptor.create(new FileImageInputStream(file), Integer.valueOf(5), false, false, false, null, null, null, new TIFFImageReaderSpi().createReaderInstance(), null);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, "testManualRead");
    else
        Assert.assertNotNull(image.getData());
    // sum+=System.nanoTime()-time;
    Assert.assertEquals(1, image.getWidth());
    Assert.assertEquals(1, image.getHeight());
    PlanarImage.wrapRenderedImage(image).dispose();
    image = null;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReadParam(javax.imageio.ImageReadParam) Rectangle(java.awt.Rectangle) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) 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