Krita Source Code Documentation
Loading...
Searching...
No Matches
RGBE Namespace Reference

Functions

QByteArray floatToRGBE (const int width, const int height, KisPaintDeviceSP &dev)
 
void writeBytesRLE (QByteArray &rleBuffer, quint8 *data, int nBytes)
 

Function Documentation

◆ floatToRGBE()

QByteArray RGBE::floatToRGBE ( const int width,
const int height,
KisPaintDeviceSP & dev )
inline

Definition at line 44 of file RGBEExport.cpp.

45{
46 KisSequentialConstIterator it(dev, {0, 0, width, height});
47 QByteArray res;
48 res.resize(width * height * 4);
49
50 quint8 rgbe[4] = {0, 0, 0, 0};
51
52 quint8 *ptr = reinterpret_cast<quint8 *>(res.data());
53 while (it.nextPixel()) {
54 auto *src = reinterpret_cast<const float *>(it.rawDataConst());
55 auto *dst = reinterpret_cast<quint8 *>(ptr);
56 float vMax = std::max(src[2], std::max(src[0], src[1]));
57 int exp;
58
59 if (vMax < 1e-32) {
60 rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0;
61 } else {
62 vMax = frexp(vMax, &exp) * 256.0f / vMax;
63 // Clamp negative values
64 rgbe[0] = static_cast<quint8>(std::max(src[0], 0.0f) * vMax);
65 rgbe[1] = static_cast<quint8>(std::max(src[1], 0.0f) * vMax);
66 rgbe[2] = static_cast<quint8>(std::max(src[2], 0.0f) * vMax);
67 rgbe[3] = static_cast<quint8>(exp + 128);
68 }
69
70 std::memcpy(dst, rgbe, 4);
71
72 ptr += 4;
73 }
74
75 return res;
76}

References floatToRGBE().

◆ writeBytesRLE()

void RGBE::writeBytesRLE ( QByteArray & rleBuffer,
quint8 * data,
int nBytes )
inline

Definition at line 78 of file RGBEExport.cpp.

79{
80 static constexpr int minRunLen = 4;
81 int cur = 0;
82 int begRun;
83 int runCount;
84 int oldRunCount;
85 int nonRunCount;
86
87 quint8 buf[2];
88
89 while (cur < nBytes) {
90 begRun = cur;
91
92 runCount = oldRunCount = 0;
93 while ((runCount < minRunLen) && (begRun < nBytes)) {
94 begRun += runCount;
95 oldRunCount = runCount;
96 runCount = 1;
97 while ((begRun + runCount < nBytes) && (runCount < 127) && (data[begRun] == data[begRun + runCount])) {
98 runCount++;
99 }
100 }
101
102 if ((oldRunCount > 1) && (oldRunCount == begRun - cur)) {
103 buf[0] = 128 + oldRunCount;
104 buf[1] = data[cur];
105 rleBuffer.append(reinterpret_cast<const char *>(buf), sizeof(buf));
106 cur = begRun;
107 }
108
109 while (cur < begRun) {
110 nonRunCount = begRun - cur;
111 if (nonRunCount > 128) {
112 nonRunCount = 128;
113 }
114 buf[0] = nonRunCount;
115 rleBuffer.append(buf[0]);
116 rleBuffer.append(reinterpret_cast<const char *>(data + cur), sizeof(data[0]) * nonRunCount);
117 cur += nonRunCount;
118 }
119
120 if (runCount >= minRunLen) {
121 buf[0] = 128 + runCount;
122 buf[1] = data[begRun];
123 rleBuffer.append(reinterpret_cast<const char *>(buf), sizeof(buf));
124 cur += runCount;
125 }
126 }
127}

References writeBytesRLE().