PBRT
/home/felix/UBC/projects/AdaptiveLightfieldSampling/pbrt_v2/src/3rdparty/openexr-1.7.0/ImfFrameBuffer.h
00001 
00002 //
00003 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
00004 // Digital Ltd. LLC
00005 // 
00006 // All rights reserved.
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are
00010 // met:
00011 // *       Redistributions of source code must retain the above copyright
00012 // notice, this list of conditions and the following disclaimer.
00013 // *       Redistributions in binary form must reproduce the above
00014 // copyright notice, this list of conditions and the following disclaimer
00015 // in the documentation and/or other materials provided with the
00016 // distribution.
00017 // *       Neither the name of Industrial Light & Magic nor the names of
00018 // its contributors may be used to endorse or promote products derived
00019 // from this software without specific prior written permission. 
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00034 
00035 
00036 
00037 #ifndef INCLUDED_IMF_FRAME_BUFFER_H
00038 #define INCLUDED_IMF_FRAME_BUFFER_H
00039 
00040 //-----------------------------------------------------------------------------
00041 //
00042 //      class Slice
00043 //      class FrameBuffer
00044 //
00045 //-----------------------------------------------------------------------------
00046 
00047 #include <ImfName.h>
00048 #include <ImfPixelType.h>
00049 #include <map>
00050 #include <string>
00051 
00052 
00053 namespace Imf {
00054 
00055 
00056 //-------------------------------------------------------
00057 // Description of a single slice of the frame buffer:
00058 //
00059 // Note -- terminology: as part of a file, a component of
00060 // an image (e.g. red, green, blue, depth etc.) is called
00061 // a "channel".  As part of a frame buffer, an image
00062 // component is called a "slice".
00063 //-------------------------------------------------------
00064 
00065 struct Slice
00066 {
00067     //------------------------------
00068     // Data type; see ImfPixelType.h
00069     //------------------------------
00070 
00071     PixelType           type;
00072 
00073 
00074     //---------------------------------------------------------------------
00075     // Memory layout:  The address of pixel (x, y) is
00076     //
00077     //  base + (xp / xSampling) * xStride + (yp / ySampling) * yStride
00078     //
00079     // where xp and yp are computed as follows:
00080     //
00081     //  * If we are reading or writing a scanline-based file:
00082     //
00083     //      xp = x
00084     //      yp = y
00085     //
00086     //  * If we are reading a tile whose upper left coorner is at (xt, yt):
00087     //
00088     //      if xTileCoords is true then xp = x - xt, else xp = x
00089     //      if yTileCoords is true then yp = y - yt, else yp = y
00090     //
00091     //---------------------------------------------------------------------
00092 
00093     char *              base;
00094     size_t              xStride;
00095     size_t              yStride;
00096 
00097 
00098     //--------------------------------------------
00099     // Subsampling: pixel (x, y) is present in the
00100     // slice only if 
00101     //
00102     //  x % xSampling == 0 && y % ySampling == 0
00103     //
00104     //--------------------------------------------
00105 
00106     int                 xSampling;
00107     int                 ySampling;
00108 
00109 
00110     //----------------------------------------------------------
00111     // Default value, used to fill the slice when a file without
00112     // a channel that corresponds to this slice is read.
00113     //----------------------------------------------------------
00114 
00115     double              fillValue;
00116     
00117 
00118     //-------------------------------------------------------
00119     // For tiled files, the xTileCoords and yTileCoords flags
00120     // determine whether pixel addressing is performed using
00121     // absolute coordinates or coordinates relative to a
00122     // tile's upper left corner.  (See the comment on base,
00123     // xStride and yStride, above.)
00124     //
00125     // For scanline-based files these flags have no effect;
00126     // pixel addressing is always done using absolute
00127     // coordinates.
00128     //-------------------------------------------------------
00129 
00130     bool                xTileCoords;
00131     bool                yTileCoords;
00132 
00133 
00134     //------------
00135     // Constructor
00136     //------------
00137 
00138     Slice (PixelType type = HALF,
00139            char * base = 0,
00140            size_t xStride = 0,
00141            size_t yStride = 0,
00142            int xSampling = 1,
00143            int ySampling = 1,
00144            double fillValue = 0.0,
00145            bool xTileCoords = false,
00146            bool yTileCoords = false);
00147 };
00148 
00149 
00150 class FrameBuffer
00151 {
00152   public:
00153 
00154     //------------
00155     // Add a slice
00156     //------------
00157 
00158     void                        insert (const char name[],
00159                                         const Slice &slice);
00160 
00161     void                        insert (const std::string &name,
00162                                         const Slice &slice);
00163 
00164     //----------------------------------------------------------------
00165     // Access to existing slices:
00166     //
00167     // [n]              Returns a reference to the slice with name n.
00168     //                  If no slice with name n exists, an Iex::ArgExc
00169     //                  is thrown.
00170     //
00171     // findSlice(n)     Returns a pointer to the slice with name n,
00172     //                  or 0 if no slice with name n exists.
00173     //
00174     //----------------------------------------------------------------
00175 
00176     Slice &                     operator [] (const char name[]);
00177     const Slice &               operator [] (const char name[]) const;
00178 
00179     Slice &                     operator [] (const std::string &name);
00180     const Slice &               operator [] (const std::string &name) const;
00181 
00182     Slice *                     findSlice (const char name[]);
00183     const Slice *               findSlice (const char name[]) const;
00184 
00185     Slice *                     findSlice (const std::string &name);
00186     const Slice *               findSlice (const std::string &name) const;
00187 
00188 
00189     //-----------------------------------------
00190     // Iterator-style access to existing slices
00191     //-----------------------------------------
00192 
00193     typedef std::map <Name, Slice> SliceMap;
00194 
00195     class Iterator;
00196     class ConstIterator;
00197 
00198     Iterator                    begin ();
00199     ConstIterator               begin () const;
00200 
00201     Iterator                    end ();
00202     ConstIterator               end () const;
00203 
00204     Iterator                    find (const char name[]);
00205     ConstIterator               find (const char name[]) const;
00206 
00207     Iterator                    find (const std::string &name);
00208     ConstIterator               find (const std::string &name) const;
00209 
00210   private:
00211 
00212     SliceMap                    _map;
00213 };
00214 
00215 
00216 //----------
00217 // Iterators
00218 //----------
00219 
00220 class FrameBuffer::Iterator
00221 {
00222   public:
00223 
00224     Iterator ();
00225     Iterator (const FrameBuffer::SliceMap::iterator &i);
00226 
00227     Iterator &                  operator ++ ();
00228     Iterator                    operator ++ (int);
00229 
00230     const char *                name () const;
00231     Slice &                     slice () const;
00232 
00233   private:
00234 
00235     friend class FrameBuffer::ConstIterator;
00236 
00237     FrameBuffer::SliceMap::iterator _i;
00238 };
00239 
00240 
00241 class FrameBuffer::ConstIterator
00242 {
00243   public:
00244 
00245     ConstIterator ();
00246     ConstIterator (const FrameBuffer::SliceMap::const_iterator &i);
00247     ConstIterator (const FrameBuffer::Iterator &other);
00248 
00249     ConstIterator &             operator ++ ();
00250     ConstIterator               operator ++ (int);
00251 
00252     const char *                name () const;
00253     const Slice &               slice () const;
00254 
00255   private:
00256 
00257     friend bool operator == (const ConstIterator &, const ConstIterator &);
00258     friend bool operator != (const ConstIterator &, const ConstIterator &);
00259 
00260     FrameBuffer::SliceMap::const_iterator _i;
00261 };
00262 
00263 
00264 //-----------------
00265 // Inline Functions
00266 //-----------------
00267 
00268 inline
00269 FrameBuffer::Iterator::Iterator (): _i()
00270 {
00271     // empty
00272 }
00273 
00274 
00275 inline
00276 FrameBuffer::Iterator::Iterator (const FrameBuffer::SliceMap::iterator &i):
00277     _i (i)
00278 {
00279     // empty
00280 }
00281 
00282 
00283 inline FrameBuffer::Iterator &          
00284 FrameBuffer::Iterator::operator ++ ()
00285 {
00286     ++_i;
00287     return *this;
00288 }
00289 
00290 
00291 inline FrameBuffer::Iterator    
00292 FrameBuffer::Iterator::operator ++ (int)
00293 {
00294     Iterator tmp = *this;
00295     ++_i;
00296     return tmp;
00297 }
00298 
00299 
00300 inline const char *
00301 FrameBuffer::Iterator::name () const
00302 {
00303     return *_i->first;
00304 }
00305 
00306 
00307 inline Slice &  
00308 FrameBuffer::Iterator::slice () const
00309 {
00310     return _i->second;
00311 }
00312 
00313 
00314 inline
00315 FrameBuffer::ConstIterator::ConstIterator (): _i()
00316 {
00317     // empty
00318 }
00319 
00320 inline
00321 FrameBuffer::ConstIterator::ConstIterator
00322     (const FrameBuffer::SliceMap::const_iterator &i): _i (i)
00323 {
00324     // empty
00325 }
00326 
00327 
00328 inline
00329 FrameBuffer::ConstIterator::ConstIterator (const FrameBuffer::Iterator &other):
00330     _i (other._i)
00331 {
00332     // empty
00333 }
00334 
00335 inline FrameBuffer::ConstIterator &
00336 FrameBuffer::ConstIterator::operator ++ ()
00337 {
00338     ++_i;
00339     return *this;
00340 }
00341 
00342 
00343 inline FrameBuffer::ConstIterator               
00344 FrameBuffer::ConstIterator::operator ++ (int)
00345 {
00346     ConstIterator tmp = *this;
00347     ++_i;
00348     return tmp;
00349 }
00350 
00351 
00352 inline const char *
00353 FrameBuffer::ConstIterator::name () const
00354 {
00355     return *_i->first;
00356 }
00357 
00358 inline const Slice &    
00359 FrameBuffer::ConstIterator::slice () const
00360 {
00361     return _i->second;
00362 }
00363 
00364 
00365 inline bool
00366 operator == (const FrameBuffer::ConstIterator &x,
00367              const FrameBuffer::ConstIterator &y)
00368 {
00369     return x._i == y._i;
00370 }
00371 
00372 
00373 inline bool
00374 operator != (const FrameBuffer::ConstIterator &x,
00375              const FrameBuffer::ConstIterator &y)
00376 {
00377     return !(x == y);
00378 }
00379 
00380 
00381 } // namespace Imf
00382 
00383 #endif