Image.cpp

Go to the documentation of this file.
00001 /**<!-------------------------------------------------------------------->
00002    @file   Image.cpp
00003    @author Travis Fischer (fisch0920@gmail.com)
00004    @author Matthew Jacobs (jacobs.mh@gmail.com)
00005    @date   Fall 2008
00006    
00007    @brief
00008      Abstract image class supporting a standard 32-bit Rgba pixel format, as 
00009    well as an extended, 128- or 256-bit floating-point RgbaHDR pixel format 
00010    for high dynamic range images
00011    
00012    @note
00013       In addition to Qt's normal image loading / saving, represented by the 
00014    following table, this class also supports loading / saving of images in 
00015    HDR, OpenEXR, and PFM image formats.
00016    
00017    Default/built-in image formats supported by Qt:
00018    
00019     Format                 Description                     Qt Support
00020     ------  -------------------------------------------  --------------
00021       BMP   Windows Bitmap                               Read / Write
00022       GIF   Graphic Interchange Format (optional)        Read
00023       JPG   Joint Photographic Experts Group             Read / Write
00024       JPEG  Joint Photographic Experts Group             Read / Write
00025       PNG   Portable Network Graphics                    Read / Write
00026       PBM   Portable Bitmap                              Read
00027       PGM   Portable Graymap                             Read
00028       PPM   Portable Pixmap                              Read / Write
00029       TIFF  Tagged Image File Format                     Read / Write
00030       XBM   X11 Bitmap                                   Read / Write
00031       XPM   X11 Pixmap                                   Read / Write
00032    
00033    Extra High Dynamic Range image formats supported by Milton:
00034    
00035     Format                 Description                      Support
00036     ------  -------------------------------------------   ----------- 
00037       HDR   Also known as RGBE (Radiance, by Greg Ward)  Read / Write
00038       EXR   ILM's OpenEXR Format                         Read / Write*
00039       PFM   Portable Float Map Format                    Read / Write
00040    
00041    @note
00042       * OpenEXR is only supported on platforms / builds with ILM's OpenEXR 
00043    libraries which do not come bundled with Milton.
00044    <!-------------------------------------------------------------------->**/
00045 
00046 #include "Image.h"
00047 #include "HDRUtils.h"
00048 #include "HDRImage.h"
00049 #include "RgbaImage.h"
00050 
00051 #include <algorithm>
00052 #include <QImage>
00053 
00054 Image *Image::load(const std::string &filename) {
00055    std::string suffix = filename.substr(filename.rfind('.') + 1);
00056    std::transform(suffix.begin(), suffix.end(), suffix.begin(), 
00057                   (int (*)(int))std::tolower);
00058    
00059    if (suffix == "hdr" || suffix == "rgbe") {
00060       return HDRUtils::loadHDR(filename);
00061    } else if (suffix == "exr") {
00062       return HDRUtils::loadEXR(filename);
00063    } else if (suffix == "pfm") {
00064       return HDRUtils::loadPFM(filename);
00065    } else { // default to Qt's image loading capabilities
00066       QImage image;
00067       if (!image.load(QString(filename.c_str())))
00068          return NULL;
00069       
00070       if (image.format() != QImage::Format_RGB32)
00071          image = image.convertToFormat(QImage::Format_RGB32);
00072       
00073       const unsigned width  = image.width();
00074       const unsigned height = image.height();
00075       
00076       RgbaImage *rgbaImage = new RgbaImage(width, height);
00077       Rgba32 *data = rgbaImage->getData();
00078       QRgb *qrgb   = (QRgb *)image.bits();
00079       
00080       // convert from ARGB to RGBA 
00081       for(unsigned i = width * height; i--;) {
00082          const QRgb &argb = qrgb[i];
00083          
00084          data[i] = Rgba32(qRed(argb), qGreen(argb), qBlue(argb));
00085       }
00086       
00087       return rgbaImage;
00088    }
00089 }
00090 
00091 bool Image::save(const std::string &filename) const {
00092    std::string suffix = filename.substr(filename.rfind('.') + 1);
00093    std::transform(suffix.begin(), suffix.end(), suffix.begin(), 
00094                   (int (*)(int))std::tolower);
00095    
00096    if (suffix == "hdr" || suffix == "rgbe") {
00097       return HDRUtils::saveHDR(filename, this);
00098    } else if (suffix == "exr") {
00099       return HDRUtils::saveEXR(filename, this);
00100    } else if (suffix == "pfm") {
00101       return HDRUtils::savePFM(filename, this);
00102    } else {
00103       QImage image(m_width, m_height, QImage::Format_RGB32);
00104       QRgb *qrgb = (QRgb *)image.bits();
00105       
00106       // convert from RGBA to ARGB
00107       for(unsigned i = m_height; i--;) {
00108          for(unsigned j = m_width; j--;) {
00109             const Rgba32 &rgba32 = getPixel<Rgba32>(i, j);
00110             
00111             qrgb[i * m_width + j] = 
00112                qRgba(rgba32.r, rgba32.g, rgba32.b, rgba32.a);
00113          }
00114       }
00115       
00116       return image.save(QString(filename.c_str()), NULL, -1);
00117    }
00118 }
00119 

Generated on 28 Feb 2009 for Milton by doxygen 1.5.6