mirror of
https://invent.kde.org/frameworks/kimageformats.git
synced 2025-05-28 00:30:23 -04:00
psd: Fix UB type punning
BUGS: 471829
This commit is contained in:
parent
6559bf8994
commit
491b223c15
@ -42,6 +42,7 @@
|
|||||||
#include <QColorSpace>
|
#include <QColorSpace>
|
||||||
|
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
typedef quint32 uint;
|
typedef quint32 uint;
|
||||||
typedef quint16 ushort;
|
typedef quint16 ushort;
|
||||||
@ -816,10 +817,18 @@ inline quint32 xchg(quint32 v)
|
|||||||
inline float xchg(float v)
|
inline float xchg(float v)
|
||||||
{
|
{
|
||||||
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
|
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
|
||||||
|
# ifdef Q_CC_MSVC
|
||||||
float *pf = &v;
|
float *pf = &v;
|
||||||
quint32 f = xchg(*reinterpret_cast<quint32*>(pf));
|
quint32 f = xchg(*reinterpret_cast<quint32*>(pf));
|
||||||
quint32 *pi = &f;
|
quint32 *pi = &f;
|
||||||
return *reinterpret_cast<float*>(pi);
|
return *reinterpret_cast<float*>(pi);
|
||||||
|
# else
|
||||||
|
quint32 t;
|
||||||
|
std::memcpy(&t, &v, sizeof(quint32));
|
||||||
|
t = xchg(t);
|
||||||
|
std::memcpy(&v, &t, sizeof(quint32));
|
||||||
|
return v;
|
||||||
|
# endif
|
||||||
#else
|
#else
|
||||||
return v; // never tested
|
return v; // never tested
|
||||||
#endif
|
#endif
|
||||||
@ -835,15 +844,13 @@ inline void planarToChunchy(uchar *target, const char *source, qint32 width, qin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T, T min = 0, T max = 1>
|
template<class T>
|
||||||
inline void planarToChunchyFloat(uchar *target, const char *source, qint32 width, qint32 c, qint32 cn)
|
inline void planarToChunchyFloatToUInt16(uchar *target, const char *source, qint32 width, qint32 c, qint32 cn)
|
||||||
{
|
{
|
||||||
auto s = reinterpret_cast<const T*>(source);
|
auto s = reinterpret_cast<const T*>(source);
|
||||||
auto t = reinterpret_cast<quint16*>(target);
|
auto t = reinterpret_cast<quint16*>(target);
|
||||||
for (qint32 x = 0; x < width; ++x) {
|
for (qint32 x = 0; x < width; ++x) {
|
||||||
auto tmp = xchg(s[x]);
|
t[x * cn + c] = quint16(std::min(xchg(s[x]) * std::numeric_limits<quint16>::max() + 0.5, double(std::numeric_limits<quint16>::max())));
|
||||||
auto ftmp = (*reinterpret_cast<float*>(&tmp) - double(min)) / (double(max) - double(min));
|
|
||||||
t[x * cn + c] = quint16(std::min(ftmp * std::numeric_limits<quint16>::max() + 0.5, double(std::numeric_limits<quint16>::max())));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1238,7 +1245,7 @@ static bool LoadPSD(QDataStream &stream, const PSDHeader &header, QImage &img)
|
|||||||
planarToChunchy<float>(scanLine, rawStride.data(), header.width, c, imgChannels);
|
planarToChunchy<float>(scanLine, rawStride.data(), header.width, c, imgChannels);
|
||||||
}
|
}
|
||||||
else if (header.depth == 32 && header.color_mode == CM_GRAYSCALE) { // 32-bits float images: Grayscale (coverted to equivalent integer 16-bits)
|
else if (header.depth == 32 && header.color_mode == CM_GRAYSCALE) { // 32-bits float images: Grayscale (coverted to equivalent integer 16-bits)
|
||||||
planarToChunchyFloat<quint32>(scanLine, rawStride.data(), header.width, c, imgChannels);
|
planarToChunchyFloatToUInt16<float>(scanLine, rawStride.data(), header.width, c, imgChannels);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user