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 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio
4 
5 
6 #pragma once
7 
8 #include <stdexcept>
9 #include <vector>
10 
11 // We're including stdint.h to get int64_t and INT64_MIN. But on some
12 // platforms, stdint.h only defines them if __STDC_LIMIT_MACROS is defined,
13 // so we do so. But, oops, if user code included stdint.h before this file,
14 // and without defining the macro, it may have had ints one and only include
15 // and not seen the definitions we need, so at least try to make a helpful
16 // compile-time error in that case.
17 // And very old MSVC 9 versions don't even have stdint.h.
18 #if defined(_MSC_VER) && _MSC_VER < 1600
19 typedef __int64 int64_t;
20 #else
21 # ifndef __STDC_LIMIT_MACROS
22 # define __STDC_LIMIT_MACROS /* needed for some defs in stdint.h */
23 # endif
24 # include <cstdint>
25 # if !defined(INT64_MIN)
26 # error You must define __STDC_LIMIT_MACROS prior to including stdint.h
27 # endif
28 #endif
29 
32 
33 
35 
36 
37 /// image_view : a non-owning reference to an image-like array (indexed by
38 /// x, y, z, and channel) with known dimensions and optionally non-default
39 /// strides (expressed in bytes) through the data. An image_view<T> is
40 /// mutable (the values in the image may be modified), whereas an
41 /// image_view<const T> is not mutable.
42 template<typename T> class image_view {
43 public:
44  typedef T value_type;
45  typedef T& reference;
46  typedef const T& const_reference;
47  typedef int64_t stride_t;
48 #ifdef INT64_MIN
49  static const stride_t AutoStride = INT64_MIN;
50 #else
51  // Some systems don't have INT64_MIN defined. Sheesh.
52  static const stride_t AutoStride = (-9223372036854775807LL - 1);
53 #endif
54 
55  /// Default ctr -- points to nothing
56  image_view() { init(); }
57 
58  /// Copy constructor
60  {
61  init(copy.m_data, copy.m_nchannels, copy.m_width, copy.m_height,
62  copy.m_depth, copy.m_chanstride, copy.m_xstride, copy.m_ystride,
63  copy.m_zstride);
64  }
65 
66  /// Construct from T*, dimensions, and (possibly default) strides (in
67  /// bytes).
68  image_view(T* data, int nchannels, int width, int height, int depth = 1,
71  {
72  init(data, nchannels, width, height, depth, chanstride, xstride,
73  ystride, zstride);
74  }
75 
76  /// assignments -- not a deep copy, just make this image_view
77  /// point to the same data as the operand.
79  {
80  init(copy.m_data, copy.m_nchannels, copy.m_width, copy.m_height,
81  copy.m_depth, copy.m_chanstride, copy.m_xstride, copy.m_ystride,
82  copy.m_zstride);
83  return *this;
84  }
85 
86  /// iav(x,y,z)returns a strided_ptr<T,1> for the pixel (x,y,z). The z
87  /// can be omitted for 2D images. Note than the resulting
88  /// strided_ptr can then have individual channels accessed with
89  /// operator[]. This particular strided pointer has stride multiplier
90  /// 1, because this class uses bytes as strides, not sizeof(T).
91  strided_ptr<T, 1> operator()(int x, int y, int z = 0)
92  {
93  return strided_ptr<T, 1>(getptr(0, x, y, z), m_chanstride);
94  }
95 
96  int nchannels() const { return m_nchannels; }
97  int width() const { return m_width; }
98  int height() const { return m_height; }
99  int depth() const { return m_depth; }
100 
101  stride_t chanstride() const { return m_chanstride; }
102  stride_t xstride() const { return m_xstride; }
103  stride_t ystride() const { return m_ystride; }
104  stride_t zstride() const { return m_zstride; }
105 
106  const T* data() const { return m_data; }
107 
108  void clear() { init(); }
109 
110 private:
111  const T* m_data;
112  int m_nchannels, m_width, m_height, m_depth;
113  stride_t m_chanstride, m_xstride, m_ystride, m_zstride;
114 
115  void init(T* data, int nchannels, int width, int height, int depth = 1,
118  {
119  m_data = data;
120  m_nchannels = nchannels;
121  m_width = width;
122  m_height = height;
123  m_depth = depth;
124  m_chanstride = chanstride != AutoStride ? chanstride : sizeof(T);
125  m_xstride = xstride != AutoStride ? xstride
126  : m_nchannels * m_chanstride;
127  m_ystride = ystride != AutoStride ? ystride : m_width * m_xstride;
128  m_zstride = zstride != AutoStride ? zstride : m_height * m_ystride;
129  }
130 
131  inline T* getptr(int c, int x, int y, int z = 0) const
132  {
133  return (T*)((char*)m_data + c * m_chanstride + x * m_xstride
134  + y * m_ystride + z * m_zstride);
135  }
136  inline T& get(int c, int x, int y, int z = 0) const
137  {
138  return *getptr(c, x, y, z);
139  }
140 };
141 
142 
int64_t stride_t
Definition: imageio.h:48
static const stride_t AutoStride
Definition: image_view.h:52
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
image_view & operator=(const image_view &copy)
Definition: image_view.h:78
GLdouble GLdouble GLdouble z
Definition: glcorearb.h:848
GLint y
Definition: glcorearb.h:103
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:68
stride_t zstride() const
Definition: image_view.h:104
image_view()
Default ctr – points to nothing.
Definition: image_view.h:56
GLint GLsizei GLsizei height
Definition: glcorearb.h:103
stride_t ystride() const
Definition: image_view.h:103
int64_t stride_t
Definition: image_view.h:47
strided_ptr< T, 1 > operator()(int x, int y, int z=0)
Definition: image_view.h:91
const T * data() const
Definition: image_view.h:106
int nchannels() const
Definition: image_view.h:96
const T & const_reference
Definition: image_view.h:46
GLint GLenum GLint x
Definition: glcorearb.h:409
stride_t chanstride() const
Definition: image_view.h:101
int width() const
Definition: image_view.h:97
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:476
int depth() const
Definition: image_view.h:99
image_view(const image_view &copy)
Copy constructor.
Definition: image_view.h:59
stride_t xstride() const
Definition: image_view.h:102
GLint GLsizei width
Definition: glcorearb.h:103
void clear()
Definition: image_view.h:108
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:94
int height() const
Definition: image_view.h:98
T & reference
Definition: image_view.h:45
Definition: format.h:895
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:93