1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/* This file is part of the KDE project
Copyright (C) 2005 Christoph Hormann <chris_hormann@gmx.de>
Copyright (C) 2005 Ignacio Castaņo <castanyo@yahoo.es>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#include "hdr.h"
#include <tqimage.h>
#include <tqdatastream.h>
#include <kdebug.h>
#include <kglobal.h>
typedef TQ_UINT8 uchar;
namespace { // Private.
#define MAXLINE 1024
#define MINELEN 8 // minimum scanline length for encoding
#define MAXELEN 0x7fff // maximum scanline length for encoding
static inline uchar ClipToByte(float value)
{
if (value > 255.0f) return 255;
//else if (value < 0.0f) return 0; // we know value is positive.
return uchar(value);
}
// read an old style line from the hdr image file
// if 'first' is true the first byte is already read
static bool Read_Old_Line (uchar * image, int width, TQDataStream & s)
{
int rshift = 0;
int i;
while (width > 0)
{
s >> image[0];
s >> image[1];
s >> image[2];
s >> image[3];
if (s.atEnd()) return false;
if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1))
{
for (i = image[3] << rshift; i > 0; i--)
{
//memcpy(image, image-4, 4);
(uint &)image[0] = (uint &)image[0-4];
image += 4;
width--;
}
rshift += 8;
}
else
{
image += 4;
width--;
rshift = 0;
}
}
return true;
}
static void RGBE_To_QRgbLine(uchar * image, QRgb * scanline, int width)
{
for (int j = 0; j < width; j++)
{
// v = ldexp(1.0, int(image[3]) - 128);
float v;
int e = int(image[3]) - 128;
if( e > 0 )
{
v = float(1 << e);
}
else
{
v = 1.0f / float(1 << -e);
}
scanline[j] = tqRgb( ClipToByte(float(image[0]) * v),
ClipToByte(float(image[1]) * v),
ClipToByte(float(image[2]) * v) );
image += 4;
}
}
// Load the HDR image.
static bool LoadHDR( TQDataStream & s, const int width, const int height, TQImage & img )
{
uchar val, code;
// Create dst image.
if( !img.create( width, height, 32 ) )
{
return false;
}
TQMemArray<uchar> image( width * 4 );
for (int cline = 0; cline < height; cline++)
{
QRgb * scanline = (QRgb *) img.scanLine( cline );
// determine scanline type
if ((width < MINELEN) || (MAXELEN < width))
{
Read_Old_Line(image.data(), width, s);
RGBE_To_QRgbLine(image.data(), scanline, width);
continue;
}
s >> val;
if (s.atEnd())
{
return true;
}
if (val != 2)
{
s.device()->at( s.device()->at() - 1 );
Read_Old_Line(image.data(), width, s);
RGBE_To_QRgbLine(image.data(), scanline, width);
continue;
}
s >> image[1];
s >> image[2];
s >> image[3];
if (s.atEnd())
{
return true;
}
if ((image[1] != 2) || (image[2] & 128))
{
image[0] = 2;
Read_Old_Line(image.data()+4, width-1, s);
RGBE_To_QRgbLine(image.data(), scanline, width);
continue;
}
if ((image[2] << 8 | image[3]) != width)
{
return false;
}
// read each component
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < width; )
{
s >> code;
if (s.atEnd())
{
return false;
}
if (code > 128)
{
// run
code &= 127;
s >> val;
while( code != 0 )
{
image[i + j * 4] = val;
j++;
code--;
}
}
else
{
// non-run
while( code != 0 )
{
s >> image[i + j * 4];
j++;
code--;
}
}
}
}
RGBE_To_QRgbLine(image.data(), scanline, width);
}
return true;
}
} // namespace
KDE_EXPORT void kimgio_hdr_read( TQImageIO * io )
{
int len;
char line[MAXLINE];
//bool validHeader = false;
bool validFormat = false;
// Parse header
do {
len = io->ioDevice()->readLine(line, MAXLINE);
/*if (strcmp(line, "#?RADIANCE\n") == 0 || strcmp(line, "#?RGBE\n") == 0)
{
validHeader = true;
}*/
if (strcmp(line, "FORMAT=32-bit_rle_rgbe\n") == 0)
{
validFormat = true;
}
} while((len > 0) && (line[0] != '\n'));
if( /*!validHeader ||*/ !validFormat )
{
kdDebug(399) << "Unknown HDR format." << endl;
io->setImage( TQImage() );
io->setStatus( -1 );
return;
}
io->ioDevice()->readLine(line, MAXLINE);
char s1[3], s2[3];
int width, height;
if (sscanf(line, "%2[+-XY] %d %2[+-XY] %d\n", s1, &height, s2, &width) != 4)
//if( sscanf(line, "-Y %d +X %d", &height, &width) < 2 )
{
kdDebug(399) << "Invalid HDR file." << endl;
io->setImage( TQImage() );
io->setStatus( -1 );
return;
}
TQDataStream s( io->ioDevice() );
TQImage img;
if( !LoadHDR(s, width, height, img) )
{
kdDebug(399) << "Error loading HDR file." << endl;
io->setImage( TQImage() );
io->setStatus( -1 );
return;
}
io->setImage( img );
io->setStatus( 0 );
}
KDE_EXPORT void kimgio_hdr_write( TQImageIO * )
{
// intentionally not implemented (since writing low dynamic range data to a HDR file is nonsense.)
}
|