Update to 2.0.0 tree from current Fremantle build
[opencv] / 3rdparty / include / OpenEXR / ImfChannelList.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_CHANNEL_LIST_H
38 #define INCLUDED_IMF_CHANNEL_LIST_H
39
40 //-----------------------------------------------------------------------------
41 //
42 //      class Channel
43 //      class ChannelList
44 //
45 //-----------------------------------------------------------------------------
46
47 #include <ImfName.h>
48 #include <ImfPixelType.h>
49 #include <map>
50 #include <set>
51 #include <string>
52
53
54 namespace Imf {
55
56
57 struct Channel
58 {
59     //------------------------------
60     // Data type; see ImfPixelType.h
61     //------------------------------
62
63     PixelType           type;
64
65
66     //--------------------------------------------
67     // Subsampling: pixel (x, y) is present in the
68     // channel only if 
69     //
70     //  x % xSampling == 0 && y % ySampling == 0
71     //
72     //--------------------------------------------
73
74     int                 xSampling;
75     int                 ySampling;
76
77
78     //------------
79     // Constructor
80     //------------
81     
82     Channel (PixelType type = HALF,
83              int xSampling = 1,
84              int ySampling = 1);
85
86
87     //------------
88     // Operator ==
89     //------------
90
91     bool                operator == (const Channel &other) const;
92 };
93
94
95 class ChannelList
96 {
97   public:
98
99     //--------------
100     // Add a channel
101     //--------------
102
103     void                        insert (const char name[],
104                                         const Channel &channel);
105
106     //------------------------------------------------------------------
107     // Access to existing channels:
108     //
109     // [n]              Returns a reference to the channel with name n.
110     //                  If no channel with name n exists, an Iex::ArgExc
111     //                  is thrown.
112     //
113     // findChannel(n)   Returns a pointer to the channel with name n,
114     //                  or 0 if no channel with name n exists.
115     //
116     //------------------------------------------------------------------
117
118     Channel &                   operator [] (const char name[]);
119     const Channel &             operator [] (const char name[]) const;
120
121     Channel *                   findChannel (const char name[]);
122     const Channel *             findChannel (const char name[]) const;
123
124
125     //-------------------------------------------
126     // Iterator-style access to existing channels
127     //-------------------------------------------
128
129     typedef std::map <Name, Channel> ChannelMap;
130
131     class Iterator;
132     class ConstIterator;
133
134     Iterator                    begin ();
135     ConstIterator               begin () const;
136     Iterator                    end ();
137     ConstIterator               end () const;
138     Iterator                    find (const char name[]);
139     ConstIterator               find (const char name[]) const;
140
141     
142     //-----------------------------------------------------------------
143     // Support for image layers:
144     //
145     // In an image file with many channels it is sometimes useful to
146     // group the channels into "layers", that is, into sets of channels
147     // that logically belong together.  Grouping channels into layers
148     // is done using a naming convention:  channel C in layer L is
149     // called "L.C".
150     //
151     // For example, a computer graphic image may contain separate
152     // R, G and B channels for light that originated at each of
153     // several different virtual light sources.  The channels in
154     // this image might be called "light1.R", "light1.G", "light1.B",
155     // "light2.R", "light2.G", "light2.B", etc.
156     // 
157     // Note that this naming convention allows layers to be nested;
158     // for example, "light1.specular.R" identifies the "R" channel
159     // in the "specular" sub-layer of layer "light1".
160     //
161     // Channel names that don't contain a "." or that contain a
162     // "." only at the beginning or at the end are not considered
163     // to be part of any layer.
164     //
165     // layers(lns)              sorts the channels in this ChannelList
166     //                          into layers and stores the names of
167     //                          all layers, sorted alphabetically,
168     //                          into string set lns.
169     //
170     // channelsInLayer(ln,f,l)  stores a pair of iterators in f and l
171     //                          such that the loop
172     //
173     //                          for (ConstIterator i = f; i != l; ++i)
174     //                             ...
175     //
176     //                          iterates over all channels in layer ln.
177     //                          channelsInLayer (ln, l, p) calls
178     //                          channelsWithPrefix (ln + ".", l, p).
179     //
180     //-----------------------------------------------------------------
181
182     void                layers (std::set <std::string> &layerNames) const;
183
184     void                channelsInLayer (const std::string &layerName,
185                                          Iterator &first,
186                                          Iterator &last);
187
188     void                channelsInLayer (const std::string &layerName,
189                                          ConstIterator &first,
190                                          ConstIterator &last) const;
191
192
193     //-------------------------------------------------------------------
194     // Find all channels whose name begins with a given prefix:
195     //
196     // channelsWithPrefix(p,f,l) stores a pair of iterators in f and l
197     // such that the following loop iterates over all channels whose name
198     // begins with string p:
199     //
200     //          for (ConstIterator i = f; i != l; ++i)
201     //              ...
202     //
203     //-------------------------------------------------------------------
204
205     void                        channelsWithPrefix (const char prefix[],
206                                                     Iterator &first,
207                                                     Iterator &last);
208
209     void                        channelsWithPrefix (const char prefix[],
210                                                     ConstIterator &first,
211                                                     ConstIterator &last) const;
212
213     //------------
214     // Operator ==
215     //------------
216
217     bool                        operator == (const ChannelList &other) const;
218
219   private:
220
221     ChannelMap                  _map;
222 };
223
224
225 //----------
226 // Iterators
227 //----------
228
229 class ChannelList::Iterator
230 {
231   public:
232
233     Iterator ();
234     Iterator (const ChannelList::ChannelMap::iterator &i);
235
236     Iterator &                  operator ++ ();
237     Iterator                    operator ++ (int);
238
239     const char *                name () const;
240     Channel &                   channel () const;
241
242   private:
243
244     friend class ChannelList::ConstIterator;
245
246     ChannelList::ChannelMap::iterator _i;
247 };
248
249
250 class ChannelList::ConstIterator
251 {
252   public:
253
254     ConstIterator ();
255     ConstIterator (const ChannelList::ChannelMap::const_iterator &i);
256     ConstIterator (const ChannelList::Iterator &other);
257
258     ConstIterator &             operator ++ ();
259     ConstIterator               operator ++ (int);
260
261     const char *                name () const;
262     const Channel &             channel () const;
263
264   private:
265
266     friend bool operator == (const ConstIterator &, const ConstIterator &);
267     friend bool operator != (const ConstIterator &, const ConstIterator &);
268
269     ChannelList::ChannelMap::const_iterator _i;
270 };
271
272
273 //-----------------
274 // Inline Functions
275 //-----------------
276
277 inline
278 ChannelList::Iterator::Iterator (): _i()
279 {
280     // empty
281 }
282
283
284 inline
285 ChannelList::Iterator::Iterator (const ChannelList::ChannelMap::iterator &i):
286     _i (i)
287 {
288     // empty
289 }
290
291
292 inline ChannelList::Iterator &          
293 ChannelList::Iterator::operator ++ ()
294 {
295     ++_i;
296     return *this;
297 }
298
299
300 inline ChannelList::Iterator    
301 ChannelList::Iterator::operator ++ (int)
302 {
303     Iterator tmp = *this;
304     ++_i;
305     return tmp;
306 }
307
308
309 inline const char *
310 ChannelList::Iterator::name () const
311 {
312     return *_i->first;
313 }
314
315
316 inline Channel &        
317 ChannelList::Iterator::channel () const
318 {
319     return _i->second;
320 }
321
322
323 inline
324 ChannelList::ConstIterator::ConstIterator (): _i()
325 {
326     // empty
327 }
328
329 inline
330 ChannelList::ConstIterator::ConstIterator
331     (const ChannelList::ChannelMap::const_iterator &i): _i (i)
332 {
333     // empty
334 }
335
336
337 inline
338 ChannelList::ConstIterator::ConstIterator (const ChannelList::Iterator &other):
339     _i (other._i)
340 {
341     // empty
342 }
343
344 inline ChannelList::ConstIterator &
345 ChannelList::ConstIterator::operator ++ ()
346 {
347     ++_i;
348     return *this;
349 }
350
351
352 inline ChannelList::ConstIterator               
353 ChannelList::ConstIterator::operator ++ (int)
354 {
355     ConstIterator tmp = *this;
356     ++_i;
357     return tmp;
358 }
359
360
361 inline const char *
362 ChannelList::ConstIterator::name () const
363 {
364     return *_i->first;
365 }
366
367 inline const Channel &  
368 ChannelList::ConstIterator::channel () const
369 {
370     return _i->second;
371 }
372
373
374 inline bool
375 operator == (const ChannelList::ConstIterator &x,
376              const ChannelList::ConstIterator &y)
377 {
378     return x._i == y._i;
379 }
380
381
382 inline bool
383 operator != (const ChannelList::ConstIterator &x,
384              const ChannelList::ConstIterator &y)
385 {
386     return !(x == y);
387 }
388
389
390 } // namespace Imf
391
392 #endif