00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00028 #ifndef IMAGE_H
00029 #define IMAGE_H
00030
00031 #include <error.h>
00032
00033
00046 template <typename T, unsigned int N=2>
00047 class Image {
00048 public:
00049 typedef T type;
00050 enum { dim = N };
00051 typedef unsigned int uint;
00052
00053 public:
00054
00055 Image();
00056 Image(uint width, uint height);
00057 Image(uint width, uint height, uint depth);
00058 Image(const T* data, uint width, uint height);
00059 Image(const T* data, uint width, uint height, uint depth);
00060 Image(const uint size[]);
00061 Image(const Image&);
00062 ~Image();
00063
00064
00065 void setSize(const uint size[]);
00066 const uint* getSize() const { return m_size; }
00067 uint getSize(uint n) const;
00068 const uint* size() const { return m_size; }
00069 uint size(uint n) const;
00070
00071
00072 void setSize(uint width, uint height);
00073 void setSize(uint width, uint height, uint depth);
00074
00075
00076 const T& get(const uint index[]) const;
00077 void set(const uint index[], const T &val);
00078
00079
00080 const T& get(uint i, uint j) const;
00081 const T& get(uint i, uint j, uint k) const;
00082 void set(uint i, uint j, const T &val);
00083 void set(uint i, uint j, uint k, const T &val);
00084
00085 public:
00086 typedef T* iterator;
00087
00089 iterator begin()
00090 { return reinterpret_cast<iterator>(&m_data[0]); }
00091
00093 iterator end()
00094 { uint size = 1; for(uint m=0; m<N; ++m) size = size * m_size[m];
00095 return reinterpret_cast<iterator>(&m_data[size-1]+1); }
00096
00097
00098 typedef const T* const_iterator;
00099
00101 const_iterator begin() const
00102 { return reinterpret_cast<const_iterator>(&m_data[0]); }
00103
00105 const_iterator end() const
00106 { uint size = 1; for(uint m=0; m<N; ++m) size = size * m_size[m];
00107 return reinterpret_cast<const_iterator>(&m_data[size-1]+1); }
00108
00109 protected:
00110 T* m_data;
00111 uint m_size[N];
00112 };
00113
00114
00115
00116
00117
00118
00119
00120 template <typename T, unsigned int N>
00121 Image<T, N>::Image()
00122 : m_data(0)
00123 {
00124 for(unsigned int n=0; n<N; ++n) m_size[n] = 0;
00125 }
00126
00127
00128 template <typename T, unsigned int N>
00129 Image<T, N>::Image(uint width, uint height)
00130 : m_data(0)
00131 {
00132
00133 setSize(width, height);
00134 }
00135
00136
00137 template <typename T, unsigned int N>
00138 Image<T, N>::Image(uint width, uint height, uint depth)
00139 : m_data(0)
00140 {
00141
00142 setSize(width, height, depth);
00143 }
00144
00145
00146 template <typename T, unsigned int N>
00147 Image<T, N>::Image(const T* data, uint width, uint height)
00148 : m_data(0)
00149 {
00150
00151 setSize(width, height);
00152
00153 for(unsigned int i=0; i<width*height; ++i) m_data[i] = data[i];
00154 }
00155
00156
00157 template <typename T, unsigned int N>
00158 Image<T, N>::Image(const T* data, uint width, uint height, uint depth)
00159 : m_data(0)
00160 {
00161
00162 setSize(width, height, depth);
00163
00164 for(unsigned int i=0; i<width*height*depth; ++i) m_data[i] = data[i];
00165 }
00166
00167
00168 template <typename T, unsigned int N>
00169 Image<T, N>::Image(const uint size[])
00170 : m_data(0)
00171 {
00172
00173 setSize(size);
00174 }
00175
00176
00177 template <typename T, unsigned int N>
00178 Image<T, N>::Image(const Image& image)
00179 : m_data(0)
00180 {
00181
00182 for(uint n=0; n<N; ++n) m_size[n] = image.m_size[n];
00183 setSize(m_size);
00184
00185
00186 uint totalmem = 1;
00187 for(uint n=0; n<N; ++n) totalmem *= m_size[n];
00188 for(uint i=0; i<totalmem; ++i) m_data[i] = image.m_data[i];
00189 }
00190
00191
00192 template <typename T, unsigned int N>
00193 Image<T, N>::~Image()
00194 {
00195
00196 if(m_data) delete [] m_data;
00197 }
00198
00199
00200 template <typename T, unsigned int N>
00201 void Image<T, N>::setSize(uint width, uint height)
00202 {
00203
00204 m_size[0] = width;
00205 m_size[1] = height;
00206
00207
00208 if(m_data) delete [] m_data;
00209 m_data = 0;
00210
00211
00212 if(!m_size[0] || !m_size[1]) return;
00213
00214
00215 m_data = new T [width*height];
00216 CHECK("allocated memory", m_data);
00217
00218
00219 for(uint i=0; i<width*height; ++i)
00220 m_data[i] = 0;
00221 }
00222
00223
00224 template <typename T, unsigned int N>
00225 void Image<T, N>::setSize(uint width, uint height, uint depth)
00226 {
00227
00228 m_size[0] = width;
00229 m_size[1] = height;
00230 m_size[2] = depth;
00231
00232
00233 if(m_data) delete [] m_data;
00234 m_data = 0;
00235
00236
00237 if(!m_size[0] || !m_size[1] || !m_size[2]) return;
00238
00239
00240 m_data = new T [width*height*depth];
00241 CHECK("allocated memory", m_data);
00242
00243
00244 for(uint i=0; i<width*height*depth; ++i)
00245 m_data[i] = 0;
00246 }
00247
00248
00249 template <typename T, unsigned int N>
00250 void Image<T, N>::setSize(const uint size[])
00251 {
00252
00253 for(uint n=0; n<N; ++n) m_size[n] = size[n];
00254
00255
00256 if(m_data) delete [] m_data;
00257 m_data = 0;
00258
00259
00260 for(uint n=0; n<N; ++n) if(!m_size[n]) return;
00261
00262
00263 uint totalmem = 1;
00264 for(uint n=0; n<N; ++n) totalmem *= m_size[n];
00265 m_data = new T [totalmem];
00266 CHECK("allocated memory", m_data);
00267
00268
00269 for(uint i=0; i<totalmem; ++i)
00270 m_data[i] = 0;
00271 }
00272
00273
00274 template <typename T, unsigned int N>
00275 unsigned int Image<T, N>::getSize(uint n) const
00276 {
00277 CHECKRANGE("dimension", n, 0, N);
00278 return m_size[n];
00279 }
00280
00281
00282 template <typename T, unsigned int N>
00283 unsigned int Image<T, N>::size(uint n) const
00284 {
00285 CHECKRANGE("dimension", n, 0, N);
00286 return m_size[n];
00287 }
00288
00289
00290 template <typename T, unsigned int N>
00291 const T& Image<T, N>::get(uint i, uint j) const
00292 {
00293
00294 CHECK("initialization", m_data);
00295 CHECKRANGE("size x", i, 0, m_size[0]);
00296 CHECKRANGE("size y", j, 0, m_size[1]);
00297
00298
00299 uint index = (j*m_size[0])+i;
00300 return m_data[index];
00301 }
00302
00303
00304 template <typename T, unsigned int N>
00305 const T& Image<T, N>::get(uint i, uint j, uint k) const
00306 {
00307
00308 CHECK("initialization", m_data);
00309 CHECKRANGE("size x", i, 0, m_size[0]);
00310 CHECKRANGE("size y", j, 0, m_size[1]);
00311 CHECKRANGE("size z", k, 0, m_size[2]);
00312
00313
00314 uint index = ((k*m_size[1]+j)*m_size[0])+i;
00315 return m_data[index];
00316 }
00317
00318
00319 template <typename T, unsigned int N>
00320 const T& Image<T, N>::get(const uint index[]) const
00321 {
00322
00323 CHECK("initialization", m_data);
00324 for(uint n=0; n<N; ++n)
00325 CHECKRANGE("size", index[n], 0, m_size[n]);
00326
00327
00328 uint offset = index[N-1];
00329 for(uint n=N-1; n>0; --n)
00330 offset = offset*m_size[n-1] + index[n-1];
00331 return m_data[offset];
00332 }
00333
00334
00335 template <typename T, unsigned int N>
00336 void Image<T, N>::set(uint i, uint j, const T &val)
00337 {
00338
00339 CHECK("initialization", m_data);
00340 CHECKRANGE("size x", i, 0, m_size[0]);
00341 CHECKRANGE("size y", j, 0, m_size[1]);
00342
00343
00344 uint index = (j*m_size[0])+i;
00345 m_data[index] = val;
00346 }
00347
00348
00349 template <typename T, unsigned int N>
00350 void Image<T, N>::set(uint i, uint j, uint k, const T &val)
00351 {
00352
00353 CHECK("initialization", m_data);
00354 CHECKRANGE("size x", i, 0, m_size[0]);
00355 CHECKRANGE("size y", j, 0, m_size[1]);
00356 CHECKRANGE("size z", k, 0, m_size[2]);
00357
00358
00359 uint index = ((k*m_size[1]+j)*m_size[0])+i;
00360 m_data[index] = val;
00361 }
00362
00363
00364 template <typename T, unsigned int N>
00365 void Image<T, N>::set(const uint index[], const T &val)
00366 {
00367
00368 if(!m_data) throw Error("no data");
00369 for(uint n=0; n<N; ++n)
00370 CHECKRANGE("size", index[n], 0, m_size[n]);
00371
00372
00373 uint offset = index[N-1];
00374 for(uint n=N-1; n>0; --n)
00375 offset = offset*m_size[n-1] + index[n-1];
00376 m_data[offset] = val;
00377 }
00378
00379
00380 #endif