From 3a4eebddc949f4b2a94ae8c35115d1b9d55b94cd Mon Sep 17 00:00:00 2001 From: Vlad Zahorodnii Date: Tue, 19 Dec 2023 14:16:28 +0200 Subject: [PATCH] core: Rework OutputTransform::combine() The naive way to combine output transforms is not great, especially because of the for loop. The output transform kind enums have the following bit pattern: - rotate-0: 000 - rotate-90: 001 - rotate-180: 010 - rotate-270: 011 - flip-0: 100 - flip-90: 101 - flip-180: 110 - flip-270: 111 `(rotate-a + rotate-b) & 0x3` can be used to combine two rotation transforms. The rotatation is clockwise. If the first transform is a flip transform, then we should rotate counter-clockwise, i.e. `(rotate-a - rotate-b) & 0x3`. --- src/core/output.cpp | 87 ++++----------------------------------------- 1 file changed, 7 insertions(+), 80 deletions(-) diff --git a/src/core/output.cpp b/src/core/output.cpp index e2ec638aa3..f0ea308987 100644 --- a/src/core/output.cpp +++ b/src/core/output.cpp @@ -158,87 +158,14 @@ QSizeF OutputTransform::map(const QSizeF &size) const OutputTransform OutputTransform::combine(OutputTransform other) const { - int rotations = 0; - switch (other.kind()) { - case Kind::Normal: - case Kind::Flipped: - rotations = 0; - break; - case Kind::Rotated90: - case Kind::Flipped90: - rotations = 1; - break; - case Kind::Rotated180: - case Kind::Flipped180: - rotations = 2; - break; - case Kind::Rotated270: - case Kind::Flipped270: - rotations = 3; - break; - } - OutputTransform ret = m_kind; - for (int i = 0; i < rotations; i++) { - switch (ret.kind()) { - case Kind::Normal: - ret = Kind::Rotated90; - break; - case Kind::Rotated90: - ret = Kind::Rotated180; - break; - case Kind::Rotated180: - ret = Kind::Rotated270; - break; - case Kind::Rotated270: - ret = Kind::Normal; - break; - case Kind::Flipped: - ret = Kind::Flipped270; - break; - case Kind::Flipped90: - ret = Kind::Flipped; - break; - case Kind::Flipped180: - ret = Kind::Flipped90; - break; - case Kind::Flipped270: - ret = Kind::Flipped180; - break; - } - } - const bool otherFlipped = other.kind() == OutputTransform::Kind::Flipped - || other.kind() == OutputTransform::Kind::Flipped90 - || other.kind() == OutputTransform::Kind::Flipped180 - || other.kind() == OutputTransform::Kind::Flipped270; - if (otherFlipped) { - switch (ret.kind()) { - case Kind::Normal: - ret = Kind::Flipped; - break; - case Kind::Rotated90: - ret = Kind::Flipped90; - break; - case Kind::Rotated180: - ret = Kind::Flipped180; - break; - case Kind::Rotated270: - ret = Kind::Flipped270; - break; - case Kind::Flipped: - ret = Kind::Normal; - break; - case Kind::Flipped90: - ret = Kind::Rotated90; - break; - case Kind::Flipped180: - ret = Kind::Rotated180; - break; - case Kind::Flipped270: - ret = Kind::Rotated270; - break; - } + const int flip = (m_kind ^ other.m_kind) & 0x4; + int rotate; + if (m_kind & 0x4) { + rotate = (m_kind - other.m_kind) & 0x3; // rotate counter-clockwise if flipped + } else { + rotate = (m_kind + other.m_kind) & 0x3; } - return ret; + return OutputTransform(Kind(flip | rotate)); } Output::Output(QObject *parent)