Java Code Examples for javax.imageio.metadata.IIOMetadataNode

The following are top voted examples for showing how to use javax.imageio.metadata.IIOMetadataNode. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: OpenJSharp   File: COMMarkerSegment.java   View source code 6 votes vote down vote up
/**
 * Constructs a marker segment from a native tree node.  If the node
 * is an <code>IIOMetadataNode</code> and contains a user object,
 * that object is used rather than the string attribute.  If the
 * string attribute is used, the default encoding is used.
 */
COMMarkerSegment(Node node) throws IIOInvalidTreeException{
    super(JPEG.COM);
    if (node instanceof IIOMetadataNode) {
        IIOMetadataNode ourNode = (IIOMetadataNode) node;
        data = (byte []) ourNode.getUserObject();
    }
    if (data == null) {
        String comment =
            node.getAttributes().getNamedItem("comment").getNodeValue();
        if (comment != null) {
            data = comment.getBytes(); // Default encoding
        } else {
            throw new IIOInvalidTreeException("Empty comment node!", node);
        }
    }
}
 
Example 2
Project: openjdk-jdk10   File: PngCreationTimeTest.java   View source code 6 votes vote down vote up
public static IIOMetadataNode createStandardMetadataNodeTree() {
    /*
     * Create standard metadata tree with creation time in
     * Standard(Root)/Document/ImageCreationTime node
     */
    IIOMetadataNode createTimeNode = new IIOMetadataNode("ImageCreationTime");
    createTimeNode.setAttribute("year", "2016");
    createTimeNode.setAttribute("month", "12");
    createTimeNode.setAttribute("day", "21");
    createTimeNode.setAttribute("hour", "18");
    createTimeNode.setAttribute("minute", "30");
    createTimeNode.setAttribute("second", "00");

    // Create the Document node
    IIOMetadataNode documentNode = new IIOMetadataNode("Document");
    documentNode.appendChild(createTimeNode);

    // Create a root node append the Document node
    IIOMetadataNode root = new IIOMetadataNode("javax_imageio_1.0");
    root.appendChild(documentNode);

    return root;
}
 
Example 3
Project: OpenJSharp   File: SOSMarkerSegment.java   View source code 6 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("sos");
    node.setAttribute("numScanComponents",
                      Integer.toString(componentSpecs.length));
    node.setAttribute("startSpectralSelection",
                      Integer.toString(startSpectralSelection));
    node.setAttribute("endSpectralSelection",
                      Integer.toString(endSpectralSelection));
    node.setAttribute("approxHigh",
                      Integer.toString(approxHigh));
    node.setAttribute("approxLow",
                      Integer.toString(approxLow));
    for (int i = 0; i < componentSpecs.length; i++) {
        node.appendChild(componentSpecs[i].getNativeNode());
    }

    return node;
}
 
Example 4
Project: OpenJSharp   File: JFIFMarkerSegment.java   View source code 6 votes vote down vote up
/**
 * Returns a tree of DOM nodes representing this object and any
 * subordinate JFXX extension or ICC Profile segments.
 */
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("app0JFIF");
    node.setAttribute("majorVersion", Integer.toString(majorVersion));
    node.setAttribute("minorVersion", Integer.toString(minorVersion));
    node.setAttribute("resUnits", Integer.toString(resUnits));
    node.setAttribute("Xdensity", Integer.toString(Xdensity));
    node.setAttribute("Ydensity", Integer.toString(Ydensity));
    node.setAttribute("thumbWidth", Integer.toString(thumbWidth));
    node.setAttribute("thumbHeight", Integer.toString(thumbHeight));
    if (!extSegments.isEmpty()) {
        IIOMetadataNode JFXXnode = new IIOMetadataNode("JFXX");
        node.appendChild(JFXXnode);
        for (Iterator iter = extSegments.iterator(); iter.hasNext();) {
            JFIFExtensionMarkerSegment seg =
                (JFIFExtensionMarkerSegment) iter.next();
            JFXXnode.appendChild(seg.getNativeNode());
        }
    }
    if (iccSegment != null) {
        node.appendChild(iccSegment.getNativeNode());
    }

    return node;
}
 
