14 #include <tqdatastream.h>
17 #include <tdeglobal.h>
19 typedef TQ_UINT8 uchar;
25 #define MAXELEN 0x7fff
27 static inline uchar ClipToByte(
float value)
29 if (value > 255.0f)
return 255;
36 static bool Read_Old_Line (uchar * image,
int width, TQDataStream & s)
48 if (s.atEnd())
return false;
50 if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1))
52 for (i = image[3] << rshift; i > 0; i--)
55 (uint &)image[0] = (uint &)image[0-4];
72 static void RGBE_To_QRgbLine(uchar * image, TQRgb * scanline,
int width)
74 for (
int j = 0; j < width; j++)
78 int e = int(image[3]) - 128;
85 v = 1.0f / float(1 << -e);
88 scanline[j] = tqRgb( ClipToByte(
float(image[0]) * v),
89 ClipToByte(
float(image[1]) * v),
90 ClipToByte(
float(image[2]) * v) );
97 static bool LoadHDR( TQDataStream & s,
const int width,
const int height, TQImage & img )
102 if( !img.create( width, height, 32 ) )
107 TQMemArray<uchar> image( width * 4 );
109 for (
int cline = 0; cline < height; cline++)
111 TQRgb * scanline = (TQRgb *) img.scanLine( cline );
114 if ((width < MINELEN) || (MAXELEN < width))
116 Read_Old_Line(image.data(), width, s);
117 RGBE_To_QRgbLine(image.data(), scanline, width);
130 s.device()->at( s.device()->at() - 1 );
131 Read_Old_Line(image.data(), width, s);
132 RGBE_To_QRgbLine(image.data(), scanline, width);
145 if ((image[1] != 2) || (image[2] & 128))
148 Read_Old_Line(image.data()+4, width-1, s);
149 RGBE_To_QRgbLine(image.data(), scanline, width);
153 if ((image[2] << 8 | image[3]) != width)
159 for (
int i = 0; i < 4; i++)
161 for (
int j = 0; j < width; )
175 image[i + j * 4] = val;
185 s >> image[i + j * 4];
193 RGBE_To_QRgbLine(image.data(), scanline, width);
202 TDE_EXPORT
void kimgio_hdr_read( TQImageIO * io )
207 bool validFormat =
false;
211 len = io->ioDevice()->readLine(line, MAXLINE);
217 if (strcmp(line,
"FORMAT=32-bit_rle_rgbe\n") == 0)
222 }
while((len > 0) && (line[0] !=
'\n'));
226 kdDebug(399) <<
"Unknown HDR format." << endl;
227 io->setImage( TQImage() );
232 io->ioDevice()->readLine(line, MAXLINE);
236 if (sscanf(line,
"%2[+-XY] %d %2[+-XY] %d\n", s1, &height, s2, &width) != 4)
239 kdDebug(399) <<
"Invalid HDR file." << endl;
240 io->setImage( TQImage() );
245 TQDataStream s( io->ioDevice() );
248 if( !LoadHDR(s, width, height, img) )
250 kdDebug(399) <<
"Error loading HDR file." << endl;
251 io->setImage( TQImage() );
261 TDE_EXPORT
void kimgio_hdr_write( TQImageIO * )