QColorVector: make the (QPointF) ctor a named one

Instead of an explicit ctor, make conversion from QPointF an
explicitly-named ctor.

This prepares the class for being converted to a pure struct,
alleviating its use in arrays without the additional QUninitialized
kludge that Coverity doesn't seem to understand.

Amends 78a7e54f8f5c4ca6ce1ee6b0ac82c42b21738ac5.

As a drive-by, take the QPointF by value, fixing
clazy-function-args-by-value.

Coverity-Id: 444249
Coverity-Id: 425860
Change-Id: I925e94b21bf041a6fb03c56ef9a2da85d8285982
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
Marc Mutz 2024-03-26 10:28:24 +01:00 committed by Allan Sandfeld Jensen
parent 447a82ebe4
commit 01ee796b96
2 changed files with 20 additions and 22 deletions

View File

@ -29,11 +29,8 @@ class QColorVector
public: public:
QColorVector() = default; QColorVector() = default;
constexpr QColorVector(float x, float y, float z, float w = 0.0f) noexcept : x(x), y(y), z(z), w(w) { } constexpr QColorVector(float x, float y, float z, float w = 0.0f) noexcept : x(x), y(y), z(z), w(w) { }
explicit constexpr QColorVector(const QPointF &chr) // from XY chromaticity static constexpr QColorVector fromXYChromaticity(QPointF chr)
: x(chr.x() / chr.y()) { return {float(chr.x() / chr.y()), 1.0f, float((1.0f - chr.x() - chr.y()) / chr.y())}; }
, y(1.0f)
, z((1.0f - chr.x() - chr.y()) / chr.y())
{ }
float x = 0.0f; // X, x, L, or red/cyan float x = 0.0f; // X, x, L, or red/cyan
float y = 0.0f; // Y, y, a, or green/magenta float y = 0.0f; // Y, y, a, or green/magenta
float z = 0.0f; // Z, Y, b, or blue/yellow float z = 0.0f; // Z, Y, b, or blue/yellow
@ -75,8 +72,8 @@ public:
// Common whitepoints: // Common whitepoints:
static constexpr QPointF D50Chromaticity() { return QPointF(0.34567, 0.35850); } static constexpr QPointF D50Chromaticity() { return QPointF(0.34567, 0.35850); }
static constexpr QPointF D65Chromaticity() { return QPointF(0.31271, 0.32902); } static constexpr QPointF D65Chromaticity() { return QPointF(0.31271, 0.32902); }
static constexpr QColorVector D50() { return QColorVector(D50Chromaticity()); } static constexpr QColorVector D50() { return fromXYChromaticity(D50Chromaticity()); }
static constexpr QColorVector D65() { return QColorVector(D65Chromaticity()); } static constexpr QColorVector D65() { return fromXYChromaticity(D65Chromaticity()); }
QColorVector xyzToLab() const QColorVector xyzToLab() const
{ {

View File

@ -81,14 +81,14 @@ bool QColorSpacePrimaries::areValid() const
QColorMatrix QColorSpacePrimaries::toXyzMatrix() const QColorMatrix QColorSpacePrimaries::toXyzMatrix() const
{ {
// This converts to XYZ in some undefined scale. // This converts to XYZ in some undefined scale.
QColorMatrix toXyz = { QColorVector(redPoint), QColorMatrix toXyz = { QColorVector::fromXYChromaticity(redPoint),
QColorVector(greenPoint), QColorVector::fromXYChromaticity(greenPoint),
QColorVector(bluePoint) }; QColorVector::fromXYChromaticity(bluePoint) };
// Since the white point should be (1.0, 1.0, 1.0) in the // Since the white point should be (1.0, 1.0, 1.0) in the
// input, we can figure out the scale by using the // input, we can figure out the scale by using the
// inverse conversion on the white point. // inverse conversion on the white point.
QColorVector wXyz(whitePoint); const auto wXyz = QColorVector::fromXYChromaticity(whitePoint);
QColorVector whiteScale = toXyz.inverted().map(wXyz); QColorVector whiteScale = toXyz.inverted().map(wXyz);
// Now we have scaled conversion to XYZ relative to the given whitepoint // Now we have scaled conversion to XYZ relative to the given whitepoint
@ -155,7 +155,7 @@ QColorSpacePrivate::QColorSpacePrivate(const QColorSpacePrimaries &primaries,
, transferFunction(transferFunction) , transferFunction(transferFunction)
, colorModel(QColorSpace::ColorModel::Rgb) , colorModel(QColorSpace::ColorModel::Rgb)
, gamma(gamma) , gamma(gamma)
, whitePoint(primaries.whitePoint) , whitePoint(QColorVector::fromXYChromaticity(primaries.whitePoint))
{ {
Q_ASSERT(primaries.areValid()); Q_ASSERT(primaries.areValid());
toXyz = primaries.toXyzMatrix(); toXyz = primaries.toXyzMatrix();
@ -173,7 +173,7 @@ QColorSpacePrivate::QColorSpacePrivate(const QPointF &whitePoint,
, transferFunction(transferFunction) , transferFunction(transferFunction)
, colorModel(QColorSpace::ColorModel::Gray) , colorModel(QColorSpace::ColorModel::Gray)
, gamma(gamma) , gamma(gamma)
, whitePoint(whitePoint) , whitePoint(QColorVector::fromXYChromaticity(whitePoint))
{ {
chad = QColorMatrix::chromaticAdaptation(this->whitePoint); chad = QColorMatrix::chromaticAdaptation(this->whitePoint);
toXyz = chad; toXyz = chad;
@ -185,7 +185,7 @@ QColorSpacePrivate::QColorSpacePrivate(const QPointF &whitePoint, const QList<ui
, transferFunction(QColorSpace::TransferFunction::Custom) , transferFunction(QColorSpace::TransferFunction::Custom)
, colorModel(QColorSpace::ColorModel::Gray) , colorModel(QColorSpace::ColorModel::Gray)
, gamma(0) , gamma(0)
, whitePoint(whitePoint) , whitePoint(QColorVector::fromXYChromaticity(whitePoint))
{ {
chad = QColorMatrix::chromaticAdaptation(this->whitePoint); chad = QColorMatrix::chromaticAdaptation(this->whitePoint);
toXyz = chad; toXyz = chad;
@ -209,7 +209,7 @@ QColorSpacePrivate::QColorSpacePrivate(const QColorSpacePrimaries &primaries, co
, transferFunction(QColorSpace::TransferFunction::Custom) , transferFunction(QColorSpace::TransferFunction::Custom)
, colorModel(QColorSpace::ColorModel::Rgb) , colorModel(QColorSpace::ColorModel::Rgb)
, gamma(0) , gamma(0)
, whitePoint(primaries.whitePoint) , whitePoint(QColorVector::fromXYChromaticity(primaries.whitePoint))
{ {
Q_ASSERT(primaries.areValid()); Q_ASSERT(primaries.areValid());
toXyz = primaries.toXyzMatrix(); toXyz = primaries.toXyzMatrix();
@ -231,7 +231,7 @@ QColorSpacePrivate::QColorSpacePrivate(const QColorSpacePrimaries &primaries,
{ {
Q_ASSERT(primaries.areValid()); Q_ASSERT(primaries.areValid());
toXyz = primaries.toXyzMatrix(); toXyz = primaries.toXyzMatrix();
whitePoint = QColorVector(primaries.whitePoint); whitePoint = QColorVector::fromXYChromaticity(primaries.whitePoint);
chad = QColorMatrix::chromaticAdaptation(whitePoint); chad = QColorMatrix::chromaticAdaptation(whitePoint);
toXyz = chad * toXyz; toXyz = chad * toXyz;
setTransferFunctionTables(redTransferFunctionTable, setTransferFunctionTables(redTransferFunctionTable,
@ -314,7 +314,7 @@ void QColorSpacePrivate::setToXyzMatrix()
} }
QColorSpacePrimaries colorSpacePrimaries(primaries); QColorSpacePrimaries colorSpacePrimaries(primaries);
toXyz = colorSpacePrimaries.toXyzMatrix(); toXyz = colorSpacePrimaries.toXyzMatrix();
whitePoint = QColorVector(colorSpacePrimaries.whitePoint); whitePoint = QColorVector::fromXYChromaticity(colorSpacePrimaries.whitePoint);
chad = QColorMatrix::chromaticAdaptation(whitePoint); chad = QColorMatrix::chromaticAdaptation(whitePoint);
toXyz = chad * toXyz; toXyz = chad * toXyz;
} }
@ -940,9 +940,10 @@ void QColorSpace::setPrimaries(const QPointF &whitePoint, const QPointF &redPoin
return; return;
} }
QColorMatrix toXyz = primaries.toXyzMatrix(); QColorMatrix toXyz = primaries.toXyzMatrix();
QColorMatrix chad = QColorMatrix::chromaticAdaptation(QColorVector(whitePoint)); QColorMatrix chad = QColorMatrix::chromaticAdaptation(QColorVector::fromXYChromaticity(whitePoint));
toXyz = chad * toXyz; toXyz = chad * toXyz;
if (QColorVector(primaries.whitePoint) == d_ptr->whitePoint && toXyz == d_ptr->toXyz && chad == d_ptr->chad) if (QColorVector::fromXYChromaticity(primaries.whitePoint) == d_ptr->whitePoint
&& toXyz == d_ptr->toXyz && chad == d_ptr->chad)
return; return;
detach(); detach();
if (d_ptr->transformModel == TransformModel::ElementListProcessing) if (d_ptr->transformModel == TransformModel::ElementListProcessing)
@ -953,7 +954,7 @@ void QColorSpace::setPrimaries(const QPointF &whitePoint, const QPointF &redPoin
d_ptr->colorModel = QColorSpace::ColorModel::Rgb; d_ptr->colorModel = QColorSpace::ColorModel::Rgb;
d_ptr->toXyz = toXyz; d_ptr->toXyz = toXyz;
d_ptr->chad = chad; d_ptr->chad = chad;
d_ptr->whitePoint = QColorVector(primaries.whitePoint); d_ptr->whitePoint = QColorVector::fromXYChromaticity(primaries.whitePoint);
d_ptr->identifyColorSpace(); d_ptr->identifyColorSpace();
} }
@ -980,7 +981,7 @@ void QColorSpace::setWhitePoint(const QPointF &whitePoint)
d_ptr = new QColorSpacePrivate(whitePoint, TransferFunction::Custom, 0.0f); d_ptr = new QColorSpacePrivate(whitePoint, TransferFunction::Custom, 0.0f);
return; return;
} }
if (QColorVector(whitePoint) == d_ptr->whitePoint) if (QColorVector::fromXYChromaticity(whitePoint) == d_ptr->whitePoint)
return; return;
detach(); detach();
if (d_ptr->transformModel == TransformModel::ElementListProcessing) if (d_ptr->transformModel == TransformModel::ElementListProcessing)
@ -991,7 +992,7 @@ void QColorSpace::setWhitePoint(const QPointF &whitePoint)
// An RGB color model stays RGB, a gray stays gray, but an undefined one can now be considered gray // An RGB color model stays RGB, a gray stays gray, but an undefined one can now be considered gray
if (d_ptr->colorModel == QColorSpace::ColorModel::Undefined) if (d_ptr->colorModel == QColorSpace::ColorModel::Undefined)
d_ptr->colorModel = QColorSpace::ColorModel::Gray; d_ptr->colorModel = QColorSpace::ColorModel::Gray;
QColorVector wXyz(whitePoint); QColorVector wXyz(QColorVector::fromXYChromaticity(whitePoint));
if (d_ptr->transformModel == QColorSpace::TransformModel::ThreeComponentMatrix) { if (d_ptr->transformModel == QColorSpace::TransformModel::ThreeComponentMatrix) {
if (d_ptr->colorModel == QColorSpace::ColorModel::Rgb) { if (d_ptr->colorModel == QColorSpace::ColorModel::Rgb) {
// Rescale toXyz to new whitepoint // Rescale toXyz to new whitepoint