Example 5
Project: OpenJSharp   File: SOFMarkerSegment.java   View source code 6 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("sof");
    node.setAttribute("process", Integer.toString(tag-JPEG.SOF0));
    node.setAttribute("samplePrecision",
                      Integer.toString(samplePrecision));
    node.setAttribute("numLines",
                      Integer.toString(numLines));
    node.setAttribute("samplesPerLine",
                      Integer.toString(samplesPerLine));
    node.setAttribute("numFrameComponents",
                      Integer.toString(componentSpecs.length));
    for (int i = 0; i < componentSpecs.length; i++) {
        node.appendChild(componentSpecs[i].getNativeNode());
    }

    return node;
}
 
Example 6
Project: OpenJSharp   File: BMPMetadata.java   View source code 6 votes vote down vote up
protected IIOMetadataNode getStandardChromaNode() {

        if ((palette != null) && (paletteSize > 0)) {
            IIOMetadataNode node = new IIOMetadataNode("Chroma");
            IIOMetadataNode subNode = new IIOMetadataNode("Palette");
            int numComps = palette.length / paletteSize;
            subNode.setAttribute("value", "" + numComps);

            for (int i = 0, j = 0; i < paletteSize; i++) {
                IIOMetadataNode subNode1 = new IIOMetadataNode("PaletteEntry");
                subNode1.setAttribute("index", ""+i);
                subNode1.setAttribute("red", "" + palette[j++]);
                subNode1.setAttribute("green", "" + palette[j++]);
                subNode1.setAttribute("blue", "" + palette[j++]);
                if (numComps == 4 && palette[j] != 0)
                    subNode1.setAttribute("alpha", "" + palette[j++]);
                subNode.appendChild(subNode1);
            }
            node.appendChild(subNode);
            return node;
        }

        return null;
    }
 
Example 7
Project: openjdk-jdk10   File: BMPMetadata.java   View source code 6 votes vote down vote up
protected IIOMetadataNode getStandardDataNode() {
    IIOMetadataNode node = new IIOMetadataNode("Data");

    String bits = "";
    if (bitsPerPixel == 24)
        bits = "8 8 8 ";
    else if (bitsPerPixel == 16 || bitsPerPixel == 32) {
        bits = "" + countBits(redMask) + " " + countBits(greenMask) +
              countBits(blueMask) + "" + countBits(alphaMask);
    }

    IIOMetadataNode subNode = new IIOMetadataNode("BitsPerSample");
    subNode.setAttribute("value", bits);
    node.appendChild(subNode);

    return node;
}
 
Example 8
Project: jdk8u-jdk   File: COMMarkerSegment.java   View source code 6 votes vote down vote up
/**
 * Constructs a marker segment from a native tree node.  If the node
 * is an <code>IIOMetadataNode</code> and contains a user object,
 * that object is used rather than the string attribute.  If the
 * string attribute is used, the default encoding is used.
 */
COMMarkerSegment(Node node) throws IIOInvalidTreeException{
    super(JPEG.COM);
    if (node instanceof IIOMetadataNode) {
        IIOMetadataNode ourNode = (IIOMetadataNode) node;
        data = (byte []) ourNode.getUserObject();
    }
    if (data == null) {
        String comment =
            node.getAttributes().getNamedItem("comment").getNodeValue();
        if (comment != null) {
            data = comment.getBytes(); // Default encoding
        } else {
            throw new IIOInvalidTreeException("Empty comment node!", node);
        }
    }
}
 
Example 9
Project: openjdk-jdk10   File: ShortHistogramTest.java   View source code 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 10
Project: openjdk-jdk10   File: BMPMetadata.java   View source code 6 votes vote down vote up
protected IIOMetadataNode getStandardChromaNode() {

        if ((palette != null) && (paletteSize > 0)) {
            IIOMetadataNode node = new IIOMetadataNode("Chroma");
            IIOMetadataNode subNode = new IIOMetadataNode("Palette");
            int numComps = palette.length / paletteSize;
            subNode.setAttribute("value", "" + numComps);

            for (int i = 0, j = 0; i < paletteSize; i++) {
                IIOMetadataNode subNode1 = new IIOMetadataNode("PaletteEntry");
                subNode1.setAttribute("index", ""+i);
                subNode1.setAttribute("red", "" + palette[j++]);
                subNode1.setAttribute("green", "" + palette[j++]);
                subNode1.setAttribute("blue", "" + palette[j++]);
                if (numComps == 4 && palette[j] != 0)
                    subNode1.setAttribute("alpha", "" + palette[j++]);
                subNode.appendChild(subNode1);
            }
            node.appendChild(subNode);
            return node;
        }

        return null;
    }
 
