generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for animated GIF using ImageIO
- Loading branch information
Showing
22 changed files
with
432 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/client/java/net/vinrobot/mcemote/client/imageio/BufferedFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package net.vinrobot.mcemote.client.imageio; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.time.Duration; | ||
|
||
public record BufferedFrame( | ||
BufferedImage image, | ||
Duration duration | ||
) { | ||
NativeFrame toNativeFrame() { | ||
return new NativeFrame(NativeImageHelper.fromBufferedImage(image), duration); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/client/java/net/vinrobot/mcemote/client/imageio/NativeFrame.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package net.vinrobot.mcemote.client.imageio; | ||
|
||
import net.minecraft.client.texture.NativeImage; | ||
|
||
import java.time.Duration; | ||
|
||
public record NativeFrame( | ||
NativeImage image, | ||
Duration duration | ||
) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/client/java/net/vinrobot/mcemote/client/imageio/NativeImageIO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package net.vinrobot.mcemote.client.imageio; | ||
|
||
import net.vinrobot.mcemote.client.imageio.plugins.gif.GifReader; | ||
import net.vinrobot.mcemote.client.imageio.plugins.webp.WebPReader; | ||
|
||
import javax.imageio.IIOException; | ||
import javax.imageio.ImageIO; | ||
import javax.imageio.ImageReader; | ||
import javax.imageio.stream.ImageInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.time.Duration; | ||
import java.util.List; | ||
|
||
public class NativeImageIO { | ||
public static NativeFrame[] readAll(final URL url) throws IOException { | ||
try (final InputStream input = url.openStream()) { | ||
return readAll(input); | ||
} | ||
} | ||
|
||
public static NativeFrame[] readAll(final InputStream input) throws IOException { | ||
return readBufferedFrames(input).stream().map(BufferedFrame::toNativeFrame).toArray(NativeFrame[]::new); | ||
} | ||
|
||
private static List<BufferedFrame> readBufferedFrames(final InputStream input) throws IOException { | ||
try (final ImageInputStream stream = ImageIO.createImageInputStream(input)) { | ||
if (stream == null) { | ||
throw new IIOException("Can't create an ImageInputStream!"); | ||
} | ||
|
||
final ImageReader reader = ImageIO.getImageReaders(stream).next(); | ||
try { | ||
reader.setInput(stream, true, false); | ||
return switch (reader.getFormatName()) { | ||
case "gif" -> GifReader.readFrames(reader); | ||
case "webp" -> WebPReader.readFrames(reader); | ||
default -> List.of(new BufferedFrame(reader.read(0), Duration.ofDays(1))); | ||
}; | ||
} finally { | ||
reader.dispose(); | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/client/java/net/vinrobot/mcemote/client/imageio/plugins/gif/DisposalMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package net.vinrobot.mcemote.client.imageio.plugins.gif; | ||
|
||
public enum DisposalMethod { | ||
RESTORE_TO_BACKGROUND("restoreToBackgroundColor"), | ||
RESTORE_TO_PREVIOUS("restoreToPrevious"), | ||
DO_NOT_DISPOSE("doNotDispose"), | ||
NONE("none"), | ||
UNSPECIFIED(null); | ||
|
||
private final String identifier; | ||
|
||
DisposalMethod(final String identifier) { | ||
this.identifier = identifier; | ||
} | ||
|
||
public static DisposalMethod getByIdentifier(String identifier) { | ||
for (DisposalMethod method : values()) { | ||
if (method.identifier.equals(identifier)) { | ||
return method; | ||
} | ||
} | ||
return UNSPECIFIED; | ||
} | ||
} |
Oops, something went wrong.