Skip to content

Commit

Permalink
PlatformImage: Fix TRANS_MIRROR_ROT90 transform and optimize ROT180
Browse files Browse the repository at this point in the history
There was a commit a while ago fixing the optimized MIRROR_ROT270
transform in a similar manner, so ROT90 being broken was kinda
expected. Also, ROT180 can be optimized a bit further by using
the same half-width inner loop logic from TRANS_MIRROR to cut on
overall loop iterations. Speaking of TRANS_MIRROR, the "tempPixel"
variable wasn't actually needed there, so it was removed.
  • Loading branch information
AShiningRay committed Dec 8, 2024
1 parent 48c8ac4 commit 4b422fc
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/org/recompile/mobile/PlatformImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,22 @@ public static final BufferedImage transformImage(final BufferedImage image, fina
break;

case Sprite.TRANS_ROT180:
/*
* Since this one also has the effect of mirroring the image horizontally like TRANS_MIRROR alongside a
* vertical transformation, we can optimize it by only going up to half of the image's width, making two
* pixel assignments on each inner loop iteration from the image's edges to the center, then checking if
* the width is odd, to just copy the pixel in the middle as it won't change on the transformed image.
*/
for (int y = 0; y < height; y++)
{
int targetPos = (height - 1 - y) * width;
for (int x = 0; x < width; x++)
for (int x = 0; x < width / 2; x++)
{
targetData[targetPos + (width - 1 - x)] = sourceData[y * width + x];
targetData[targetPos + x] = sourceData[y * width + (width - 1 - x)];
}
// If image width is odd, copy the middle pixel directly as there's no need to swap anything.
if (width % 2 != 0) { targetData[targetPos + (width / 2)] = sourceData[y * width + (width / 2)]; }
}
//dumpImage(image, "");
//dumpImage(transimage, "_rot180");
Expand All @@ -340,18 +349,14 @@ public static final BufferedImage transformImage(final BufferedImage image, fina
*
* This transform is such a case. Making operations on columns, and eliminating that inner row loop actually
* results in far worse performance since columns would be accessed way more often. So the next best thing is
* only working on half of the image's width, making two pixel assignments on each inner loop iteration from
* the image's edges to the center, then checking if the width is odd, to just copy the pixel in the middle
* as it won't change.
* only working on half of the image's width, just like TRANS_ROT180.
*/
int tempPixel;
for (int y = 0; y < height; y++)
{
int targetRow = y * width;
for (int x = 0; x < width / 2; x++)
{
tempPixel = sourceData[targetRow + x];
targetData[targetRow + (width - 1 - x)] = tempPixel;
targetData[targetRow + (width - 1 - x)] = sourceData[targetRow + x];
targetData[targetRow + x] = sourceData[targetRow + (width - 1 - x)];
}

Expand All @@ -365,12 +370,12 @@ public static final BufferedImage transformImage(final BufferedImage image, fina
case Sprite.TRANS_MIRROR_ROT90:
for (int y = 0; y < height; y++)
{
int targetPos = (height - 1 - y) * width;
int targetRow = height - 1 - y;
for (int x = 0; x < width; x++)
{
targetData[targetPos + (width - 1 - x)] = sourceData[y * width + x];
targetData[x * height + targetRow] = sourceData[y * width + x];
}
}
}
//dumpImage(image, "");
//dumpImage(transimage, "_mirror90");
break;
Expand Down

0 comments on commit 4b422fc

Please sign in to comment.