Example 11
Project: openjdk-jdk10   File: SetAttributeNode.java   View source code 6 votes vote down vote up
public static void test1() {
    IIOMetadataNode parent = new IIOMetadataNode("parent");
    IIOMetadataNode elem   = new IIOMetadataNode("elem");

    MyAttrNode attrNode = new MyAttrNode("name", "value");
    elem.setAttributeNode(attrNode);
    attrNode.setOwnerElement(elem);

    try {
        parent.setAttributeNode(attrNode);
    } catch (DOMException e) {
        if (e.code != DOMException.INUSE_ATTRIBUTE_ERR) {
            throw new RuntimeException("Test 1 failed: " +
                                       "Invalid exception code: " +
                                       e.code);
        }
        return;
    }

    throw new RuntimeException("Test 1 failed: DOMException not thrown");
}
 
Example 12
Project: jdk8u-jdk   File: SOSMarkerSegment.java   View source code 6 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("sos");
    node.setAttribute("numScanComponents",
                      Integer.toString(componentSpecs.length));
    node.setAttribute("startSpectralSelection",
                      Integer.toString(startSpectralSelection));
    node.setAttribute("endSpectralSelection",
                      Integer.toString(endSpectralSelection));
    node.setAttribute("approxHigh",
                      Integer.toString(approxHigh));
    node.setAttribute("approxLow",
                      Integer.toString(approxLow));
    for (int i = 0; i < componentSpecs.length; i++) {
        node.appendChild(componentSpecs[i].getNativeNode());
    }

    return node;
}
 
Example 13
Project: jdk8u-jdk   File: JFIFMarkerSegment.java   View source code 6 votes vote down vote up
/**
 * Returns a tree of DOM nodes representing this object and any
 * subordinate JFXX extension or ICC Profile segments.
 */
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("app0JFIF");
    node.setAttribute("majorVersion", Integer.toString(majorVersion));
    node.setAttribute("minorVersion", Integer.toString(minorVersion));
    node.setAttribute("resUnits", Integer.toString(resUnits));
    node.setAttribute("Xdensity", Integer.toString(Xdensity));
    node.setAttribute("Ydensity", Integer.toString(Ydensity));
    node.setAttribute("thumbWidth", Integer.toString(thumbWidth));
    node.setAttribute("thumbHeight", Integer.toString(thumbHeight));
    if (!extSegments.isEmpty()) {
        IIOMetadataNode JFXXnode = new IIOMetadataNode("JFXX");
        node.appendChild(JFXXnode);
        for (Iterator iter = extSegments.iterator(); iter.hasNext();) {
            JFIFExtensionMarkerSegment seg =
                (JFIFExtensionMarkerSegment) iter.next();
            JFXXnode.appendChild(seg.getNativeNode());
        }
    }
    if (iccSegment != null) {
        node.appendChild(iccSegment.getNativeNode());
    }

    return node;
}
 
Example 14
Project: jdk8u-jdk   File: SOFMarkerSegment.java   View source code 6 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("sof");
    node.setAttribute("process", Integer.toString(tag-JPEG.SOF0));
    node.setAttribute("samplePrecision",
                      Integer.toString(samplePrecision));
    node.setAttribute("numLines",
                      Integer.toString(numLines));
    node.setAttribute("samplesPerLine",
                      Integer.toString(samplesPerLine));
    node.setAttribute("numFrameComponents",
                      Integer.toString(componentSpecs.length));
    for (int i = 0; i < componentSpecs.length; i++) {
        node.appendChild(componentSpecs[i].getNativeNode());
    }

    return node;
}
 
