Fix rotation of images with odd number of rows (#6926)

* Fix line in middle of 180 degree rotated image

For images with an odd number of rows, a 180 degree rotation resulted in
one row of un-rotated pixels because it was effectively rotated twice.

* Replace verbose swap with std::swap
This commit is contained in:
Lawrence37 2024-02-04 15:40:38 -08:00 committed by GitHub
parent 0d0834cbe7
commit 2f7c2e973c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -18,6 +18,7 @@
*/ */
#pragma once #pragma once
#include <utility>
#include <vector> #include <vector>
#include <lcms2.h> #include <lcms2.h>
@ -396,7 +397,7 @@ public:
swap(rotatedImg); swap(rotatedImg);
} else if (deg == 180) { } else if (deg == 180) {
int height2 = height / 2 + (height & 1); int height2 = height / 2;
#ifdef _OPENMP #ifdef _OPENMP
// difficult to find a cutoff value where parallelization is counter productive because of processor's data cache collision... // difficult to find a cutoff value where parallelization is counter productive because of processor's data cache collision...
@ -406,13 +407,22 @@ public:
for (int i = 0; i < height2; i++) { for (int i = 0; i < height2; i++) {
for (int j = 0; j < width; j++) { for (int j = 0; j < width; j++) {
T tmp;
int x = width - 1 - j; int x = width - 1 - j;
int y = height - 1 - i; int y = height - 1 - i;
tmp = v(i, j); std::swap(v(i, j), v(y, x));
v(i, j) = v(y, x); }
v(y, x) = tmp; }
// Middle row of odd-height images: only go half way otherwise the
// pixels will be swapped twice.
if (height & 1) {
int i = height / 2;
int width2 = width / 2;
for (int j = 0; j < width2; j++) {
int x = width - 1 - j;
std::swap(v(i, j), v(i, x));
} }
} }
#ifdef _OPENMP #ifdef _OPENMP
@ -828,7 +838,7 @@ public:
swap(rotatedImg); swap(rotatedImg);
} else if (deg == 180) { } else if (deg == 180) {
int height2 = height / 2 + (height & 1); int height2 = height / 2;
#ifdef _OPENMP #ifdef _OPENMP
// difficult to find a cutoff value where parallelization is counter productive because of processor's data cache collision... // difficult to find a cutoff value where parallelization is counter productive because of processor's data cache collision...
@ -838,21 +848,26 @@ public:
for (int i = 0; i < height2; i++) { for (int i = 0; i < height2; i++) {
for (int j = 0; j < width; j++) { for (int j = 0; j < width; j++) {
T tmp;
int x = width - 1 - j; int x = width - 1 - j;
int y = height - 1 - i; int y = height - 1 - i;
tmp = r(i, j); std::swap(r(i, j), r(y, x));
r(i, j) = r(y, x); std::swap(g(i, j), g(y, x));
r(y, x) = tmp; std::swap(b(i, j), b(y, x));
}
}
tmp = g(i, j); // Middle row of odd-height images: only go half way otherwise the
g(i, j) = g(y, x); // pixels will be swapped twice.
g(y, x) = tmp; if (height & 1) {
int i = height / 2;
int width2 = width / 2;
for (int j = 0; j < width2; j++) {
int x = width - 1 - j;
tmp = b(i, j); std::swap(r(i, j), r(i, x));
b(i, j) = b(y, x); std::swap(g(i, j), g(i, x));
b(y, x) = tmp; std::swap(b(i, j), b(i, x));
} }
} }
#ifdef _OPENMP #ifdef _OPENMP
@ -1481,26 +1496,31 @@ public:
swap(rotatedImg); swap(rotatedImg);
} else if (deg == 180) { } else if (deg == 180) {
int height2 = height / 2 + (height & 1); int height2 = height / 2;
// Maybe not sufficiently optimized, but will do what it has to do // Maybe not sufficiently optimized, but will do what it has to do
for (int i = 0; i < height2; i++) { for (int i = 0; i < height2; i++) {
for (int j = 0; j < width; j++) { for (int j = 0; j < width; j++) {
T tmp;
int x = width - 1 - j; int x = width - 1 - j;
int y = height - 1 - i; int y = height - 1 - i;
tmp = r(i, j); std::swap(r(i, j), r(y, x));
r(i, j) = r(y, x); std::swap(g(i, j), g(y, x));
r(y, x) = tmp; std::swap(b(i, j), b(y, x));
}
}
tmp = g(i, j); // Middle row of odd-height images: only go half way otherwise the
g(i, j) = g(y, x); // pixels will be swapped twice.
g(y, x) = tmp; if (height & 1) {
int i = height / 2;
int width2 = width / 2;
for (int j = 0; j < width2; j++) {
int x = width - 1 - j;
tmp = b(i, j); std::swap(r(i, j), r(i, x));
b(i, j) = b(y, x); std::swap(g(i, j), g(i, x));
b(y, x) = tmp; std::swap(b(i, j), b(i, x));
} }
} }
} }