use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class TIFFImageMetadata method getStandardDimensionNode.
public IIOMetadataNode getStandardDimensionNode() {
IIOMetadataNode dimension_node = new IIOMetadataNode("Dimension");
// scratch node
IIOMetadataNode node = null;
TIFFField f;
long[] xres = null;
long[] yres = null;
f = getTIFFField(BaselineTIFFTagSet.TAG_X_RESOLUTION);
if (f != null) {
xres = (long[]) f.getAsRational(0).clone();
}
f = getTIFFField(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
if (f != null) {
yres = (long[]) f.getAsRational(0).clone();
}
if (xres != null && yres != null) {
node = new IIOMetadataNode("PixelAspectRatio");
// Compute (1/xres)/(1/yres)
// (xres_denom/xres_num)/(yres_denom/yres_num) =
// (xres_denom/xres_num)*(yres_num/yres_denom) =
// (xres_denom*yres_num)/(xres_num*yres_denom)
float ratio = (float) ((double) xres[1] * yres[0]) / (xres[0] * yres[1]);
node.setAttribute("value", Float.toString(ratio));
dimension_node.appendChild(node);
}
if (xres != null || yres != null) {
// Get unit field.
f = getTIFFField(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT);
// Set resolution unit.
int resolutionUnit = f != null ? f.getAsInt(0) : BaselineTIFFTagSet.RESOLUTION_UNIT_INCH;
// Have size if either centimeters or inches.
boolean gotPixelSize = resolutionUnit != BaselineTIFFTagSet.RESOLUTION_UNIT_NONE;
// Convert pixels/inch to pixels/centimeter.
if (resolutionUnit == BaselineTIFFTagSet.RESOLUTION_UNIT_INCH) {
// Divide xres by 2.54
if (xres != null) {
xres[0] *= 100;
xres[1] *= 254;
}
// Divide yres by 2.54
if (yres != null) {
yres[0] *= 100;
yres[1] *= 254;
}
}
if (gotPixelSize) {
if (xres != null) {
float horizontalPixelSize = (float) (10.0 * xres[1] / xres[0]);
node = new IIOMetadataNode("HorizontalPixelSize");
node.setAttribute("value", Float.toString(horizontalPixelSize));
dimension_node.appendChild(node);
}
if (yres != null) {
float verticalPixelSize = (float) (10.0 * yres[1] / yres[0]);
node = new IIOMetadataNode("VerticalPixelSize");
node.setAttribute("value", Float.toString(verticalPixelSize));
dimension_node.appendChild(node);
}
}
}
f = getTIFFField(BaselineTIFFTagSet.TAG_RESOLUTION_UNIT);
int resolutionUnit = f != null ? f.getAsInt(0) : BaselineTIFFTagSet.RESOLUTION_UNIT_INCH;
if (resolutionUnit == BaselineTIFFTagSet.RESOLUTION_UNIT_INCH || resolutionUnit == BaselineTIFFTagSet.RESOLUTION_UNIT_CENTIMETER) {
f = getTIFFField(BaselineTIFFTagSet.TAG_X_POSITION);
if (f != null) {
long[] xpos = (long[]) f.getAsRational(0);
float xPosition = (float) xpos[0] / (float) xpos[1];
// Convert to millimeters.
if (resolutionUnit == BaselineTIFFTagSet.RESOLUTION_UNIT_INCH) {
xPosition *= 254F;
} else {
xPosition *= 10F;
}
node = new IIOMetadataNode("HorizontalPosition");
node.setAttribute("value", Float.toString(xPosition));
dimension_node.appendChild(node);
}
f = getTIFFField(BaselineTIFFTagSet.TAG_Y_POSITION);
if (f != null) {
long[] ypos = (long[]) f.getAsRational(0);
float yPosition = (float) ypos[0] / (float) ypos[1];
// Convert to millimeters.
if (resolutionUnit == BaselineTIFFTagSet.RESOLUTION_UNIT_INCH) {
yPosition *= 254F;
} else {
yPosition *= 10F;
}
node = new IIOMetadataNode("VerticalPosition");
node.setAttribute("value", Float.toString(yPosition));
dimension_node.appendChild(node);
}
}
f = getTIFFField(BaselineTIFFTagSet.TAG_ORIENTATION);
if (f != null) {
int o = f.getAsInt(0);
if (o >= 0 && o < orientationNames.length) {
node = new IIOMetadataNode("ImageOrientation");
node.setAttribute("value", orientationNames[o]);
dimension_node.appendChild(node);
}
}
return dimension_node;
}
use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class TIFFImageMetadata method getStandardChromaNode.
public IIOMetadataNode getStandardChromaNode() {
IIOMetadataNode chroma_node = new IIOMetadataNode("Chroma");
// scratch node
IIOMetadataNode node = null;
TIFFField f;
// Set the PhotometricInterpretation and the palette color flag.
int photometricInterpretation = -1;
boolean isPaletteColor = false;
f = getTIFFField(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION);
if (f != null) {
photometricInterpretation = f.getAsInt(0);
isPaletteColor = photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_PALETTE_COLOR;
}
// Determine the number of channels.
int numChannels = -1;
if (isPaletteColor) {
numChannels = 3;
} else {
f = getTIFFField(BaselineTIFFTagSet.TAG_SAMPLES_PER_PIXEL);
if (f != null) {
numChannels = f.getAsInt(0);
} else {
// f == null
f = getTIFFField(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE);
if (f != null) {
numChannels = f.getCount();
}
}
}
if (photometricInterpretation != -1) {
if (photometricInterpretation >= 0 && photometricInterpretation < colorSpaceNames.length) {
node = new IIOMetadataNode("ColorSpaceType");
String csName;
if (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_CMYK && numChannels == 3) {
csName = "CMY";
} else {
csName = colorSpaceNames[photometricInterpretation];
}
node.setAttribute("name", csName);
chroma_node.appendChild(node);
}
node = new IIOMetadataNode("BlackIsZero");
node.setAttribute("value", (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO) ? "FALSE" : "TRUE");
chroma_node.appendChild(node);
}
if (numChannels != -1) {
node = new IIOMetadataNode("NumChannels");
node.setAttribute("value", Integer.toString(numChannels));
chroma_node.appendChild(node);
}
f = getTIFFField(BaselineTIFFTagSet.TAG_COLOR_MAP);
if (f != null) {
// NOTE: The presence of hasAlpha is vestigial: there is
// no way in TIFF to represent an alpha component in a palette
// color image. See bug 5086341.
boolean hasAlpha = false;
node = new IIOMetadataNode("Palette");
int len = f.getCount() / (hasAlpha ? 4 : 3);
for (int i = 0; i < len; i++) {
IIOMetadataNode entry = new IIOMetadataNode("PaletteEntry");
entry.setAttribute("index", Integer.toString(i));
int r = (f.getAsInt(i) * 255) / 65535;
int g = (f.getAsInt(len + i) * 255) / 65535;
int b = (f.getAsInt(2 * len + i) * 255) / 65535;
entry.setAttribute("red", Integer.toString(r));
entry.setAttribute("green", Integer.toString(g));
entry.setAttribute("blue", Integer.toString(b));
if (hasAlpha) {
int alpha = 0;
entry.setAttribute("alpha", Integer.toString(alpha));
}
node.appendChild(entry);
}
chroma_node.appendChild(node);
}
return chroma_node;
}
use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class TIFFImageMetadata method getStandardDataNode.
public IIOMetadataNode getStandardDataNode() {
IIOMetadataNode data_node = new IIOMetadataNode("Data");
// scratch node
IIOMetadataNode node = null;
TIFFField f;
boolean isPaletteColor = false;
f = getTIFFField(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION);
if (f != null) {
isPaletteColor = f.getAsInt(0) == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_PALETTE_COLOR;
}
f = getTIFFField(BaselineTIFFTagSet.TAG_PLANAR_CONFIGURATION);
String planarConfiguration = "PixelInterleaved";
if (f != null && f.getAsInt(0) == BaselineTIFFTagSet.PLANAR_CONFIGURATION_PLANAR) {
planarConfiguration = "PlaneInterleaved";
}
node = new IIOMetadataNode("PlanarConfiguration");
node.setAttribute("value", planarConfiguration);
data_node.appendChild(node);
f = getTIFFField(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION);
if (f != null) {
int photometricInterpretation = f.getAsInt(0);
String sampleFormat = "UnsignedIntegral";
if (photometricInterpretation == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_PALETTE_COLOR) {
sampleFormat = "Index";
} else {
f = getTIFFField(BaselineTIFFTagSet.TAG_SAMPLE_FORMAT);
if (f != null) {
int format = f.getAsInt(0);
if (format == BaselineTIFFTagSet.SAMPLE_FORMAT_SIGNED_INTEGER) {
sampleFormat = "SignedIntegral";
} else if (format == BaselineTIFFTagSet.SAMPLE_FORMAT_UNSIGNED_INTEGER) {
sampleFormat = "UnsignedIntegral";
} else if (format == BaselineTIFFTagSet.SAMPLE_FORMAT_FLOATING_POINT) {
sampleFormat = "Real";
} else {
// don't know
sampleFormat = null;
}
}
}
if (sampleFormat != null) {
node = new IIOMetadataNode("SampleFormat");
node.setAttribute("value", sampleFormat);
data_node.appendChild(node);
}
}
f = getTIFFField(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE);
int[] bitsPerSample = null;
if (f != null) {
bitsPerSample = f.getAsInts();
} else {
f = getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
int compression = f != null ? f.getAsInt(0) : BaselineTIFFTagSet.COMPRESSION_NONE;
if (getTIFFField(EXIFParentTIFFTagSet.TAG_EXIF_IFD_POINTER) != null || compression == BaselineTIFFTagSet.COMPRESSION_JPEG || compression == BaselineTIFFTagSet.COMPRESSION_OLD_JPEG || getTIFFField(BaselineTIFFTagSet.TAG_JPEG_INTERCHANGE_FORMAT) != null) {
f = getTIFFField(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION);
if (f != null && (f.getAsInt(0) == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO || f.getAsInt(0) == BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO)) {
bitsPerSample = new int[] { 8 };
} else {
bitsPerSample = new int[] { 8, 8, 8 };
}
} else {
bitsPerSample = new int[] { 1 };
}
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bitsPerSample.length; i++) {
if (i > 0) {
sb.append(" ");
}
sb.append(Integer.toString(bitsPerSample[i]));
}
node = new IIOMetadataNode("BitsPerSample");
if (isPaletteColor) {
node.setAttribute("value", repeat(sb.toString(), 3));
} else {
node.setAttribute("value", sb.toString());
}
data_node.appendChild(node);
// SampleMSB
f = getTIFFField(BaselineTIFFTagSet.TAG_FILL_ORDER);
int fillOrder = f != null ? f.getAsInt(0) : BaselineTIFFTagSet.FILL_ORDER_LEFT_TO_RIGHT;
sb = new StringBuffer();
for (int i = 0; i < bitsPerSample.length; i++) {
if (i > 0) {
sb.append(" ");
}
int maxBitIndex = bitsPerSample[i] == 1 ? 7 : bitsPerSample[i] - 1;
int msb = fillOrder == BaselineTIFFTagSet.FILL_ORDER_LEFT_TO_RIGHT ? maxBitIndex : 0;
sb.append(Integer.toString(msb));
}
node = new IIOMetadataNode("SampleMSB");
if (isPaletteColor) {
node.setAttribute("value", repeat(sb.toString(), 3));
} else {
node.setAttribute("value", sb.toString());
}
data_node.appendChild(node);
return data_node;
}
use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class TIFFImageMetadata method getStandardCompressionNode.
public IIOMetadataNode getStandardCompressionNode() {
IIOMetadataNode compression_node = new IIOMetadataNode("Compression");
// scratch node
IIOMetadataNode node = null;
TIFFField f;
f = getTIFFField(BaselineTIFFTagSet.TAG_COMPRESSION);
if (f != null) {
String compressionTypeName = null;
int compression = f.getAsInt(0);
// obligate initialization.
boolean isLossless = true;
if (compression == BaselineTIFFTagSet.COMPRESSION_NONE) {
compressionTypeName = "None";
isLossless = true;
} else {
int[] compressionNumbers = TIFFImageWriter.compressionNumbers;
for (int i = 0; i < compressionNumbers.length; i++) {
if (compression == compressionNumbers[i]) {
compressionTypeName = TIFFImageWriter.compressionTypes[i];
isLossless = TIFFImageWriter.isCompressionLossless[i];
break;
}
}
}
if (compressionTypeName != null) {
node = new IIOMetadataNode("CompressionTypeName");
node.setAttribute("value", compressionTypeName);
compression_node.appendChild(node);
node = new IIOMetadataNode("Lossless");
node.setAttribute("value", isLossless ? "TRUE" : "FALSE");
compression_node.appendChild(node);
}
}
node = new IIOMetadataNode("NumProgressiveScans");
node.setAttribute("value", "1");
compression_node.appendChild(node);
return compression_node;
}
use of javax.imageio.metadata.IIOMetadataNode in project imageio-ext by geosolutions-it.
the class TIFFImageMetadata method getStandardDocumentNode.
public IIOMetadataNode getStandardDocumentNode() {
IIOMetadataNode document_node = new IIOMetadataNode("Document");
// scratch node
IIOMetadataNode node = null;
TIFFField f;
node = new IIOMetadataNode("FormatVersion");
node.setAttribute("value", "6.0");
document_node.appendChild(node);
f = getTIFFField(BaselineTIFFTagSet.TAG_NEW_SUBFILE_TYPE);
if (f != null) {
int newSubFileType = f.getAsInt(0);
String value = null;
if ((newSubFileType & BaselineTIFFTagSet.NEW_SUBFILE_TYPE_TRANSPARENCY) != 0) {
value = "TransparencyMask";
} else if ((newSubFileType & BaselineTIFFTagSet.NEW_SUBFILE_TYPE_REDUCED_RESOLUTION) != 0) {
value = "ReducedResolution";
} else if ((newSubFileType & BaselineTIFFTagSet.NEW_SUBFILE_TYPE_SINGLE_PAGE) != 0) {
value = "SinglePage";
}
if (value != null) {
node = new IIOMetadataNode("SubimageInterpretation");
node.setAttribute("value", value);
document_node.appendChild(node);
}
}
f = getTIFFField(BaselineTIFFTagSet.TAG_DATE_TIME);
if (f != null) {
String s = f.getAsString(0);
// DateTime should be formatted as "YYYY:MM:DD hh:mm:ss".
if (s.length() == 19) {
node = new IIOMetadataNode("ImageCreationTime");
// Files with incorrect DateTime format have been
// observed so anticipate an exception from substring()
// and only add the node if the format is presumably
// correct.
boolean appendNode;
try {
node.setAttribute("year", s.substring(0, 4));
node.setAttribute("month", s.substring(5, 7));
node.setAttribute("day", s.substring(8, 10));
node.setAttribute("hour", s.substring(11, 13));
node.setAttribute("minute", s.substring(14, 16));
node.setAttribute("second", s.substring(17, 19));
appendNode = true;
} catch (IndexOutOfBoundsException e) {
appendNode = false;
}
if (appendNode) {
document_node.appendChild(node);
}
}
}
return document_node;
}
Aggregations