From 2364d92184a2abf61ba4c49543e1e7fcead44f3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Miguel=20Moreno?= Date: Sat, 29 Jan 2022 11:20:45 +0100 Subject: [PATCH] Added support for transparent animated GIFs - Updated ImageFile#renderImages() > Closes #27 --- .../io/josemmo/bukkit/plugin/storage/ImageFile.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/io/josemmo/bukkit/plugin/storage/ImageFile.java b/src/main/java/io/josemmo/bukkit/plugin/storage/ImageFile.java index 40f038c..a0431d5 100644 --- a/src/main/java/io/josemmo/bukkit/plugin/storage/ImageFile.java +++ b/src/main/java/io/josemmo/bukkit/plugin/storage/ImageFile.java @@ -87,10 +87,12 @@ protected ImageFile(@NotNull String name, @NotNull String path) { int originalHeight = reader.getHeight(0); BufferedImage tmpImage = new BufferedImage(originalWidth, originalHeight, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D tmpGraphics = tmpImage.createGraphics(); + tmpGraphics.setBackground(new Color(0, 0, 0, 0)); // Create temporary scaled canvas (for resizing) BufferedImage tmpScaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D tmpScaledGraphics = tmpScaledImage.createGraphics(); + tmpScaledGraphics.setBackground(new Color(0, 0, 0, 0)); // Read images from file byte[][] renderedImages = new byte[numOfSteps][width*height]; @@ -99,6 +101,7 @@ protected ImageFile(@NotNull String name, @NotNull String path) { // Extract step metadata int imageLeft = 0; int imageTop = 0; + boolean disposePrevious = false; if (format.equalsIgnoreCase("gif")) { IIOMetadata metadata = reader.getImageMetadata(step); IIOMetadataNode metadataRoot = (IIOMetadataNode) metadata.getAsTree(metadata.getNativeMetadataFormatName()); @@ -112,10 +115,17 @@ protected ImageFile(@NotNull String name, @NotNull String path) { IIOMetadataNode controlExtensionNode = (IIOMetadataNode) metadataRoot.item(i); int delay = Integer.parseInt(controlExtensionNode.getAttribute("delayTime")); delays.compute(delay, (__, count) -> (count == null) ? 1 : count+1); + disposePrevious = controlExtensionNode.getAttribute("disposalMethod").startsWith("restore"); } } } + // Clear temporary canvases (if needed) + if (disposePrevious) { + tmpGraphics.clearRect(0, 0, originalWidth, originalHeight); + tmpScaledGraphics.clearRect(0, 0, width, height); + } + // Paint step image over temporary canvas BufferedImage image = reader.read(step); tmpGraphics.drawImage(image, imageLeft, imageTop, null);