1 // -*- C++;indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
4 * KImageIO Routines to read (and perhaps in the future, write) images
5 * in the high dynamic range EXR format.
6 * Copyright (c) 2003, Brad Hards <bradh@frogmouth.net>
8 * This library is distributed under the conditions of the GNU LGPL.
15 #include <ImfRgbaFile.h>
16 #include <ImfStandardAttributes.h>
18 #include <ImfInputFile.h>
19 #include <ImfBoxAttribute.h>
20 #include <ImfChannelListAttribute.h>
21 #include <ImfCompressionAttribute.h>
22 #include <ImfFloatAttribute.h>
23 #include <ImfIntAttribute.h>
24 #include <ImfLineOrderAttribute.h>
25 #include <ImfStringAttribute.h>
26 #include <ImfVecAttribute.h>
28 #include <ImfConvert.h>
29 #include <ImfVersion.h>
30 #include <IexThrowErrnoExc.h>
38 #include <QDataStream>
39 #include <QImageIOPlugin>
41 class K_IStream
: public Imf::IStream
44 K_IStream( QIODevice
*dev
, const QByteArray
& fileName
):
45 IStream( fileName
.data() ), m_dev ( dev
)
48 virtual bool read( char c
[], int n
);
49 virtual Imf::Int64
tellg( );
50 virtual void seekg( Imf::Int64 pos
);
51 virtual void clear( );
57 bool K_IStream::read( char c
[], int n
)
59 qint64 result
= m_dev
->read( c
, n
);
62 } else if ( result
== 0 ) {
63 throw Iex::InputExc( "Unexpected end of file" );
64 } else // negative value {
65 Iex::throwErrnoExc( "Error in read", result
);
69 Imf::Int64
K_IStream::tellg( )
74 void K_IStream::seekg( Imf::Int64 pos
)
79 void K_IStream::clear( )
84 /* this does a conversion from the ILM Half (equal to Nvidia Half)
85 * format into the normal 32 bit pixel format. Process is from the
88 QRgb
RgbaToQrgba(struct Imf::Rgba imagePixel
)
92 // 1) Compensate for fogging by subtracting defog
93 // from the raw pixel values.
94 // Response: We work with defog of 0.0, so this is a no-op
96 // 2) Multiply the defogged pixel values by
97 // 2^(exposure + 2.47393).
98 // Response: We work with exposure of 0.0.
99 // (2^2.47393) is 5.55555
100 r
= imagePixel
.r
* 5.55555;
101 g
= imagePixel
.g
* 5.55555;
102 b
= imagePixel
.b
* 5.55555;
103 a
= imagePixel
.a
* 5.55555;
105 // 3) Values, which are now 1.0, are called "middle gray".
106 // If defog and exposure are both set to 0.0, then
107 // middle gray corresponds to a raw pixel value of 0.18.
108 // In step 6, middle gray values will be mapped to an
109 // intensity 3.5 f-stops below the display's maximum
111 // Response: no apparent content.
113 // 4) Apply a knee function. The knee function has two
114 // parameters, kneeLow and kneeHigh. Pixel values
115 // below 2^kneeLow are not changed by the knee
116 // function. Pixel values above kneeLow are lowered
117 // according to a logarithmic curve, such that the
118 // value 2^kneeHigh is mapped to 2^3.5 (in step 6,
119 // this value will be mapped to the display's
120 // maximum intensity).
121 // Response: kneeLow = 0.0 (2^0.0 => 1); kneeHigh = 5.0 (2^5 =>32)
123 r
= 1.0 + Imath::Math
<float>::log ((r
-1.0) * 0.184874 + 1) / 0.184874;
125 g
= 1.0 + Imath::Math
<float>::log ((g
-1.0) * 0.184874 + 1) / 0.184874;
127 b
= 1.0 + Imath::Math
<float>::log ((b
-1.0) * 0.184874 + 1) / 0.184874;
129 a
= 1.0 + Imath::Math
<float>::log ((a
-1.0) * 0.184874 + 1) / 0.184874;
131 // 5) Gamma-correct the pixel values, assuming that the
132 // screen's gamma is 0.4545 (or 1/2.2).
133 r
= Imath::Math
<float>::pow (r
, 0.4545);
134 g
= Imath::Math
<float>::pow (g
, 0.4545);
135 b
= Imath::Math
<float>::pow (b
, 0.4545);
136 a
= Imath::Math
<float>::pow (a
, 0.4545);
138 // 6) Scale the values such that pixels middle gray
139 // pixels are mapped to 84.66 (or 3.5 f-stops below
140 // the display's maximum intensity).
142 // 7) Clamp the values to [0, 255].
143 return qRgba( char (Imath::clamp ( r
* 84.66f
, 0.f
, 255.f
) ),
144 char (Imath::clamp ( g
* 84.66f
, 0.f
, 255.f
) ),
145 char (Imath::clamp ( b
* 84.66f
, 0.f
, 255.f
) ),
146 char (Imath::clamp ( a
* 84.66f
, 0.f
, 255.f
) ) );
149 EXRHandler::EXRHandler()
153 bool EXRHandler::canRead() const
155 if (canRead(device())) {
162 QByteArray
EXRHandler::name() const
165 return QByteArray("exr");
168 bool EXRHandler::read( QImage
*outImage
)
174 K_IStream
istr( device(), QByteArray() );
175 Imf::RgbaInputFile
file( istr
);
176 Imath::Box2i dw
= file
.dataWindow();
178 width
= dw
.max
.x
- dw
.min
.x
+ 1;
179 height
= dw
.max
.y
- dw
.min
.y
+ 1;
181 Imf::Array2D
<Imf::Rgba
> pixels
;
182 pixels
.resizeErase (height
, width
);
184 file
.setFrameBuffer (&pixels
[0][0] - dw
.min
.x
- dw
.min
.y
* width
, 1, width
);
185 file
.readPixels (dw
.min
.y
, dw
.max
.y
);
187 QImage
image(width
, height
, QImage::Format_RGB32
);
191 // somehow copy pixels into image
192 for ( int y
=0; y
< height
; y
++ ) {
193 for ( int x
=0; x
< width
; x
++ ) {
194 // copy pixels(x,y) into image(x,y)
195 image
.setPixel( x
, y
, RgbaToQrgba( pixels
[y
][x
] ) );
203 catch (const std::exception
&exc
)
205 kDebug() << exc
.what();
211 bool EXRHandler::write( const QImage
&image
)
219 bool EXRHandler::canRead(QIODevice
*device
)
222 qWarning("EXRHandler::canRead() called with no device");
226 qint64 oldPos
= device
->pos();
228 const QByteArray head
= device
->readLine(4);
229 int readBytes
= head
.size();
230 if (device
->isSequential()) {
231 while (readBytes
> 0)
232 device
->ungetChar(head
[readBytes
-- - 1]);
234 device
->seek(oldPos
);
237 return Imf::isImfMagic( head
.data() );
243 QStringList
EXRPlugin::keys() const
245 return QStringList() << "exr" << "EXR";
249 QImageIOPlugin::Capabilities
EXRPlugin::capabilities(QIODevice
*device
, const QByteArray
&format
) const
251 if ( format
== "exr" || format
== "EXR" )
252 return Capabilities(CanRead
);
253 if ( !format
.isEmpty() )
255 if ( !device
->isOpen() )
259 if (device
->isReadable() && EXRHandler::canRead(device
))
264 QImageIOHandler
*EXRPlugin::create(QIODevice
*device
, const QByteArray
&format
) const
266 QImageIOHandler
*handler
= new EXRHandler
;
267 handler
->setDevice(device
);
268 handler
->setFormat(format
);
272 Q_EXPORT_STATIC_PLUGIN( EXRPlugin
)
273 Q_EXPORT_PLUGIN2( exr
, EXRPlugin
)