1 /*
2  *  This file is part of RawTherapee.
3  *
4  *  Copyright (c) 2011 Jan Rinze Peterzon (janrinze@gmail.com)
5  *
6  *  RawTherapee is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  RawTherapee is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with RawTherapee.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*
21  *  Declaration of flexible 2D arrays
22  *
23  *  Usage:
24  *
25  *      array2D<type> name (X-size,Y-size);
26  *      array2D<type> name (X-size,Y-size,type ** data);
27  *
28  *      creates an array which is valid within the normal C/C++ scope "{ ... }"
29  *
30  *      access to elements is a simple as:
31  *
32  *          array2D<float> my_array (10,10); // creates 10x10 array of floats
33  *          value =  my_array[3][5];
34  *          my_array[4][6]=value;
35  *
36  *      or copy an existing 2D array
37  *
38  *          float ** mydata;
39  *          array2D<float> my_array (10,10,mydata);
40  *
41  *
42  *      Useful extra pointers
43  *
44  *          <type> ** my_array      gives access to the pointer for access with [][]
45  *          <type> *  my_array      gives access to the flat stored data.
46  *
47  *      Advanced usage:
48  *          array2D<float> my_array             ; // empty container.
49  *          my_array(10,10)                     ; // resize to 10x10 array
50  *          my_array(10,10,ARRAY2D_CLEAR_DATA)  ; // resize to 10x10 and clear data
51  *          my_array(10,10,ARRAY2D_CLEAR_DATA|ARRAY2D_LOCK_DATA)  ; same but set a lock on changes
52  *
53  *          !! locked arrays cannot be resized and cannot be unlocked again !!
54  */
55 #ifndef ARRAY2D_H_
56 #define ARRAY2D_H_
57 #include <csignal>  // for raise()
58 #include <cassert>
59 
60 // flags for use
61 #define ARRAY2D_LOCK_DATA   1
62 #define ARRAY2D_CLEAR_DATA  2
63 #define ARRAY2D_BYREFERENCE 4
64 #define ARRAY2D_VERBOSE     8
65 
66 #include <cstring>
67 #include <cstdio>
68 
69 
70 template<typename T>
71 class array2D
72 {
73 
74 private:
75     int x, y, owner;
76     unsigned int flags;
77     T ** ptr;
78     T * data;
79     bool lock; // useful lock to ensure data is not changed anymore.
80     void ar_realloc(int w, int h, int offset = 0)
81     {
82         if ((ptr) && ((h > y) || (4 * h < y))) {
83             delete[] ptr;
84             ptr = nullptr;
85         }
86 
87         if ((data) && (((h * w) > (x * y)) || ((h * w) < ((x * y) / 4)))) {
88             delete[] data;
89             data = nullptr;
90         }
91 
92         if (ptr == nullptr) {
93             ptr = new T*[h];
94         }
95 
96         if (data == nullptr) {
97             data = new T[h * w + offset];
98         }
99 
100         x = w;
101         y = h;
102 
103         for (int i = 0; i < h; i++) {
104             ptr[i] = data + offset + w * i;
105         }
106 
107         owner = 1;
108     }
109 public:
110 
111     // use as empty declaration, resize before use!
112     // very useful as a member object
array2D()113     array2D() :
114         x(0), y(0), owner(0), flags(0), ptr(nullptr), data(nullptr), lock(false)
115     {
116         //printf("got empty array2D init\n");
117     }
118 
119     // creator type1
120     array2D(int w, int h, unsigned int flgs = 0)
121     {
122         flags = flgs;
123         lock = flags & ARRAY2D_LOCK_DATA;
124         data = new T[h * w];
125         owner = 1;
126         x = w;
127         y = h;
128         ptr = new T*[h];
129 
130         for (int i = 0; i < h; i++) {
131             ptr[i] = data + i * w;
132         }
133 
134         if (flags & ARRAY2D_CLEAR_DATA) {
135             memset(data, 0, w * h * sizeof(T));
136         }
137     }
138 
139     // creator type 2
140     array2D(int w, int h, T ** source, unsigned int flgs = 0)
141     {
142         flags = flgs;
143         //if (lock) { printf("array2D attempt to overwrite data\n");raise(SIGSEGV);}
144         lock = flags & ARRAY2D_LOCK_DATA;
145         // when by reference
146         // TODO: improve this code with ar_realloc()
147         owner = (flags & ARRAY2D_BYREFERENCE) ? 0 : 1;
148 
149         if (owner) {
150             data = new T[h * w];
151         } else {
152             data = nullptr;
153         }
154 
155         x = w;
156         y = h;
157         ptr = new T*[h];
158 
159         for (int i = 0; i < h; i++) {
160             if (owner) {
161                 ptr[i] = data + i * w;
162 
163                 for (int j = 0; j < w; j++) {
164                     ptr[i][j] = source[i][j];
165                 }
166             } else {
167                 ptr[i] = source[i];
168             }
169         }
170     }
171 
172     // destructor
~array2D()173     ~array2D()
174     {
175 
176         if (flags & ARRAY2D_VERBOSE) {
177             printf(" deleting array2D size %dx%d \n", x, y);
178         }
179 
180         if ((owner) && (data)) {
181             delete[] data;
182         }
183 
184         if (ptr) {
185             delete[] ptr;
186         }
187     }
188 
free()189     void free()
190     {
191         if ((owner) && (data)) {
192             delete[] data;
193             data = nullptr;
194         }
195 
196         if (ptr) {
197             delete [] ptr;
198             ptr = nullptr;
199         }
200     }
201 
202     // use with indices
203     T * operator[](int index) const
204     {
205         assert((index >= 0) && (index < y));
206         return ptr[index];
207     }
208 
209     // use as pointer to T**
210     operator T**()
211     {
212         return ptr;
213     }
214 
215     // use as pointer to data
216     operator T*()
217     {
218         // only if owner this will return a valid pointer
219         return data;
220     }
221 
222 
223     // useful within init of parent object
224     // or use as resize of 2D array
operator()225     void operator()(int w, int h, unsigned int flgs = 0, int offset = 0)
226     {
227         flags = flgs;
228 
229         if (flags & ARRAY2D_VERBOSE) {
230             printf("got init request %dx%d flags=%u\n", w, h, flags);
231             printf("previous was data %p ptr %p \n", data, ptr);
232         }
233 
234         if (lock) { // our object was locked so don't allow a change.
235             printf("got init request but object was locked!\n");
236             raise( SIGSEGV);
237         }
238 
239         lock = flags & ARRAY2D_LOCK_DATA;
240 
241         ar_realloc(w, h, offset);
242 
243         if (flags & ARRAY2D_CLEAR_DATA) {
244             memset(data + offset, 0, w * h * sizeof(T));
245         }
246     }
247 
248     // import from flat data
operator()249     void operator()(int w, int h, T* copy, unsigned int flgs = 0)
250     {
251         flags = flgs;
252 
253         if (flags & ARRAY2D_VERBOSE) {
254             printf("got init request %dx%d flags=%u\n", w, h, flags);
255             printf("previous was data %p ptr %p \n", data, ptr);
256         }
257 
258         if (lock) { // our object was locked so don't allow a change.
259             printf("got init request but object was locked!\n");
260             raise( SIGSEGV);
261         }
262 
263         lock = flags & ARRAY2D_LOCK_DATA;
264 
265         ar_realloc(w, h);
266         memcpy(data, copy, w * h * sizeof(T));
267     }
width()268     int width() const
269     {
270         return x;
271     }
height()272     int height() const
273     {
274         return y;
275     }
276 
277     operator bool()
278     {
279         return (x > 0 && y > 0);
280     }
281 
282 };
283 template<typename T, const size_t num>
284 class multi_array2D
285 {
286 private:
287     array2D<T> list[num];
288 
289 public:
290     multi_array2D(int x, int y, int flags = 0, int offset = 0)
291     {
292         for (size_t i = 0; i < num; i++) {
293             list[i](x, y, flags, (i + 1) * offset);
294         }
295     }
296 
~multi_array2D()297     ~multi_array2D()
298     {
299         //printf("trying to delete the list of array2D objects\n");
300     }
301 
302     array2D<T> & operator[](int index)
303     {
304         assert(static_cast<size_t>(index) < num);
305         return list[index];
306     }
307 };
308 #endif /* array2D_H_ */
309