• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kimgio
 

kimgio

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

kimgio

Skip menu "kimgio"
  • Main Page
  • File List
  • Related Pages

kimgio

Skip menu "kimgio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kimgio by doxygen 1.9.4
This website is maintained by Timothy Pearson.