Search in sources :

Example 31 with RenderedOp

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

the class ImageIOUtilities method disposeImage.

/**
 * Allow to dispose this image, as well as the related image sources.
 *
 * @param rOp
 *            the image to be disposed.
 */
public static void disposeImage(RenderedImage rOp) {
    if (rOp != null) {
        if (rOp instanceof RenderedOp) {
            RenderedOp renderedOp = (RenderedOp) rOp;
            final int nSources = renderedOp.getNumSources();
            if (nSources > 0) {
                for (int k = 0; k < nSources; k++) {
                    Object source = null;
                    try {
                        source = renderedOp.getSourceObject(k);
                    } catch (ArrayIndexOutOfBoundsException e) {
                    // Ignore
                    }
                    if (source != null) {
                        if (source instanceof RenderedOp) {
                            disposeImage((RenderedOp) source);
                        } else if (source instanceof BufferedImage) {
                            ((BufferedImage) source).flush();
                            source = null;
                        }
                    }
                }
            } else {
                // get the reader
                Object imageReader = rOp.getProperty(ImageReadDescriptor.PROPERTY_NAME_IMAGE_READER);
                if (imageReader != null && imageReader instanceof ImageReader) {
                    final ImageReader reader = (ImageReader) imageReader;
                    final ImageInputStream stream = (ImageInputStream) reader.getInput();
                    try {
                        stream.close();
                    } catch (Throwable e) {
                    // swallow this
                    }
                    try {
                        reader.dispose();
                    } catch (Throwable e) {
                    // swallow this
                    }
                }
            }
            final Object roi = rOp.getProperty("ROI");
            if (roi != null && (roi instanceof ROI || roi instanceof RenderedImage)) {
                ROI roiImage = (ROI) roi;
                PlanarImage image = roiImage.getAsImage();
                if (image != null) {
                    image.dispose();
                    image = null;
                    roiImage = null;
                }
            }
            if (rOp instanceof PlanarImage) {
                ((PlanarImage) rOp).dispose();
            } else if (rOp instanceof BufferedImage) {
                ((BufferedImage) rOp).flush();
                rOp = null;
            }
        }
    }
}
Also used : RenderedOp(javax.media.jai.RenderedOp) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader) RenderedImage(java.awt.image.RenderedImage) ROI(javax.media.jai.ROI) BufferedImage(java.awt.image.BufferedImage) PlanarImage(javax.media.jai.PlanarImage)

Example 32 with RenderedOp

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

the class ImageIOUtilities method visualizeRescaled.

/**
 * Visualize the image, after rescaling its values, given a threshold for
 * the ROI. This is useful to rescale an image coming from a source which
 * may contain noDataValues. The ROI threshold allows to set a minimal value
 * to be included in the computation of the future JAI extrema operation
 * used before the JAI rescale operation.
 *
 * @param image
 *                RenderedImage to visualize
 * @param title
 *                title for the frame
 * @param roiThreshold
 *                the threshold for the inner ROI
 */
static void visualizeRescaled(RenderedImage image, String title, int roiThreshold) {
    ROI roi = new ROI(image, roiThreshold);
    ParameterBlock pb = new ParameterBlock();
    // The source image
    pb.addSource(image);
    if (roi != null)
        // The region of the image to scan
        pb.add(roi);
    // Perform the extrema operation on the source image
    RenderedOp op = JAI.create("extrema", pb);
    // Retrieve both the maximum and minimum pixel value
    double[][] extrema = (double[][]) op.getProperty("extrema");
    final double[] scale = new double[] { (255) / (extrema[1][0] - extrema[0][0]) };
    final double[] offset = new double[] { ((255) * extrema[0][0]) / (extrema[0][0] - extrema[1][0]) };
    // Preparing to rescaling values
    ParameterBlock pbRescale = new ParameterBlock();
    pbRescale.add(scale);
    pbRescale.add(offset);
    pbRescale.addSource(image);
    RenderedOp rescaledImage = JAI.create("Rescale", pbRescale);
    ParameterBlock pbConvert = new ParameterBlock();
    pbConvert.addSource(rescaledImage);
    pbConvert.add(DataBuffer.TYPE_BYTE);
    RenderedOp destImage = JAI.create("format", pbConvert);
    visualize(destImage, title);
}
Also used : ParameterBlock(java.awt.image.renderable.ParameterBlock) RenderedOp(javax.media.jai.RenderedOp) ROI(javax.media.jai.ROI)

Example 33 with RenderedOp

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

the class TestImageInputStream method URLImageInputStream.

// /**
// * @throws IOException
// * @throws FileNotFoundException
// */
// public void testGZIPImageInputStream() throws FileNotFoundException,
// IOException {
// 
// // decompress the gzipped eraf to a stringbuffer
// final GZIPImageInputStream gzipIIS = new GZIPImageInputStream(
// new FileImageInputStreamExtImpl(TestData.file(this,
// "jam.txt.gz")));
// 
// StringBuffer buf = new StringBuffer();
// String s;
// 
// while ((s = gzipIIS.readLine()) != null) {
// buf.append(s).append("\n");
// }
// gzipIIS.close();
// if (TestData.isInteractiveTest())
// LOGGER.info("\n\n\nReading gzipped\n\n\n" + buf.toString() + "\n\n\n");
// 
// //compare them
// assertTrue(buf2==buf);
// // decompress the gzipped eraf to a stringbuffer
// final GZIPImageInputStream gzipIIS = new GZIPImageInputStream(
// new FileImageInputStreamExtImpl(TestData.file(this,
// "jam.txt.gz")));
// 
// StringBuffer buf = new StringBuffer();
// String s;
// int b;
// while ((b = gzipIIS.read())>0) {
// buf.append((char)b);
// }
// gzipIIS.close();
// LOGGER.info(buf.toString());
// 
// get the original unzipped eraf
// final FileImageInputStreamExtImpl fileIIS = new
// FileImageInputStreamExtImpl(
// TestData.file(this, "jam.txt"));
// 
// StringBuffer buf2 = new StringBuffer();
// String s2 = "";
// while ((s2 = fileIIS.readLine()) != null) {
// buf2.append(s2).append("\n");
// }
// fileIIS.close();
// if (TestData.isInteractiveTest())
// LOGGER.info("\n\n\nReading gzipped\n\n\n" + buf.toString() + "\n\n\n");
// 
// // compare them
// // assertTrue(buf2.toString() == buf.toString());
// 
// 
// 
// }
/**
 * Testing capabilities of {@link URLImageInputStreamSpi}.
 */
