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

Functions

bool LoadHDR (QDataStream &s, const int width, const int height, KisSequentialIterator &it)
 
bool ReadOldLine (quint8 *image, int width, QDataStream &s)
 
void RGBEToPaintDevice (quint8 *image, int width, KisSequentialIterator &it)
 

Function Documentation

◆ LoadHDR()

bool RGBEIMPORT::LoadHDR ( QDataStream & s,
const int width,
const int height,
KisSequentialIterator & it )

Definition at line 89 of file RGBEImportUtils.cpp.

90{
91 quint8 val;
92 quint8 code;
93
94 QByteArray lineArray;
95 lineArray.resize(4 * width);
96 quint8 *image = (quint8 *)lineArray.data();
97
98 for (int cline = 0; cline < height; cline++) {
99 // determine scanline type
100 if ((width < MINELEN) || (MAXELEN < width)) {
101 if (!ReadOldLine(image, width, s)) {
102 return false;
103 }
104 RGBEToPaintDevice(image, width, it);
105 continue;
106 }
107
108 if (s.atEnd()) {
109 return false;
110 }
111
112 s >> val;
113
114 if (val != 2) {
115 s.device()->ungetChar(val);
116 if (!ReadOldLine(image, width, s)) {
117 return false;
118 }
119 RGBEToPaintDevice(image, width, it);
120 continue;
121 }
122
123 s >> image[1];
124 s >> image[2];
125
126 // check if we can read the whole pixel block correctly
127 if (s.atEnd()) {
128 return false;
129 }
130
131 s >> image[3];
132
133 if ((image[1] != 2) || (image[2] & 128)) {
134 image[0] = 2;
135 if (!ReadOldLine(image + 4, width - 1, s)) {
136 return false;
137 }
138 RGBEToPaintDevice(image, width, it);
139 continue;
140 }
141
142 if ((image[2] << 8 | image[3]) != width) {
143 dbgFile << "Line of pixels had width" << (image[2] << 8 | image[3]) << "instead of" << width;
144 return false;
145 }
146
147 // read each component
148 for (int i = 0; i < 4; i++) {
149 for (int j = 0; j < width;) {
150 s >> code;
151 if (s.atEnd()) {
152 dbgFile << "Truncated HDR file";
153 return false;
154 }
155 if (code > 128) {
156 // run
157 code &= 127;
158 s >> val;
159 if (j + code - 1 >= width) {
160 dbgFile << "Broken file detected: cannot duplicate data past image bounds!";
161 return false;
162 }
163 while (code != 0) {
164 image[i + j * 4] = val;
165 j++;
166 code--;
167 }
168 } else {
169 // non-run
170 if (j + code - 1 >= width) {
171 dbgFile << "Broken file detected: cannot extract data past image bounds!";
172 return false;
173 }
174 while (code != 0) {
175 s >> image[i + j * 4];
176 j++;
177 code--;
178 }
179 }
180 }
181 }
182
183 RGBEToPaintDevice(image, width, it);
184 }
185
186 return true;
187}
#define MINELEN
#define MAXELEN
#define dbgFile
Definition kis_debug.h:53
bool ReadOldLine(quint8 *image, int width, QDataStream &s)
void RGBEToPaintDevice(quint8 *image, int width, KisSequentialIterator &it)

References dbgFile, MAXELEN, MINELEN, ReadOldLine(), and RGBEToPaintDevice().

◆ ReadOldLine()

bool RGBEIMPORT::ReadOldLine ( quint8 * image,
int width,
QDataStream & s )

Definition at line 27 of file RGBEImportUtils.cpp.

28{
29 int rshift = 0;
30 int i;
31
32 while (width > 0) {
33 s >> image[0];
34 s >> image[1];
35 s >> image[2];
36
37 // check if we can read the whole pixel block correctly
38 if (s.atEnd()) {
39 return false;
40 }
41
42 s >> image[3];
43
44 if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) {
45 const int length = image[3] << rshift;
46 if (length > width) {
47 dbgFile << "Broken file detected: cannot duplicate pixels past image bounds!";
48 return false;
49 }
50 for (i = length; i > 0; i--) {
51 memcpy(image, image-4, 4);
52 image += 4;
53 width--;
54 }
55 rshift += 8;
56 } else {
57 image += 4;
58 width--;
59 rshift = 0;
60 }
61 }
62 return true;
63}
qreal length(const QPointF &vec)
Definition Ellipse.cc:82

References dbgFile, and length().

◆ RGBEToPaintDevice()

void RGBEIMPORT::RGBEToPaintDevice ( quint8 * image,
int width,
KisSequentialIterator & it )

Definition at line 65 of file RGBEImportUtils.cpp.

66{
67 for (int j = 0; j < width; j++) {
68 it.nextPixel();
69 auto *dst = reinterpret_cast<float *>(it.rawData());
70
71 if (image[3]) {
72 const float v = std::ldexp(1.0f, int(image[3]) - (128 + 8));
73 const float pixelData[4] = {float(image[0]) * v,
74 float(image[1]) * v,
75 float(image[2]) * v,
76 1.0f};
77 memcpy(dst, pixelData, 4 * sizeof(float));
78 } else {
79 // Zero exponent handle
80 const float pixelData[4] = {0.0f, 0.0f, 0.0f, 1.0f};
81 memcpy(dst, pixelData, 4 * sizeof(float));
82 }
83
84 image += 4;
85 }
86}
qreal v
ALWAYS_INLINE quint8 * rawData()

References KisSequentialIteratorBase< IteratorPolicy, SourcePolicy, ProgressPolicy >::nextPixel(), KisSequentialIteratorBase< IteratorPolicy, SourcePolicy, ProgressPolicy >::rawData(), and v.