Search in sources :

Example 6 with IIOMetadataNode

use of javax.imageio.metadata.IIOMetadataNode in project ChatGameFontificator by GlitchCog.

the class ControlWindow method generateMetadataNode.

private IIOMetadataNode generateMetadataNode(String key, String value) {
    IIOMetadataNode node = new IIOMetadataNode("tEXtEntry");
    node.setAttribute("keyword", key);
    node.setAttribute("value", value);
    return node;
}
Also used : IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Example 7 with IIOMetadataNode

use of javax.imageio.metadata.IIOMetadataNode in project ChatGameFontificator by GlitchCog.

the class AnimatedGifUtil method loadDittoAnimatedGif.

/**
     * Fix (ditto) for Java's animated GIF interpretation
     * 
     * Adapted from http://stackoverflow.com/questions/26801433/fix-frame-rate-of-animated-gif-in-java#answer-26829534
     * 
     * @param url
     *            The URL for the animated GIF to be loaded
     * @param dim
     *            The dimension object to be filled by the width and height of the loaded animated GIF
     * @return The loaded animated GIF
     * @throws Exception
     */
public static Image loadDittoAnimatedGif(final URL url, Dimension dim) {
    final Image dimImage = new ImageIcon(url).getImage();
    Image image = null;
    try {
        ImageReader gifReader = ImageIO.getImageReadersByFormatName(GIF_EXTENSION).next();
        InputStream imageStream = url.openStream();
        gifReader.setInput(ImageIO.createImageInputStream(imageStream));
        IIOMetadata imageMetaData = gifReader.getImageMetadata(0);
        String metaFormatName = imageMetaData.getNativeMetadataFormatName();
        final int frameCount = gifReader.getNumImages(true);
        ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
        ImageOutputStream ios = ImageIO.createImageOutputStream(baoStream);
        ImageWriter writer = ImageIO.getImageWriter(gifReader);
        writer.setOutput(ios);
        writer.prepareWriteSequence(null);
        final int imgWidth = dimImage.getWidth(null);
        final int imgHeight = dimImage.getHeight(null);
        dim.setSize(imgWidth, imgHeight);
        for (int i = 0; i < frameCount; i++) {
            // This read method takes into account the frame's top and left coordinates
            BufferedImage frame = gifReader.read(i);
            IIOMetadataNode nodes = (IIOMetadataNode) gifReader.getImageMetadata(i).getAsTree(metaFormatName);
            for (int j = 0; j < nodes.getLength(); j++) {
                IIOMetadataNode node = (IIOMetadataNode) nodes.item(j);
                if (GRAPHIC_CTRL_EXT.equalsIgnoreCase(node.getNodeName())) {
                    int delay = Integer.parseInt(node.getAttribute(ATTRIBUTE_DELAY_TIME));
                    // Minimum delay for browsers, which delay animated GIFs much more than would be to spec
                    if (delay < MIN_ANI_GIF_DELAY) {
                        node.setAttribute(ATTRIBUTE_DELAY_TIME, Integer.toString(MIN_ANI_GIF_DELAY));
                    }
                    // at least not for BTTV's (ditto)
                    if (node.getAttribute(ATTRIBUTE_DISPOSAL_METHOD).equals(DISPOSE_RESTORE_TO_PREVIOUS)) {
                        node.setAttribute(ATTRIBUTE_DISPOSAL_METHOD, DISPOSE_RESTORE_TO_BGCOLOR);
                    }
                }
            }
            IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(frame), null);
            metadata.setFromTree(metadata.getNativeMetadataFormatName(), nodes);
            // This modified frame is necessary to get the correct image placement, width, and height in the final GIF
            BufferedImage frameMod = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
            Graphics big = frameMod.getGraphics();
            big.drawImage(frame, 0, 0, null);
            IIOImage fixedFrame = new IIOImage(frameMod, null, metadata);
            writer.writeToSequence(fixedFrame, writer.getDefaultWriteParam());
        }
        writer.endWriteSequence();
        if (ios != null) {
            ios.close();
        }
        image = Toolkit.getDefaultToolkit().createImage(baoStream.toByteArray());
    } catch (Exception e) {
        // If anything goes wrong, just load it normally
        logger.error("Error loading animated GIF (ditto) from " + url, e);
        image = new ImageIcon(url).getImage();
        dim.setSize(image.getWidth(null), image.getHeight(null));
    }
    return image;
}
Also used : ImageIcon(javax.swing.ImageIcon) InputStream(java.io.InputStream) ImageWriter(javax.imageio.ImageWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IIOImage(javax.imageio.IIOImage) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IIOImage(javax.imageio.IIOImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) Graphics(java.awt.Graphics) ImageReader(javax.imageio.ImageReader) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 8 with IIOMetadataNode

use of javax.imageio.metadata.IIOMetadataNode in project jdk8u_jdk by JetBrains.

the class GIFImageMetadata method getStandardTextNode.

// Document not in image
public IIOMetadataNode getStandardTextNode() {
    if (comments == null) {
        return null;
    }
    Iterator commentIter = comments.iterator();
    if (!commentIter.hasNext()) {
        return null;
    }
    IIOMetadataNode text_node = new IIOMetadataNode("Text");
    // scratch node
    IIOMetadataNode node = null;
    while (commentIter.hasNext()) {
        byte[] comment = (byte[]) commentIter.next();
        String s = null;
        try {
            s = new String(comment, "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Encoding ISO-8859-1 unknown!");
        }
        node = new IIOMetadataNode("TextEntry");
        node.setAttribute("value", s);
        node.setAttribute("encoding", "ISO-8859-1");
        node.setAttribute("compression", "none");
        text_node.appendChild(node);
    }
    return text_node;
}
Also used : Iterator(java.util.Iterator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Example 9 with IIOMetadataNode

use of javax.imageio.metadata.IIOMetadataNode in project jdk8u_jdk by JetBrains.

the class GIFImageMetadata method getNativeTree.

private Node getNativeTree() {
    // scratch node
    IIOMetadataNode node;
    IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName);
    // Image descriptor
    node = new IIOMetadataNode("ImageDescriptor");
    node.setAttribute("imageLeftPosition", Integer.toString(imageLeftPosition));
    node.setAttribute("imageTopPosition", Integer.toString(imageTopPosition));
    node.setAttribute("imageWidth", Integer.toString(imageWidth));
    node.setAttribute("imageHeight", Integer.toString(imageHeight));
    node.setAttribute("interlaceFlag", interlaceFlag ? "TRUE" : "FALSE");
    root.appendChild(node);
    // Local color table
    if (localColorTable != null) {
        node = new IIOMetadataNode("LocalColorTable");
        int numEntries = localColorTable.length / 3;
        node.setAttribute("sizeOfLocalColorTable", Integer.toString(numEntries));
        node.setAttribute("sortFlag", sortFlag ? "TRUE" : "FALSE");
        for (int i = 0; i < numEntries; i++) {
            IIOMetadataNode entry = new IIOMetadataNode("ColorTableEntry");
            entry.setAttribute("index", Integer.toString(i));
            int r = localColorTable[3 * i] & 0xff;
            int g = localColorTable[3 * i + 1] & 0xff;
            int b = localColorTable[3 * i + 2] & 0xff;
            entry.setAttribute("red", Integer.toString(r));
            entry.setAttribute("green", Integer.toString(g));
            entry.setAttribute("blue", Integer.toString(b));
            node.appendChild(entry);
        }
        root.appendChild(node);
    }
    // Graphic control extension
    node = new IIOMetadataNode("GraphicControlExtension");
    node.setAttribute("disposalMethod", disposalMethodNames[disposalMethod]);
    node.setAttribute("userInputFlag", userInputFlag ? "TRUE" : "FALSE");
    node.setAttribute("transparentColorFlag", transparentColorFlag ? "TRUE" : "FALSE");
    node.setAttribute("delayTime", Integer.toString(delayTime));
    node.setAttribute("transparentColorIndex", Integer.toString(transparentColorIndex));
    root.appendChild(node);
    if (hasPlainTextExtension) {
        node = new IIOMetadataNode("PlainTextExtension");
        node.setAttribute("textGridLeft", Integer.toString(textGridLeft));
        node.setAttribute("textGridTop", Integer.toString(textGridTop));
        node.setAttribute("textGridWidth", Integer.toString(textGridWidth));
        node.setAttribute("textGridHeight", Integer.toString(textGridHeight));
        node.setAttribute("characterCellWidth", Integer.toString(characterCellWidth));
        node.setAttribute("characterCellHeight", Integer.toString(characterCellHeight));
        node.setAttribute("textForegroundColor", Integer.toString(textForegroundColor));
        node.setAttribute("textBackgroundColor", Integer.toString(textBackgroundColor));
        node.setAttribute("text", toISO8859(text));
        root.appendChild(node);
    }
    // Application extensions
    int numAppExtensions = applicationIDs == null ? 0 : applicationIDs.size();
    if (numAppExtensions > 0) {
        node = new IIOMetadataNode("ApplicationExtensions");
        for (int i = 0; i < numAppExtensions; i++) {
            IIOMetadataNode appExtNode = new IIOMetadataNode("ApplicationExtension");
            byte[] applicationID = (byte[]) applicationIDs.get(i);
            appExtNode.setAttribute("applicationID", toISO8859(applicationID));
            byte[] authenticationCode = (byte[]) authenticationCodes.get(i);
            appExtNode.setAttribute("authenticationCode", toISO8859(authenticationCode));
            byte[] appData = (byte[]) applicationData.get(i);
            appExtNode.setUserObject((byte[]) appData.clone());
            node.appendChild(appExtNode);
        }
        root.appendChild(node);
    }
    // Comment extensions
    int numComments = comments == null ? 0 : comments.size();
    if (numComments > 0) {
        node = new IIOMetadataNode("CommentExtensions");
        for (int i = 0; i < numComments; i++) {
            IIOMetadataNode commentNode = new IIOMetadataNode("CommentExtension");
            byte[] comment = (byte[]) comments.get(i);
            commentNode.setAttribute("value", toISO8859(comment));
            node.appendChild(commentNode);
        }
        root.appendChild(node);
    }
    return root;
}
Also used : IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Example 10 with IIOMetadataNode

use of javax.imageio.metadata.IIOMetadataNode in project jdk8u_jdk by JetBrains.

the class GIFImageMetadata method getStandardDimensionNode.

public IIOMetadataNode getStandardDimensionNode() {
    IIOMetadataNode dimension_node = new IIOMetadataNode("Dimension");
    // scratch node
    IIOMetadataNode node = null;
    // PixelAspectRatio not in image
    node = new IIOMetadataNode("ImageOrientation");
    node.setAttribute("value", "Normal");
    dimension_node.appendChild(node);
    // HorizontalPixelSize not in format
    // VerticalPixelSize not in format
    // HorizontalPhysicalPixelSpacing not in format
    // VerticalPhysicalPixelSpacing not in format
    // HorizontalPosition not in format
    // VerticalPosition not in format
    node = new IIOMetadataNode("HorizontalPixelOffset");
    node.setAttribute("value", Integer.toString(imageLeftPosition));
    dimension_node.appendChild(node);
    node = new IIOMetadataNode("VerticalPixelOffset");
    node.setAttribute("value", Integer.toString(imageTopPosition));
    dimension_node.appendChild(node);
    return dimension_node;
}
Also used : IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode)

Aggregations

IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)65 Iterator (java.util.Iterator)5 IIOImage (javax.imageio.IIOImage)4 IIOMetadata (javax.imageio.metadata.IIOMetadata)4 Node (org.w3c.dom.Node)4 BufferedImage (java.awt.image.BufferedImage)3 ListIterator (java.util.ListIterator)3 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)3 ImageWriter (javax.imageio.ImageWriter)3 ImageOutputStream (javax.imageio.stream.ImageOutputStream)3 Attr (org.w3c.dom.Attr)3 Graphics (java.awt.Graphics)2 Point (java.awt.Point)2 IndexColorModel (java.awt.image.IndexColorModel)2 ImageReader (javax.imageio.ImageReader)2 Element (org.w3c.dom.Element)2 ProductFlavorContainer (com.android.builder.model.ProductFlavorContainer)1 AndroidModel (com.android.tools.idea.model.AndroidModel)1 ChatPanel (com.glitchcog.fontificator.gui.chat.ChatPanel)1 Module (com.intellij.openapi.module.Module)1