use of javax.imageio.metadata.IIOInvalidTreeException in project jdk8u_jdk by JetBrains.
the class JPEGMetadata method setFromNativeTree.
private void setFromNativeTree(Node root) throws IIOInvalidTreeException {
if (resetSequence == null) {
resetSequence = markerSequence;
}
markerSequence = new ArrayList();
// Build a whole new marker sequence from the tree
String name = root.getNodeName();
if (name != ((isStream) ? JPEG.nativeStreamMetadataFormatName : JPEG.nativeImageMetadataFormatName)) {
throw new IIOInvalidTreeException("Invalid root node name: " + name, root);
}
if (!isStream) {
if (root.getChildNodes().getLength() != 2) {
// JPEGvariety and markerSequence
throw new IIOInvalidTreeException("JPEGvariety and markerSequence nodes must be present", root);
}
Node JPEGvariety = root.getFirstChild();
if (JPEGvariety.getChildNodes().getLength() != 0) {
markerSequence.add(new JFIFMarkerSegment(JPEGvariety.getFirstChild()));
}
}
Node markerSequenceNode = isStream ? root : root.getLastChild();
setFromMarkerSequenceNode(markerSequenceNode);
}
use of javax.imageio.metadata.IIOInvalidTreeException in project jdk8u_jdk by JetBrains.
the class SOSMarkerSegment method updateFromNativeNode.
void updateFromNativeNode(Node node, boolean fromScratch) throws IIOInvalidTreeException {
NamedNodeMap attrs = node.getAttributes();
int numComponents = getAttributeValue(node, attrs, "numScanComponents", 1, 4, true);
int value = getAttributeValue(node, attrs, "startSpectralSelection", 0, 63, false);
startSpectralSelection = (value != -1) ? value : startSpectralSelection;
value = getAttributeValue(node, attrs, "endSpectralSelection", 0, 63, false);
endSpectralSelection = (value != -1) ? value : endSpectralSelection;
value = getAttributeValue(node, attrs, "approxHigh", 0, 15, false);
approxHigh = (value != -1) ? value : approxHigh;
value = getAttributeValue(node, attrs, "approxLow", 0, 15, false);
approxLow = (value != -1) ? value : approxLow;
// Now the children
NodeList children = node.getChildNodes();
if (children.getLength() != numComponents) {
throw new IIOInvalidTreeException("numScanComponents must match the number of children", node);
}
componentSpecs = new ScanComponentSpec[numComponents];
for (int i = 0; i < numComponents; i++) {
componentSpecs[i] = new ScanComponentSpec(children.item(i));
}
}
use of javax.imageio.metadata.IIOInvalidTreeException in project jdk8u_jdk by JetBrains.
the class JPEGMetadata method mergeStandardChromaNode.
/*
* In general, it could be possible to convert all non-pixel data to some
* textual form and include it in comments, but then this would create the
* expectation that these comment forms be recognized by the reader, thus
* creating a defacto extension to JPEG metadata capabilities. This is
* probably best avoided, so the following convert only text nodes to
* comments, and lose the keywords as well.
*/
private void mergeStandardChromaNode(Node node, NodeList siblings) throws IIOInvalidTreeException {
if (transparencyDone) {
throw new IIOInvalidTreeException("Transparency node must follow Chroma node", node);
}
Node csType = node.getFirstChild();
if ((csType == null) || !csType.getNodeName().equals("ColorSpaceType")) {
// If there is no ColorSpaceType node, we have nothing to do
return;
}
String csName = csType.getAttributes().getNamedItem("name").getNodeValue();
int numChannels = 0;
boolean wantJFIF = false;
boolean wantAdobe = false;
int transform = 0;
boolean willSubsample = false;
// JFIF compatible
byte[] ids = { 1, 2, 3, 4 };
if (csName.equals("GRAY")) {
numChannels = 1;
wantJFIF = true;
} else if (csName.equals("YCbCr")) {
numChannels = 3;
wantJFIF = true;
willSubsample = true;
} else if (csName.equals("PhotoYCC")) {
numChannels = 3;
wantAdobe = true;
transform = JPEG.ADOBE_YCC;
ids[0] = (byte) 'Y';
ids[1] = (byte) 'C';
ids[2] = (byte) 'c';
} else if (csName.equals("RGB")) {
numChannels = 3;
wantAdobe = true;
transform = JPEG.ADOBE_UNKNOWN;
ids[0] = (byte) 'R';
ids[1] = (byte) 'G';
ids[2] = (byte) 'B';
} else if ((csName.equals("XYZ")) || (csName.equals("Lab")) || (csName.equals("Luv")) || (csName.equals("YxY")) || (csName.equals("HSV")) || (csName.equals("HLS")) || (csName.equals("CMY")) || (csName.equals("3CLR"))) {
numChannels = 3;
} else if (csName.equals("YCCK")) {
numChannels = 4;
wantAdobe = true;
transform = JPEG.ADOBE_YCCK;
willSubsample = true;
} else if (csName.equals("CMYK")) {
numChannels = 4;
wantAdobe = true;
transform = JPEG.ADOBE_UNKNOWN;
} else if (csName.equals("4CLR")) {
numChannels = 4;
} else {
// We can't handle them, so don't modify any metadata
return;
}
boolean wantAlpha = false;
for (int i = 0; i < siblings.getLength(); i++) {
Node trans = siblings.item(i);
if (trans.getNodeName().equals("Transparency")) {
wantAlpha = wantAlpha(trans);
// out of for
break;
}
}
if (wantAlpha) {
numChannels++;
wantJFIF = false;
if (ids[0] == (byte) 'R') {
ids[3] = (byte) 'A';
wantAdobe = false;
}
}
JFIFMarkerSegment jfif = (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class, true);
AdobeMarkerSegment adobe = (AdobeMarkerSegment) findMarkerSegment(AdobeMarkerSegment.class, true);
SOFMarkerSegment sof = (SOFMarkerSegment) findMarkerSegment(SOFMarkerSegment.class, true);
SOSMarkerSegment sos = (SOSMarkerSegment) findMarkerSegment(SOSMarkerSegment.class, true);
// nowhere to send it to.
if ((sof != null) && (sof.tag == JPEG.SOF2)) {
// Progressive
if ((sof.componentSpecs.length != numChannels) && (sos != null)) {
return;
}
}
// JFIF header might be removed
if (!wantJFIF && (jfif != null)) {
markerSequence.remove(jfif);
}
// Now add a JFIF if we do want one, but only if it isn't stream metadata
if (wantJFIF && !isStream) {
markerSequence.add(0, new JFIFMarkerSegment());
}
// stream metadata
if (wantAdobe) {
if ((adobe == null) && !isStream) {
adobe = new AdobeMarkerSegment(transform);
insertAdobeMarkerSegment(adobe);
} else {
adobe.transform = transform;
}
} else if (adobe != null) {
markerSequence.remove(adobe);
}
boolean updateQtables = false;
boolean updateHtables = false;
boolean progressive = false;
int[] subsampledSelectors = { 0, 1, 1, 0 };
int[] nonSubsampledSelectors = { 0, 0, 0, 0 };
int[] newTableSelectors = willSubsample ? subsampledSelectors : nonSubsampledSelectors;
// Keep the old componentSpecs array
SOFMarkerSegment.ComponentSpec[] oldCompSpecs = null;
// SOF might be modified
if (sof != null) {
oldCompSpecs = sof.componentSpecs;
progressive = (sof.tag == JPEG.SOF2);
// Now replace the SOF with a new one; it might be the same, but
// this is easier.
markerSequence.set(markerSequence.indexOf(sof), new SOFMarkerSegment(progressive, // we never need extended
false, willSubsample, ids, numChannels));
// in place in the new SOF segment above.
for (int i = 0; i < oldCompSpecs.length; i++) {
if (oldCompSpecs[i].QtableSelector != newTableSelectors[i]) {
updateQtables = true;
}
}
if (progressive) {
// if the component ids are different, update all the existing scans
// ignore Huffman tables
boolean idsDiffer = false;
for (int i = 0; i < oldCompSpecs.length; i++) {
if (ids[i] != oldCompSpecs[i].componentId) {
idsDiffer = true;
}
}
if (idsDiffer) {
// update the ids in each SOS marker segment
for (Iterator iter = markerSequence.iterator(); iter.hasNext(); ) {
MarkerSegment seg = (MarkerSegment) iter.next();
if (seg instanceof SOSMarkerSegment) {
SOSMarkerSegment target = (SOSMarkerSegment) seg;
for (int i = 0; i < target.componentSpecs.length; i++) {
int oldSelector = target.componentSpecs[i].componentSelector;
// above.
for (int j = 0; j < oldCompSpecs.length; j++) {
if (oldCompSpecs[j].componentId == oldSelector) {
target.componentSpecs[i].componentSelector = ids[j];
}
}
}
}
}
}
} else {
if (sos != null) {
// update the tables.
for (int i = 0; i < sos.componentSpecs.length; i++) {
if ((sos.componentSpecs[i].dcHuffTable != newTableSelectors[i]) || (sos.componentSpecs[i].acHuffTable != newTableSelectors[i])) {
updateHtables = true;
}
}
// Might be the same as the old one, but this is easier.
markerSequence.set(markerSequence.indexOf(sos), new SOSMarkerSegment(willSubsample, ids, numChannels));
}
}
} else {
// should be stream metadata if there isn't an SOF, but check it anyway
if (isStream) {
// update tables - routines below check if it's really necessary
updateQtables = true;
updateHtables = true;
}
}
if (updateQtables) {
List tableSegments = new ArrayList();
for (Iterator iter = markerSequence.iterator(); iter.hasNext(); ) {
MarkerSegment seg = (MarkerSegment) iter.next();
if (seg instanceof DQTMarkerSegment) {
tableSegments.add(seg);
}
}
// If we are not subsampling, we just need one, so don't do anything
if (!tableSegments.isEmpty() && willSubsample) {
// Is it really necessary? There should be at least 2 tables.
// If there is only one, assume it's a scaled "standard"
// luminance table, extract the scaling factor, and generate a
// scaled "standard" chrominance table.
// Find the table with selector 1.
boolean found = false;
for (Iterator iter = tableSegments.iterator(); iter.hasNext(); ) {
DQTMarkerSegment testdqt = (DQTMarkerSegment) iter.next();
for (Iterator tabiter = testdqt.tables.iterator(); tabiter.hasNext(); ) {
DQTMarkerSegment.Qtable tab = (DQTMarkerSegment.Qtable) tabiter.next();
if (tab.tableID == 1) {
found = true;
}
}
}
if (!found) {
// find the table with selector 0. There should be one.
DQTMarkerSegment.Qtable table0 = null;
for (Iterator iter = tableSegments.iterator(); iter.hasNext(); ) {
DQTMarkerSegment testdqt = (DQTMarkerSegment) iter.next();
for (Iterator tabiter = testdqt.tables.iterator(); tabiter.hasNext(); ) {
DQTMarkerSegment.Qtable tab = (DQTMarkerSegment.Qtable) tabiter.next();
if (tab.tableID == 0) {
table0 = tab;
}
}
}
// Assuming that the table with id 0 is a luminance table,
// compute a new chrominance table of the same quality and
// add it to the last DQT segment
DQTMarkerSegment dqt = (DQTMarkerSegment) tableSegments.get(tableSegments.size() - 1);
dqt.tables.add(dqt.getChromaForLuma(table0));
}
}
}
if (updateHtables) {
List tableSegments = new ArrayList();
for (Iterator iter = markerSequence.iterator(); iter.hasNext(); ) {
MarkerSegment seg = (MarkerSegment) iter.next();
if (seg instanceof DHTMarkerSegment) {
tableSegments.add(seg);
}
}
// If we are not subsampling, we just need one, so don't do anything
if (!tableSegments.isEmpty() && willSubsample) {
// Is it really necessary? There should be at least 2 dc and 2 ac
// tables. If there is only one, add a
// "standard " chrominance table.
// find a table with selector 1. AC/DC is irrelevant
boolean found = false;
for (Iterator iter = tableSegments.iterator(); iter.hasNext(); ) {
DHTMarkerSegment testdht = (DHTMarkerSegment) iter.next();
for (Iterator tabiter = testdht.tables.iterator(); tabiter.hasNext(); ) {
DHTMarkerSegment.Htable tab = (DHTMarkerSegment.Htable) tabiter.next();
if (tab.tableID == 1) {
found = true;
}
}
}
if (!found) {
// Create new standard dc and ac chrominance tables and add them
// to the last DHT segment
DHTMarkerSegment lastDHT = (DHTMarkerSegment) tableSegments.get(tableSegments.size() - 1);
lastDHT.addHtable(JPEGHuffmanTable.StdDCLuminance, true, 1);
lastDHT.addHtable(JPEGHuffmanTable.StdACLuminance, true, 1);
}
}
}
}
use of javax.imageio.metadata.IIOInvalidTreeException in project jdk8u_jdk by JetBrains.
the class JPEGImageWriter method convertImageMetadataOnThread.
private IIOMetadata convertImageMetadataOnThread(IIOMetadata inData, ImageTypeSpecifier imageType, ImageWriteParam param) {
// If it's one of ours, just return it
if (inData instanceof JPEGMetadata) {
JPEGMetadata jpegData = (JPEGMetadata) inData;
if (!jpegData.isStream) {
return inData;
} else {
// XXX Maybe this should put out a warning?
return null;
}
}
// the standard tree from the input, if it exists.
if (inData.isStandardMetadataFormatSupported()) {
String formatName = IIOMetadataFormatImpl.standardMetadataFormatName;
Node tree = inData.getAsTree(formatName);
if (tree != null) {
JPEGMetadata jpegData = new JPEGMetadata(imageType, param, this);
try {
jpegData.setFromTree(formatName, tree);
} catch (IIOInvalidTreeException e) {
// XXX Maybe this should put out a warning?
return null;
}
return jpegData;
}
}
return null;
}
use of javax.imageio.metadata.IIOInvalidTreeException in project jdk8u_jdk by JetBrains.
the class JFIFMarkerSegment method updateFromNativeNode.
/**
* Updates the data in this object from the given DOM Node tree.
* If fromScratch is true, this object is being constructed.
* Otherwise an existing object is being modified.
* Throws an IIOInvalidTreeException if the tree is invalid in
* any way.
*/
void updateFromNativeNode(Node node, boolean fromScratch) throws IIOInvalidTreeException {
// none of the attributes are required
NamedNodeMap attrs = node.getAttributes();
if (attrs.getLength() > 0) {
int value = getAttributeValue(node, attrs, "majorVersion", 0, 255, false);
majorVersion = (value != -1) ? value : majorVersion;
value = getAttributeValue(node, attrs, "minorVersion", 0, 255, false);
minorVersion = (value != -1) ? value : minorVersion;
value = getAttributeValue(node, attrs, "resUnits", 0, 2, false);
resUnits = (value != -1) ? value : resUnits;
value = getAttributeValue(node, attrs, "Xdensity", 1, 65535, false);
Xdensity = (value != -1) ? value : Xdensity;
value = getAttributeValue(node, attrs, "Ydensity", 1, 65535, false);
Ydensity = (value != -1) ? value : Ydensity;
value = getAttributeValue(node, attrs, "thumbWidth", 0, 255, false);
thumbWidth = (value != -1) ? value : thumbWidth;
value = getAttributeValue(node, attrs, "thumbHeight", 0, 255, false);
thumbHeight = (value != -1) ? value : thumbHeight;
}
if (node.hasChildNodes()) {
NodeList children = node.getChildNodes();
int count = children.getLength();
if (count > 2) {
throw new IIOInvalidTreeException("app0JFIF node cannot have > 2 children", node);
}
for (int i = 0; i < count; i++) {
Node child = children.item(i);
String name = child.getNodeName();
if (name.equals("JFXX")) {
if ((!extSegments.isEmpty()) && fromScratch) {
throw new IIOInvalidTreeException("app0JFIF node cannot have > 1 JFXX node", node);
}
NodeList exts = child.getChildNodes();
int extCount = exts.getLength();
for (int j = 0; j < extCount; j++) {
Node ext = exts.item(j);
extSegments.add(new JFIFExtensionMarkerSegment(ext));
}
}
if (name.equals("app2ICC")) {
if ((iccSegment != null) && fromScratch) {
throw new IIOInvalidTreeException("> 1 ICC APP2 Marker Segment not supported", node);
}
iccSegment = new ICCMarkerSegment(child);
}
}
}
}
Aggregations