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 <https://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 #pragma once
56 
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 #include "noncopyable.h"
70 
71 template<typename T>
72 class array2D :
73     public rtengine::NonCopyable
74 {
75 
76 private:
77     int x, y, owner;
78     unsigned int flags;
79     T ** ptr;
80     T * data;
81     bool lock; // useful lock to ensure data is not changed anymore.
82     void ar_realloc(int w, int h, int offset = 0)
83     {
84         if ((ptr) && ((h > y) || (4 * h < y))) {
85             delete[] ptr;
86             ptr = nullptr;
87         }
88 
89         if ((data) && (((h * w) > (x * y)) || ((h * w) < ((x * y) / 4)))) {
90             delete[] data;
91             data = nullptr;
92         }
93 
94         if (ptr == nullptr) {
95             ptr = new T*[h];
96         }
97 
98         if (data == nullptr) {
99             data = new T[h * w + offset];
100         }
101 
102         x = w;
103         y = h;
104 
105         for (int i = 0; i < h; i++) {
106             ptr[i] = data + offset + w * i;
107         }
108 
109         owner = 1;
110     }
111 public:
112 
113     // use as empty declaration, resize before use!
114     // very useful as a member object
array2D()115     array2D() :
116         x(0), y(0), owner(0), flags(0), ptr(nullptr), data(nullptr), lock(false)
117     {
118         //printf("got empty array2D init\n");
119     }
120 
121     // creator type1
122     array2D(int w, int h, unsigned int flgs = 0)
123     {
124         flags = flgs;
125         lock = flags & ARRAY2D_LOCK_DATA;
126         data = new T[h * w];
127         owner = 1;
128         x = w;
129         y = h;
130         ptr = new T*[h];
131 
132         for (int i = 0; i < h; i++) {
133             ptr[i] = data + i * w;
134         }
135 
136         if (flags & ARRAY2D_CLEAR_DATA) {
137             memset(data, 0, w * h * sizeof(T));
138         }
139     }
140 
141     // creator type 2
142     array2D(int w, int h, T ** source, unsigned int flgs = 0)
143     {
144         flags = flgs;
145         //if (lock) { printf("array2D attempt to overwrite data\n");raise(SIGSEGV);}
146         lock = flags & ARRAY2D_LOCK_DATA;
147         // when by reference
148         // TODO: improve this code with ar_realloc()
149         owner = (flags & ARRAY2D_BYREFERENCE) ? 0 : 1;
150 
151         if (owner) {
152             data = new T[h * w];
153         } else {
154             data = nullptr;
155         }
156 
157         x = w;
158         y = h;
159         ptr = new T*[h];
160 
161         for (int i = 0; i < h; i++) {
162             if (owner) {
163                 ptr[i] = data + i * w;
164 
165                 for (int j = 0; j < w; j++) {
166                     ptr[i][j] = source[i][j];
167                 }
168             } else {
169                 ptr[i] = source[i];
170             }
171         }
172     }
173 
174     // destructor
~array2D()175     ~array2D()
176     {
177 
178         if (flags & ARRAY2D_VERBOSE) {
179             printf(" deleting array2D size %dx%d \n", x, y);
180         }
181 
182         if ((owner) && (data)) {
183             delete[] data;
184         }
185 
186         if (ptr) {
187             delete[] ptr;
188         }
189     }
190 
191     void fill(const T val, bool multiThread = false)
192     {
193 #ifdef _OPENMP
194         #pragma omp parallel for if(multiThread)
195 #endif
196         for (int i = 0; i < x * y; ++i) {
197             data[i] = val;
198         }
199     }
200 
free()201     void free()
202     {
203         if ((owner) && (data)) {
204             delete[] data;
205             data = nullptr;
206         }
207 
208         if (ptr) {
209             delete [] ptr;
210             ptr = nullptr;
211         }
212     }
213 
214     // use with indices
215     T * operator[](int index) const
216     {
217         assert((index >= 0) && (index < y));
218         return ptr[index];
219     }
220 
221     // use as pointer to T**
222     operator T**()
223     {
224         return ptr;
225     }
226 
227     // use as pointer to T**
228     operator const T* const *()
229     {
230         return ptr;
231     }
232 
233     // use as pointer to data
234     operator T*()
235     {
236         // only if owner this will return a valid pointer
237         return data;
238     }
239 
240 
241     // useful within init of parent object
242     // or use as resize of 2D array
operator()243     void operator()(int w, int h, unsigned int flgs = 0, int offset = 0)
244     {
245         flags = flgs;
246 
247         if (flags & ARRAY2D_VERBOSE) {
248             printf("got init request %dx%d flags=%u\n", w, h, flags);
249             printf("previous was data %p ptr %p \n", data, ptr);
250         }
251 
252         if (lock) { // our object was locked so don't allow a change.
253             printf("got init request but object was locked!\n");
254             raise( SIGSEGV);
255         }
256 
257         lock = flags & ARRAY2D_LOCK_DATA;
258 
259         ar_realloc(w, h, offset);
260 
261         if (flags & ARRAY2D_CLEAR_DATA) {
262             memset(data + offset, 0, static_cast<unsigned long>(w) * h * sizeof(T));
263         }
264     }
265 
266     // import from flat data
operator()267     void operator()(int w, int h, T* copy, unsigned int flgs = 0)
268     {
269         flags = flgs;
270 
271         if (flags & ARRAY2D_VERBOSE) {
272             printf("got init request %dx%d flags=%u\n", w, h, flags);
273             printf("previous was data %p ptr %p \n", data, ptr);
274         }
275 
276         if (lock) { // our object was locked so don't allow a change.
277             printf("got init request but object was locked!\n");
278             raise( SIGSEGV);
279         }
280 
281         lock = flags & ARRAY2D_LOCK_DATA;
282 
283         ar_realloc(w, h);
284         memcpy(data, copy, w * h * sizeof(T));
285     }
width()286     int width() const
287     {
288         return x;
289     }
height()290     int height() const
291     {
292         return y;
293     }
294 
295     operator bool()
296     {
297         return (x > 0 && y > 0);
298     }
299 
300 };
301 template<typename T, const size_t num>
302 class multi_array2D
303 {
304 private:
305     array2D<T> list[num];
306 
307 public:
308     multi_array2D(int x, int y, int flags = 0, int offset = 0)
309     {
310         for (size_t i = 0; i < num; i++) {
311             list[i](x, y, flags, (i + 1) * offset);
312         }
313     }
314 
~multi_array2D()315     ~multi_array2D()
316     {
317         //printf("trying to delete the list of array2D objects\n");
318     }
319 
320     array2D<T> & operator[](int index)
321     {
322         assert(static_cast<size_t>(index) < num);
323         return list[index];
324     }
325 };
326