image.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #ifndef LASS_GUARDIAN_OF_INCLUSION_IO_IMAGE_H
00051 #define LASS_GUARDIAN_OF_INCLUSION_IO_IMAGE_H
00052
00053 #include "io_common.h"
00054
00055 #include "../num/num_common.h"
00056 #include "../num/endianness.h"
00057 #include "../prim/color_rgba.h"
00058 #include "../prim/point_2d.h"
00059
00060 namespace lass
00061 {
00062
00063
00064
00065 namespace io
00066 {
00067
00068 class BinaryIStream;
00069 class BinaryOStream;
00070
00071 class LASS_DLL Image
00072 {
00073 public:
00074
00075 typedef prim::ColorRGBA TPixel;
00076 typedef std::vector<TPixel> TRaster;
00077
00078 typedef prim::ColorRGBA::TValue TValue;
00079 typedef prim::ColorRGBA::TParam TParam;
00080 typedef prim::ColorRGBA::TNumTraits TNumTraits;
00081
00082 typedef TValue (*TFilterFunction)(TValue);
00083
00084 typedef prim::Point2D<num::Tfloat32> TChromaticity;
00085 enum { numChromaticities = 4 };
00086 struct ColorSpace
00087 {
00088 TChromaticity red;
00089 TChromaticity green;
00090 TChromaticity blue;
00091 TChromaticity white;
00092 num::Tfloat32 gamma;
00093 bool isFromFile;
00094 const TChromaticity& operator[](size_t index) const { LASS_ASSERT(index < numChromaticities); return (&red)[index]; }
00095 TChromaticity& operator[](size_t index) { LASS_ASSERT(index < numChromaticities); return (&red)[index]; }
00096 const bool operator==(const ColorSpace& other) const
00097 {
00098 return red == other.red && green == other.green && blue == other.blue && white == other.white && gamma == other.gamma;
00099 }
00100 const bool operator!=(const ColorSpace& other) const { return !(*this == other); }
00101 };
00102
00103 class BadFormat: public util::Exception
00104 {
00105 public:
00106 BadFormat(const std::string& msg, const std::string& loc): util::Exception(msg, loc) {}
00107 private:
00108 LASS_UTIL_EXCEPTION_PRIVATE_IMPL(BadFormat)
00109 };
00110
00111
00112
00113 Image();
00114 Image(unsigned rows, unsigned cols);
00115 Image(const std::string& filename);
00116 Image(const Image& other);
00117 ~Image();
00118
00119
00120
00121
00122 void reset();
00123 void reset(unsigned rows, unsigned cols);
00124 void reset(const std::string& filename);
00125 void reset(const Image& other);
00126
00127 void open(const std::string& filename);
00128 void open(BinaryIStream& stream, const std::string& formatTag);
00129 void save(const std::string& filename);
00130 void save(BinaryOStream& stream, const std::string& formatTag);
00131
00132 Image& operator=(const Image& other);
00133 void swap(Image& other);
00134
00135 const TPixel& operator[](unsigned flatIndex) const { return raster_[flatIndex]; }
00136 TPixel& operator[](unsigned flatIndex) { return raster_[flatIndex]; }
00137
00138 const TPixel& operator()(unsigned row, unsigned col) const;
00139 TPixel& operator()(unsigned row, unsigned col);
00140
00141 const TPixel& at(signed row, signed col) const;
00142 TPixel& at(signed row, signed col);
00143
00144 const TPixel* const data() const;
00145 TPixel* const data();
00146
00147 const ColorSpace& colorSpace() const;
00148 ColorSpace& colorSpace();
00149 void transformColors(const ColorSpace& newColorSpace);
00150
00151 const unsigned rows() const;
00152 const unsigned cols() const;
00153 const bool isEmpty() const;
00154
00155
00156
00157 void over(const Image& other);
00158 void in(const Image& other);
00159 void out(const Image& other);
00160 void atop(const Image& other);
00161 void through(const Image& other);
00162 void rover(const Image& other);
00163 void rin(const Image& other);
00164 void rout(const Image& other);
00165 void ratop(const Image& other);
00166 void rthrough(const Image& other);
00167 void plus(const Image& other);
00168
00169 void clampNegatives();
00170
00171
00172
00173 void filterMedian(unsigned boxSize);
00174 void filterGamma(TParam gammaExponent);
00175 void filterExposure(TParam exposureTime);
00176 void filterInverseExposure(TParam exposureTime);
00177 void filter(TFilterFunction function);
00178
00179 private:
00180
00181 struct HeaderLass
00182 {
00183 num::Tuint32 lass;
00184 num::Tuint32 version;
00185 num::Tuint32 rows;
00186 num::Tuint32 cols;
00187
00188 void readFrom(BinaryIStream& stream);
00189 void writeTo(BinaryOStream& stream);
00190 };
00191
00192 struct HeaderTarga
00193 {
00194 num::Tuint8 idLength;
00195 num::Tuint8 colorMapType;
00196 num::Tuint8 imageType;
00197 num::Tuint16 colorMapOrigin;
00198 num::Tuint16 colorMapLength;
00199 num::Tuint8 colorMapEntrySize;
00200 num::Tuint16 imageXorigin;
00201 num::Tuint16 imageYorigin;
00202 num::Tuint16 imageWidth;
00203 num::Tuint16 imageHeight;
00204 num::Tuint8 imagePixelSize;
00205 num::Tuint8 imageDescriptor;
00206
00207 const unsigned numAttributeBits() const { return imageDescriptor & 0x0F; }
00208 const bool flipHorizontalFlag() const { return ((imageDescriptor >> 4) & 0x01) == 0x01; }
00209 const bool flipVerticalFlag() const { return ((imageDescriptor >> 5) & 0x01) == 0x01; }
00210 const bool interleavingFlag() const { return ((imageDescriptor >> 6) & 0x01) == 0x01; }
00211
00212 void readFrom(BinaryIStream& stream);
00213 void writeTo(BinaryOStream& stream);
00214 };
00215
00216 struct HeaderRadianceHdr
00217 {
00218 enum
00219 {
00220 sizeColorCorr = 3,
00221 sizePrimaries = 8
00222 };
00223
00224 float exposure;
00225 float colorCorr[sizeColorCorr];
00226 float primaries[sizePrimaries];
00227 unsigned height;
00228 unsigned width;
00229 bool yIncreasing;
00230 bool xIncreasing;
00231 bool isRgb;
00232 bool isDefaultPrimaries;
00233
00234 HeaderRadianceHdr();
00235 void readFrom(BinaryIStream& stream);
00236 void writeTo(BinaryOStream& stream);
00237 };
00238
00239 struct HeaderPfm
00240 {
00241 unsigned width;
00242 unsigned height;
00243 float aspect;
00244 num::Endianness endianness;
00245 bool isGrey;
00246
00247 HeaderPfm();
00248 void readFrom(BinaryIStream& stream);
00249 void writeTo(BinaryOStream& stream);
00250 };
00251
00252 struct HeaderIgi
00253 {
00254 num::Tint32 magic;
00255 num::Tint32 version;
00256 num::Tfloat64 numSamples;
00257 num::Tuint32 width;
00258 num::Tuint32 height;
00259 num::Tuint32 superSampling;
00260 num::Tint32 zipped;
00261 num::Tint32 dataSize;
00262 num::Tuint32 rgb;
00263
00264 enum { padding = 5000 };
00265
00266 void readFrom(BinaryIStream& stream);
00267 void writeTo(BinaryOStream& stream);
00268 };
00269
00270 typedef BinaryIStream& (Image::*TFileOpener)(BinaryIStream&);
00271 typedef BinaryOStream& (Image::*TFileSaver)(BinaryOStream&) const;
00272
00273 struct FileFormat
00274 {
00275 TFileOpener open;
00276 TFileSaver save;
00277 FileFormat(TFileOpener iOpen, TFileSaver iSave): open(iOpen), save(iSave) {}
00278 FileFormat(): open(0), save(0) {}
00279 };
00280
00281 typedef std::map<std::string, FileFormat> TFileFormats;
00282
00283 unsigned resize(unsigned rows, unsigned cols);
00284 unsigned flatIndex(unsigned rows, unsigned cols) const
00285 {
00286 return rows * cols_ + cols;
00287 }
00288
00289 BinaryIStream& openLass(BinaryIStream& stream);
00290 BinaryIStream& openTarga(BinaryIStream& stream);
00291 BinaryIStream& openTargaTrueColor(BinaryIStream& stream, const HeaderTarga& iHeader);
00292 BinaryIStream& openRadianceHdr(BinaryIStream& stream);
00293 BinaryIStream& openPfm(BinaryIStream& stream);
00294 BinaryIStream& openIgi(BinaryIStream& stream);
00295
00296 BinaryOStream& saveLass(BinaryOStream& stream) const;
00297 BinaryOStream& saveTarga(BinaryOStream& stream) const;
00298 BinaryOStream& saveRadianceHdr(BinaryOStream& stream) const;
00299 BinaryOStream& savePfm(BinaryOStream& stream) const;
00300 BinaryOStream& saveIgi(BinaryOStream& stream) const;
00301
00302 FileFormat findFormat(const std::string& formatTag);
00303 std::string readRadianceHdrString(BinaryIStream& stream) const;
00304
00305 static BinaryIStream& readLine(BinaryIStream& stream, std::string& line);
00306 static BinaryOStream& writeLine(BinaryOStream& stream, const std::string& line);
00307
00308 static TFileFormats fillFileFormats();
00309
00310 static const ColorSpace defaultColorSpace();
00311 static const ColorSpace xyzColorSpace();
00312
00313 ColorSpace colorSpace_;
00314 unsigned rows_;
00315 unsigned cols_;
00316 TRaster raster_;
00317
00318 static TFileFormats fileFormats_;
00319 static num::Tuint32 magicLass_;
00320 static std::string magicRadiance_;
00321 static num::Tint32 magicIgi_;
00322 };
00323
00324
00325
00326 }
00327
00328 }
00329
00330 #endif
00331
00332