HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
image_view.h
Go to the documentation of this file.
1 /*
2  Copyright 2014 Larry Gritz and the other authors and contributors.
3  All Rights Reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are
7  met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the software's owners nor the names of its
14  contributors may be used to endorse or promote products derived from
15  this software without specific prior written permission.
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28  (This is the Modified BSD License)
29 */
30 
31 
32 #pragma once
33 
34 #include <stdexcept>
35 #include <vector>
36 
37 // We're including stdint.h to get int64_t and INT64_MIN. But on some
38 // platforms, stdint.h only defines them if __STDC_LIMIT_MACROS is defined,
39 // so we do so. But, oops, if user code included stdint.h before this file,
40 // and without defining the macro, it may have had ints one and only include
41 // and not seen the definitions we need, so at least try to make a helpful
42 // compile-time error in that case.
43 // And very old MSVC 9 versions don't even have stdint.h.
44 #if defined(_MSC_VER) && _MSC_VER < 1600
45 typedef __int64 int64_t;
46 #else
47 # ifndef __STDC_LIMIT_MACROS
48 # define __STDC_LIMIT_MACROS /* needed for some defs in stdint.h */
49 # endif
50 # include <cstdint>
51 # if !defined(INT64_MIN)
52 # error You must define __STDC_LIMIT_MACROS prior to including stdint.h
53 # endif
54 #endif
55 
58 
59 
61 
62 
63 /// image_view : a non-owning reference to an image-like array (indexed by
64 /// x, y, z, and channel) with known dimensions and optionally non-default
65 /// strides (expressed in bytes) through the data. An image_view<T> is
66 /// mutable (the values in the image may be modified), whereas an
67 /// image_view<const T> is not mutable.
68 template<typename T> class image_view {
69 public:
70  typedef T value_type;
71  typedef T& reference;
72  typedef const T& const_reference;
73  typedef int64_t stride_t;
74 #ifdef INT64_MIN
75  static const stride_t AutoStride = INT64_MIN;
76 #else
77  // Some systems don't have INT64_MIN defined. Sheesh.
78  static const stride_t AutoStride = (-9223372036854775807LL - 1);
79 #endif
80 
81  /// Default ctr -- points to nothing
82  image_view() { init(); }
83 
84  /// Copy constructor
86  {
87  init(copy.m_data, copy.m_nchannels, copy.m_width, copy.m_height,
88  copy.m_depth, copy.m_chanstride, copy.m_xstride, copy.m_ystride,
89  copy.m_zstride);
90  }
91 
92  /// Construct from T*, dimensions, and (possibly default) strides (in
93  /// bytes).
94  image_view(T* data, int nchannels, int width, int height, int depth = 1,
97  {
98  init(data, nchannels, width, height, depth, chanstride, xstride,
99  ystride, zstride);
100  }
101 
102  /// assignments -- not a deep copy, just make this image_view
103  /// point to the same data as the operand.
105  {
106  init(copy.m_data, copy.m_nchannels, copy.m_width, copy.m_height,
107  copy.m_depth, copy.m_chanstride, copy.m_xstride, copy.m_ystride,
108  copy.m_zstride);
109  return *this;
110  }
111 
112  /// iav(x,y,z)returns a strided_ptr<T,1> for the pixel (x,y,z). The z
113  /// can be omitted for 2D images. Note than the resulting
114  /// strided_ptr can then have individual channels accessed with
115  /// operator[]. This particular strided pointer has stride multiplier
116  /// 1, because this class uses bytes as strides, not sizeof(T).
117  strided_ptr<T, 1> operator()(int x, int y, int z = 0)
118  {
119  return strided_ptr<T, 1>(getptr(0, x, y, z), m_chanstride);
120  }
121 
122  int nchannels() const { return m_nchannels; }
123  int width() const { return m_width; }
124  int height() const { return m_height; }
125  int depth() const { return m_depth; }
126 
127  stride_t chanstride() const { return m_chanstride; }
128  stride_t xstride() const { return m_xstride; }
129  stride_t ystride() const { return m_ystride; }
130  stride_t zstride() const { return m_zstride; }
131 
132  const T* data() const { return m_data; }
133 
134  void clear() { init(); }
135 
136 private:
137  const T* m_data;
138  int m_nchannels, m_width, m_height, m_depth;
139  stride_t m_chanstride, m_xstride, m_ystride, m_zstride;
140 
141  void init(T* data, int nchannels, int width, int height, int depth = 1,
144  {
145  m_data = data;
146  m_nchannels = nchannels;
147  m_width = width;
148  m_height = height;
149  m_depth = depth;
150  m_chanstride = chanstride != AutoStride ? chanstride : sizeof(T);
151  m_xstride = xstride != AutoStride ? xstride
152  : m_nchannels * m_chanstride;
153  m_ystride = ystride != AutoStride ? ystride : m_width * m_xstride;
154  m_zstride = zstride != AutoStride ? zstride : m_height * m_ystride;
155  }
156 
157  inline T* getptr(int c, int x, int y, int z = 0) const
158  {
159  return (T*)((char*)m_data + c * m_chanstride + x * m_xstride
160  + y * m_ystride + z * m_zstride);
161  }
162  inline T& get(int c, int x, int y, int z = 0) const
163  {
164  return *getptr(c, x, y, z);
165  }
166 };
167 
168 
static const stride_t AutoStride
Definition: image_view.h:78
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1252
image_view & operator=(const image_view &copy)
Definition: image_view.h:104
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1252
image_view(T *data, int nchannels, int width, int height, int depth=1, stride_t chanstride=AutoStride, stride_t xstride=AutoStride, stride_t ystride=AutoStride, stride_t zstride=AutoStride)
Definition: image_view.h:94
stride_t zstride() const
Definition: image_view.h:130
image_view()
Default ctr – points to nothing.
Definition: image_view.h:82
GLdouble GLdouble z
Definition: glew.h:1559
stride_t ystride() const
Definition: image_view.h:129
int64_t stride_t
Definition: image_view.h:73
GLint GLint GLint GLint GLint x
Definition: glew.h:1252
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1252
GLint GLenum GLsizei GLint GLsizei const void * data
Definition: glew.h:1379
strided_ptr< T, 1 > operator()(int x, int y, int z=0)
Definition: image_view.h:117
const GLfloat * c
Definition: glew.h:16296
const T * data() const
Definition: image_view.h:132
int nchannels() const
Definition: image_view.h:122
ptrdiff_t stride_t
Definition: imageio.h:69
const T & const_reference
Definition: image_view.h:72
stride_t chanstride() const
Definition: image_view.h:127
int width() const
Definition: image_view.h:123
int depth() const
Definition: image_view.h:125
image_view(const image_view &copy)
Copy constructor.
Definition: image_view.h:85
stride_t xstride() const
Definition: image_view.h:128
OIIO_API bool copy(string_view from, string_view to, std::string &err)
void clear()
Definition: image_view.h:134
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:66
int height() const
Definition: image_view.h:124
T & reference
Definition: image_view.h:71
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glew.h:1254
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:65