Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
OpenEXRInterface.hh
1 /*
2  This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4  Copyright (C) 2003-2009 (see file CONTACT for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10  BIAS is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation; either version 2.1 of the License, or
13  (at your option) any later version.
14 
15  BIAS is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with BIAS; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #ifndef _OPENEXRINTERFACE_hh_
26 #define _OPENEXRINTERFACE_hh_
27 
28 
29 #include "bias_config.h"
30 #ifdef BIAS_HAVE_OPENEXR
31 # ifdef WIN32
32 # pragma warning( push, 1)
33 # define PLATFORM_WINDOWS
34 # endif // WIN32
35 # include <ImfPixelType.h>
36 # include <ImfCompression.h>
37 # include <ImfHeader.h>
38 # include <ImfFrameBuffer.h>
39 # include <half.h>
40 # ifdef WIN32
41 # pragma warning( pop)
42 # endif // WIN32
43 #endif
44 
45 #include "ImageBase.hh"
46 #include <map>
47 
48 namespace BIAS {
49 
50  /** Methods for reading and writing OpenEXR files into and from BIAS ImageBase.
51  *
52  * \attention Not all color models have been implemented, have a look at GenerateChannelNames_() method.
53  *
54  * If an EXR image is loaded without the new BIAS attributes the color model is chosen by means of the
55  * channel count, and defaults to the most common "intensity" based color models (i.e. Grey, RGB, RGBA).
56  * \attention If an EXR image is loaded that has channels of differing pixel type (e.g. one HALF and one UINT channel)
57  * FLOAT is chosen to be the common pixel type.
58  *
59  * Since no unsigned char images are natively supported by OpenEXR the BIAS StorageType is also stored in an exported
60  * EXR image. This stored type is then used to chose the resulting ImageBase:EStorageType, when loading the image, overriding
61  * derived types. This behaviour is not optimal in any case, however most of our tools will expect Image<unsigned char>, when
62  * this has been the original source's type.
63  *
64  * This is also not a full fledged implementation of all OpenEXR capabilties (i.e. Layers, Tiles, etc.).
65  *
66  * \author bartczak 02/2009
67  **/
68  class BIASImageBase_EXPORT OpenEXRInterface {
69  public :
72 
73  int Export(const std::string& FileName,
74  const ImageBase& input,
75  const Imf::Compression& compression = Imf::ZIP_COMPRESSION);
76 
77  int Import(const std::string& FileName, ImageBase& result);
78 
79  /** When exporting image this pixel type is used, not a derived one!
80  * Imf::HALF, Imf:FLOAT, Imf::INT are valid values.
81  **/
82  void SetOverridePixelTypeEXR(Imf::PixelType overridePT);
83 
84  protected:
85 
86  /** Used to maintain control of the heap when generating slices.
87  **/
88  class ChannelData {
89  public:
90  ChannelData() : ifFloat_(NULL), ifHalf_(NULL), ifUInt_(NULL) {};
91  ChannelData(float* ifFloat) : ifFloat_(ifFloat), ifHalf_(NULL), ifUInt_(NULL) {};
92  ChannelData(half* ifHalf) : ifFloat_(NULL), ifHalf_(ifHalf), ifUInt_(NULL) {};
93  ChannelData(unsigned int* ifUInt) : ifFloat_(NULL), ifHalf_(NULL), ifUInt_(ifUInt) {};
94 
95  void ClearData() {
96  delete [] ifFloat_;
97  ifFloat_ = NULL;
98  delete [] ifHalf_;
99  ifHalf_ = NULL;
100  delete [] ifUInt_;
101  ifUInt_ = NULL;
102  }
103  enum TypeID {isFloat=0, isHalf, isUInt, empty};
104  TypeID Get(float*& ifFloat, half*& ifHalf, unsigned int*& ifUInt) const {
105  ifFloat = ifFloat_;
106  ifHalf = ifHalf_;
107  ifUInt = ifUInt_;
108  if(ifFloat!=NULL) return isFloat;
109  if(ifHalf!=NULL) return isHalf;
110  if(ifUInt!=NULL) return isUInt;
111  return empty;
112  }
113  private:
114  float* ifFloat_;
115  half* ifHalf_;
116  unsigned int* ifUInt_;
117 
118  };
119 
120  Imf::PixelType MapStorageType_(const ImageBase::EStorageType& st);
121  ImageBase::EStorageType MapPixelType_(const Imf::PixelType& pt, const std::string& storageTypeString="");
122 
123  static int GenerateChannelNames_(const ImageBase::EColorModel& cm,
124  const unsigned int& numChannels,
125  std::vector<std::string>& channelNames);
126 
127  int AddChannels_(const ImageBase::EColorModel& cm,
128  const ImageBase::EStorageType& st,
129  Imf::Header& outHeader,
130  Imf::FrameBuffer& fb,
131  const ImageBase& input);
132 
133 
134  int AddSlice_(Imf::PixelType pt,
135  const int& exrwidth,
136  const int& exrheight,
137  const std::string& channelName,
138  Imf::FrameBuffer& fb,
139  unsigned int channel = 0,
140  const ImageBase* input = NULL);
141 
142  void ClearChannels_();
143  std::map<std::string, ChannelData> channels_;
144 
146  Imf::PixelType overridePT_;
147 
148  };
149 
150 }
151 
152 
153 #endif
EColorModel
These are the most often used color models.
Definition: ImageBase.hh:127
Methods for reading and writing OpenEXR files into and from BIAS ImageBase.
TypeID Get(float *&ifFloat, half *&ifHalf, unsigned int *&ifUInt) const
std::map< std::string, ChannelData > channels_
This is the base class for images in BIAS.
Definition: ImageBase.hh:102
Used to maintain control of the heap when generating slices.