Example 15
Project: openjdk-jdk10   File: ITXtTest.java   View source code 6 votes vote down vote up
private static ITXtTest readFrom(File f) {
    try (ImageInputStream imageInputStream =
            ImageIO.createImageInputStream(f)) {

        ImageReader r = ImageIO.getImageReaders(imageInputStream).next();
        r.setInput(imageInputStream);

        IIOImage dst = r.readAll(0, null);

        // look for iTXt node
        IIOMetadata m = dst.getMetadata();
        Node root = m.getAsTree(m.getNativeMetadataFormatName());
        Node n = root.getFirstChild();
        while (n != null && !"iTXt".equals(n.getNodeName())) {
            n = n.getNextSibling();
        }
        if (n == null) {
            throw new RuntimeException("No iTXt node!");
        }
        ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
        return t;
    } catch (Throwable e) {
        throw new RuntimeException("Reading test failed.", e);
    }
}
 
Example 16
Project: jdk8u-jdk   File: BMPMetadata.java   View source code 6 votes vote down vote up
protected IIOMetadataNode getStandardChromaNode() {

        if ((palette != null) && (paletteSize > 0)) {
            IIOMetadataNode node = new IIOMetadataNode("Chroma");
            IIOMetadataNode subNode = new IIOMetadataNode("Palette");
            int numComps = palette.length / paletteSize;
            subNode.setAttribute("value", "" + numComps);

            for (int i = 0, j = 0; i < paletteSize; i++) {
                IIOMetadataNode subNode1 = new IIOMetadataNode("PaletteEntry");
                subNode1.setAttribute("index", ""+i);
                subNode1.setAttribute("red", "" + palette[j++]);
                subNode1.setAttribute("green", "" + palette[j++]);
                subNode1.setAttribute("blue", "" + palette[j++]);
                if (numComps == 4 && palette[j] != 0)
                    subNode1.setAttribute("alpha", "" + palette[j++]);
                subNode.appendChild(subNode1);
            }
            node.appendChild(subNode);
            return node;
        }

        return null;
    }
 
Example 17
Project: jdk8u-jdk   File: BMPMetadata.java   View source code 6 votes vote down vote up
protected IIOMetadataNode getStandardDataNode() {
    IIOMetadataNode node = new IIOMetadataNode("Data");

    String bits = "";
    if (bitsPerPixel == 24)
        bits = "8 8 8 ";
    else if (bitsPerPixel == 16 || bitsPerPixel == 32) {
        bits = "" + countBits(redMask) + " " + countBits(greenMask) +
              countBits(blueMask) + "" + countBits(alphaMask);
    }

    IIOMetadataNode subNode = new IIOMetadataNode("BitsPerSample");
    subNode.setAttribute("value", bits);
    node.appendChild(subNode);

    return node;
}
 
Example 18
Project: jdk8u-jdk   File: SetAttributeNode.java   View source code 6 votes vote down vote up
public static void test1() {
    IIOMetadataNode parent = new IIOMetadataNode("parent");
    IIOMetadataNode elem   = new IIOMetadataNode("elem");

    MyAttrNode attrNode = new MyAttrNode("name", "value");
    elem.setAttributeNode(attrNode);
    attrNode.setOwnerElement(elem);

    try {
        parent.setAttributeNode(attrNode);
    } catch (DOMException e) {
        if (e.code != DOMException.INUSE_ATTRIBUTE_ERR) {
            throw new RuntimeException("Test 1 failed: " +
                                       "Invalid exception code: " +
                                       e.code);
        }
        return;
    }

    throw new RuntimeException("Test 1 failed: DOMException not thrown");
}
 
Example 19
Project: jdk8u-jdk   File: SetAttributeNode.java   View source code 6 votes vote down vote up
public static void test2() {
    String name = "attr";
    String oldValue = "old value";
    String newValue = "new value";
    Attr retAttr;

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, newValue);

    retAttr = parent.setAttributeNode(attrNode1);
    retAttr = parent.setAttributeNode(attrNode2);

    String actName = retAttr.getNodeName();
    String actValue = retAttr.getValue();

    if (!actName.equals(name) || !actValue.equals(oldValue)) {
        throw new RuntimeException("Test 2 failed: Invalid attribute " +
                                   "returned: " +
                                   "(name: " + actName +
                                   ", value: " + actValue + ")");
    }
}
 
