qRoundOrZero_T: fix possible assert in qRound

also:
- qRoundOrZero_T: added documentation
- EXR: I don't set the ISO Speed ​​if it's zero.

BUG: 524678
This commit is contained in:
Mirco Miranda
2026-08-26 22:45:12 +00:00
committed by Albert Astals Cid
parent 47ea774621
commit a6027264d4
2 changed files with 18 additions and 10 deletions
+2 -1
View File
@@ -368,7 +368,8 @@ static void readMetadata(const Imf::Header &header, QImage &image)
// shot metadata // shot metadata
if (auto isoSpeed = header.findTypedAttribute<Imf::FloatAttribute>("isoSpeed")) { if (auto isoSpeed = header.findTypedAttribute<Imf::FloatAttribute>("isoSpeed")) {
image.setText(QStringLiteral(META_KEY_ISOSPEEDRATINGS), QLocale::c().toString(qRound(isoSpeed->value()))); if (auto v = qRoundOrZero(isoSpeed->value()))
image.setText(QStringLiteral(META_KEY_ISOSPEEDRATINGS), QLocale::c().toString(v));
} }
if (auto expTime = header.findTypedAttribute<Imf::FloatAttribute>("expTime")) { if (auto expTime = header.findTypedAttribute<Imf::FloatAttribute>("expTime")) {
image.setText(QStringLiteral(META_KEY_EXPOSURETIME), QLocale::c().toString(expTime->value())); image.setText(QStringLiteral(META_KEY_EXPOSURETIME), QLocale::c().toString(expTime->value()));
+15 -8
View File
@@ -165,20 +165,27 @@ inline bool checkImageSize(const QSize& size, qint32 bytesPerPixel)
return checkImageSize(size.width(), size.height(), bytesPerPixel); return checkImageSize(size.width(), size.height(), bytesPerPixel);
} }
/*!
* \brief qRoundOrZero_T
* In images, many float values can only be positive (e.g., resolution). This function calculates
* the roundness of the passed value, returning 0 if the value is negative or invalid.
* \return 0 when \a d is negative, NaN, Inf or std::numeric_limits<TI>::max(). Otherwise the qRound of \a d.
*/
template<class TI, class SF> // SF = source FP, TI = target INT template<class TI, class SF> // SF = source FP, TI = target INT
TI qRoundOrZero_T(SF d, bool *ok = nullptr) TI qRoundOrZero_T(SF d, bool *ok = nullptr)
{ {
bool tmp = false;
if (ok == nullptr) {
ok = &tmp;
}
// checks for undefined behavior // checks for undefined behavior
if (qIsNaN(d) || qIsInf(d) || d < SF() || d > SF(std::numeric_limits<TI>::max())) { if (qIsNaN(d) || qIsInf(d) || d < SF()) {
if (ok) {
*ok = false; *ok = false;
} else {
*ok = d < SF(std::numeric_limits<TI>::max());
} }
return 0; return *ok ? qRound(d) : 0;
}
if (ok) {
*ok = true;
}
return qRound(d);
} }
inline qint32 qRoundOrZero(double d, bool *ok = nullptr) inline qint32 qRoundOrZero(double d, bool *ok = nullptr)