Search in sources :

Example 1 with TIFFImageReadParam

use of it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam in project imageio-ext by geosolutions-it.

the class TIFFRenderedImage method cloneImageReadParam.

/**
 * Creates a copy of <code>param</code>. The source subsampling and
 * and bands settings and the destination bands and offset settings
 * are copied. If <code>param</code> is a <code>TIFFImageReadParam</code>
 * then the <code>TIFFDecompressor</code> and
 * <code>TIFFColorConverter</code> settings are also copied; otherwise
 * they are explicitly set to <code>null</code>.
 *
 * @param param the parameters to be copied.
 * @param copyTagSets whether the <code>TIFFTagSet</code> settings
 * should be copied if set.
 * @return copied parameters.
 */
private ImageReadParam cloneImageReadParam(ImageReadParam param, boolean copyTagSets) {
    // Create a new TIFFImageReadParam.
    TIFFImageReadParam newParam = new TIFFImageReadParam();
    // Copy the basic settings.
    newParam.setSourceSubsampling(param.getSourceXSubsampling(), param.getSourceYSubsampling(), param.getSubsamplingXOffset(), param.getSubsamplingYOffset());
    newParam.setSourceBands(param.getSourceBands());
    newParam.setDestinationBands(param.getDestinationBands());
    newParam.setDestinationOffset(param.getDestinationOffset());
    // Set the decompressor and color converter.
    if (param instanceof TIFFImageReadParam) {
        // Copy the settings from the input parameter.
        TIFFImageReadParam tparam = (TIFFImageReadParam) param;
        newParam.setTIFFDecompressor(tparam.getTIFFDecompressor());
        newParam.setColorConverter(tparam.getColorConverter());
        if (copyTagSets) {
            List tagSets = tparam.getAllowedTagSets();
            if (tagSets != null) {
                Iterator tagSetIter = tagSets.iterator();
                if (tagSetIter != null) {
                    while (tagSetIter.hasNext()) {
                        TIFFTagSet tagSet = (TIFFTagSet) tagSetIter.next();
                        newParam.addAllowedTagSet(tagSet);
                    }
                }
            }
        }
    } else {
        // Set the decompressor and color converter to null.
        newParam.setTIFFDecompressor(null);
        newParam.setColorConverter(null);
    }
    return newParam;
}
Also used : Iterator(java.util.Iterator) List(java.util.List) TIFFTagSet(it.geosolutions.imageio.plugins.tiff.TIFFTagSet) TIFFImageReadParam(it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam)

Example 2 with TIFFImageReadParam

use of it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam in project imageio-ext by geosolutions-it.

the class TIFFImageReader method read.

