exr.cpp
00001 // -*- C++;indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*- 00002 00013 #include "config.h" 00014 00015 #ifdef HAVE_EXR 00016 00017 #include <ImfRgbaFile.h> 00018 #include <ImfStandardAttributes.h> 00019 #include <ImathBox.h> 00020 #include <ImfInputFile.h> 00021 #include <ImfBoxAttribute.h> 00022 #include <ImfChannelListAttribute.h> 00023 #include <ImfCompressionAttribute.h> 00024 #include <ImfFloatAttribute.h> 00025 #include <ImfIntAttribute.h> 00026 #include <ImfLineOrderAttribute.h> 00027 #include <ImfStringAttribute.h> 00028 #include <ImfVecAttribute.h> 00029 #include <ImfArray.h> 00030 #include <ImfConvert.h> 00031 00032 #include <iostream> 00033 00034 #include <stdlib.h> 00035 00036 #include <kurl.h> 00037 #include <kprocess.h> 00038 #include <klocale.h> 00039 #include <kgenericfactory.h> 00040 #include <kdebug.h> 00041 00042 #include <tqimage.h> 00043 #include <tqcstring.h> 00044 #include <tqfile.h> 00045 #include <tqdatetime.h> 00046 #include <tqdict.h> 00047 #include <tqvalidator.h> 00048 #include <tqcolor.h> 00049 00050 #include "exr.h" 00051 00052 using namespace Imf; 00053 00054 /* this does a conversion from the ILM Half (equal to Nvidia Half) 00055 * format into the normal 32 bit pixel format. Process is from the 00056 * ILM code. 00057 */ 00058 QRgb RgbaToQrgba(struct Rgba imagePixel) 00059 { 00060 float r,g,b,a; 00061 00062 // 1) Compensate for fogging by subtracting defog 00063 // from the raw pixel values. 00064 // Response: We work with defog of 0.0, so this is a no-op 00065 00066 // 2) Multiply the defogged pixel values by 00067 // 2^(exposure + 2.47393). 00068 // Response: We work with exposure of 0.0. 00069 // (2^2.47393) is 5.55555 00070 r = imagePixel.r * 5.55555; 00071 g = imagePixel.g * 5.55555; 00072 b = imagePixel.b * 5.55555; 00073 a = imagePixel.a * 5.55555; 00074 00075 // 3) Values, which are now 1.0, are called "middle gray". 00076 // If defog and exposure are both set to 0.0, then 00077 // middle gray corresponds to a raw pixel value of 0.18. 00078 // In step 6, middle gray values will be mapped to an 00079 // intensity 3.5 f-stops below the display's maximum 00080 // intensity. 00081 // Response: no apparent content. 00082 00083 // 4) Apply a knee function. The knee function has two 00084 // parameters, kneeLow and kneeHigh. Pixel values 00085 // below 2^kneeLow are not changed by the knee 00086 // function. Pixel values above kneeLow are lowered 00087 // according to a logarithmic curve, such that the 00088 // value 2^kneeHigh is mapped to 2^3.5 (in step 6, 00089 // this value will be mapped to the the display's 00090 // maximum intensity). 00091 // Response: kneeLow = 0.0 (2^0.0 => 1); kneeHigh = 5.0 (2^5 =>32) 00092 if (r > 1.0) 00093 r = 1.0 + Imath::Math<float>::log ((r-1.0) * 0.184874 + 1) / 0.184874; 00094 if (g > 1.0) 00095 g = 1.0 + Imath::Math<float>::log ((g-1.0) * 0.184874 + 1) / 0.184874; 00096 if (b > 1.0) 00097 b = 1.0 + Imath::Math<float>::log ((b-1.0) * 0.184874 + 1) / 0.184874; 00098 if (a > 1.0) 00099 a = 1.0 + Imath::Math<float>::log ((a-1.0) * 0.184874 + 1) / 0.184874; 00100 // 00101 // 5) Gamma-correct the pixel values, assuming that the 00102 // screen's gamma is 0.4545 (or 1/2.2). 00103 r = Imath::Math<float>::pow (r, 0.4545); 00104 g = Imath::Math<float>::pow (g, 0.4545); 00105 b = Imath::Math<float>::pow (b, 0.4545); 00106 a = Imath::Math<float>::pow (a, 0.4545); 00107 00108 // 6) Scale the values such that pixels middle gray 00109 // pixels are mapped to 84.66 (or 3.5 f-stops below 00110 // the display's maximum intensity). 00111 // 00112 // 7) Clamp the values to [0, 255]. 00113 return tqRgba( char (Imath::clamp ( r * 84.66f, 0.f, 255.f ) ), 00114 char (Imath::clamp ( g * 84.66f, 0.f, 255.f ) ), 00115 char (Imath::clamp ( b * 84.66f, 0.f, 255.f ) ), 00116 char (Imath::clamp ( a * 84.66f, 0.f, 255.f ) ) ); 00117 } 00118 00119 KDE_EXPORT void kimgio_exr_read( TQImageIO *io ) 00120 { 00121 try 00122 { 00123 int width, height; 00124 00125 // This won't work if io is not TQFile ! 00126 RgbaInputFile file (TQFile::encodeName(io->fileName())); 00127 Imath::Box2i dw = file.dataWindow(); 00128 00129 width = dw.max.x - dw.min.x + 1; 00130 height = dw.max.y - dw.min.y + 1; 00131 00132 Array2D<Rgba> pixels; 00133 pixels.resizeErase (height, width); 00134 00135 file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width); 00136 file.readPixels (dw.min.y, dw.max.y); 00137 00138 TQImage image(width, height, 32, 0, TQImage::BigEndian); 00139 if( image.isNull()) 00140 return; 00141 00142 // somehow copy pixels into image 00143 for ( int y=0; y < height; y++ ) { 00144 for ( int x=0; x < width; x++ ) { 00145 // copy pixels(x,y) into image(x,y) 00146 image.setPixel( x, y, RgbaToQrgba( pixels[y][x] ) ); 00147 } 00148 } 00149 00150 io->setImage( image ); 00151 io->setStatus( 0 ); 00152 } 00153 catch (const std::exception &exc) 00154 { 00155 kdDebug(399) << exc.what() << endl; 00156 return; 00157 } 00158 } 00159 00160 00161 KDE_EXPORT void kimgio_exr_write(TQImageIO *) 00162 { 00163 // TODO: stub 00164 } 00165 00166 00167 #endif