Skip to content

Commit

Permalink
Fix the Rotate180 code for in-place rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xC0000054 committed Feb 7, 2022
1 parent e633804 commit 906d0ed
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/Avif Reader/ImageTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,44 @@ internal static unsafe void Rotate90CCW(ref Surface surface)

internal static unsafe void Rotate180(Surface surface)
{
int lastColumn = surface.Width - 1;
int lastRow = surface.Height - 1;
int width = surface.Width;
int height = surface.Height;

for (int y = 0; y < surface.Height; y++)
int halfHeight = height / 2;
int lastColumn = width - 1;

for (int y = 0; y < halfHeight; y++)
{
ColorBgra* topPtr = surface.GetRowPointerUnchecked(y);
ColorBgra* bottomPtr = surface.GetPointPointerUnchecked(lastColumn, height - y - 1);

for (int x = 0; x < width; x++)
{
ColorBgra temp = *bottomPtr;
*bottomPtr = *topPtr;
*topPtr = temp;

topPtr++;
bottomPtr--;
}
}

// The middle row must be handled separately if the height is odd.
if ((height & 1) == 1)
{
for (int x = 0; x < surface.Width; x++)
int halfWidth = width / 2;

ColorBgra* leftPtr = surface.GetRowPointerUnchecked(halfHeight);
ColorBgra* rightPtr = surface.GetPointPointerUnchecked(lastColumn, halfHeight);

for (int x = 0; x < halfWidth; x++)
{
surface[x, y] = surface[lastColumn - x, lastRow - y];
ColorBgra temp = *rightPtr;
*rightPtr = *leftPtr;
*leftPtr = temp;

leftPtr++;
rightPtr--;
}
}
}
Expand Down

0 comments on commit 906d0ed

Please sign in to comment.