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++

Replace qBound() with kisBoundFast() in blendmodes

Dmitry Kazakov155a392c2726
diff --git a/libs/global/kis_global.h b/libs/global/kis_global.h
index d919ca53c5..186e0c3ed2 100644
--- a/libs/global/kis_global.h
+++ b/libs/global/kis_global.h
@@ -33,6 +33,23 @@ const quint8 MAX_SELECTED = std::numeric_limits<quint8>::max();
const quint8 MIN_SELECTED = std::numeric_limits<quint8>::min();
const quint8 SELECTION_THRESHOLD = 1;
+template <typename T>
+constexpr inline const T &kisBoundFast(const T &min, const T &val, const T &max)
+{
+ /**
+ * This is a fork of an old verion of qBound. It has the following properties:
+ * 1) Does **not** have asserts (we cannot have them in blendmodes)
+ * 2) Does not have automatic type deduction. The user must explicitly type the
+ * common type in case of ambiguity.
+ *
+ * Rules of thumb:
+ *
+ * 1) If you are writing time-critical code (e.g. blendmodes), use kisBoundFast()
+ * 2) Otherwise use qBound() or std::clamp (the latter may optionally have an assert as well)
+ */
+ return qMax(min, qMin(max, val));
+}
+
enum OutlineStyle {
OUTLINE_NONE = 0,
OUTLINE_CIRCLE,
diff --git a/libs/pigment/KoColorSpaceMaths.h b/libs/pigment/KoColorSpaceMaths.h
index e78edde9dd..316f817abc 100644
--- a/libs/pigment/KoColorSpaceMaths.h
+++ b/libs/pigment/KoColorSpaceMaths.h
@@ -15,6 +15,8 @@
#include "KoChannelInfo.h"
#include "KoLut.h"
+#include <kis_global.h>
+
#undef _T
/**
@@ -270,7 +272,7 @@ public:
}
inline static dst_compositetype clamp(dst_compositetype val) {
- return qBound<dst_compositetype>(KoColorSpaceMathsTraits<_Tdst>::min, val, KoColorSpaceMathsTraits<_Tdst>::max);
+ return kisBoundFast<dst_compositetype>(KoColorSpaceMathsTraits<_Tdst>::min, val, KoColorSpaceMathsTraits<_Tdst>::max);
}
/**
diff --git a/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.h b/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.h
index 8a7d3b73ef..d695fb36d4 100644
--- a/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.h
+++ b/libs/ui/tool/strokes/KisMaskingBrushCompositeOp.h
@@ -196,9 +196,9 @@ inline T colorDodgeAlphaHelper(T src, T dst)
if (src == unitValue<T>()) {
return dst == zeroValue<T>() ? zeroValue<T>() : unitValue<T>();
}
- return qBound(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),
- div(dst, inv(src)),
- composite_type(KoColorSpaceMathsTraits<T>::unitValue));
+ return kisBoundFast(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),
+ div(dst, inv(src)),
+ composite_type(KoColorSpaceMathsTraits<T>::unitValue));
}
// Integer version of color dodge alpha
@@ -263,9 +263,9 @@ inline T colorBurnAlphaHelper(T src, T dst)
if(src == zeroValue<T>()) {
return dst == unitValue<T>() ? zeroValue<T>() : unitValue<T>();
}
- return qBound(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),
- div(inv(dst), src),
- composite_type(KoColorSpaceMathsTraits<T>::unitValue));
+ return kisBoundFast(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),
+ div(inv(dst), src),
+ composite_type(KoColorSpaceMathsTraits<T>::unitValue));
}
// Integer version of color burn alpha
@@ -479,9 +479,9 @@ inline T hardMixSofterPhotoshopAlpha(T src, T dst) {
typedef typename KoColorSpaceMathsTraits<T>::compositetype composite_type;
const composite_type srcScaleFactor = static_cast<composite_type>(2);
const composite_type dstScaleFactor = static_cast<composite_type>(3);
- return qBound(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),
- dstScaleFactor * dst - srcScaleFactor * inv(src),
- composite_type(KoColorSpaceMathsTraits<T>::unitValue));
+ return kisBoundFast(composite_type(KoColorSpaceMathsTraits<T>::zeroValue),
+ dstScaleFactor * dst - srcScaleFactor * inv(src),
+ composite_type(KoColorSpaceMathsTraits<T>::unitValue));
}
template <typename channels_type>
@@ -580,14 +580,14 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_HEIGHT, true
using composite_type = typename KoColorSpaceMathsTraits<channels_type>::compositetype;
using namespace Arithmetic;
if constexpr (use_soft_texturing) {
- return qBound(composite_type(zeroValue<channels_type>()),
- div(dst, invertedStrength) -
- mul(src, StrengthCompositeFunctionBase<channels_type>::strength),
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ div(dst, invertedStrength) -
+ mul(src, StrengthCompositeFunctionBase<channels_type>::strength),
+ composite_type(unitValue<channels_type>()));
} else {
- return qBound(composite_type(zeroValue<channels_type>()),
- div(dst, invertedStrength) - (composite_type(src) + invertedStrength),
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ div(dst, invertedStrength) - (composite_type(src) + invertedStrength),
+ composite_type(unitValue<channels_type>()));
}
}
};
@@ -613,16 +613,16 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_HEIGH
const channels_type srcTimesStrength = mul(src, StrengthCompositeFunctionBase<channels_type>::strength);
const composite_type multiply = modifiedDst * inv(srcTimesStrength) / unitValue<channels_type>();
const composite_type height = modifiedDst - srcTimesStrength;
- return qBound(composite_type(zeroValue<channels_type>()),
- qMax(multiply, height),
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ qMax(multiply, height),
+ composite_type(unitValue<channels_type>()));
} else {
const composite_type modifiedDst = div(dst, invertedStrength) - invertedStrength;
const composite_type multiply = modifiedDst * inv(src) / unitValue<channels_type>();
const composite_type height = modifiedDst - src;
- return qBound(composite_type(zeroValue<channels_type>()),
- qMax(multiply, height),
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ qMax(multiply, height),
+ composite_type(unitValue<channels_type>()));
}
}
};
@@ -642,9 +642,9 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_HEIGHT_PHOTO
channels_type apply(channels_type src, channels_type dst)
{
using namespace Arithmetic;
- return qBound(composite_type(zeroValue<channels_type>()),
- dst * weight / unitValue<channels_type>() - src,
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ dst * weight / unitValue<channels_type>() - src,
+ composite_type(unitValue<channels_type>()));
}
};
@@ -663,10 +663,10 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_HEIGHT_PHOTO
channels_type apply(channels_type src, channels_type dst)
{
using namespace Arithmetic;
- return qBound(composite_type(zeroValue<channels_type>()),
- composite_type(dst) + dst * weight / unitValue<channels_type>() -
- mul(src, StrengthCompositeFunctionBase<channels_type>::strength),
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ composite_type(dst) + dst * weight / unitValue<channels_type>() -
+ mul(src, StrengthCompositeFunctionBase<channels_type>::strength),
+ composite_type(unitValue<channels_type>()));
}
};
@@ -689,9 +689,9 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_HEIGH
const composite_type modifiedDst = dst * weight / unitValue<channels_type>();
const composite_type multiply = inv(src) * modifiedDst / unitValue<channels_type>();
const composite_type height = modifiedDst - src;
- return qBound(composite_type(zeroValue<channels_type>()),
- qMax(multiply, height),
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ qMax(multiply, height),
+ composite_type(unitValue<channels_type>()));
}
};
@@ -715,9 +715,9 @@ struct CompositeFunction<channels_type, KIS_MASKING_BRUSH_COMPOSITE_LINEAR_HEIGH
const channels_type srcTimesStrength = mul(src, StrengthCompositeFunctionBase<channels_type>::strength);
const composite_type multiply = modifiedDst * inv(srcTimesStrength) / unitValue<channels_type>();
const composite_type height = modifiedDst - srcTimesStrength;
- return qBound(composite_type(zeroValue<channels_type>()),
- qMax(multiply, height),
- composite_type(unitValue<channels_type>()));
+ return kisBoundFast(composite_type(zeroValue<channels_type>()),
+ qMax(multiply, height),
+ composite_type(unitValue<channels_type>()));
}
};