Example 20
Project: jdk8u-jdk   File: SetAttributeNode.java   View source code 6 votes vote down vote up
public static void test4() {
    String name = "name";
    String correctValue = "correct value";
    String wrongValue = "wrong value";

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, wrongValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, correctValue);

    parent.setAttributeNode(attrNode1);
    parent.setAttributeNode(attrNode2);

    Attr actAttr = parent.getAttributeNode(name);
    String actValue = actAttr.getValue();

    if (!actValue.equals(correctValue)) {
        throw new RuntimeException("Test 4 failed: Return value is: " +
                                   actValue);
    }
}
 
Example 21
Project: jdk8u-jdk   File: ITXtTest.java   View source code 6 votes vote down vote up
public static ITXtTest getFromNode(IIOMetadataNode n) {
    ITXtTest t = new ITXtTest();

    if (!"iTXt".equals(n.getNodeName())) {
        throw new RuntimeException("Invalid node");
    }
    IIOMetadataNode e = (IIOMetadataNode)n.getFirstChild();
    if (!"iTXtEntry".equals(e.getNodeName())) {
        throw new RuntimeException("Invalid entry node");
    }
    t.keyword = e.getAttribute("keyword");
    t.isCompressed =
        Boolean.valueOf(e.getAttribute("compressionFlag")).booleanValue();
    t.compression =
        Integer.valueOf(e.getAttribute("compressionMethod")).intValue();
    t.language = e.getAttribute("languageTag");
    t.trasKeyword = e.getAttribute("translatedKeyword");
    t.text = e.getAttribute("text");

    return t;
}
 
Example 22
Project: jdk8u-jdk   File: ITXtTest.java   View source code 6 votes vote down vote up
private static ITXtTest readFrom(File f) {
    try {
        ImageInputStream iis = ImageIO.createImageInputStream(f);
        ImageReader r = ImageIO.getImageReaders(iis).next();
        r.setInput(iis);

        IIOImage dst = r.readAll(0, null);

        // look for iTXt node
        IIOMetadata m = dst.getMetadata();
        Node root = m.getAsTree(m.getNativeMetadataFormatName());
        Node n = root.getFirstChild();
        while (n != null && !"iTXt".equals(n.getNodeName())) {
            n = n.getNextSibling();
        }
        if (n == null) {
            throw new RuntimeException("No iTXt node!");
        }
        ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
        return t;
    } catch (Throwable e) {
        throw new RuntimeException("Reading test failed.", e);
    }
}
 
Example 23
Project: jdk8u-jdk   File: ShortHistogramTest.java   View source code 6 votes vote down vote up
private IIOMetadataNode gethISTNode(BufferedImage bi) {
    IndexColorModel icm = (IndexColorModel)bi.getColorModel();
    int mapSize = icm.getMapSize();

    int[] hist = new int[mapSize];
    Arrays.fill(hist, 0);

    Raster r = bi.getData();
    for (int y = 0; y < bi.getHeight(); y++) {
        for (int x = 0; x < bi.getWidth(); x++) {
            int s = r.getSample(x, y, 0);
            hist[s] ++;
        }
    }

    IIOMetadataNode hIST = new IIOMetadataNode("hIST");
    for (int i = 0; i < hist.length; i++) {
        IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
        n.setAttribute("index", "" + i);
        n.setAttribute("value", "" + hist[i]);
        hIST.appendChild(n);
    }

    return hIST;
}
 
Example 24
Project: openjdk-jdk10   File: COMMarkerSegment.java   View source code 6 votes vote down vote up
/**
 * Constructs a marker segment from a native tree node.  If the node
 * is an {@code IIOMetadataNode} and contains a user object,
 * that object is used rather than the string attribute.  If the
 * string attribute is used, the default encoding is used.
 */
COMMarkerSegment(Node node) throws IIOInvalidTreeException{
    super(JPEG.COM);
    if (node instanceof IIOMetadataNode) {
        IIOMetadataNode ourNode = (IIOMetadataNode) node;
        data = (byte []) ourNode.getUserObject();
    }
    if (data == null) {
        String comment =
            node.getAttributes().getNamedItem("comment").getNodeValue();
        if (comment != null) {
            data = comment.getBytes(); // Default encoding
        } else {
            throw new IIOInvalidTreeException("Empty comment node!", node);
        }
    }
}
 
