TonicTones
Src/Loaders/LdrFormatsLoader/LdrFormatsLoader.cpp
00001 //      LdrFormatsLoader.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 <LdrFormatsLoader.h>
00022 
00023 #define tr(s) QObject::tr(s)
00024 
00025 Q_EXPORT_PLUGIN2(TT_OpenExrLoader, LdrFormatsLoaderFactory)
00026 
00027 
00037 QString LdrFormatsLoader::name() const
00038 {
00039     return tr("LDR Formats Loader");
00040 }
00041 
00045 void LdrFormatsLoader::setFileName(const QString& f)
00046 {
00047     fileName = f;
00048 }
00049 
00053 bool LdrFormatsLoader::openFile()
00054 {
00055     return image.load(fileName);
00056 }
00057 
00061 QSize LdrFormatsLoader::getSize()
00062 {
00063     return image.size();
00064 }
00065 
00069 Color* LdrFormatsLoader::getData()
00070 {
00071     QSize size = image.size();
00072     int width = size.width();
00073     int height = size.height();
00074     
00075     float rgbToXyzMatrix  [3][3] = {{0.5141364, 0.3238786,  0.16036376},
00076                                     {0.265068,  0.67023428, 0.06409157},
00077                                     {0.0241188, 0.1228178,  0.84442666}};
00078     
00079     Color* data = new Color[width*height];
00080     float w;
00081     int ii, jj;
00082     for (int i=0; i<height; ++i)
00083         for (int j=0; j<width; ++j)
00084         {
00085             QRgb pixel = image.pixel(j,i);
00086             float rgb[3] = {float(qRed(pixel))/255.0,
00087                             float(qGreen(pixel))/255.0,
00088                             float(qBlue(pixel))/255.0};
00089             float xyz[3] = {0.0, 0.0, 0.0};
00090             
00091             for (ii = 0; ii < 3; ++ii)
00092                 for (jj = 0; jj < 3; ++jj)
00093                     xyz[ii] += rgbToXyzMatrix[ii][jj] * rgb[jj];
00094             
00095             if((w = xyz[0] + xyz[1] + xyz[2]) > 0.0)
00096                 data[i*width + j] = Color(xyz[1],     // Y
00097                                           xyz[0]/w,   // x
00098                                           xyz[1]/w);  // y
00099             else
00100                 data[i*width + j] = Color(0.0,0.0,0.0);
00101 
00102         }
00103 
00104     return data;
00105 }
00106 
00110 HdrImage::ColorSpace LdrFormatsLoader::getColorSpace()
00111 {
00112     return HdrImage::Yxy;
00113 }
00114 
00115 
00116 //----------------------------------------------------------------------
00117 
00118 
00130 QStringList LdrFormatsLoaderFactory::extensions() const 
00131 {
00132     return QStringList() << "bmp" << "gif" << "jpg" << "jpeg" << "png" << "tiff";
00133 }
00134 
00138 ImageLoaderPtr LdrFormatsLoaderFactory::createLoader(const QString& fileName) const
00139 {
00140     ImageLoaderPtr loader(new LdrFormatsLoader);
00141     loader->setFileName(fileName);
00142     return loader;
00143 }
00144 
00145 
 All Classes Functions Variables