@Test
public void URLImageInputStream() {
    LOGGER.info("Testing capabilities of URLImageInputStreamSpi");
    // get a URL pointing to a FILE
    final URL inURLToFile = TestData.getResource(this, "a.txt");
    // get an ImageInputStream
    ImageInputStream instream;
    try {
        instream = ImageIO.createImageInputStream(inURLToFile);
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
        instream = null;
    }
    Assert.assertNotNull("Unable to get an URLImageInputStreamSpi from a URL pointing to a File", instream);
    // get a URL pointing to an http page
    try {
        final URL httpURL = new URL("http://www.corriere.it/");
        instream = ImageIO.createImageInputStream(httpURL);
    } catch (MalformedURLException e) {
        LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
        instream = null;
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
        instream = null;
    }
    Assert.assertNotNull("Unable to get an URLImageInputStreamSpi from a URL pointing to an http page", instream);
    try {
        final URL url = TestData.url(this, "sample.jpeg");
        final ImageInputStream stream = ImageIO.createImageInputStream(url);
        final RenderedOp image = JAI.create("ImageRead", stream);
        if (TestData.isInteractiveTest())
            visualize(image, "testURLImageInputStreamSpi");
        else
            image.getAsBufferedImage();
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    LOGGER.info("Testing capabilities of URLImageInputStreamSpi: SUCCESS!!!");
}
Also used : MalformedURLException(java.net.MalformedURLException) RenderedOp(javax.media.jai.RenderedOp) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) URL(java.net.URL) Test(org.junit.Test)

Example 34 with RenderedOp

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

the class TestImageInputStream method fileImageInputStreamExtImpl.

/**
 * Testing capabilities of {@link FileImageInputStreamExtImpl}.
 */
@Test
public void fileImageInputStreamExtImpl() {
    LOGGER.info("Testing capabilities of FileImageInputStreamExt");
    try {
        final File url = TestData.file(this, "sample.jpeg");
        final ImageInputStream stream = ImageIO.createImageInputStream(url);
        final RenderedOp image = JAI.create("ImageRead", stream);
        if (TestData.isInteractiveTest())
            visualize(image, "testFileImageInputStreamExtImpl");
        else
            image.getAsBufferedImage();
    } catch (IOException e) {
        LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    LOGGER.info("Testing capabilities of URLImageInputStreamSpi: SUCCESS!!!");
}
Also used : RenderedOp(javax.media.jai.RenderedOp) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) File(java.io.File) Test(org.junit.Test)

Example 35 with RenderedOp

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

the class AsciiGridTest method testReadGrassGZ.

/**
 * Read a GRASS, compressed (GZ) file
 */
public void testReadGrassGZ() throws FileNotFoundException, IOException {
    // be run only when extensive tests are requested.
    if (TestData.isExtensiveTest()) {
        String title = new String("JAI ImageRead on a GRASS GZipped file ");
        LOGGER.info("\n\n " + title + " \n");
        File inputFile = TestData.file(this, "spearfish.asc.gz");
        final GZIPInputStream stream = new GZIPInputStream(new FileInputStream(inputFile));
        ParameterBlockJAI pbjImageRead = new ParameterBlockJAI("ImageRead");
        pbjImageRead.setParameter("Input", stream);
        RenderedOp image = JAI.create("ImageRead", pbjImageRead);
        if (TestData.isInteractiveTest())
            ImageIOUtilities.visualize(image, title, true);
        else {
            assertNotNull(image.getTiles());
            image.dispose();
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) RenderedOp(javax.media.jai.RenderedOp) ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

RenderedOp (javax.media.jai.RenderedOp)82 File (java.io.File)72 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)71 Test (org.junit.Test)61 ImageReadParam (javax.imageio.ImageReadParam)48 AbstractGDALTest (it.geosolutions.imageio.gdalframework.AbstractGDALTest)32 ImageWriter (javax.imageio.ImageWriter)28 ImageLayout (javax.media.jai.ImageLayout)28 ImageWriteParam (javax.imageio.ImageWriteParam)23 RenderingHints (java.awt.RenderingHints)21 FileImageOutputStreamExtImpl (it.geosolutions.imageio.stream.output.FileImageOutputStreamExtImpl)20 ImageReader (javax.imageio.ImageReader)14 FileNotFoundException (java.io.FileNotFoundException)13 Rectangle (java.awt.Rectangle)9 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 IOException (java.io.IOException)4 ImageInputStream (javax.imageio.stream.ImageInputStream)4 FileImageInputStreamExtImpl (it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl)3 ColorSpace (java.awt.color.ColorSpace)3 ColorModel (java.awt.image.ColorModel)3