diff options
author | isXander <xandersmith2008@gmail.com> | 2023-05-21 14:36:37 +0100 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2023-05-21 14:36:37 +0100 |
commit | c84415116455d108ad07fc8dd6232c9acc94c40f (patch) | |
tree | 9c6a20b611e45eedeb512b8294f7147575d56af9 /common/src/main/java/dev/isxander/yacl/gui | |
parent | 21afea0da3956f2d8cca81a54fa9820152e0c077 (diff) | |
download | YetAnotherConfigLib-c84415116455d108ad07fc8dd6232c9acc94c40f.tar.gz YetAnotherConfigLib-c84415116455d108ad07fc8dd6232c9acc94c40f.tar.bz2 YetAnotherConfigLib-c84415116455d108ad07fc8dd6232c9acc94c40f.zip |
Automatic WebP frame duration detection
Diffstat (limited to 'common/src/main/java/dev/isxander/yacl/gui')
-rw-r--r-- | common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java b/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java index 8ea8ba3..1376a27 100644 --- a/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java +++ b/common/src/main/java/dev/isxander/yacl/gui/ImageRenderer.java @@ -149,11 +149,11 @@ public interface ImageRenderer extends AutoCloseable { return createGIF(resource.open(), textureLocation); } - public static AnimatedNativeImageBacked createWEBPFromTexture(ResourceLocation textureLocation, int frameDelayMS) throws IOException { + public static AnimatedNativeImageBacked createWEBPFromTexture(ResourceLocation textureLocation) throws IOException { ResourceManager resourceManager = Minecraft.getInstance().getResourceManager(); Resource resource = resourceManager.getResource(textureLocation).orElseThrow(); - return createWEBP(resource.open(), textureLocation, frameDelayMS); + return createWEBP(resource.open(), textureLocation); } public static AnimatedNativeImageBacked createGIF(InputStream is, ResourceLocation uniqueLocation) { @@ -173,10 +173,30 @@ public interface ImageRenderer extends AutoCloseable { } } - public static AnimatedNativeImageBacked createWEBP(InputStream is, ResourceLocation uniqueLocation, int frameDelayMS) { + public static AnimatedNativeImageBacked createWEBP(InputStream is, ResourceLocation uniqueLocation) { try (is) { - ImageReader reader = ImageIO.getImageReadersBySuffix("webp").next(); + ImageReader reader = new WebPImageReaderSpi().createReaderInstance(); reader.setInput(ImageIO.createImageInputStream(is)); + + // WebP reader does not expose frame delay, prepare for reflection hell + int frameDelayMS; + reader.getNumImages(true); // Force reading of all frames + try { + Class<?> webpReaderClass = Class.forName("com.twelvemonkeys.imageio.plugins.webp.WebPImageReader"); + Field framesField = webpReaderClass.getDeclaredField("frames"); + framesField.setAccessible(true); + List<?> frames = (List<?>) framesField.get(reader); + Object firstFrame = frames.get(0); + + Class<?> animationFrameClass = Class.forName("com.twelvemonkeys.imageio.plugins.webp.AnimationFrame"); + Field durationField = animationFrameClass.getDeclaredField("duration"); + durationField.setAccessible(true); + frameDelayMS = (int) durationField.get(firstFrame); + } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } + // that was fun + return createFromImageReader(reader, frameDelayMS, uniqueLocation); } catch (IOException e) { throw new RuntimeException(e); |