13 #include <QtGui/QImage>
14 #include <QtCore/QDataStream>
23 #define MINELEN 8 // minimum scanline length for encoding
24 #define MAXELEN 0x7fff // maximum scanline length for encoding
26 static inline uchar ClipToByte(
float value)
28 if (value > 255.0f)
return 255;
35 static bool Read_Old_Line (
uchar * image,
int width, QDataStream & s)
47 if (s.atEnd())
return false;
49 if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1))
51 for (i = image[3] << rshift; i > 0; i--)
54 (
uint &)image[0] = (
uint &)image[0-4];
71 static void RGBE_To_QRgbLine(
uchar * image, QRgb * scanline,
int width)
73 for (
int j = 0; j < width; j++)
77 int e = int(image[3]) - 128;
84 v = 1.0f / float(1 << -e);
87 scanline[j] = qRgb( ClipToByte(
float(image[0]) * v),
88 ClipToByte(
float(image[1]) * v),
89 ClipToByte(
float(image[2]) * v) );
96 static bool LoadHDR( QDataStream & s,
const int width,
const int height, QImage & img )
101 if( !img.create( width, height, 32 ) )
106 QMemArray<uchar> image( width * 4 );
108 for (
int cline = 0; cline < height; cline++)
110 QRgb * scanline = (QRgb *) img.scanLine( cline );
115 Read_Old_Line(image.data(), width, s);
116 RGBE_To_QRgbLine(image.data(), scanline, width);
129 s.device()->at( s.device()->at() - 1 );
130 Read_Old_Line(image.data(), width, s);
131 RGBE_To_QRgbLine(image.data(), scanline, width);
144 if ((image[1] != 2) || (image[2] & 128))
147 Read_Old_Line(image.data()+4, width-1, s);
148 RGBE_To_QRgbLine(image.data(), scanline, width);
152 if ((image[2] << 8 | image[3]) != width)
158 for (
int i = 0; i < 4; i++)
160 for (
int j = 0; j < width; )
174 image[i + j * 4] = val;
184 s >> image[i + j * 4];
192 RGBE_To_QRgbLine(image.data(), scanline, width);
206 bool validFormat =
false;
210 len = io->ioDevice()->readLine(line,
MAXLINE);
216 if (strcmp(line,
"FORMAT=32-bit_rle_rgbe\n") == 0)
221 }
while((len > 0) && (line[0] !=
'\n'));
225 kDebug(399) <<
"Unknown HDR format.";
231 io->ioDevice()->readLine(line,
MAXLINE);
235 if (sscanf(line,
"%2[+-XY] %d %2[+-XY] %d\n", s1, &height, s2, &width) != 4)
238 kDebug(399) <<
"Invalid HDR file.";
244 QDataStream s( io->ioDevice() );
247 if( !LoadHDR(s, width, height, img) )
249 kDebug(399) <<
"Error loading HDR file.";
void kimgio_hdr_read(QImageIO *io)
void kimgio_hdr_write(QImageIO *)