1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef IMFDEEPFRAMEBUFFER_H_
7 #define IMFDEEPFRAMEBUFFER_H_
8 
9 #include "ImfForward.h"
10 
11 #include "ImfFrameBuffer.h"
12 
13 OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
14 
15 //--------------------------------------------------------
16 // Description of a single deep slice of the frame buffer:
17 //--------------------------------------------------------
18 
19 struct IMF_EXPORT_TYPE DeepSlice : public Slice
20 {
21     //---------------------------------------------------------------------
22     // The stride for each sample in this slice.
23     //
24     // Memory layout:  The address of sample i in pixel (x, y) is
25     //
26     //  base + (xp / xSampling) * xStride + (yp / ySampling) * yStride
27     //       + i * sampleStride
28     //
29     // where xp and yp are computed as follows:
30     //
31     //  * If we are reading or writing a scanline-based file:
32     //
33     //      xp = x
34     //      yp = y
35     //
36     //  * If we are reading a tile whose upper left coorner is at (xt, yt):
37     //
38     //      if xTileCoords is true then xp = x - xt, else xp = x
39     //      if yTileCoords is true then yp = y - yt, else yp = y
40     //
41     //---------------------------------------------------------------------
42 
43     int sampleStride;
44 
45     //------------
46     // Constructor
47     //------------
48     IMF_EXPORT
49     DeepSlice (PixelType type = HALF,
50                char * base = 0,
51                size_t xStride = 0,
52                size_t yStride = 0,
53                size_t sampleStride = 0,
54                int xSampling = 1,
55                int ySampling = 1,
56                double fillValue = 0.0,
57                bool xTileCoords = false,
58                bool yTileCoords = false);
59 };
60 
61 //-----------------
62 // DeepFrameBuffer.
63 //-----------------
64 
65 class IMF_EXPORT_TYPE DeepFrameBuffer
66 {
67   public:
68 
69 
70     //------------
71     // Add a slice
72     //------------
73 
74     IMF_EXPORT
75     void                        insert (const char name[],
76                                         const DeepSlice &slice);
77 
78     IMF_EXPORT
79     void                        insert (const std::string &name,
80                                         const DeepSlice &slice);
81 
82     //----------------------------------------------------------------
83     // Access to existing slices:
84     //
85     // [n]              Returns a reference to the slice with name n.
86     //                  If no slice with name n exists, an IEX_NAMESPACE::ArgExc
87     //                  is thrown.
88     //
89     // findSlice(n)     Returns a pointer to the slice with name n,
90     //                  or 0 if no slice with name n exists.
91     //
92     //----------------------------------------------------------------
93 
94     IMF_EXPORT
95     DeepSlice &                 operator [] (const char name[]);
96     IMF_EXPORT
97     const DeepSlice &           operator [] (const char name[]) const;
98 
99     IMF_EXPORT
100     DeepSlice &                 operator [] (const std::string &name);
101     IMF_EXPORT
102     const DeepSlice &           operator [] (const std::string &name) const;
103 
104     IMF_EXPORT
105     DeepSlice *                 findSlice (const char name[]);
106     IMF_EXPORT
107     const DeepSlice *           findSlice (const char name[]) const;
108 
109     IMF_EXPORT
110     DeepSlice *                 findSlice (const std::string &name);
111     IMF_EXPORT
112     const DeepSlice *           findSlice (const std::string &name) const;
113 
114 
115     //-----------------------------------------
116     // Iterator-style access to existing slices
117     //-----------------------------------------
118 
119     typedef std::map <Name, DeepSlice> SliceMap;
120 
121     class Iterator;
122     class ConstIterator;
123 
124     IMF_EXPORT
125     Iterator                    begin ();
126     IMF_EXPORT
127     ConstIterator               begin () const;
128 
129     IMF_EXPORT
130     Iterator                    end ();
131     IMF_EXPORT
132     ConstIterator               end () const;
133 
134     IMF_EXPORT
135     Iterator                    find (const char name[]);
136     IMF_EXPORT
137     ConstIterator               find (const char name[]) const;
138 
139     IMF_EXPORT
140     Iterator                    find (const std::string &name);
141     IMF_EXPORT
142     ConstIterator               find (const std::string &name) const;
143 
144     //----------------------------------------------------
145     // Public function for accessing a sample count slice.
146     //----------------------------------------------------
147 
148     IMF_EXPORT
149     void                        insertSampleCountSlice(const Slice & slice);
150     IMF_EXPORT
151     const Slice &               getSampleCountSlice() const;
152 
153   private:
154 
155     SliceMap                    _map;
156     Slice                       _sampleCounts;
157 };
158 
159 //----------
160 // Iterators
161 //----------
162 
163 class IMF_EXPORT_TYPE DeepFrameBuffer::Iterator
164 {
165   public:
166 
167     IMF_EXPORT
168     Iterator ();
169     IMF_EXPORT
170     Iterator (const DeepFrameBuffer::SliceMap::iterator &i);
171 
172     IMF_EXPORT
173     Iterator &                  operator ++ ();
174     IMF_EXPORT
175     Iterator                    operator ++ (int);
176 
177     IMF_EXPORT
178     const char *                name () const;
179     IMF_EXPORT
180     DeepSlice &                 slice () const;
181 
182   private:
183 
184     friend class DeepFrameBuffer::ConstIterator;
185 
186     DeepFrameBuffer::SliceMap::iterator _i;
187 };
188 
189 
190 class IMF_EXPORT_TYPE DeepFrameBuffer::ConstIterator
191 {
192   public:
193 
194     IMF_EXPORT
195     ConstIterator ();
196     IMF_EXPORT
197     ConstIterator (const DeepFrameBuffer::SliceMap::const_iterator &i);
198     IMF_EXPORT
199     ConstIterator (const DeepFrameBuffer::Iterator &other);
200 
201     IMF_EXPORT
202     ConstIterator &             operator ++ ();
203     IMF_EXPORT
204     ConstIterator               operator ++ (int);
205 
206     IMF_EXPORT
207     const char *                name () const;
208     IMF_EXPORT
209     const DeepSlice &           slice () const;
210 
211   private:
212 
213     friend bool operator == (const ConstIterator &, const ConstIterator &);
214     friend bool operator != (const ConstIterator &, const ConstIterator &);
215 
216     DeepFrameBuffer::SliceMap::const_iterator _i;
217 };
218 
219 
220 //-----------------
221 // Inline Functions
222 //-----------------
223 
224 inline
Iterator()225 DeepFrameBuffer::Iterator::Iterator (): _i()
226 {
227     // empty
228 }
229 
230 
231 inline
Iterator(const DeepFrameBuffer::SliceMap::iterator & i)232 DeepFrameBuffer::Iterator::Iterator (const DeepFrameBuffer::SliceMap::iterator &i):
233     _i (i)
234 {
235     // empty
236 }
237 
238 
239 inline DeepFrameBuffer::Iterator &
240 DeepFrameBuffer::Iterator::operator ++ ()
241 {
242     ++_i;
243     return *this;
244 }
245 
246 
247 inline DeepFrameBuffer::Iterator
248 DeepFrameBuffer::Iterator::operator ++ (int)
249 {
250     Iterator tmp = *this;
251     ++_i;
252     return tmp;
253 }
254 
255 
256 inline const char *
name()257 DeepFrameBuffer::Iterator::name () const
258 {
259     return *_i->first;
260 }
261 
262 
263 inline DeepSlice &
slice()264 DeepFrameBuffer::Iterator::slice () const
265 {
266     return _i->second;
267 }
268 
269 
270 inline
ConstIterator()271 DeepFrameBuffer::ConstIterator::ConstIterator (): _i()
272 {
273     // empty
274 }
275 
276 inline
ConstIterator(const DeepFrameBuffer::SliceMap::const_iterator & i)277 DeepFrameBuffer::ConstIterator::ConstIterator
278     (const DeepFrameBuffer::SliceMap::const_iterator &i): _i (i)
279 {
280     // empty
281 }
282 
283 
284 inline
ConstIterator(const DeepFrameBuffer::Iterator & other)285 DeepFrameBuffer::ConstIterator::ConstIterator (const DeepFrameBuffer::Iterator &other):
286     _i (other._i)
287 {
288     // empty
289 }
290 
291 inline DeepFrameBuffer::ConstIterator &
292 DeepFrameBuffer::ConstIterator::operator ++ ()
293 {
294     ++_i;
295     return *this;
296 }
297 
298 
299 inline DeepFrameBuffer::ConstIterator
300 DeepFrameBuffer::ConstIterator::operator ++ (int)
301 {
302     ConstIterator tmp = *this;
303     ++_i;
304     return tmp;
305 }
306 
307 
308 inline const char *
name()309 DeepFrameBuffer::ConstIterator::name () const
310 {
311     return *_i->first;
312 }
313 
314 inline const DeepSlice &
slice()315 DeepFrameBuffer::ConstIterator::slice () const
316 {
317     return _i->second;
318 }
319 
320 
321 inline bool
322 operator == (const DeepFrameBuffer::ConstIterator &x,
323              const DeepFrameBuffer::ConstIterator &y)
324 {
325     return x._i == y._i;
326 }
327 
328 
329 inline bool
330 operator != (const DeepFrameBuffer::ConstIterator &x,
331              const DeepFrameBuffer::ConstIterator &y)
332 {
333     return !(x == y);
334 }
335 
336 
337 OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
338 
339 
340 
341 
342 
343 
344 #endif /* IMFDEEPFRAMEBUFFER_H_ */
345