HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ImfFlatImageChannel.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2014, 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 #ifndef INCLUDED_IMF_FLAT_IMAGE_CHANNEL_H
36 #define INCLUDED_IMF_FLAT_IMAGE_CHANNEL_H
37 
38 //----------------------------------------------------------------------------
39 //
40 // class FlatImageChannel,
41 // template class TypedFlatImageChannel<T>
42 //
43 // For an explanation of images, levels and channels,
44 // see the comments in header file Image.h.
45 //
46 //----------------------------------------------------------------------------
47 
48 #include <ImfImageChannel.h>
49 #include <ImfPixelType.h>
50 #include <ImfFrameBuffer.h>
51 #include <ImathBox.h>
52 #include <half.h>
53 
55 
56 class FlatImageLevel;
57 
58 //
59 // Image channels:
60 //
61 // A TypedFlatImageChannel<T> holds the pixel data for a single channel
62 // of one level of a flat image. The pixels in the channel are of type T,
63 // where T is either half, float or unsigned int. Storage is allocated
64 // only for pixels within the data window of the level.
65 //
66 
68 {
69  public:
70 
71  //
72  // Construct an OpenEXR frame buffer slice for this channel.
73  // This function is needed reading an image from an OpenEXR
74  // file and for saving an image in an OpenEXR file.
75  //
76 
77  virtual Slice slice () const = 0;
78 
79 
80  //
81  // Access to the flat image level to which this channel belongs.
82  //
83 
84  FlatImageLevel & flatLevel ();
85  const FlatImageLevel & flatLevel () const;
86 
87  protected:
88 
89  friend class FlatImageLevel;
90 
92  int xSampling,
93  int ySampling,
94  bool pLinear);
95 
96  virtual ~FlatImageChannel();
97 
98  virtual void resize ();
99 
100  virtual void resetBasePointer () = 0;
101 };
102 
103 
104 template <class T>
106 {
107  public:
108 
109  //
110  // The OpenEXR pixel type of this channel (HALF, FLOAT or UINT).
111  //
112 
113  virtual PixelType pixelType () const;
114 
115 
116  //
117  // Construct an OpenEXR frame buffer slice for this channel.
118  //
119 
120  virtual Slice slice () const;
121 
122 
123  //
124  // Access to the pixel at pixel space location (x, y), without
125  // bounds checking. Accessing a location outside the data window
126  // of the image level results in undefined behavior.
127  //
128 
129  T & operator () (int x, int y);
130  const T & operator () (int x, int y) const;
131 
132 
133  //
134  // Access to the pixel at pixel space location (x, y), with bounds
135  // checking. Accessing a location outside the data window of the
136  // image level throws an Iex::ArgExc exception.
137  //
138 
139  T & at (int x, int y);
140  const T & at (int x, int y) const;
141 
142  //
143  // Faster access to all pixels in a single horizontal row of the
144  // channel. Rows are numbered from 0 to pixelsPerColumn()-1, and
145  // each row contains pixelsPerRow() values.
146  // Access is not bounds checked; accessing out of bounds rows or
147  // pixels results in undefined behavior.
148  //
149 
150  T * row (int r);
151  const T * row (int r) const;
152 
153  private:
154 
155  friend class FlatImageLevel;
156 
157  //
158  // The constructor and destructor are not public because flat
159  // image channels exist only as parts of a flat image level.
160  //
161 
163  int xSampling,
164  int ySampling,
165  bool pLinear);
166 
167  virtual ~TypedFlatImageChannel ();
168 
169  virtual void resize ();
170 
171  virtual void resetBasePointer ();
172 
173  T * _pixels; // Pointer to allocated storage
174  T * _base; // Base pointer for faster pixel access
175 };
176 
177 
178 //
179 // Channel typedefs for the pixel data types supported by OpenEXR.
180 //
181 
185 
186 
187 //-----------------------------------------------------------------------------
188 // Implementation of templates and inline functions
189 //-----------------------------------------------------------------------------
190 
191 
192 template <class T>
195  int xSampling,
196  int ySampling,
197  bool pLinear)
198 :
199  FlatImageChannel (level, xSampling, ySampling, pLinear),
200  _pixels (0),
201  _base (0)
202 {
203  resize();
204 }
205 
206 
207 template <class T>
209 {
210  delete [] _pixels;
211 }
212 
213 
214 template <>
215 inline PixelType
217 {
218  return HALF;
219 }
220 
221 
222 template <>
223 inline PixelType
225 {
226  return FLOAT;
227 }
228 
229 
230 template <>
231 inline PixelType
233 {
234  return UINT;
235 }
236 
237 
238 template <class T>
239 Slice
241 {
242  return Slice (pixelType(), // type
243  (char *) _base, // base
244  sizeof (T), // xStride
245  pixelsPerRow() * sizeof (T), // yStride
246  xSampling(),
247  ySampling());
248 }
249 
250 
251 template <class T>
252 inline T &
254 {
255  return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
256 }
257 
258 
259 template <class T>
260 inline const T &
262 {
263  return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
264 }
265 
266 
267 template <class T>
268 inline T &
270 {
271  boundsCheck (x, y);
272  return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
273 }
274 
275 
276 template <class T>
277 inline const T &
279 {
280  boundsCheck (x, y);
281  return _base[(y / ySampling()) * pixelsPerRow() + (x / xSampling())];
282 }
283 
284 
285 template <class T>
286 inline T *
288 {
289  return _base + r * pixelsPerRow();
290 }
291 
292 
293 template <class T>
294 inline const T *
296 {
297  return _base + n * pixelsPerRow();
298 }
299 
300 
301 template <class T>
302 void
304 {
305  delete [] _pixels;
306  _pixels = 0;
307 
308  FlatImageChannel::resize(); // may throw an exception
309 
310  _pixels = new T [numPixels()];
311 
312  for (size_t i = 0; i < numPixels(); ++i)
313  _pixels[i] = T (0);
314 
315  resetBasePointer ();
316 }
317 
318 
319 template <class T>
320 void
322 {
323  _base = _pixels -
324  (level().dataWindow().min.y / ySampling()) * pixelsPerRow() -
325  (level().dataWindow().min.x / xSampling());
326 }
327 
329 
330 #endif
virtual PixelType pixelType() const
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:109
int ySampling() const
virtual void resize()
GLint level
Definition: glcorearb.h:107
GLint y
Definition: glcorearb.h:102
png_uint_32 i
Definition: png.h:2877
virtual void resize()
GLdouble n
Definition: glcorearb.h:2007
PixelType
Definition: ImfPixelType.h:51
bool pLinear() const
int xSampling() const
TypedFlatImageChannel< float > FlatFloatChannel
TypedFlatImageChannel< half > FlatHalfChannel
virtual Slice slice() const
#define IMF_EXPORT
Definition: ImfExport.h:59
T & operator()(int x, int y)
GLint GLenum GLint x
Definition: glcorearb.h:408
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:108
GLboolean r
Definition: glcorearb.h:1221
TypedFlatImageChannel< unsigned int > FlatUIntChannel