diff --git a/src/Avif Reader/ImageTransform.cs b/src/Avif Reader/ImageTransform.cs index da2793e..dd5c524 100644 --- a/src/Avif Reader/ImageTransform.cs +++ b/src/Avif Reader/ImageTransform.cs @@ -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--; } } }