Search in sources :

Example 1 with EXIFMetadata

use of it.geosolutions.imageio.plugins.exif.EXIFMetadata in project imageio-ext by geosolutions-it.

the class TurboJpegImageWriter method write.

@Override
public void write(IIOMetadata metadata, IIOImage image, ImageWriteParam writeParam) throws IOException {
    // Getting image properties
    RenderedImage srcImage = image.getRenderedImage();
    srcImage = refineImage(srcImage);
    final ComponentSampleModel sm = (ComponentSampleModel) srcImage.getSampleModel();
    int[] bandOffsets = sm.getBandOffsets();
    // Getting image Write params
    TurboJpegImageWriteParam param = (TurboJpegImageWriteParam) writeParam;
    // use default as needed
    if (param == null) {
        param = (TurboJpegImageWriteParam) getDefaultWriteParam();
    }
    final EXIFMetadata exif = param.getExif();
    int componentSampling = param.getComponentSubsampling();
    final int quality = (int) (param.getCompressionQuality() * 100);
    int pf = TJ.PF_RGB;
    if (bandOffsets.length == 3) {
        if (componentSampling == -1) {
            componentSampling = TurboJpegImageWriteParam.DEFAULT_RGB_COMPONENT_SUBSAMPLING;
        }
        if ((bandOffsets[0] == 2) && (bandOffsets[2] == 0)) {
            pf = TJ.PF_BGR;
        }
    } else if (bandOffsets.length == 1 && componentSampling == -1) {
        pf = TJ.PF_GRAY;
        componentSampling = TJ.SAMP_GRAY;
    } else {
        throw new IllegalArgumentException("TurboJPEG won't work with this type of sampleModel");
    }
    if (componentSampling < 0) {
        throw new IOException("Subsampling level not set");
    }
    final int pixelsize = sm.getPixelStride();
    final int width = srcImage.getWidth();
    final int height = srcImage.getHeight();
    final int pitch = pixelsize * width;
    TJCompressor compressor = null;
    try {
        // final long jsize = TurboJpegUtilities.bufSize(width, height);
        Rectangle rect = new Rectangle(srcImage.getMinX(), srcImage.getMinY(), srcImage.getWidth(), srcImage.getHeight());
        Raster data = srcImage.getData(rect);
        final byte[] inputImageData = ((DataBufferByte) data.getDataBuffer()).getData();
        final byte[] outputImageData;
        try {
            compressor = new TJCompressor();
            compressor.setSourceImage(inputImageData, width, pitch, height, pf);
            compressor.setJPEGQuality(quality);
            compressor.setSubsamp(componentSampling);
            outputImageData = compressor.compress(TJ.FLAG_FASTDCT);
        } catch (Exception ex) {
            throw new IOException("Error in turbojpeg comressor: " + ex.getMessage(), ex);
        }
        final int imageDataSize = compressor.getCompressedSize();
        if (exif != null) {
            EXIFUtilities.insertEXIFintoStream(((ImageOutputStreamAdapter) outputStream).getTarget(), outputImageData, imageDataSize, exif);
        } else {
            outputStream.write(outputImageData, 0, imageDataSize);
        }
    } finally {
        if (compressor != null) {
            try {
                compressor.close();
            } catch (Exception t) {
                LOGGER.log(Level.SEVERE, t.getLocalizedMessage(), t);
            }
        }
    }
}
Also used : Raster(java.awt.image.Raster) Rectangle(java.awt.Rectangle) TJCompressor(org.libjpegturbo.turbojpeg.TJCompressor) ComponentSampleModel(java.awt.image.ComponentSampleModel) IOException(java.io.IOException) DataBufferByte(java.awt.image.DataBufferByte) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) EXIFMetadata(it.geosolutions.imageio.plugins.exif.EXIFMetadata) RenderedImage(java.awt.image.RenderedImage)

Example 2 with EXIFMetadata

use of it.geosolutions.imageio.plugins.exif.EXIFMetadata in project imageio-ext by geosolutions-it.

the class JPEGWriterTest method testExifReplace.

@Test
@Ignore
public void testExifReplace() throws IOException {
    EXIFMetadata exif = initExif();
    FileImageInputStreamExt inStream = new FileImageInputStreamExtImpl(new File("/media/bigdisk/data/turbojpeg/lastExif.jpeg"));
    EXIFUtilities.replaceEXIFs(inStream, exif);
}
Also used : EXIFMetadata(it.geosolutions.imageio.plugins.exif.EXIFMetadata) FileImageInputStreamExt(it.geosolutions.imageio.stream.input.FileImageInputStreamExt) FileImageInputStreamExtImpl(it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with EXIFMetadata

use of it.geosolutions.imageio.plugins.exif.EXIFMetadata in project imageio-ext by geosolutions-it.

the class JPEGWriterTest method getDefaultInstance.

public static EXIFMetadata getDefaultInstance() {
    List<TIFFTagWrapper> baselineTiffTags = new ArrayList<TIFFTagWrapper>(2);
    List<TIFFTagWrapper> exifTags = new ArrayList<TIFFTagWrapper>(1);
    // Make sure to set them in proper order
    TIFFTagWrapper copyrightTag = EXIFUtilities.createTag(EXIFTags.COPYRIGHT);
    TIFFTagWrapper exifPointerTag = EXIFUtilities.createTag(EXIFTags.EXIF_IFD_POINTER);
    baselineTiffTags.add(copyrightTag);
    baselineTiffTags.add(exifPointerTag);
    TIFFTagWrapper userCommentTag = EXIFUtilities.createTag(EXIFTags.USER_COMMENT);
    exifTags.add(userCommentTag);
    EXIFMetadata exif = new EXIFMetadata(baselineTiffTags, exifTags);
    return exif;
}
Also used : EXIFMetadata(it.geosolutions.imageio.plugins.exif.EXIFMetadata) ArrayList(java.util.ArrayList) TIFFTagWrapper(it.geosolutions.imageio.plugins.exif.TIFFTagWrapper)

Example 4 with EXIFMetadata

use of it.geosolutions.imageio.plugins.exif.EXIFMetadata in project imageio-ext by geosolutions-it.

the class JPEGWriterTest method initExif.

/**
 * @return
 */
protected EXIFMetadata initExif() {
    EXIFMetadata exif = getDefaultInstance();
    exif.setTag(EXIFTags.USER_COMMENT, "Sample User Comment 2".getBytes(), Type.EXIF);
    exif.setTag(EXIFTags.COPYRIGHT, "Copyright 2011 DigitalGlobe".getBytes(), Type.BASELINE);
    return exif;
}
Also used : EXIFMetadata(it.geosolutions.imageio.plugins.exif.EXIFMetadata)

Aggregations

EXIFMetadata (it.geosolutions.imageio.plugins.exif.EXIFMetadata)4 TIFFTagWrapper (it.geosolutions.imageio.plugins.exif.TIFFTagWrapper)1 FileImageInputStreamExt (it.geosolutions.imageio.stream.input.FileImageInputStreamExt)1 FileImageInputStreamExtImpl (it.geosolutions.imageio.stream.input.FileImageInputStreamExtImpl)1 Rectangle (java.awt.Rectangle)1 ComponentSampleModel (java.awt.image.ComponentSampleModel)1 DataBufferByte (java.awt.image.DataBufferByte)1 Raster (java.awt.image.Raster)1 RenderedImage (java.awt.image.RenderedImage)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 TJCompressor (org.libjpegturbo.turbojpeg.TJCompressor)1