From b5c713fef005d886b10a27c72e445db04ac3ece9 Mon Sep 17 00:00:00 2001 From: soir20 <71418127+soir20@users.noreply.github.com> Date: Mon, 11 Nov 2024 01:04:08 -0500 Subject: [PATCH] Fix frames being read out of bounds --- .../metadata/AnimationMetadataAnalyzer.java | 10 ++++++---- .../metadata/AnimationMetadataAnalyzerTest.java | 3 ++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/common/src/main/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzer.java b/common/src/main/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzer.java index ecf1af6..ce09cac 100644 --- a/common/src/main/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzer.java +++ b/common/src/main/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzer.java @@ -225,10 +225,12 @@ private AnimationMetadata readAnimationProperties(MetadataView metadata, int ima private List findFrames(NativeImage texture, int frameWidth, int frameHeight) { List frames = new ArrayList<>(); - for (int minY = 0; minY < texture.getHeight(); minY += frameHeight) { - for (int minX = 0; minX < texture.getWidth(); minX += frameWidth) { - int finalMinX = minX; - int finalMinY = minY; + int framesY = texture.getHeight() / frameHeight; + int framesX = texture.getWidth() / frameWidth; + for (int frameY = 0; frameY < framesY; frameY++) { + for (int frameX = 0; frameX < framesX; frameX++) { + int finalMinX = frameX * frameWidth; + int finalMinY = frameY * frameHeight; frames.add((x, y) -> texture.getPixelRGBA(finalMinX + x, finalMinY + y)); } } diff --git a/common/src/test/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzerTest.java b/common/src/test/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzerTest.java index fad546f..c3f5084 100644 --- a/common/src/test/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzerTest.java +++ b/common/src/test/java/io/github/moremcmeta/animationplugin/metadata/AnimationMetadataAnalyzerTest.java @@ -1066,7 +1066,7 @@ public void analyze_HasPartsFrameWidthNotMultipleOfPartSize_InvalidMetadataExcep "0", new MockMetadataView(ImmutableMap.of( "width", 4, "height", 10, - "texture", MOCK_TEXTURE.apply(10, 20), + "texture", MOCK_TEXTURE.apply(10, 22), "x", 0, "y", 0 )) @@ -1081,6 +1081,7 @@ public void analyze_HasPartsFrameWidthNotMultipleOfPartSize_InvalidMetadataExcep assertEquals(10, metadata.parts().get(0).frameHeight()); assertEquals(0, metadata.parts().get(0).xInBase()); assertEquals(0, metadata.parts().get(0).yInBase()); + assertEquals(4, metadata.parts().get(0).partFrames().orElseThrow().size()); } @Test