Search in sources :

Example 81 with IIOMetadata

use of javax.imageio.metadata.IIOMetadata in project ChatTweaks by blay09.

the class ImageLoader method loadImageInternal.

private static IChatRenderable loadImageInternal(Object obj, @Nullable File saveToFile) throws IOException {
    try (ImageInputStream in = ImageIO.createImageInputStream(obj)) {
        if (in == null) {
            throw new IOException("Failed to load image: input stream is null");
        }
        Iterator<ImageReader> it = ImageIO.getImageReaders(in);
        if (it.hasNext()) {
            ImageReader reader = it.next();
            reader.setInput(in);
            int numImages = reader.getNumImages(true);
            if (numImages > 1) {
                ImageWriter writer = null;
                ImageOutputStream out = null;
                if (saveToFile != null) {
                    out = ImageIO.createImageOutputStream(saveToFile);
                    writer = ImageIO.getImageWriter(reader);
                    writer.setOutput(out);
                }
                int[] frameTime = new int[numImages];
                int[] offsetX = new int[numImages];
                int[] offsetY = new int[numImages];
                BufferedImage[] images = new BufferedImage[numImages];
                for (int i = 0; i < images.length; i++) {
                    images[i] = reader.read(reader.getMinIndex() + i);
                    IIOMetadata metadata = reader.getImageMetadata(i);
                    String metaFormatName = metadata.getNativeMetadataFormatName();
                    if (metaFormatName == null) {
                        throw new IOException("Failed to load image: meta format name is null");
                    }
                    IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(metaFormatName);
                    NodeList childNodes = root.getChildNodes();
                    for (int j = 0; j < childNodes.getLength(); j++) {
                        if (childNodes.item(j).getNodeName().equalsIgnoreCase("GraphicControlExtension")) {
                            frameTime[i] = Integer.parseInt(((IIOMetadataNode) childNodes.item(j)).getAttribute("delayTime")) * 10;
                        }
                        if (childNodes.item(j).getNodeName().equalsIgnoreCase("ImageDescriptor")) {
                            try {
                                offsetX[i] = Integer.parseInt(((IIOMetadataNode) childNodes.item(j)).getAttribute("imageLeftPosition"));
                            } catch (NumberFormatException ignored) {
                            }
                            try {
                                offsetY[i] = Integer.parseInt(((IIOMetadataNode) childNodes.item(j)).getAttribute("imageTopPosition"));
                            } catch (NumberFormatException ignored) {
                            }
                        }
                    }
                    if (writer != null) {
                        ImageReadParam imageReadParam = new ImageReadParam();
                        IIOImage ioImage = reader.readAll(i, imageReadParam);
                        ImageWriteParam imageWriteParam = new ImageWriteParam(Locale.ENGLISH);
                        writer.prepareWriteSequence(metadata);
                        writer.writeToSequence(ioImage, imageWriteParam);
                    }
                }
                if (out != null) {
                    out.close();
                }
                IIOMetadata metadata = reader.getImageMetadata(0);
                String metaFormatName = metadata.getNativeMetadataFormatName();
                IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(metaFormatName);
                NodeList childNodes = root.getChildNodes();
                boolean cumulativeRendering = true;
                for (int i = 0; i < childNodes.getLength(); i++) {
                    if (childNodes.item(i).getNodeName().equalsIgnoreCase("GraphicControlExtension")) {
                        cumulativeRendering = ((IIOMetadataNode) childNodes.item(i)).getAttribute("disposalMethod").equals("doNotDispose");
                        break;
                    }
                }
                AnimatedChatRenderable image = new AnimatedChatRenderable(images, frameTime, offsetX, offsetY);
                image.setCumulativeRendering(cumulativeRendering);
                return image;
            } else {
                BufferedImage image = reader.read(0);
                if (saveToFile != null) {
                    try (ImageOutputStream out = ImageIO.createImageOutputStream(saveToFile)) {
                        ImageWriter writer = ImageIO.getImageWriter(reader);
                        writer.setOutput(out);
                        writer.write(image);
                    }
                }
                return new StaticChatRenderable(image);
            }
        }
    } catch (IllegalArgumentException e) {
        throw new IOException(e);
    }
    return NullRenderable.INSTANCE;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) NodeList(org.w3c.dom.NodeList) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) IIOMetadata(javax.imageio.metadata.IIOMetadata) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Aggregations

IIOMetadata (javax.imageio.metadata.IIOMetadata)81 BufferedImage (java.awt.image.BufferedImage)36 ImageWriter (javax.imageio.ImageWriter)35 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)32 IIOImage (javax.imageio.IIOImage)30 ImageOutputStream (javax.imageio.stream.ImageOutputStream)30 File (java.io.File)27 ImageWriteParam (javax.imageio.ImageWriteParam)22 Node (org.w3c.dom.Node)22 IOException (java.io.IOException)20 ImageReader (javax.imageio.ImageReader)19 IIOMetadataNode (javax.imageio.metadata.IIOMetadataNode)17 Rectangle (java.awt.Rectangle)13 NodeList (org.w3c.dom.NodeList)13 Point (java.awt.Point)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ImageInputStream (javax.imageio.stream.ImageInputStream)10 Test (org.junit.Test)9 ColorModel (java.awt.image.ColorModel)7