Add facility to read frames in RGB & RGBA format
[imageviewer.git] / array_df.h
blob82b3b84be5b3ff8fac8d91ef833ceaf68b7c71f2
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$
5 * The MIT License
7 * Copyright (c) 2008 BBC Research
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
27 * ***** END LICENSE BLOCK ***** */
29 #ifndef ARRAY_DF_H_
30 #define ARRAY_DF_H_
32 #include <memory>
33 #include <stdlib.h>
35 /*
36 * Some housekeeping around a 1d array of elements, but no
37 * accessors to this, you need to subclass Allocatable to
38 * give it some structure.
40 template<class T> class Allocatable {
41 public:
43 Allocatable(int len) :
44 _data(new T[len]), _len(len), _allocated(true)
48 Allocatable(int len, T* data) :
49 _data(data), _len(len), _allocated(false)
53 ~Allocatable()
55 if (_allocated)
56 delete[] _data;
59 /* assign a single value to every element */
60 Allocatable<T>& assign(const T& v)
62 for (int i = 0; i < _len; i++)
63 _data[i] = v;
65 return *this;
68 /* copy elements in one array to another */
69 Allocatable<T>& operator=(const Allocatable& v)
71 if (_len != v._len)
72 throw /* */;
74 for (int i = 0; i < _len; i++)
75 _data[i] = v._data[i];
77 return *this;
79 protected:
80 T* const _data;
82 private:
83 /* disable inadvertant copying */
84 Allocatable<T>(const Allocatable<T>&);
86 const int _len;
87 const bool _allocated;
91 * Non resizable, One dimensional array.
93 template<class T> class Array1d : public Allocatable<T> {
94 public:
95 Array1d(int len) :
96 Allocatable<T>(len), _len(len)
101 Array1d(int len, T* data) :
102 Allocatable<T>(len, data), _len(len)
107 int length() const
109 return _len;
113 T& operator[](const int i)
115 return *(_data + i);
119 const T& operator[](const int i) const
121 return *(_data + i);
125 private:
126 /* disable inadvertant copying */
127 Array1d<T>(const Array1d<T>&);
129 const int _len;
131 using Allocatable<T>::_data;
135 * Non resizable, Two dimensional array.
137 template<class T> class Array2d : public Allocatable<T> {
138 public:
139 Array2d(int width, int height) :
140 Allocatable<T>(width * height), _width(width), _height(height)
142 _rows = (Array1d<T>*) malloc(sizeof(Array1d<T>) * _height);
143 for (int i = 0; i < _height; i++)
144 /* magic! */
145 ::new(static_cast<void*>(&_rows[i]))
146 Array1d<T>(_width, _data + i*width);
150 /* clean up at destruction too -- this does not call destructors
151 * which it probably should. */
152 ~Array2d()
154 free(_rows);
158 int width() const
160 return _width;
163 int height() const
165 return _height;
169 Array1d<T>& operator[](const int j)
171 return _rows[j];
175 const Array1d<T>& operator[](const int j) const
177 return _rows[j];
181 Array2d<T>& operator=(const Array2d<T>& v)
183 if (v.width() != _width || v.height() != _height)
184 throw /* */;
186 this->Allocatable<T>::operator=(v);
188 return *this;
191 private:
192 /* disable inadvertant copying */
193 Array2d<T>(const Array2d<T>&);
195 const int _width;
196 const int _height;
198 /* an array of rowpointers, _height long. This allows us to
199 * do more efficient row access. */
200 Array1d<T> *_rows;
202 /* gain access to _data from allocatable */
203 using Allocatable<T>::_data;
206 #endif /*ARRAY_DF_H_*/