Example 25
Project: openjdk-jdk10   File: SOSMarkerSegment.java   View source code 6 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("sos");
    node.setAttribute("numScanComponents",
                      Integer.toString(componentSpecs.length));
    node.setAttribute("startSpectralSelection",
                      Integer.toString(startSpectralSelection));
    node.setAttribute("endSpectralSelection",
                      Integer.toString(endSpectralSelection));
    node.setAttribute("approxHigh",
                      Integer.toString(approxHigh));
    node.setAttribute("approxLow",
                      Integer.toString(approxLow));
    for (int i = 0; i < componentSpecs.length; i++) {
        node.appendChild(componentSpecs[i].getNativeNode());
    }

    return node;
}
 
Example 26
Project: openjdk-jdk10   File: JFIFMarkerSegment.java   View source code 6 votes vote down vote up
/**
 * Returns a tree of DOM nodes representing this object and any
 * subordinate JFXX extension or ICC Profile segments.
 */
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("app0JFIF");
    node.setAttribute("majorVersion", Integer.toString(majorVersion));
    node.setAttribute("minorVersion", Integer.toString(minorVersion));
    node.setAttribute("resUnits", Integer.toString(resUnits));
    node.setAttribute("Xdensity", Integer.toString(Xdensity));
    node.setAttribute("Ydensity", Integer.toString(Ydensity));
    node.setAttribute("thumbWidth", Integer.toString(thumbWidth));
    node.setAttribute("thumbHeight", Integer.toString(thumbHeight));
    if (!extSegments.isEmpty()) {
        IIOMetadataNode JFXXnode = new IIOMetadataNode("JFXX");
        node.appendChild(JFXXnode);
        for (Iterator<JFIFExtensionMarkerSegment> iter =
                extSegments.iterator(); iter.hasNext();) {
            JFIFExtensionMarkerSegment seg = iter.next();
            JFXXnode.appendChild(seg.getNativeNode());
        }
    }
    if (iccSegment != null) {
        node.appendChild(iccSegment.getNativeNode());
    }

    return node;
}
 
Example 27
Project: Helpers   File: GifWriter.java   View source code 5 votes vote down vote up
@SuppressWarnings("ResultOfMethodCallIgnored")
public GifWriter(String filename, int msBetweenFrames, boolean repeat, boolean overwrite) throws IOException {
    File file = new File(filename + ".gif");
    if(overwrite && file.exists()) file.delete();

    this.writer = ImageIO.getImageWritersByFormatName("gif").next();
    this.output = ImageIO.createImageOutputStream(file);

    // Begining black magic
    imageWriteParam = writer.getDefaultWriteParam();
    ImageTypeSpecifier imageTypeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
    imageMetaData = writer.getDefaultImageMetadata(imageTypeSpecifier, imageWriteParam);
    String metaFormatName = imageMetaData.getNativeMetadataFormatName();
    IIOMetadataNode root = (IIOMetadataNode) imageMetaData.getAsTree(metaFormatName);
    IIOMetadataNode graphicsControlExtensionNode = getNode(root, "GraphicControlExtension");
    graphicsControlExtensionNode.setAttribute("delayTime", Integer.toString(msBetweenFrames / 10));
    IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");
    child.setAttribute("applicationID", "NETSCAPE");
    child.setAttribute("authenticationCode", "2.0");
    int loop = repeat ? 0 : 1;
    child.setUserObject(new byte[]{0x1, (byte) (loop & 0xFF), (byte) ((loop >> 8) & 0xFF)});
    getNode(root, "ApplicationExtensions").appendChild(child);
    imageMetaData.setFromTree(metaFormatName, root);
    // Ending black magic

    writer.setOutput(output);
    writer.prepareWriteSequence(null);
}
 
Example 28
Project: openjdk-jdk10   File: TIFFBaseJPEGCompressor.java   View source code 5 votes vote down vote up
private static List<Node> getAllNodes(IIOMetadataNode root, List<Node> nodes) {
    if(nodes == null) nodes = new ArrayList<Node>();

    if(root.hasChildNodes()) {
        Node sibling = root.getFirstChild();
        while(sibling != null) {
            nodes.add(sibling);
            nodes = getAllNodes((IIOMetadataNode)sibling, nodes);
            sibling = sibling.getNextSibling();
        }
    }

    return nodes;
}
 
