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