Add facility to read frames in RGB & RGBA format
[imageviewer.git] / array_psi.h
blobb3bdc9f73ebdcf6394c55715f5f13274fc44800a
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_H
30 #define __ARRAY_H
32 #include <psi.h>
34 namespace psi
36 /* introduce a new type of dynamic array, that:
37 * - performs bound checking on operator[] -- enabled through (1)
38 * - disables array convolution to reduce mishtakes -- (2)
40 template<class Target> struct P : MergePolicies
41 <ArrayDynamicBase
42 ,ArrayFirstLastSize
43 ,ArrayAssign
44 /* ,indexZeroBasedNoCheck */
45 ,IndexingChecked /* (1) */
46 ,CheckedArrayAccess
47 ,ArrayArithmetic
48 /* ,ArrayConvolution *//* (2) */
49 ,ArrayIOText
50 >::Policy<Target>
56 template<class T> class Array1d : public psi::DynamicArray<T, 1, psi::P> {
57 public:
58 const int length() const
60 return Q::size(1);
64 Array1d(const int length)
66 typename Q::Params init_params;
67 /* setup the inital parameters - ie size. */
68 init_params.params(1, psi::Size(length));
70 /* tell psi about the actual array size we desire. */
71 this->params(init_params);
75 private:
76 /* TODO? shaddow copy the width,height -- allows for large performance
77 * increase. */
78 typedef psi::DynamicArray<T, 1, psi::P> Q;
81 struct Size2d {
82 int width;
83 int height;
84 Size2d(int w, int h) :
85 width(w), height(h)
91 template<class T> class Array2d : public psi::DynamicArray<T, 2, psi::P> {
92 public:
93 const int height() const
95 return Q::size(2);
98 const int width() const
100 return Q::size(1);
104 Array2d(const int width, const int height)
106 typename Q::Params init_params;
107 /* setup the inital parameters - ie size. */
108 init_params.params(1, psi::Size(width));
109 init_params.params(2, psi::Size(height));
111 /* tell psi about the actual array size we desire. */
112 this->params(init_params);
116 Array2d(const Size2d& dim)
118 typename Q::Params init_params;
119 /* setup the inital parameters - ie size. */
120 init_params.params(1, psi::Size(dim.width));
121 init_params.params(2, psi::Size(dim.height));
123 /* tell psi about the actual array size we desire. */
124 this->params(init_params);
128 private:
129 // Array2d<T>& operator=(const Array2d<T>&);
130 // Array2d(const Array2d<T>&);
131 /* TODO? shaddow copy the width,height -- allows for large performance
132 * increase. */
133 typedef psi::DynamicArray<T, 2, psi::P> Q;
136 /* Some magic arrays to get around lack of good references in C++ */
137 template<class T> class RArray1d {
138 public:
139 RArray1d(const int length) :
140 data(length)
145 /* make read access look like normal container access */
146 T& operator[](const int i) const
148 return *(data[i]);
152 /* make write access a bit irritating */
153 void set(const int i, T& thing)
155 data[i] = &thing;
158 void set(const int i, T* thing)
160 data[i] = thing;
164 int length() const
166 return data.length();
170 private:
171 /* don't allow copying of this array! */
172 RArray1d(const RArray1d<T>&);
173 RArray1d<T>& operator=(const RArray1d<T>&);
175 Array1d<T*> data;
178 #endif