Example 29
Project: marathonv5   File: ImagePanel.java   View source code 5 votes vote down vote up
private org.w3c.dom.Node getAnnotationNode(Annotation annotation, int i) {
    i++;
    String nodeKeyword = "a1810-" + i + "-" + annotation.getX() + "-" + annotation.getY() + "-" + annotation.getWidth() + "-"
            + annotation.getHeight();
    IIOMetadataNode node = new IIOMetadataNode("tEXtEntry");
    node.setAttribute("keyword", nodeKeyword);
    node.setAttribute("value", annotation.getText());
    return node;
}
 
Example 30
Project: openjdk-jdk10   File: PngCreationTimeTest.java   View source code 5 votes vote down vote up
public static void testMergeNativeTree() {
    // Merge a custom native metadata tree and inspect creation time
    if (pngMetadata != null) {
        try {
            IIOMetadataNode root = createNativeMetadataNodeTree();

            /*
             * Merge the native metadata tree created. The data should
             * reflect in Standard/Document/ImageCreationTime Node
             */
            pngMetadata.mergeTree("javax_imageio_png_1.0", root);
            Node keyNode = findNode(pngMetadata.getAsTree("javax_imageio_1.0"),
                    "ImageCreationTime");
            if (keyNode != null) {
                // Query the attributes of the node and check for the value
                NamedNodeMap attrMap = keyNode.getAttributes();
                String attrValue = attrMap.getNamedItem("year")
                                          .getNodeValue();
                int decYear = Integer.parseInt(attrValue);
                if (decYear != 2014) {
                    // Throw exception. Incorrect year value observed
                    reportExceptionAndFail("Test Failed: Incorrect"
                            + " creation time value observed.");
                }
            } else {
                // Throw exception.
                reportExceptionAndFail("Test Failed: Image creation"
                        + " time doesn't exist in metadata.");
            }
        } catch (IOException ex) {
            // Throw exception.
            reportExceptionAndFail("Test Failed: While executing"
                    + " mergeTree on metadata.");
        }
    }
}
 
Example 31
Project: imagetozxspec   File: GifConverter.java   View source code 5 votes vote down vote up
/**
 * Configures the per frame metadata
 * 
 * @param meta the default meta data
 * @param delayTime the amount of time a frame is to stay on screen in hundreds of a second (millisecond value/10)
 * @param imageIndex the index of this frame
 * @throws IIOInvalidTreeException if the meta data cannot be set
 */
private void configureMetaData(IIOMetadata meta, String delayTime, int imageIndex) throws IIOInvalidTreeException {
	String metaFormat = meta.getNativeMetadataFormatName();
	Node root = meta.getAsTree(metaFormat);
	Node child = root.getFirstChild();
	while (child != null) {
		if ("GraphicControlExtension".equals(child.getNodeName())) {
			break;
		}
		child = child.getNextSibling();
	}
	IIOMetadataNode gce = (IIOMetadataNode) child;
	gce.setAttribute("userDelay", "FALSE");
	gce.setAttribute("delayTime", delayTime);
	gce.setAttribute("disposalMethod", "none");

	if (imageIndex == 0) {
		IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions");
		IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension");
		ae.setAttribute("applicationID", "NETSCAPE");
        ae.setAttribute("authenticationCode", "2.0");
		byte[] uo = new byte[] { 0x1, (byte) (LOOP_COUNT & 0xFF), (byte) ((LOOP_COUNT >> 8) & 0xFF) };
		ae.setUserObject(uo);
		aes.appendChild(ae);
		root.appendChild(aes);
	}
	meta.setFromTree(metaFormat, root);		
}
 
Example 32
Project: OpenJSharp   File: COMMarkerSegment.java   View source code 5 votes vote down vote up
/**
 * Returns an <code>IIOMetadataNode</code> containing the data array
 * as a user object and a string encoded using ISO-8895-1, as an
 * attribute.
 */
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("com");
    node.setAttribute("comment", getComment());
    if (data != null) {
        node.setUserObject(data.clone());
    }
    return node;
}
 
