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);
}
}
}
}
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);
}
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;
}
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;
}
Aggregations