public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    prepareRead(imageIndex, param);
    // prepare for reading
    this.theImage = getDestination(param, getImageTypes(imageIndex), width, height, noData);
    srcXSubsampling = imageReadParam.getSourceXSubsampling();
    srcYSubsampling = imageReadParam.getSourceYSubsampling();
    Point p = imageReadParam.getDestinationOffset();
    dstXOffset = p.x;
    dstYOffset = p.y;
    // This could probably be made more efficient...
    Rectangle srcRegion = new Rectangle(0, 0, 0, 0);
    Rectangle destRegion = new Rectangle(0, 0, 0, 0);
    computeRegions(imageReadParam, width, height, theImage, srcRegion, destRegion);
    // Initial source pixel, taking source region and source
    // subsamplimg offsets into account
    sourceXOffset = srcRegion.x;
    sourceYOffset = srcRegion.y;
    pixelsToRead = destRegion.width * destRegion.height;
    pixelsRead = 0;
    processImageStarted(imageIndex);
    processImageProgress(0.0f);
    tilesAcross = (width + tileOrStripWidth - 1) / tileOrStripWidth;
    tilesDown = (height + tileOrStripHeight - 1) / tileOrStripHeight;
    // Attempt to get decompressor and color converted from the read param
    TIFFColorConverter colorConverter = null;
    if (imageReadParam instanceof TIFFImageReadParam) {
        TIFFImageReadParam tparam = (TIFFImageReadParam) imageReadParam;
        this.decompressor = tparam.getTIFFDecompressor();
        colorConverter = tparam.getColorConverter();
    }
    // If we didn't find one, use a standard decompressor
    if (this.decompressor == null) {
        if (compression == BaselineTIFFTagSet.COMPRESSION_NONE) {
            // Get the fillOrder field.
            TIFFField fillOrderField = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
            // Set the decompressor based on the fill order.
            if (fillOrderField != null && fillOrderField.getAsInt(0) == 2) {
                this.decompressor = new TIFFLSBDecompressor();
            } else {
                this.decompressor = new TIFFNullDecompressor();
            }
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_CCITT_T_6) {
            // Try to create the codecLib decompressor.
            if (PackageUtil.isCodecLibAvailable()) {
                try {
                    this.decompressor = new TIFFCodecLibFaxDecompressor(compression);
                    if (DEBUG) {
                        System.out.println("Using codecLib T.6 decompressor");
                    }
                } catch (RuntimeException re) {
                    if (DEBUG) {
                        System.out.println(re);
                    }
                }
            }
            // Fall back to the Java decompressor.
            if (this.decompressor == null) {
                if (DEBUG) {
                    System.out.println("Using Java T.6 decompressor");
                }
                this.decompressor = new TIFFFaxDecompressor();
            }
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_CCITT_T_4) {
            if (PackageUtil.isCodecLibAvailable()) {
                // Try to create the codecLib decompressor.
                try {
                    this.decompressor = new TIFFCodecLibFaxDecompressor(compression);
                    if (DEBUG) {
                        System.out.println("Using codecLib T.4 decompressor");
                    }
                } catch (RuntimeException re) {
                    if (DEBUG) {
                        System.out.println(re);
                    }
                }
            }
            // Fall back to the Java decompressor.
            if (this.decompressor == null) {
                if (DEBUG) {
                    System.out.println("Using Java T.4 decompressor");
                }
                this.decompressor = new TIFFFaxDecompressor();
            }
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_CCITT_RLE) {
            this.decompressor = new TIFFFaxDecompressor();
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_PACKBITS) {
            if (DEBUG) {
                System.out.println("Using TIFFPackBitsDecompressor");
            }
            this.decompressor = new TIFFPackBitsDecompressor();
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_LZW) {
            if (DEBUG) {
                System.out.println("Using TIFFLZWDecompressor");
            }
            TIFFField predictorField = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_PREDICTOR);
            int predictor = ((predictorField == null) ? BaselineTIFFTagSet.PREDICTOR_NONE : predictorField.getAsInt(0));
            this.decompressor = new TIFFLZWDecompressor(predictor);
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_JPEG) {
            this.decompressor = new TIFFJPEGDecompressor();
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_ZLIB || compression == BaselineTIFFTagSet.COMPRESSION_DEFLATE) {
            TIFFField predictorField = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_PREDICTOR);
            int predictor = ((predictorField == null) ? BaselineTIFFTagSet.PREDICTOR_NONE : predictorField.getAsInt(0));
            this.decompressor = new TIFFDeflateDecompressor(predictor);
        } else if (compression == BaselineTIFFTagSet.COMPRESSION_OLD_JPEG) {
            TIFFField JPEGProcField = imageMetadata.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_PROC);
            if (JPEGProcField == null) {
                processWarningOccurred("JPEGProc field missing; assuming baseline sequential JPEG process.");
            } else if (JPEGProcField.getAsInt(0) != BaselineTIFFTagSet.JPEG_PROC_BASELINE) {
                throw new IIOException("Old-style JPEG supported for baseline sequential JPEG process only!");
            }
            this.decompressor = new TIFFOldJPEGDecompressor();
        // throw new IIOException("Old-style JPEG not supported!");
        } else {
            throw new IIOException("Unsupported compression type (tag number = " + compression + ")!");
        }
        if (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_Y_CB_CR && compression != BaselineTIFFTagSet.COMPRESSION_JPEG && compression != BaselineTIFFTagSet.COMPRESSION_OLD_JPEG) {
            boolean convertYCbCrToRGB = theImage.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_RGB;
            TIFFDecompressor wrappedDecompressor = this.decompressor instanceof TIFFNullDecompressor ? null : this.decompressor;
            this.decompressor = new TIFFYCbCrDecompressor(wrappedDecompressor, convertYCbCrToRGB);
        }
    }
    if (DEBUG) {
        System.out.println("\nDecompressor class = " + decompressor.getClass().getName() + "\n");
    }
    if (colorConverter == null) {
        if (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_CIELAB && theImage.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_RGB) {
            colorConverter = new TIFFCIELabColorConverter();
        } else if (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_Y_CB_CR && !(this.decompressor instanceof TIFFYCbCrDecompressor) && compression != BaselineTIFFTagSet.COMPRESSION_JPEG && compression != BaselineTIFFTagSet.COMPRESSION_OLD_JPEG) {
            colorConverter = new TIFFYCbCrColorConverter(imageMetadata);
        }
    }
    decompressor.setReader(this);
    decompressor.setMetadata(imageMetadata);
    decompressor.setImage(theImage);
    decompressor.setPhotometricInterpretation(photometricInterpretation);
    decompressor.setCompression(compression);
    decompressor.setSamplesPerPixel(samplesPerPixel);
    decompressor.setBitsPerSample(bitsPerSample);
    decompressor.setSampleFormat(sampleFormat);
    decompressor.setExtraSamples(extraSamples);
    decompressor.setColorMap(colorMap);
    decompressor.setColorConverter(colorConverter);
    decompressor.setSourceXOffset(sourceXOffset);
    decompressor.setSourceYOffset(sourceYOffset);
    decompressor.setSubsampleX(srcXSubsampling);
    decompressor.setSubsampleY(srcYSubsampling);
    decompressor.setDstXOffset(dstXOffset);
    decompressor.setDstYOffset(dstYOffset);
    decompressor.setSourceBands(sourceBands);
    decompressor.setDestinationBands(destinationBands);
    // Compute bounds on the tile indices for this source region.
    int minTileX = TIFFImageWriter.XToTileX(srcRegion.x, 0, tileOrStripWidth);
    int minTileY = TIFFImageWriter.YToTileY(srcRegion.y, 0, tileOrStripHeight);
    int maxTileX = TIFFImageWriter.XToTileX(srcRegion.x + srcRegion.width - 1, 0, tileOrStripWidth);
    int maxTileY = TIFFImageWriter.YToTileY(srcRegion.y + srcRegion.height - 1, 0, tileOrStripHeight);
    boolean isAbortRequested = false;
    if (planarConfiguration == BaselineTIFFTagSet.PLANAR_CONFIGURATION_PLANAR) {
        decompressor.setPlanar(true);
        int[] sb = new int[1];
        int[] db = new int[1];
        for (int tj = minTileY; tj <= maxTileY; tj++) {
            for (int ti = minTileX; ti <= maxTileX; ti++) {
                for (int band = 0; band < numBands; band++) {
                    sb[0] = sourceBands[band];
                    decompressor.setSourceBands(sb);
                    db[0] = destinationBands[band];
                    decompressor.setDestinationBands(db);
                    // doing any actual decoding.
                    if (abortRequested()) {
                        isAbortRequested = true;
                        break;
                    }
                    decodeTile(ti, tj, band);
                }
                if (isAbortRequested)
                    break;
                reportProgress();
            }
            if (isAbortRequested)
                break;
        }
    } else {
        for (int tj = minTileY; tj <= maxTileY; tj++) {
            for (int ti = minTileX; ti <= maxTileX; ti++) {
                // doing any actual decoding.
                if (abortRequested()) {
                    isAbortRequested = true;
                    break;
                }
                decodeTile(ti, tj, -1);
                reportProgress();
            }
            if (isAbortRequested)
                break;
        }
    }
    if (isAbortRequested) {
        processReadAborted();
    } else {
        processImageComplete();
    }
    return theImage;
}
Also used : TIFFColorConverter(it.geosolutions.imageio.plugins.tiff.TIFFColorConverter) TIFFImageReadParam(it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam) TIFFDecompressor(it.geosolutions.imageio.plugins.tiff.TIFFDecompressor) TIFFField(it.geosolutions.imageio.plugins.tiff.TIFFField)

