Search in sources :

Example 1 with EnhancedImageReadParam

use of it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam in project imageio-ext by geosolutions-it.

the class ECWTest method imageRead.

// ecwp
/**
 * Test reading of a RGB image
 *
 * @throws FileNotFoundException
 * @throws IOException
 */
@Test
public void imageRead() throws FileNotFoundException, IOException {
    if (!isECWAvailable)
        return;
    final ParameterBlockJAI pbjImageRead;
    final EnhancedImageReadParam irp = new EnhancedImageReadParam();
    final String fileName = "sample.ecw";
    final File file = TestData.file(this, fileName);
    irp.setSourceSubsampling(2, 2, 0, 0);
    pbjImageRead = new ParameterBlockJAI("ImageRead");
    pbjImageRead.setParameter("Input", file);
    pbjImageRead.setParameter("readParam", irp);
    final ImageLayout l = new ImageLayout();
    l.setTileGridXOffset(0).setTileGridYOffset(0).setTileHeight(512).setTileWidth(512);
    RenderedOp image = JAI.create("ImageRead", pbjImageRead, new RenderingHints(JAI.KEY_IMAGE_LAYOUT, l));
    if (TestData.isInteractiveTest())
        Viewer.visualizeAllInformation(image, fileName);
    else
        image.getTiles();
    Assert.assertEquals(200, image.getWidth());
    Assert.assertEquals(100, image.getHeight());
    ImageIOUtilities.disposeImage(image);
}
Also used : EnhancedImageReadParam(it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam) RenderedOp(javax.media.jai.RenderedOp) ParameterBlockJAI(javax.media.jai.ParameterBlockJAI) File(java.io.File) ImageLayout(javax.media.jai.ImageLayout) RenderingHints(java.awt.RenderingHints) Test(org.junit.Test) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest)

Example 2 with EnhancedImageReadParam

use of it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam in project imageio-ext by geosolutions-it.

the class ECWTest method manualReadDestination.

@Test
public void manualReadDestination() throws FileNotFoundException, IOException {
    if (!isECWAvailable)
        return;
    final ECWImageReaderSpi spi = new ECWImageReaderSpi();
    final ECWImageReader mReader = new ECWImageReader(spi);
    final String fileName = "sample.ecw";
    final File file = TestData.file(this, fileName);
    final EnhancedImageReadParam param = new EnhancedImageReadParam();
    param.setDestinationRegion(new Rectangle(0, 0, 200, 100));
    final int imageIndex = 0;
    mReader.setInput(file);
    final RenderedImage image = mReader.readAsRenderedImage(imageIndex, param);
    if (TestData.isInteractiveTest())
        ImageIOUtilities.visualize(image, fileName);
    Assert.assertEquals(200, image.getWidth());
    Assert.assertEquals(100, image.getHeight());
    mReader.dispose();
}
Also used : EnhancedImageReadParam(it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam) Rectangle(java.awt.Rectangle) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) Test(org.junit.Test) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest)

Example 3 with EnhancedImageReadParam

use of it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam in project imageio-ext by geosolutions-it.

the class GDALImageReader method read.

/**
 * Read the raster and returns a <code>BufferedImage</code>
 *
 * @param imageIndex
 *                the index of the image to be retrieved.
 * @param param
 *                an <code>ImageReadParam</code> used to control the
 *                reading process, or <code>null</code>. Actually,
 *                setting a destinationType allows to specify the number of
 *                bands in the destination image.
 *
 * @return the desired portion of the image as a <code>BufferedImage</code>
 * @throws IllegalArgumentException
 *                 if <code>param</code> contains an invalid specification
 *                 of a source and/or destination band subset or of a
 *                 destination image.
 * @throws IOException
 *                 if an error occurs when acquiring access to the
 *                 underlying datasource
 */