Example 33
Project: openjdk-jdk10   File: SetAttributeNode.java   View source code 5 votes vote down vote up
public static void test3() {
    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode = new MyAttrNode("name", "value");
    Attr retAttr = parent.setAttributeNode(attrNode);

    if (retAttr != null) {
        throw new RuntimeException("Test 3 failed: Return value is " +
                                   "non-null");
    }
}
 
Example 34
Project: OpenJSharp   File: DQTMarkerSegment.java   View source code 5 votes vote down vote up
Qtable(Node node) throws IIOInvalidTreeException {
    if (node.getNodeName().equals("dqtable")) {
        NamedNodeMap attrs = node.getAttributes();
        int count = attrs.getLength();
        if ((count < 1) || (count > 2)) {
            throw new IIOInvalidTreeException
                ("dqtable node must have 1 or 2 attributes", node);
        }
        elementPrecision = 0;
        tableID = getAttributeValue(node, attrs, "qtableId", 0, 3, true);
        if (node instanceof IIOMetadataNode) {
            IIOMetadataNode ourNode = (IIOMetadataNode) node;
            JPEGQTable table = (JPEGQTable) ourNode.getUserObject();
            if (table == null) {
                throw new IIOInvalidTreeException
                    ("dqtable node must have user object", node);
            }
            data = table.getTable();
        } else {
            throw new IIOInvalidTreeException
                ("dqtable node must have user object", node);
        }
    } else {
        throw new IIOInvalidTreeException
            ("Invalid node, expected dqtable", node);
    }
}
 
Example 35
Project: OpenJSharp   File: DHTMarkerSegment.java   View source code 5 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("dht");
    for (int i= 0; i<tables.size(); i++) {
        Htable table = (Htable) tables.get(i);
        node.appendChild(table.getNativeNode());
    }
    return node;
}
 
Example 36
Project: OpenJSharp   File: DHTMarkerSegment.java   View source code 5 votes vote down vote up
Htable(Node node) throws IIOInvalidTreeException {
    if (node.getNodeName().equals("dhtable")) {
        NamedNodeMap attrs = node.getAttributes();
        int count = attrs.getLength();
        if (count != 2) {
            throw new IIOInvalidTreeException
                ("dhtable node must have 2 attributes", node);
        }
        tableClass = getAttributeValue(node, attrs, "class", 0, 1, true);
        tableID = getAttributeValue(node, attrs, "htableId", 0, 3, true);
        if (node instanceof IIOMetadataNode) {
            IIOMetadataNode ourNode = (IIOMetadataNode) node;
            JPEGHuffmanTable table =
                (JPEGHuffmanTable) ourNode.getUserObject();
            if (table == null) {
                throw new IIOInvalidTreeException
                    ("dhtable node must have user object", node);
            }
            numCodes = table.getLengths();
            values = table.getValues();
        } else {
            throw new IIOInvalidTreeException
                ("dhtable node must have user object", node);
        }
    } else {
        throw new IIOInvalidTreeException
            ("Invalid node, expected dqtable", node);
    }

}
 
Example 37
Project: OpenJSharp   File: DHTMarkerSegment.java   View source code 5 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("dhtable");
    node.setAttribute("class", Integer.toString(tableClass));
    node.setAttribute("htableId", Integer.toString(tableID));

    node.setUserObject(new JPEGHuffmanTable(numCodes, values));

    return node;
}
 
Example 38
Project: openjdk-jdk10   File: JFIFMarkerSegment.java   View source code 5 votes vote down vote up
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();
        }
    }
}
 
Example 39
Project: openjdk-jdk10   File: AdobeMarkerSegment.java   View source code 5 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("app14Adobe");
    node.setAttribute("version", Integer.toString(version));
    node.setAttribute("flags0", Integer.toString(flags0));
    node.setAttribute("flags1", Integer.toString(flags1));
    node.setAttribute("transform", Integer.toString(transform));

    return node;
}
 
Example 40
Project: OpenJSharp   File: JFIFMarkerSegment.java   View source code 5 votes vote down vote up
IIOMetadataNode getNativeNode() {
    IIOMetadataNode node = new IIOMetadataNode("JFIFthumbJPEG");
    if (thumbMetadata != null) {
        node.appendChild(thumbMetadata.getNativeTree());
    }
    return node;
}