Example 3 with TIFFImageReadParam

use of it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam in project imageio-ext by geosolutions-it.

the class TIFFMetadataTest method testImageRead.

/**
 * This test class is used for testing the capability of the TIFFImageReader
 * class to read EXIF metadata. TIFFImageReader class presented a bug when
 * the EXIF IFD pointer tag type was not LONG (type = 4) but was the new
 * IFD_POINTER type (type = 13) defined in the Technical Note 1 of the TIFF
 * Specification Supplement documentation. This variation provoked a
 * ClassCastException when the reader tried to cast the EXIF IFD pointer
 * data to the TIFFIFD class. This bug has been fixed by adding the
 * possibility to contain the IFD_POINTER type to the EXIF IFD pointer tag.
 * The testImageRead() method reads an image with the new TIFF tag type and
 * checks if the EXIF tags has been read by the TIFFImageReader.
 *
 * @throws IOException
 */
@Test
public void testImageRead() throws IOException {
    // Selection of the input file from the TestData directory
    File inputFile = TestData.file(this, "test_IFD.tif");
    // Instantiation of the read-params
    final TIFFImageReadParam param = new TIFFImageReadParam();
    // Instantiation of the file-reader
    TIFFImageReader reader = (TIFFImageReader) new TIFFImageReaderSpi().createReaderInstance();
    // Creation of the file input stream associated to the selected file
    FileImageInputStream stream0 = new FileImageInputStream(inputFile);
    try {
        // Setting the inputstream to the reader
        reader.setInput(stream0);
        // Reading of the image
        RenderedImage img = reader.read(0, param);
        // Reading of the associated metadata
        TIFFImageMetadata metadata = (TIFFImageMetadata) reader.getImageMetadata(0);
        // Check if the Exif pointer metadata is present
        int tagPointer = 34665;
        boolean fieldPointer = metadata.getRootIFD().containsTIFFField(tagPointer);
        assertTrue(fieldPointer);
        // Selection of the subIFD associated to the exif pointer
        TIFFIFD subIFD = (TIFFIFD) metadata.getTIFFField(tagPointer).getData();
        // Selection of the tags associated to the EXIF pointer
        int tagNumberExifVersion = 36864;
        int tagNumberDateTime = 36868;
        int tagNumberCompConfig = 37121;
        int tagNumberFlashPix = 40960;
        int tagNumberColor = 40961;
        int tagNumberXpixelRes = 40962;
        int tagNumberYpixelRes = 40963;
        // Test Assertions
        assertTrue(subIFD.containsTIFFField(tagNumberExifVersion));
        assertTrue(subIFD.containsTIFFField(tagNumberDateTime));
        assertTrue(subIFD.containsTIFFField(tagNumberCompConfig));
        assertTrue(subIFD.containsTIFFField(tagNumberFlashPix));
        assertTrue(subIFD.containsTIFFField(tagNumberColor));
        assertTrue(subIFD.containsTIFFField(tagNumberXpixelRes));
        assertTrue(subIFD.containsTIFFField(tagNumberYpixelRes));
    } 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 (stream0 != null) {
            stream0.flush();
            stream0.close();
        }
        if (reader != null) {
            reader.dispose();
        }
    }
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) TIFFImageMetadata(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageMetadata) TIFFIFD(it.geosolutions.imageioimpl.plugins.tiff.TIFFIFD) RenderedImage(java.awt.image.RenderedImage) File(java.io.File) TIFFImageReader(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader) TIFFImageReaderSpi(it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi) IOException(java.io.IOException) TIFFImageReadParam(it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam) Test(org.junit.Test)

Aggregations

TIFFImageReadParam (it.geosolutions.imageio.plugins.tiff.TIFFImageReadParam)3 TIFFColorConverter (it.geosolutions.imageio.plugins.tiff.TIFFColorConverter)1 TIFFDecompressor (it.geosolutions.imageio.plugins.tiff.TIFFDecompressor)1 TIFFField (it.geosolutions.imageio.plugins.tiff.TIFFField)1 TIFFTagSet (it.geosolutions.imageio.plugins.tiff.TIFFTagSet)1 TIFFIFD (it.geosolutions.imageioimpl.plugins.tiff.TIFFIFD)1 TIFFImageMetadata (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageMetadata)1 TIFFImageReader (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReader)1 TIFFImageReaderSpi (it.geosolutions.imageioimpl.plugins.tiff.TIFFImageReaderSpi)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 FileImageInputStream (javax.imageio.stream.FileImageInputStream)1 Test (org.junit.Test)1