use of it.geosolutions.imageio.plugins.tiff.TIFFField 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 it.geosolutions.imageio.plugins.tiff.TIFFField in project imageio-ext by geosolutions-it.
the class TIFFFaxCompressor method setMetadata.
/**
* Sets the value of the <code>metadata</code> field.
*
* <p> The implementation in this class also sets local options
* from the FILL_ORDER field if it exists.</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;
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
inverseFill = (f != null && f.getAsInt(0) == 2);
}
}
use of it.geosolutions.imageio.plugins.tiff.TIFFField in project imageio-ext by geosolutions-it.
the class TIFFFieldNode method initialize.
private synchronized void initialize() {
if (isInitialized == Boolean.TRUE)
return;
if (isIFD) {
TIFFDirectory dir = (TIFFDirectory) field.getData();
TIFFField[] fields = dir.getTIFFFields();
if (fields != null) {
TIFFTagSet[] tagSets = dir.getTagSets();
List tagSetList = Arrays.asList(tagSets);
int numFields = fields.length;
for (int i = 0; i < numFields; i++) {
TIFFField f = fields[i];
int tagNumber = f.getTagNumber();
TIFFTag tag = TIFFIFD.getTag(tagNumber, tagSetList);
Node node = f.getAsNativeNode();
if (node != null) {
appendChild(node);
}
}
}
} else {
IIOMetadataNode child;
int count = field.getCount();
if (field.getType() == TIFFTag.TIFF_UNDEFINED) {
child = new IIOMetadataNode("TIFFUndefined");
byte[] data = field.getAsBytes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count; i++) {
sb.append(Integer.toString(data[i] & 0xff));
if (i < count - 1) {
sb.append(",");
}
}
child.setAttribute("value", sb.toString());
} else {
child = new IIOMetadataNode("TIFF" + field.getTypeName(field.getType()) + "s");
TIFFTag tag = field.getTag();
String tName = tag.getName();
boolean proceed = true;
if (EXCLUDE_ATTRIBUTES && tName != null && ATTRIBUTES_EXCLUSION_SET.contains(tName)) {
proceed = false;
}
for (int i = 0; i < count && proceed; i++) {
IIOMetadataNode cchild = new IIOMetadataNode("TIFF" + field.getTypeName(field.getType()));
cchild.setAttribute("value", field.getValueAsString(i));
if (tag.hasValueNames() && field.isIntegral()) {
int value = field.getAsInt(i);
String name = tag.getValueName(value);
if (name != null) {
cchild.setAttribute("description", name);
}
}
child.appendChild(cchild);
}
}
appendChild(child);
}
isInitialized = Boolean.TRUE;
}
use of it.geosolutions.imageio.plugins.tiff.TIFFField in project imageio-ext by geosolutions-it.
the class TIFFImageMetadata method mergeNativeTree.
private void mergeNativeTree(Node root) throws IIOInvalidTreeException {
Node node = root;
if (!node.getNodeName().equals(nativeMetadataFormatName)) {
fatal(node, "Root must be " + nativeMetadataFormatName);
}
node = node.getFirstChild();
if (node == null || !node.getNodeName().equals("TIFFIFD")) {
fatal(root, "Root must have \"TIFFIFD\" child");
}
TIFFIFD ifd = parseIFD(node);
List rootIFDTagSets = rootIFD.getTagSetList();
Iterator tagSetIter = ifd.getTagSetList().iterator();
while (tagSetIter.hasNext()) {
Object o = tagSetIter.next();
if (o instanceof TIFFTagSet && !rootIFDTagSets.contains(o)) {
rootIFD.addTagSet((TIFFTagSet) o);
}
}
Iterator ifdIter = ifd.iterator();
while (ifdIter.hasNext()) {
TIFFField field = (TIFFField) ifdIter.next();
rootIFD.addTIFFField(field);
}
}
use of it.geosolutions.imageio.plugins.tiff.TIFFField in project imageio-ext by geosolutions-it.
the class TIFFImageMetadata method getStandardTextNode.
public IIOMetadataNode getStandardTextNode() {
IIOMetadataNode text_node = null;
// scratch node
IIOMetadataNode node = null;
TIFFField f;
int[] textFieldTagNumbers = new int[] { BaselineTIFFTagSet.TAG_DOCUMENT_NAME, BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION, BaselineTIFFTagSet.TAG_MAKE, BaselineTIFFTagSet.TAG_MODEL, BaselineTIFFTagSet.TAG_PAGE_NAME, BaselineTIFFTagSet.TAG_SOFTWARE, BaselineTIFFTagSet.TAG_ARTIST, BaselineTIFFTagSet.TAG_HOST_COMPUTER, BaselineTIFFTagSet.TAG_INK_NAMES, BaselineTIFFTagSet.TAG_COPYRIGHT };
for (int i = 0; i < textFieldTagNumbers.length; i++) {
f = getTIFFField(textFieldTagNumbers[i]);
if (f != null) {
String value = f.getAsString(0);
if (text_node == null) {
text_node = new IIOMetadataNode("Text");
}
node = new IIOMetadataNode("TextEntry");
node.setAttribute("keyword", f.getTag().getName());
node.setAttribute("value", value);
text_node.appendChild(node);
}
}
return text_node;
}
Aggregations