use of javax.imageio.stream.MemoryCacheImageInputStream in project imageio-ext by geosolutions-it.
the class TIFFJPEGCompressor method setMetadata.
/**
* Sets the value of the <code>metadata</code> field.
*
* <p>The implementation in this class also adds the TIFF fields
* JPEGTables, YCbCrSubSampling, YCbCrPositioning, and
* ReferenceBlackWhite superseding any prior settings of those
* fields.</p>
*
* @param metadata the <code>IIOMetadata</code> object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata) metadata;
TIFFIFD rootIFD = tim.getRootIFD();
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_SAMPLES_PER_PIXEL);
int numBands = f.getAsInt(0);
if (numBands == 1) {
// Remove YCbCr fields not relevant for grayscale.
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING);
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_POSITIONING);
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
} else {
// numBands == 3
// Replace YCbCr fields.
// YCbCrSubSampling
TIFFField YCbCrSubSamplingField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING), TIFFTag.TIFF_SHORT, 2, new char[] { CHROMA_SUBSAMPLING, CHROMA_SUBSAMPLING });
rootIFD.addTIFFField(YCbCrSubSamplingField);
// YCbCrPositioning
TIFFField YCbCrPositioningField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_CB_CR_POSITIONING), TIFFTag.TIFF_SHORT, 1, new char[] { BaselineTIFFTagSet.Y_CB_CR_POSITIONING_CENTERED });
rootIFD.addTIFFField(YCbCrPositioningField);
// ReferenceBlackWhite
TIFFField referenceBlackWhiteField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE), TIFFTag.TIFF_RATIONAL, 6, new long[][] { // no headroon/footroom
{ 0, 1 }, { 255, 1 }, { 128, 1 }, { 255, 1 }, { 128, 1 }, { 255, 1 } });
rootIFD.addTIFFField(referenceBlackWhiteField);
}
// JPEGTables field is written if and only if one is
// already present in the metadata. If one is present
// and has either zero length or does not represent a
// valid tables-only stream, then a JPEGTables field
// will be written initialized to the standard tables-
// only stream written by the JPEG writer.
// Retrieve the JPEGTables field.
TIFFField JPEGTablesField = tim.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
// Initialize JPEG writer to one supporting abbreviated streams.
if (JPEGTablesField != null) {
// Intialize the JPEG writer to one that supports stream
// metadata, i.e., abbreviated streams, and may or may not
// support image metadata.
initJPEGWriter(true, false);
}
// streams was available.
if (JPEGTablesField != null && JPEGWriter != null) {
if (DEBUG)
System.out.println("Has JPEGTables ...");
// Set the abbreviated stream flag.
this.writeAbbreviatedStream = true;
// Branch based on field value count.
if (JPEGTablesField.getCount() > 0) {
if (DEBUG)
System.out.println("JPEGTables > 0");
// Derive the stream metadata from the field.
// Get the field values.
byte[] tables = JPEGTablesField.getAsBytes();
// Create an input stream for the tables.
ByteArrayInputStream bais = new ByteArrayInputStream(tables);
MemoryCacheImageInputStream iis = new MemoryCacheImageInputStream(bais);
// Read the tables stream using the JPEG reader.
ImageReader jpegReader = getJPEGTablesReader();
jpegReader.setInput(iis);
// Initialize the stream metadata object.
try {
JPEGStreamMetadata = jpegReader.getStreamMetadata();
} catch (Exception e) {
// Fall back to default tables.
JPEGStreamMetadata = null;
} finally {
jpegReader.reset();
}
if (DEBUG)
System.out.println(JPEGStreamMetadata);
}
if (JPEGStreamMetadata == null) {
if (DEBUG)
System.out.println("JPEGTables == 0");
// Derive the field from default stream metadata.
// Get default stream metadata.
JPEGStreamMetadata = JPEGWriter.getDefaultStreamMetadata(JPEGParam);
// Create an output stream for the tables.
ByteArrayOutputStream tableByteStream = new ByteArrayOutputStream();
MemoryCacheImageOutputStream tableStream = new MemoryCacheImageOutputStream(tableByteStream);
// Write a tables-only stream.
JPEGWriter.setOutput(tableStream);
try {
JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
tableStream.flush();
JPEGWriter.endWriteSequence();
// Get the tables-only stream content.
byte[] tables = tableByteStream.toByteArray();
if (DEBUG)
System.out.println("tables.length = " + tables.length);
// Add the JPEGTables field.
JPEGTablesField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_JPEG_TABLES), TIFFTag.TIFF_UNDEFINED, tables.length, tables);
rootIFD.addTIFFField(JPEGTablesField);
} catch (Exception e) {
// Do not write JPEGTables field.
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
this.writeAbbreviatedStream = false;
}
}
} else {
// Do not write JPEGTables field.
// Remove any field present.
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
// Initialize the writer preferring codecLib.
initJPEGWriter(false, false);
}
}
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project imageio-ext by geosolutions-it.
the class URLImageInputStreamSpi method createInputStreamInstance.
/**
* @see javax.imageio.spi.ImageInputStreamSpi#createInputStreamInstance(java.lang.Object,
* boolean, java.io.File)
*/
public ImageInputStream createInputStreamInstance(Object input, boolean useCache, File cacheDir) {
// is it a URL?
if (!(input instanceof URL)) {
if (LOGGER.isLoggable(Level.FINE))
LOGGER.fine("The provided input is not a valid URL.");
return null;
}
try {
// URL that points to a file?
final URL sourceURL = ((URL) input);
final File tempFile = ImageIOUtilities.urlToFile(sourceURL);
if (tempFile.exists() && tempFile.isFile() && tempFile.canRead())
return fileStreamSPI.createInputStreamInstance(tempFile, useCache, cacheDir);
// URL that does NOT points to a file, let's open up a stream
if (useCache)
return new MemoryCacheImageInputStream(sourceURL.openStream());
else
return new FileCacheImageInputStream(sourceURL.openStream(), cacheDir);
} catch (IOException e) {
if (LOGGER.isLoggable(Level.FINE))
LOGGER.log(Level.FINE, e.getLocalizedMessage(), e);
return null;
}
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project TranskribusCore by Transkribus.
the class ImgUtils method readImage.
// static {
// ImageIO.scanForPlugins();
// }
/**
* Reads image in the specified image file. For multiimage tiff files, the first image is read.
*/
public static BufferedImage readImage(byte[] data) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
ImageInputStream iis = new MemoryCacheImageInputStream(bais);
return readImage(iis);
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project OpenOLAT by OpenOLAT.
the class AbstractImageHelper method getImageSize.
private Size getImageSize(File media, String suffix) {
Size result = null;
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageReader reader = iter.next();
try (InputStream mediaStream = new FileInputStream(media);
ImageInputStream stream = new MemoryCacheImageInputStream(mediaStream)) {
reader.setInput(stream);
int readerMinIndex = reader.getMinIndex();
int width = reader.getWidth(readerMinIndex);
int height = reader.getHeight(readerMinIndex);
result = new Size(width, height, 0, 0, false);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix);
}
return result;
}
use of javax.imageio.stream.MemoryCacheImageInputStream in project OpenOLAT by OpenOLAT.
the class AbstractImageHelper method getImageSize.
private Size getImageSize(VFSLeaf media, String suffix) {
Size result = null;
Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
if (iter.hasNext()) {
ImageInputStream stream = null;
InputStream mediaStream = null;
ImageReader reader = iter.next();
try {
mediaStream = media.getInputStream();
if (mediaStream != null) {
stream = new MemoryCacheImageInputStream(mediaStream);
reader.setInput(stream);
int readerMinIndex = reader.getMinIndex();
int width = reader.getWidth(readerMinIndex);
int height = reader.getHeight(readerMinIndex);
result = new Size(width, height, 0, 0, false);
}
} catch (IOException e) {
log.error(e.getMessage());
} finally {
IOUtils.closeQuietly(stream);
IOUtils.closeQuietly(mediaStream);
reader.dispose();
}
} else {
log.error("No reader found for given format: " + suffix);
}
return result;
}
Aggregations