-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImage.cpp
424 lines (384 loc) · 11.8 KB
/
Image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// Implementation code for Image class and related classes.
#include "Image.h"
#include <cstring>
#include <cstdio>
#include <cctype>
using namespace img;
using namespace std;
/////////////////////////////////////////////////////////////////////
// Routines for the underlying data structure ImageData
/////////////////////////////////////////////////////////////////////
template <class T>
void
ImageData<T>::clearChanged() {
if (parent)
throw ImageError("ImageData::clearChanged() called for a subimage");
isAltered = false;
for (typename list<ImageData<T>*>::iterator i = children.begin();
i != children.end();
++i)
(*i)->isAltered = false;
}
// build or destroy the data array
template <class T>
void ImageData<T>::acquireArrays(Bounds<int> inBounds) {
if (!inBounds) {
// Do not allocate any space if the input bounds are undefined
return;
}
bounds = inBounds;
int xsize, ysize;
T *dataArray, *dptr, **rPtrs;
xsize = bounds.getXMax() - bounds.getXMin() + 1;
ysize = bounds.getYMax() - bounds.getYMin() + 1;
dataArray = new T[xsize*ysize];
try {
rPtrs = new T*[ysize];
} catch (std::bad_alloc& b) {
//Delete dataArray if row-pointers allocation fails
delete [] dataArray;
dataArray = nullptr;
throw; // Rethrow bad_alloc
}
rowPointers = rPtrs - bounds.getYMin();
dptr = dataArray - bounds.getXMin();
for (int i=bounds.getYMin(); i<=bounds.getYMax(); i++) {
rowPointers[i] = dptr;
dptr += xsize;
}
ownDataArray = true;
ownRowPointers = true;
isContiguous = true;
}
template <class T>
void
ImageData<T>::discardArrays() {
if (ownDataArray) {
//Free the data array(s)
if (isContiguous) {
delete [] const_location(bounds.getXMin(),bounds.getYMin());
} else {
T *dptr;
for (int i=bounds.getYMin(); i<=bounds.getYMax(); i++) {
dptr = rowPointers[i]+bounds.getXMin();
delete [] dptr;
rowPointers[i] = nullptr;
}
}
ownDataArray = false;
}
if (ownRowPointers) {
// Free the row pointer array:
T **rptr;
rptr = rowPointers + bounds.getYMin();
delete [] rptr;
rowPointers = nullptr;
ownRowPointers = false;
}
}
// image with unspecified data values:
template <class T>
ImageData<T>::ImageData(const Bounds<int> inBounds): rowPointers(nullptr),
parent(nullptr),
isAltered(false),
lock(false),
ownDataArray(false),
ownRowPointers(false) {
acquireArrays(inBounds);
}
// image filled with a scalar:
template <class T>
ImageData<T>::ImageData(const Bounds<int> inBounds,
const T initValue): rowPointers(nullptr),
parent(nullptr),
isAltered(false),
lock(false),
ownDataArray(false),
ownRowPointers(false) {
acquireArrays(inBounds);
// Initialize the data
Assert(isContiguous);
long int i=0;
long npix = bounds.getXMax() - bounds.getXMin() + 1;
npix *= bounds.getYMax() - bounds.getYMin() + 1;
for (T *initptr=location(bounds.getXMin(), bounds.getYMin());
i<npix;
i++, initptr++)
*initptr = initValue;
}
// construct a subimage of a parent:
// Note that there is no assumption that parent is contiguous, its
// storage state could change.
template <class T>
ImageData<T>::ImageData(const Bounds<int> inBounds,
const ImageData<T>* _parent):
bounds(inBounds),
parent(const_cast<ImageData<T>*> (_parent)),
isAltered(_parent->isAltered),
lock(_parent->lock),
ownDataArray(false),
ownRowPointers(false),
rowPointers(_parent->rowPointers),
isContiguous(false) {}
// Destructor: Be sure to free all memory
template <class T>
ImageData<T>::~ImageData() {
if (!children.empty() ) {
// Do not throw from within destructor, just print error, quit
cerr << "ERROR: ImageData::~ImageData for object that still has children"
<<endl;
exit(1);
}
if (parent) parent->unlinkChild(this);
discardArrays();
}
template <class T>
void
ImageData<T>::unlinkChild(ImageData<T>* child) const {
#ifdef _OPENMP
#pragma omp critical(imageio)
#endif
{
typename list<ImageData<T>*>::iterator cptr =
find(children.begin(), children.end(), child);
if (cptr==children.end() && !uncaught_exception())
throw ImageError("ImageData::unlinkChild cannot find the child");
children.erase(cptr);
}
}
template <class T>
void
ImageData<T>::linkChild(ImageData<T>* child) const {
#ifdef _OPENMP
#pragma omp critical(imageio)
#endif
{
children.push_back(child);
}
}
template <class T>
void
ImageData<T>::deleteSubimages() const {
#ifdef _OPENMP
#pragma omp critical(imageio)
#endif
{
while (!children.empty()) {
delete *(children.begin());
children.erase(children.begin());
}
}
}
// Get a subimage of this one
template <class T>
ImageData<T>* ImageData<T>::subimage(const Bounds<int> bsub) const {
if (!bounds.includes(bsub))
throw ImageError("Attempt to create subimage outside of ImageData"
" bounds");
ImageData<T>* child = new ImageData<T>(bsub, this);
linkChild(child);
return child;
}
// Create a new (sub)image that is duplicate of this ones data
template <class T>
ImageData<T>*
ImageData<T>::duplicate() const {
ImageData<T>* dup = new ImageData<T>(bounds);
// If the bounds are undefined and this is null image, we are done
if (!bounds)
return dup;
Assert(dup->isContiguous);
// Copy the data from old array to new array
const T *inptr;
T *dptr;
long int xsize,ysize;
xsize = bounds.getXMax() - bounds.getXMin() + 1;
ysize = bounds.getYMax() - bounds.getYMin() + 1;
if (isContiguous) {
// Can be done is a single large copy if both contiguous
inptr = const_location(bounds.getXMin(),bounds.getYMin());
dptr = dup->location(bounds.getXMin(),bounds.getYMin());
memmove( (void *)dptr, (void *)inptr, sizeof(T)*xsize*ysize);
} else {
// Do copy by rows
for (int i=bounds.getYMin(); i<=bounds.getYMax(); i++) {
inptr = const_location(bounds.getXMin(), i);
dptr = dup->location(bounds.getXMin(), i);
memmove( (void *)dptr, (void *)inptr, sizeof(T)*xsize);
}
}
return dup;
}
// Change image size (flushes data), if data are under this object's
// control and there are no subimages to invalidate.
template <class T>
void
ImageData<T>::resize(const Bounds<int> newBounds) {
if (newBounds==bounds) return;
if (!ownDataArray || !ownRowPointers)
throw ImageError("Cannot ImageData::resize() when data are"
" not owned by object");
if (!children.empty())
throw ImageError("Attempt to ImageData::resize() with subimages"
" in use.");
if (parent)
throw ImageError("Attempt to ImageData::resize() a subimage");
checkLock("resize()");
discardArrays();
acquireArrays(newBounds);
}
// Make this ImageData be a copy of another
template <class T>
void
ImageData<T>::copyFrom(const ImageData<T>& rhs) {
checkLock("copyFrom()");
if (!children.empty())
throw ImageError("Attempt to ImageData::copyFrom() with subimages"
" in use.");
if (parent)
throw ImageError("Attempt to ImageData::copyFrom() for a subimage");
resize(rhs.getBounds());
// Copy the data from old array to new array
if (!bounds) return; // but quit if there is no data to copy.
T *dptr;
const T* inptr;
long int xsize = bounds.getXMax() - bounds.getXMin() + 1;
// Do copy by rows
for (int i=bounds.getYMin(); i<=bounds.getYMax(); i++) {
dptr = location(bounds.getXMin(), i);
inptr = rhs.const_location(bounds.getXMin(), i);
memmove( (void *)dptr, (void *)inptr, sizeof(T)*xsize);
}
}
// Change origin of array, saving data
template <class T>
void
ImageData<T>::shift(int x0, int y0) {
checkLock("shift()");
if (!ownDataArray || !ownRowPointers)
throw ImageError("Cannot ImageData::shift() when data are"
" not owned by object");
if (!children.empty())
throw ImageError("Attempt to ImageData::shift() with subimages"
" in use.");
if (parent)
throw ImageError("Attempt to ImageData::shift() for a subimage");
if (!bounds)
throw ImageError("Attempt to ImageData::shift() with undefined bounds");
int dx = x0 - bounds.getXMin();
int dy = y0 - bounds.getYMin();
for (int i=bounds.getYMin(); i<=bounds.getYMax(); i++)
rowPointers[i] -= dx;
rowPointers -= dy;
bounds.setXMin(bounds.getXMin() + dx);
bounds.setXMax(bounds.getXMax() + dx);
bounds.setYMin(bounds.getYMin() + dy);
bounds.setYMax(bounds.getYMax() + dy);
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Image<> class operations
/////////////////////////////////////////////////////////////////////////////
// Create subimage of given image.
template <class T>
Image<T>
Image<T>::subimage(const Bounds<int> bsub) {
// Make a subimage of current pixel array:
ImageData<T>* subD=D->subimage(bsub);
// New image gets this subimage along with the full header
// of the parent.
return Image(subD,H, new int(0), hcount);
}
// Create subimage of given image, this time const.
// Notice it's necessary to create a non-const ImageData to build a new Image.
template <class T>
const Image<T>
Image<T>::subimage(const Bounds<int> bsub) const {
// Make a subimage of current pixel array, return :
ImageData<T>* subD=D->subimage(bsub);
// New image gets this subimage along with the full header
// of the parent.
return Image(subD,H, new int(0), hcount);
}
// Make a fresh copy of the image (with fresh header)
template <class T>
Image<T>
Image<T>::duplicate() const {
ImageData<T>* dD=D->duplicate();
Header* dH=H->duplicate();
return Image(dD, dH);
}
// Image arithmetic operations
template <class T>
void
Image<T>::operator+=(const Image<T> rhs) {
if (!getBounds().includes(rhs.getBounds()))
throw ImageError("this does not bound rhs in +=");
transform_pixel(*this, rhs, plus<T>());
}
template <class T>
void
Image<T>::operator-=(const Image<T> rhs) {
if (!getBounds().includes(rhs.getBounds()))
throw ImageError("this does not bound rhs in -=");
transform_pixel(*this, rhs, minus<T>());
}
template <class T>
void
Image<T>::operator*=(const Image<T> rhs) {
if (!getBounds().includes(rhs.getBounds()))
throw ImageError("this does not bound rhs in *=");
transform_pixel(*this, rhs, multiplies<T>());
}
template <class T>
void
Image<T>::operator/=(const Image<T> rhs) {
if (!getBounds().includes(rhs.getBounds()))
throw ImageError("this does not bound rhs in /=");
transform_pixel(*this, rhs, divides<T>());
}
template <class T>
Image<T>
Image<T>::operator+(const Image<T> rhs) const {
if (getBounds() != rhs.getBounds())
throw ImageError("Mismatched image bound for +");
Image<T> result(getBounds());
transform_pixel(result, *this, rhs, plus<T>());
return result;
}
template <class T>
Image<T>
Image<T>::operator-(const Image<T> rhs) const {
if (getBounds() != rhs.getBounds())
throw ImageError("Mismatched image bounds for -");
Image<T> result(getBounds());
transform_pixel(result, *this, rhs, minus<T>());
return result;
}
template <class T>
Image<T>
Image<T>::operator*(const Image<T> rhs) const {
if (getBounds() != rhs.getBounds())
throw ImageError("Mismatched image bounds for +");
Image<T> result(getBounds());
transform_pixel(result, *this, rhs, multiplies<T>());
return result;
}
template <class T>
Image<T>
Image<T>::operator/(const Image<T> rhs) const {
if (getBounds() != rhs.getBounds())
throw ImageError("Mismatched image bounds for /");
Image<T> result(getBounds());
transform_pixel(result, *this, rhs, divides<T>());
return result;
}
// instantiate for expected types
template class img::Image<double>;
template class img::Image<float>;
template class img::Image<int>;
template class img::Image<short>;
template class img::ImageData<double>;
template class img::ImageData<float>;
template class img::ImageData<int>;
template class img::ImageData<short>;