TonicTones
Src/Loaders/OpenExrLoader/OpenExrLoader.cpp
00001 //      OpenExrLoader.cpp
00002 //      
00003 //      Copyright 2010 Jérémy Laumon <jeremy.laumon@gmail.com>
00004 //      
00005 //      This program is free software; you can redistribute it and/or modify
00006 //      it under the terms of the GNU General Public License as published by
00007 //      the Free Software Foundation; either version 2 of the License, or
00008 //      (at your option) any later version.
00009 //      
00010 //      This program is distributed in the hope that it will be useful,
00011 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //      GNU General Public License for more details.
00014 //      
00015 //      You should have received a copy of the GNU General Public License
00016 //      along with this program; if not, write to the Free Software
00017 //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018 //      MA 02110-1301, USA.
00019 
00020 
00021 #include <OpenExrLoader.h>
00022 #include <ImfArray.h>
00023 #include <ImfChromaticities.h>
00024 #include <ImfStandardAttributes.h>
00025 #include <ImfRgbaYca.h>
00026 
00027 #define tr(s) QObject::tr(s)
00028 
00029 Q_EXPORT_PLUGIN2(TT_OpenExrLoader, OpenExrLoaderFactory)
00030 
00031 
00038 OpenExrLoader::OpenExrLoader() :
00039     file(NULL)
00040 {
00041     
00042 }
00043 
00047 QString OpenExrLoader::name() const
00048 {
00049     return tr("OpenEXR Loader");
00050 }
00051 
00055 void OpenExrLoader::setFileName(const QString& f)
00056 {
00057     fileName = f;
00058 }
00059 
00063 bool OpenExrLoader::openFile()
00064 {
00065     delete file;
00066     try
00067     {
00068         file = new Imf::RgbaInputFile(fileName.toStdString().c_str());
00069         return true;
00070     }
00071     catch(const std::exception&)
00072     {
00073         return false;
00074     }
00075 }
00076 
00080 QSize OpenExrLoader::getSize()
00081 {
00082     try
00083     {
00084         dw = file->header().dataWindow();
00085         width = dw.max.x - dw.min.x + 1;
00086         height = dw.max.y - dw.min.y + 1;
00087         return QSize(width,height);
00088     }
00089     catch(const std::exception&)
00090     {
00091         return QSize(0,0);
00092     }
00093 }
00094 
00098 Color* OpenExrLoader::getData()
00099 {
00100     Imf::Array2D<Imf::Rgba> pixels;
00101         
00102     pixels.resizeErase (height, width);
00103     file->setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
00104     file->readPixels (dw.min.y, dw.max.y);
00105 
00106     Imf::Chromaticities cr; // has default values
00107 
00108     if (Imf::hasChromaticities(file->header()))
00109         cr = chromaticities(file->header());
00110         
00111     Imath::M44f m = Imf::RGBtoXYZ(cr, 1);
00112     
00113     Color* data = new Color[width*height];
00114     float w;
00115     for (int i=0; i<height; ++i)
00116         for (int j=0; j<width; ++j)
00117         {
00118             Imath::V3f xyz = Imath::V3f (pixels[i][j].r, pixels[i][j].g, pixels[i][j].b) * m;
00119 
00120             if((w = xyz[0] + xyz[1] + xyz[2]) > 0.0)
00121                 data[i*width + j] = Color(xyz[1],     // Y
00122                                           xyz[0]/w,   // x
00123                                           xyz[1]/w);  // y
00124             else
00125                 data[i*width + j] = Color(0.0,0.0,0.0);
00126 
00127         }
00128 
00129     return data;
00130 }
00131 
00135 HdrImage::ColorSpace OpenExrLoader::getColorSpace()
00136 {
00137     return HdrImage::Yxy;
00138 }
00139 
00140 //----------------------------------------------------------------------
00141 
00153 QStringList OpenExrLoaderFactory::extensions() const 
00154 {
00155     return QStringList() << "exr";
00156 }
00157 
00161 ImageLoaderPtr OpenExrLoaderFactory::createLoader(const QString& fileName) const
00162 {
00163     ImageLoaderPtr loader(new OpenExrLoader);
00164     loader->setFileName(fileName);
00165     return loader;
00166 }
00167 
00168 
 All Classes Functions Variables