GteImage.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <LowLevel/GteLogger.h>
11 #include <vector>
12 
13 // Expose this define to test for out-of-range indices in the classes
14 // Image2, Image3, and Image4.
15 //#define GTE_IMAGICS_ASSERT_ON_INVALID_INDEX
16 
17 namespace gte
18 {
19 
20 template <typename PixelType>
21 class Image
22 {
23 public:
24  // Construction and destruction. The variadic argument of the last
25  // constructor must contain 'numDimensions' positive values of type
26  // 'int', each defining a dimension of the image.
27  virtual ~Image();
28  Image();
29  Image(std::vector<int> const& dimensions);
30 
31  // Support for copy semantics.
32  Image(Image const& image);
33  Image& operator=(Image const& image);
34 
35  // Support for move semantics.
36  Image(Image&& image);
37  Image& operator=(Image&& image);
38 
39  // Support for changing the image dimensions. All pixel data is lost by
40  // this operation.
41  void Reconstruct(std::vector<int> const& dimensions);
42 
43  // Access to image data.
44  inline std::vector<int> const& GetDimensions() const;
45  inline int GetNumDimensions() const;
46  inline int GetDimension(int d) const;
47  inline std::vector<size_t> const& GetOffsets() const;
48  inline size_t GetOffset(int d) const;
49  inline std::vector<PixelType> const& GetPixels() const;
50  inline std::vector<PixelType>& GetPixels();
51  inline size_t GetNumPixels() const;
52 
53  // Conversions between n-dim and 1-dim structures. The 'coord' arrays
54  // must have GetNumDimensions() elements.
55  size_t GetIndex(int const* coord) const;
56  void GetCoordinates(size_t index, int* coord) const;
57 
58  // Access the data as a 1-dimensional array. The operator[] functions
59  // test for valid i when iterator checking is enabled and assert on
60  // invalid i. The Get() functions test for valid i and clamp when
61  // invalid; these functions cannot fail.
62  inline PixelType& operator[] (size_t i);
63  inline PixelType const& operator[] (size_t i) const;
64  PixelType& Get(size_t i);
65  PixelType const& Get(size_t i) const;
66 
67 protected:
68  std::vector<int> mDimensions;
69  std::vector<size_t> mOffsets;
70  std::vector<PixelType> mPixels;
71 };
72 
73 
74 template <typename PixelType>
76 {
77 }
78 
79 template <typename PixelType>
81 {
82 }
83 
84 template <typename PixelType>
85 Image<PixelType>::Image(std::vector<int> const& dimensions)
86 {
87  Reconstruct(dimensions);
88 }
89 
90 template <typename PixelType>
92 {
93  *this = image;
94 }
95 
96 template <typename PixelType>
98 {
99  mDimensions = image.mDimensions;
100  mOffsets = image.mOffsets;
101  mPixels = image.mPixels;
102  return *this;
103 }
104 
105 template <typename PixelType>
107 {
108  *this = std::move(image);
109 }
110 
111 template <typename PixelType>
113 {
114  mDimensions = std::move(image.mDimensions);
115  mOffsets = std::move(image.mOffsets);
116  mPixels = std::move(image.mPixels);
117  return *this;
118 }
119 
120 template <typename PixelType>
121 void Image<PixelType>::Reconstruct(std::vector<int> const& dimensions)
122 {
123  mDimensions.clear();
124  mOffsets.clear();
125  mPixels.clear();
126 
127  if (dimensions.size() > 0)
128  {
129  for (auto dim : dimensions)
130  {
131  if (dim <= 0)
132  {
133  return;
134  }
135  }
136 
137  mDimensions = dimensions;
138  mOffsets.resize(dimensions.size());
139 
140  size_t numPixels = 1;
141  for (size_t d = 0; d < dimensions.size(); ++d)
142  {
143  numPixels *= static_cast<size_t>(mDimensions[d]);
144  }
145 
146  mOffsets[0] = 1;
147  for (size_t d = 1; d < dimensions.size(); ++d)
148  {
149  mOffsets[d] = static_cast<size_t>(mDimensions[d - 1]) * mOffsets[d - 1];
150  }
151 
152  mPixels.resize(numPixels);
153  }
154 }
155 
156 template <typename PixelType> inline
157 std::vector<int> const& Image<PixelType>::GetDimensions() const
158 {
159  return mDimensions;
160 }
161 
162 template <typename PixelType> inline
164 {
165  return static_cast<int>(mDimensions.size());
166 }
167 
168 template <typename PixelType> inline
170 {
171  return mDimensions[d];
172 }
173 
174 template <typename PixelType> inline
175 std::vector<size_t> const& Image<PixelType>::GetOffsets() const
176 {
177  return mOffsets;
178 }
179 
180 template <typename PixelType> inline
181 size_t Image<PixelType>::GetOffset(int d) const
182 {
183  return mOffsets[d];
184 }
185 
186 template <typename PixelType> inline
187 std::vector<PixelType> const& Image<PixelType>::GetPixels() const
188 {
189  return mPixels;
190 }
191 
192 template <typename PixelType> inline
194 {
195  return mPixels.size();
196 }
197 
198 template <typename PixelType> inline
199 std::vector<PixelType>& Image<PixelType>::GetPixels()
200 {
201  return mPixels;
202 }
203 
204 template <typename PixelType>
205 size_t Image<PixelType>::GetIndex(int const* coord) const
206 {
207  // assert: coord is array of mNumDimensions elements
208  int const numDimensions = static_cast<int>(mDimensions.size());
209  size_t index = coord[0];
210  for (int d = 1; d < numDimensions; ++d)
211  {
212  index += mOffsets[d] * coord[d];
213  }
214  return index;
215 }
216 
217 template <typename PixelType>
219 {
220  // assert: coord is array of numDimensions elements
221  int const numDimensions = static_cast<int>(mDimensions.size());
222  for (int d = 0; d < numDimensions; ++d)
223  {
224  coord[d] = index % mDimensions[d];
225  index /= mDimensions[d];
226  }
227 }
228 
229 template <typename PixelType> inline
230 PixelType& Image<PixelType>::operator[] (size_t i)
231 {
232  return mPixels[i];
233 }
234 
235 template <typename PixelType> inline
236 PixelType const& Image<PixelType>::operator[] (size_t i) const
237 {
238  return mPixels[i];
239 }
240 
241 template <typename PixelType>
242 PixelType& Image<PixelType>::Get(size_t i)
243 {
244  return (i < mPixels.size() ? mPixels[i] : mPixels.front());
245 }
246 
247 template <typename PixelType>
248 PixelType const& Image<PixelType>::Get(size_t i) const
249 {
250  return (i < mPixels.size() ? mPixels[i] : mPixels.front());
251 }
252 
253 }
std::vector< int > mDimensions
Definition: GteImage.h:68
Image & operator=(Image const &image)
Definition: GteImage.h:97
int GetDimension(int d) const
Definition: GteImage.h:169
GLenum GLenum GLsizei void * image
Definition: glext.h:2724
size_t GetOffset(int d) const
Definition: GteImage.h:181
GLuint coord
Definition: glext.h:5871
std::vector< size_t > mOffsets
Definition: GteImage.h:69
size_t GetIndex(int const *coord) const
Definition: GteImage.h:205
size_t GetNumPixels() const
Definition: GteImage.h:193
std::vector< int > const & GetDimensions() const
Definition: GteImage.h:157
std::vector< PixelType > const & GetPixels() const
Definition: GteImage.h:187
void Reconstruct(std::vector< int > const &dimensions)
Definition: GteImage.h:121
void GetCoordinates(size_t index, int *coord) const
Definition: GteImage.h:218
int GetNumDimensions() const
Definition: GteImage.h:163
GLuint index
Definition: glcorearb.h:781
PixelType & operator[](size_t i)
Definition: GteImage.h:230
PixelType & Get(size_t i)
Definition: GteImage.h:242
std::vector< PixelType > mPixels
Definition: GteImage.h:70
std::vector< size_t > const & GetOffsets() const
Definition: GteImage.h:175
virtual ~Image()
Definition: GteImage.h:75


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00