Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / include / OpenEXR / ImfFrameBuffer.h
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 // 
6 // All rights reserved.
7 // 
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // *       Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // *       Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // *       Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission. 
20 // 
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34
35
36
37 #ifndef INCLUDED_IMF_FRAME_BUFFER_H
38 #define INCLUDED_IMF_FRAME_BUFFER_H
39
40 //-----------------------------------------------------------------------------
41 //
42 //      class Slice
43 //      class FrameBuffer
44 //
45 //-----------------------------------------------------------------------------
46
47 #include <ImfName.h>
48 #include <ImfPixelType.h>
49 #include <map>
50
51
52 namespace Imf {
53
54
55 //-------------------------------------------------------
56 // Description of a single slice of the frame buffer:
57 //
58 // Note -- terminology: as part of a file, a component of
59 // an image (e.g. red, green, blue, depth etc.) is called
60 // a "channel".  As part of a frame buffer, an image
61 // component is called a "slice".
62 //-------------------------------------------------------
63
64 struct Slice
65 {
66     //------------------------------
67     // Data type; see ImfPixelType.h
68     //------------------------------
69
70     PixelType           type;
71
72
73     //---------------------------------------------------------------------
74     // Memory layout:  The address of pixel (x, y) is
75     //
76     //  base + (xp / xSampling) * xStride + (yp / ySampling) * yStride
77     //
78     // where xp and yp are computed as follows:
79     //
80     //  * If we are reading or writing a scanline-based file:
81     //
82     //      xp = x
83     //      yp = y
84     //
85     //  * If we are reading a tile whose upper left coorner is at (xt, yt):
86     //
87     //      if xTileCoords is true then xp = x - xt, else xp = x
88     //      if yTileCoords is true then yp = y - yt, else yp = y
89     //
90     //---------------------------------------------------------------------
91
92     char *              base;
93     size_t              xStride;
94     size_t              yStride;
95
96
97     //--------------------------------------------
98     // Subsampling: pixel (x, y) is present in the
99     // slice only if 
100     //
101     //  x % xSampling == 0 && y % ySampling == 0
102     //
103     //--------------------------------------------
104
105     int                 xSampling;
106     int                 ySampling;
107
108
109     //----------------------------------------------------------
110     // Default value, used to fill the slice when a file without
111     // a channel that corresponds to this slice is read.
112     //----------------------------------------------------------
113
114     double              fillValue;
115     
116
117     //-------------------------------------------------------
118     // For tiled files, the xTileCoords and yTileCoords flags
119     // determine whether pixel addressing is performed using
120     // absolute coordinates or coordinates relative to a
121     // tile's upper left corner.  (See the comment on base,
122     // xStride and yStride, above.)
123     //
124     // For scanline-based files these flags have no effect;
125     // pixel addressing is always done using absolute
126     // coordinates.
127     //-------------------------------------------------------
128
129     bool                xTileCoords;
130     bool                yTileCoords;
131
132
133     //------------
134     // Constructor
135     //------------
136
137     Slice (PixelType type = HALF,
138            char * base = 0,
139            size_t xStride = 0,
140            size_t yStride = 0,
141            int xSampling = 1,
142            int ySampling = 1,
143            double fillValue = 0.0,
144            bool xTileCoords = false,
145            bool yTileCoords = false);
146 };
147
148
149 class FrameBuffer
150 {
151   public:
152
153     //------------
154     // Add a slice
155     //------------
156
157     void                        insert (const char name[],
158                                         const Slice &slice);
159
160     //----------------------------------------------------------------
161     // Access to existing slices:
162     //
163     // [n]              Returns a reference to the slice with name n.
164     //                  If no slice with name n exists, an Iex::ArgExc
165     //                  is thrown.
166     //
167     // findSlice(n)     Returns a pointer to the slice with name n,
168     //                  or 0 if no slice with name n exists.
169     //
170     //----------------------------------------------------------------
171
172     Slice &                     operator [] (const char name[]);
173     const Slice &               operator [] (const char name[]) const;
174
175     Slice *                     findSlice (const char name[]);
176     const Slice *               findSlice (const char name[]) const;
177
178
179     //-----------------------------------------
180     // Iterator-style access to existing slices
181     //-----------------------------------------
182
183     typedef std::map <Name, Slice> SliceMap;
184
185     class Iterator;
186     class ConstIterator;
187
188     Iterator                    begin ();
189     ConstIterator               begin () const;
190     Iterator                    end ();
191     ConstIterator               end () const;
192     Iterator                    find (const char name[]);
193     ConstIterator               find (const char name[]) const;
194
195   private:
196
197     SliceMap                    _map;
198 };
199
200
201 //----------
202 // Iterators
203 //----------
204
205 class FrameBuffer::Iterator
206 {
207   public:
208
209     Iterator ();
210     Iterator (const FrameBuffer::SliceMap::iterator &i);
211
212     Iterator &                  operator ++ ();
213     Iterator                    operator ++ (int);
214
215     const char *                name () const;
216     Slice &                     slice () const;
217
218   private:
219
220     friend class FrameBuffer::ConstIterator;
221
222     FrameBuffer::SliceMap::iterator _i;
223 };
224
225
226 class FrameBuffer::ConstIterator
227 {
228   public:
229
230     ConstIterator ();
231     ConstIterator (const FrameBuffer::SliceMap::const_iterator &i);
232     ConstIterator (const FrameBuffer::Iterator &other);
233
234     ConstIterator &             operator ++ ();
235     ConstIterator               operator ++ (int);
236
237     const char *                name () const;
238     const Slice &               slice () const;
239
240   private:
241
242     friend bool operator == (const ConstIterator &, const ConstIterator &);
243     friend bool operator != (const ConstIterator &, const ConstIterator &);
244
245     FrameBuffer::SliceMap::const_iterator _i;
246 };
247
248
249 //-----------------
250 // Inline Functions
251 //-----------------
252
253 inline
254 FrameBuffer::Iterator::Iterator (): _i()
255 {
256     // empty
257 }
258
259
260 inline
261 FrameBuffer::Iterator::Iterator (const FrameBuffer::SliceMap::iterator &i):
262     _i (i)
263 {
264     // empty
265 }
266
267
268 inline FrameBuffer::Iterator &          
269 FrameBuffer::Iterator::operator ++ ()
270 {
271     ++_i;
272     return *this;
273 }
274
275
276 inline FrameBuffer::Iterator    
277 FrameBuffer::Iterator::operator ++ (int)
278 {
279     Iterator tmp = *this;
280     ++_i;
281     return tmp;
282 }
283
284
285 inline const char *
286 FrameBuffer::Iterator::name () const
287 {
288     return *_i->first;
289 }
290
291
292 inline Slice &  
293 FrameBuffer::Iterator::slice () const
294 {
295     return _i->second;
296 }
297
298
299 inline
300 FrameBuffer::ConstIterator::ConstIterator (): _i()
301 {
302     // empty
303 }
304
305 inline
306 FrameBuffer::ConstIterator::ConstIterator
307     (const FrameBuffer::SliceMap::const_iterator &i): _i (i)
308 {
309     // empty
310 }
311
312
313 inline
314 FrameBuffer::ConstIterator::ConstIterator (const FrameBuffer::Iterator &other):
315     _i (other._i)
316 {
317     // empty
318 }
319
320 inline FrameBuffer::ConstIterator &
321 FrameBuffer::ConstIterator::operator ++ ()
322 {
323     ++_i;
324     return *this;
325 }
326
327
328 inline FrameBuffer::ConstIterator               
329 FrameBuffer::ConstIterator::operator ++ (int)
330 {
331     ConstIterator tmp = *this;
332     ++_i;
333     return tmp;
334 }
335
336
337 inline const char *
338 FrameBuffer::ConstIterator::name () const
339 {
340     return *_i->first;
341 }
342
343 inline const Slice &    
344 FrameBuffer::ConstIterator::slice () const
345 {
346     return _i->second;
347 }
348
349
350 inline bool
351 operator == (const FrameBuffer::ConstIterator &x,
352              const FrameBuffer::ConstIterator &y)
353 {
354     return x._i == y._i;
355 }
356
357
358 inline bool
359 operator != (const FrameBuffer::ConstIterator &x,
360              const FrameBuffer::ConstIterator &y)
361 {
362     return !(x == y);
363 }
364
365
366 } // namespace Imf
367
368 #endif