Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Remove excessive copying in KoGamutMappingUtils::cuspForHue

Dmitry Kazakov99c4c2226d4d
diff --git a/libs/pigment/KoGamutMappingUtils.cpp b/libs/pigment/KoGamutMappingUtils.cpp
index 0839ea7a41..ac5f4de014 100644
--- a/libs/pigment/KoGamutMappingUtils.cpp
+++ b/libs/pigment/KoGamutMappingUtils.cpp
@@ -338,29 +338,32 @@ QMap<float, QPointF> KoGamutMappingUtils::getGamutCuspDescription(const KoColorP
QPointF KoGamutMappingUtils::cuspForHue(const QMap<float, QPointF> &gamutCuspDescription, const float hue)
{
- QPointF cusp;
-
- int foundAngle = -1;
-
- for (int i = 0; i < gamutCuspDescription.size(); i++) {
- if (hue < gamutCuspDescription.keys().at(i)) {
- break;
- }
- foundAngle = i;
+ if (gamutCuspDescription.isEmpty()) return QPointF();
+
+ float firstAngle = 0.0;
+ float secondAngle = 0.0;
+
+ auto it = gamutCuspDescription.upperBound(hue);
+ if (it == gamutCuspDescription.begin()) {
+ firstAngle = gamutCuspDescription.lastKey();
+ secondAngle = gamutCuspDescription.firstKey();
+ } else if (it == gamutCuspDescription.end()) {
+ firstAngle = gamutCuspDescription.lastKey();
+ secondAngle = gamutCuspDescription.firstKey();
+ } else {
+ firstAngle = std::prev(it).key();
+ secondAngle = it.key();
}
- if (foundAngle < 0) foundAngle = gamutCuspDescription.size()-1;
- const float angle = gamutCuspDescription.keys().at(foundAngle);
- const float nextAngle = gamutCuspDescription.keys().value(foundAngle+1, gamutCuspDescription.keys().first());
+
+ const float angle = firstAngle;
+ const float nextAngle = secondAngle;
QPointF lch1 = gamutCuspDescription.value(angle);
QPointF lch2 = gamutCuspDescription.value(nextAngle);
- float t = (hue - angle) / (nextAngle - angle);
- if (hue < angle) {
- t = (hue - angle + 1.0) / (nextAngle + 1.0 - angle);
- }
- cusp = QLineF(lch1, lch2).pointAt(t);
-
- return cusp;
+ const float t = hue < angle
+ ? (hue - angle + 1.0) / (nextAngle + 1.0 - angle)
+ : (hue - angle) / (nextAngle - angle);
+ return QLineF(lch1, lch2).pointAt(t);
}