public BufferedImage read(final int imageIndex, final ImageReadParam param) throws IOException {
    // //
    // 
    // Retrieving the requested dataset
    // 
    // //
    final GDALCommonIIOImageMetadata item = getDatasetMetadata(imageIndex);
    int width = item.getWidth();
    int height = item.getHeight();
    final Dataset originalDataset = datasetsMap.get(item.getDatasetName());
    if (originalDataset == null)
        throw new IOException("Error while acquiring the input dataset " + item.getDatasetName());
    Dataset dataset = originalDataset;
    Dataset warpedDataset = null;
    try {
        if (param instanceof GDALImageReadParam) {
            GDALImageReadParam gdalImageReadParam = (GDALImageReadParam) param;
            // Check for source != destination
            String sourceWkt = dataset.GetProjection();
            String destinationWkt = gdalImageReadParam.getDestinationWkt();
            SpatialReference sourceReference = new SpatialReference(sourceWkt);
            SpatialReference destinationReference = new SpatialReference(destinationWkt);
            // lets warp the image using GDAL.
            if (sourceReference.IsSame(destinationReference) == 0) {
                dataset = gdal.AutoCreateWarpedVRT(dataset, dataset.GetProjection(), destinationWkt, gdalImageReadParam.getResampleAlgorithm().getGDALResampleAlgorithm(), gdalImageReadParam.getMaxError());
                warpedDataset = dataset;
                // width and height may have changed from original metadata
                width = warpedDataset.getRasterXSize();
                height = warpedDataset.getRasterYSize();
            }
        }
        final SampleModel itemSampleModel = item.getSampleModel();
        int itemNBands = itemSampleModel.getNumBands();
        int nDestBands;
        BufferedImage bi = null;
        final ImageReadParam imageReadParam;
        if (param == null)
            imageReadParam = getDefaultReadParam();
        else
            imageReadParam = param;
        // //
        // 
        // First, check for a specified ImageTypeSpecifier
        // 
        // //
        ImageTypeSpecifier imageType = imageReadParam.getDestinationType();
        SampleModel destSampleModel = null;
        if (imageType != null) {
            destSampleModel = imageType.getSampleModel();
            nDestBands = destSampleModel.getNumBands();
        } else {
            bi = imageReadParam.getDestination();
            if (bi != null)
                nDestBands = bi.getSampleModel().getNumBands();
            else
                nDestBands = itemNBands;
        }
        // //
        // 
        // Second, bands settings check
        // 
        // //
        checkReadParamBandSettings(imageReadParam, itemNBands, nDestBands);
        int[] srcBands = imageReadParam.getSourceBands();
        // int[] destBands = imageReadParam.getDestinationBands();
        // 
        // //
        // 
        // Third, destination image check
        // 
        // //
        // if (bi != null && imageType == null) {
        // if ((srcBands == null) && (destBands == null)) {
        // SampleModel biSampleModel = bi.getSampleModel();
        // if (!bi.getColorModel().equals(item.getColorModel())
        // || biSampleModel.getDataType() != itemSampleModel
        // .getDataType())
        // throw new IllegalArgumentException(
        // "Provided destination image does not have a valid ColorModel or
        // SampleModel");
        // }
        // }
        // //
        // 
        // Computing regions of interest
        // 
        // //
        Rectangle srcRegion = new Rectangle(0, 0, 0, 0);
        Rectangle destRegion = new Rectangle(0, 0, 0, 0);
        computeRegions(imageReadParam, width, height, bi, srcRegion, destRegion);
        if (imageReadParam != null) {
            if (imageReadParam instanceof EnhancedImageReadParam) {
                final EnhancedImageReadParam eparam = (EnhancedImageReadParam) imageReadParam;
                final Rectangle dstRegion = eparam.getDestinationRegion();
                if (dstRegion != null) {
                    destRegion.height = dstRegion.height;
                    destRegion.width = dstRegion.width;
                }
            }
        }
        if (LOGGER.isLoggable(Level.FINE)) {
            LOGGER.fine("Source Region = " + srcRegion.toString());
            LOGGER.fine("Destination Region = " + destRegion.toString());
        }
        // 
        if (bi == null) {
            // //
            // 
            // No destination image has been specified.
            // Creating a new BufferedImage
            // 
            // //
            ColorModel cm;
            if (imageType == null) {
                cm = item.getColorModel();
                bi = new BufferedImage(cm, (WritableRaster) readDatasetRaster(item.getSampleModel(), dataset, srcRegion, destRegion, srcBands), false, null);
            } else {
                cm = imageType.getColorModel();
                bi = new BufferedImage(cm, (WritableRaster) readDatasetRaster(destSampleModel, dataset, srcRegion, destRegion, srcBands), false, null);
            }
        } else {
            // //
            // 
            // the destination image has been specified.
            // 
            // //
            // Rectangle destSize = (Rectangle) destRegion.clone();
            // destSize.setLocation(0, 0);
            Raster readRaster = readDatasetRaster(item.getSampleModel(), dataset, srcRegion, destRegion, srcBands);
            WritableRaster raster = bi.getRaster().createWritableChild(0, 0, bi.getWidth(), bi.getHeight(), 0, 0, null);
            // TODO: Work directly on a Databuffer avoiding setRect?
            raster.setRect(destRegion.x, destRegion.y, readRaster);
        // Raster readRaster = readDatasetRaster(item, srcRegion,
        // destRegion,
        // srcBands);
        // WritableRaster raster = bi.getRaster().createWritableChild(
        // destRegion.x, destRegion.y, destRegion.width,
        // destRegion.height, destRegion.x, destRegion.y, null);
        // //TODO: Work directly on a Databuffer avoiding setRect?
        // raster.setRect(readRaster);
        }
        return bi;
    } finally {
        if (warpedDataset != null) {
            GDALUtilities.closeDataSet(warpedDataset);
        }
    }
}
Also used : Dataset(org.gdal.gdal.Dataset) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) Rectangle(java.awt.Rectangle) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) EnhancedImageReadParam(it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam) EnhancedImageReadParam(it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam) ImageReadParam(javax.imageio.ImageReadParam) SampleModel(java.awt.image.SampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) SpatialReference(org.gdal.osr.SpatialReference)

Aggregations

EnhancedImageReadParam (it.geosolutions.imageio.imageioimpl.EnhancedImageReadParam)3 AbstractGDALTest (it.geosolutions.imageio.gdalframework.AbstractGDALTest)2 Rectangle (java.awt.Rectangle)2 File (java.io.File)2 Test (org.junit.Test)2 RenderingHints (java.awt.RenderingHints)1 BandedSampleModel (java.awt.image.BandedSampleModel)1 BufferedImage (java.awt.image.BufferedImage)1 ColorModel (java.awt.image.ColorModel)1 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)1 Raster (java.awt.image.Raster)1 RenderedImage (java.awt.image.RenderedImage)1 SampleModel (java.awt.image.SampleModel)1 WritableRaster (java.awt.image.WritableRaster)1 IOException (java.io.IOException)1 ImageReadParam (javax.imageio.ImageReadParam)1 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)1 ImageLayout (javax.media.jai.ImageLayout)1 ParameterBlockJAI (javax.media.jai.ParameterBlockJAI)1 RenderedOp (javax.media.jai.RenderedOp)1