public void mergeTree(String formatName, Node root) throws IIOInvalidTreeException { if (formatName.equals(nativeMetadataFormatName)) { if (root == null) { throw new IllegalArgumentException("root == null!"); } mergeNativeTree(root); } else if (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName)) { if (root == null) { throw new IllegalArgumentException("root == null!"); } mergeStandardTree(root); } else { throw new IllegalArgumentException("Not a recognized format!"); } }
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("ByteOrder")) { fatal(node, "Root must have \"ByteOrder\" child"); } NamedNodeMap attrs = node.getAttributes(); String order = attrs.getNamedItem("value").getNodeValue(); if (order == null) { fatal(node, "ByteOrder node must have a \"value\" attribute"); } if (order.equals(bigEndianString)) { this.byteOrder = ByteOrder.BIG_ENDIAN; } else if (order.equals(littleEndianString)) { this.byteOrder = ByteOrder.LITTLE_ENDIAN; } else { fatal(node, "Incorrect value for ByteOrder \"value\" attribute"); } }
JFIFThumbJPEG(Node node) throws IIOInvalidTreeException { if (node.getChildNodes().getLength() > 1) { throw new IIOInvalidTreeException ("JFIFThumbJPEG node must have 0 or 1 child", node); } Node child = node.getFirstChild(); if (child != null) { String name = child.getNodeName(); if (!name.equals("markerSequence")) { throw new IIOInvalidTreeException ("JFIFThumbJPEG child must be a markerSequence node", node); } thumbMetadata = new JPEGMetadata(false, true); thumbMetadata.setFromMarkerSequenceNode(child); } }
JFIFThumbUncompressed(Node node, String name) throws IIOInvalidTreeException { thumbWidth = 0; thumbHeight = 0; this.name = name; NamedNodeMap attrs = node.getAttributes(); int count = attrs.getLength(); if (count > 2) { throw new IIOInvalidTreeException (name +" node cannot have > 2 attributes", node); } if (count != 0) { int 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; } }
/** * Converts a standard {@code javax_imageio_1.0} tree to a * {@code TIFFImageMetadata} object. * * @param inData The metadata object. * @return a {@code TIFFImageMetadata} or {@code null} if * the standard tree derived from the input object is {@code null}. * @throws IllegalArgumentException if {@code inData} is * {@code null}. * @throws IllegalArgumentException if {@code inData} does not support * the standard metadata format. * @throws IIOInvalidTreeException if {@code inData} generates an * invalid standard metadata tree. */ private TIFFImageMetadata convertStandardImageMetadata(IIOMetadata inData) throws IIOInvalidTreeException { if(inData == null) { throw new NullPointerException("inData == null!"); } else if(!inData.isStandardMetadataFormatSupported()) { throw new IllegalArgumentException ("inData does not support standard metadata format!"); } TIFFImageMetadata outData = null; String formatName = IIOMetadataFormatImpl.standardMetadataFormatName; Node tree = inData.getAsTree(formatName); if (tree != null) { List<TIFFTagSet> tagSets = new ArrayList<TIFFTagSet>(1); tagSets.add(BaselineTIFFTagSet.getInstance()); outData = new TIFFImageMetadata(tagSets); outData.setFromTree(formatName, tree); } return outData; }
void updateFromNativeNode(Node node, boolean fromScratch) throws IIOInvalidTreeException { // Only the transform is required NamedNodeMap attrs = node.getAttributes(); transform = getAttributeValue(node, attrs, "transform", 0, 2, true); int count = attrs.getLength(); if (count > 4) { throw new IIOInvalidTreeException ("Adobe APP14 node cannot have > 4 attributes", node); } if (count > 1) { int value = getAttributeValue(node, attrs, "version", 100, 255, false); version = (value != -1) ? value : version; value = getAttributeValue(node, attrs, "flags0", 0, 65535, false); flags0 = (value != -1) ? value : flags0; value = getAttributeValue(node, attrs, "flags1", 0, 65535, false); flags1 = (value != -1) ? value : flags1; } }
private int getEnumeratedAttribute(Node node, String name, String[] legalNames, int defaultValue, boolean required) throws IIOInvalidTreeException { Node attr = node.getAttributes().getNamedItem(name); if (attr == null) { if (!required) { return defaultValue; } else { fatal(node, "Required attribute " + name + " not present!"); } } String value = attr.getNodeValue(); for (int i = 0; i < legalNames.length; i++) { if (value.equals(legalNames[i])) { return i; } } fatal(node, "Illegal value for attribute " + name + "!"); return -1; }
/** * Merge a JFIF subtree into the marker sequence, if the subtree * is non-empty. * If a JFIF marker exists, update it from the subtree. * If none exists, create one from the subtree and insert it at the * beginning of the marker sequence. */ private void mergeJFIFsubtree(Node JPEGvariety) throws IIOInvalidTreeException { if (JPEGvariety.getChildNodes().getLength() != 0) { Node jfifNode = JPEGvariety.getFirstChild(); // is there already a jfif marker segment? JFIFMarkerSegment jfifSeg = (JFIFMarkerSegment) findMarkerSegment(JFIFMarkerSegment.class, true); if (jfifSeg != null) { jfifSeg.updateFromNativeNode(jfifNode, false); } else { // Add it as the first element in the list. markerSequence.add(0, new JFIFMarkerSegment(jfifNode)); } } }
/** * Merge the given DRI node into the marker sequence. * If there already exists a DRI marker segment, the restart interval * value is updated. * If there is no DRI segment, then a new one is created and added as * follows: * If there is an SOF segment, the new DRI segment is inserted before * it. * If there is no SOF segment, the new DRI segment is inserted before * the first SOS segment, if there is one. * If there is no SOS segment, the new DRI segment is added to the end * of the sequence. */ private void mergeDRINode(Node node) throws IIOInvalidTreeException { DRIMarkerSegment dri = (DRIMarkerSegment) findMarkerSegment(DRIMarkerSegment.class, true); if (dri != null) { dri.updateFromNativeNode(node, false); } else { DRIMarkerSegment newGuy = new DRIMarkerSegment(node); int firstSOF = findMarkerSegmentPosition(SOFMarkerSegment.class, true); int firstSOS = findMarkerSegmentPosition(SOSMarkerSegment.class, true); if (firstSOF != -1) { markerSequence.add(firstSOF, newGuy); } else if (firstSOS != -1) { markerSequence.add(firstSOS, newGuy); } else { markerSequence.add(newGuy); } } }
public void mergeTree(String formatName, Node root) throws IIOInvalidTreeException{ if (formatName.equals(nativeMetadataFormatName)) { if (root == null) { throw new NullPointerException("root == null!"); } mergeNativeTree(root); } else if (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName)) { if (root == null) { throw new NullPointerException("root == null!"); } mergeStandardTree(root); } else { throw new IllegalArgumentException("Not a recognized format!"); } }
/** * Merge the given SOS node into the marker sequence. * If there already exists a single SOS marker segment, then the values * are updated from the node. * If there are more than one existing SOS marker segments, then an * IIOInvalidTreeException is thrown, as SOS segments cannot be merged * into a set of progressive scans. * If there are no SOS marker segments, a new one is created and added * to the end of the sequence. */ private void mergeSOSNode(Node node) throws IIOInvalidTreeException { SOSMarkerSegment firstSOS = (SOSMarkerSegment) findMarkerSegment(SOSMarkerSegment.class, true); SOSMarkerSegment lastSOS = (SOSMarkerSegment) findMarkerSegment(SOSMarkerSegment.class, false); if (firstSOS != null) { if (firstSOS != lastSOS) { throw new IIOInvalidTreeException ("Can't merge SOS node into a tree with > 1 SOS node", node); } firstSOS.updateFromNativeNode(node, false); } else { markerSequence.add(new SOSMarkerSegment(node)); } }
private IIOMetadata upgradeMetadata(IIOMetadata src, BufferedImage bi) { String format = src.getNativeMetadataFormatName(); System.out.println("Native format: " + format); Node root = src.getAsTree(format); // add hIST node Node n = lookupChildNode(root, "hIST"); if (n == null) { System.out.println("Appending new hIST node..."); Node hIST = gethISTNode(bi); root.appendChild(hIST); } System.out.println("Upgraded metadata tree:"); dump(root, ""); System.out.println("Merging metadata..."); try { src.mergeTree(format, root); } catch (IIOInvalidTreeException e) { throw new RuntimeException("Test FAILED!", e); } return src; }
private boolean getBooleanAttribute(Node node, String name, boolean defaultValue, boolean required) throws IIOInvalidTreeException { Node attr = node.getAttributes().getNamedItem(name); if (attr == null) { if (!required) { return defaultValue; } else { fatal(node, "Required attribute " + name + " not present!"); } } String value = attr.getNodeValue(); // Allow lower case booleans for backward compatibility, #5082756 if (value.equals("TRUE") || value.equals("true")) { return true; } else if (value.equals("FALSE") || value.equals("false")) { return false; } else { fatal(node, "Attribute " + name + " must be 'TRUE' or 'FALSE'!"); return false; } }
private void mergeStandardTree(Node root) throws IIOInvalidTreeException { transparencyDone = false; NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Node node = children.item(i); String name = node.getNodeName(); if (name.equals("Chroma")) { mergeStandardChromaNode(node, children); } else if (name.equals("Compression")) { mergeStandardCompressionNode(node); } else if (name.equals("Data")) { mergeStandardDataNode(node); } else if (name.equals("Dimension")) { mergeStandardDimensionNode(node); } else if (name.equals("Document")) { mergeStandardDocumentNode(node); } else if (name.equals("Text")) { mergeStandardTextNode(node); } else if (name.equals("Transparency")) { mergeStandardTransparencyNode(node); } else { throw new IIOInvalidTreeException("Invalid node: " + name, node); } } }
static int getAttributeValue(Node node, NamedNodeMap attrs, String name, int min, int max, boolean required) throws IIOInvalidTreeException { if (attrs == null) { attrs = node.getAttributes(); } String valueString = attrs.getNamedItem(name).getNodeValue(); int value = -1; if (valueString == null) { if (required) { throw new IIOInvalidTreeException (name + " attribute not found", node); } } else { value = Integer.parseInt(valueString); if ((value < min) || (value > max)) { throw new IIOInvalidTreeException (name + " attribute out of range", node); } } return value; }
protected static int getEnumeratedAttribute(Node node, String name, String[] legalNames, int defaultValue, boolean required) throws IIOInvalidTreeException { Node attr = node.getAttributes().getNamedItem(name); if (attr == null) { if (!required) { return defaultValue; } else { fatal(node, "Required attribute " + name + " not present!"); } } String value = attr.getNodeValue(); for (int i = 0; i < legalNames.length; i++) { if(value.equals(legalNames[i])) { return i; } } fatal(node, "Illegal value for attribute " + name + "!"); return -1; }
protected static boolean getBooleanAttribute(Node node, String name, boolean defaultValue, boolean required) throws IIOInvalidTreeException { Node attr = node.getAttributes().getNamedItem(name); if (attr == null) { if (!required) { return defaultValue; } else { fatal(node, "Required attribute " + name + " not present!"); } } String value = attr.getNodeValue(); // Allow lower case booleans for backward compatibility, #5082756 if (value.equals("TRUE") || value.equals("true")) { return true; } else if (value.equals("FALSE") || value.equals("false")) { return false; } else { fatal(node, "Attribute " + name + " must be 'TRUE' or 'FALSE'!"); return false; } }
/** * Converts a native * {@code javax_imageio_tiff_image_1.0} tree to a * {@code TIFFImageMetadata} object. * * @param inData The metadata object. * @return a {@code TIFFImageMetadata} or {@code null} if * the native tree derived from the input object is {@code null}. * @throws IllegalArgumentException if {@code inData} is * {@code null} or does not support the native metadata format. * @throws IIOInvalidTreeException if {@code inData} generates an * invalid native metadata tree. */ private TIFFImageMetadata convertNativeImageMetadata(IIOMetadata inData) throws IIOInvalidTreeException { if(inData == null) { throw new NullPointerException("inData == null!"); } else if(!Arrays.asList(inData.getMetadataFormatNames()).contains( TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME)) { throw new IllegalArgumentException ("inData does not support native metadata format!"); } TIFFImageMetadata outData = null; String formatName = TIFFImageMetadata.NATIVE_METADATA_FORMAT_NAME; Node tree = inData.getAsTree(formatName); if (tree != null) { List<TIFFTagSet> tagSets = new ArrayList<TIFFTagSet>(1); tagSets.add(BaselineTIFFTagSet.getInstance()); outData = new TIFFImageMetadata(tagSets); outData.setFromTree(formatName, tree); } return outData; }
void updateFromNativeNode(Node node, boolean fromScratch) throws IIOInvalidTreeException { NamedNodeMap attrs = node.getAttributes(); int value = getAttributeValue(node, attrs, "process", 0, 2, false); tag = (value != -1) ? value+JPEG.SOF0 : tag; // If samplePrecision is present, it must be 8. // This just checks. We don't bother to assign the value. value = getAttributeValue(node, attrs, "samplePrecision", 8, 8, false); value = getAttributeValue(node, attrs, "numLines", 0, 65535, false); numLines = (value != -1) ? value : numLines; value = getAttributeValue(node, attrs, "samplesPerLine", 0, 65535, false); samplesPerLine = (value != -1) ? value : samplesPerLine; int numComponents = getAttributeValue(node, attrs, "numFrameComponents", 1, 4, false); NodeList children = node.getChildNodes(); if (children.getLength() != numComponents) { throw new IIOInvalidTreeException ("numFrameComponents must match number of children", node); } componentSpecs = new ComponentSpec [numComponents]; for (int i = 0; i < numComponents; i++) { componentSpecs[i] = new ComponentSpec(children.item(i)); } }
ComponentSpec(Node node) throws IIOInvalidTreeException { NamedNodeMap attrs = node.getAttributes(); componentId = getAttributeValue(node, attrs, "componentId", 0, 255, true); HsamplingFactor = getAttributeValue(node, attrs, "HsamplingFactor", 1, 255, true); VsamplingFactor = getAttributeValue(node, attrs, "VsamplingFactor", 1, 255, true); QtableSelector = getAttributeValue(node, attrs, "QtableSelector", 0, 3, true); }
SOSMarkerSegment(Node node) throws IIOInvalidTreeException { super(JPEG.SOS); startSpectralSelection = 0; endSpectralSelection = 63; approxHigh = 0; approxLow = 0; updateFromNativeNode(node, true); }
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)); } }
private void mergeNativeTree(Node root) throws IIOInvalidTreeException { String name = root.getNodeName(); if (name != ((isStream) ? JPEG.nativeStreamMetadataFormatName : JPEG.nativeImageMetadataFormatName)) { throw new IIOInvalidTreeException("Invalid root node name: " + name, root); } if (root.getChildNodes().getLength() != 2) { // JPEGvariety and markerSequence throw new IIOInvalidTreeException( "JPEGvariety and markerSequence nodes must be present", root); } mergeJFIFsubtree(root.getFirstChild()); mergeSequenceSubtree(root.getLastChild()); }
private int getIntAttribute(Node node, String name, int defaultValue, boolean required) throws IIOInvalidTreeException { String value = getStringAttribute(node, name, null, required); if (value == null) { return defaultValue; } return Integer.parseInt(value); }
ICCMarkerSegment(Node node) throws IIOInvalidTreeException { super(JPEG.APP2); if (node instanceof IIOMetadataNode) { IIOMetadataNode ourNode = (IIOMetadataNode) node; ICC_Profile prof = (ICC_Profile) ourNode.getUserObject(); if (prof != null) { // May be null profile = prof.getData(); } } }
SOFMarkerSegment(Node node) throws IIOInvalidTreeException { // All attributes are optional, so setup defaults first super(JPEG.SOF0); samplePrecision = 8; numLines = 0; samplesPerLine = 0; updateFromNativeNode(node, true); }