Skip to content

Commit

Permalink
Added support for transparent animated GIFs
Browse files Browse the repository at this point in the history
- Updated ImageFile#renderImages()

> Closes #27
  • Loading branch information
josemmo committed Jan 29, 2022
1 parent 3d68399 commit 2364d92
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/io/josemmo/bukkit/plugin/storage/ImageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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());
Expand All @@ -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);
Expand Down

0 comments on commit 2364d92

Please sign in to comment.