Repository navigation
Expand file tree
/
Copy pathImage.cpp
More file actions
301 lines (263 loc) · 9.57 KB
/
Copy pathImage.cpp
File metadata and controls
301 lines (263 loc) · 9.57 KB
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
/* -*- c++ -*-
* Copyright (c) 2012-2014 by the GalSim developers team on GitHub
* https://github.com/GalSim-developers
*
* This file is part of GalSim: The modular galaxy image simulation toolkit.
* https://github.com/GalSim-developers/GalSim
*
* GalSim is free software: redistribution and use in source and binary forms,
* with or without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions, and the disclaimer given in the accompanying LICENSE
* file.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions, and the disclaimer given in the documentation
* and/or other materials provided with the distribution.
*/
#include <sstream>
#include "Image.h"
#include "ImageArith.h"
#include "FFT.h"
namespace galsim {
/////////////////////////////////////////////////////////////////////
//// Constructor for out-of-bounds that has coordinate info
///////////////////////////////////////////////////////////////////////
std::string MakeErrorMessage(
const std::string& m, const int min, const int max, const int tried)
{
// See discussion in Std.h about this initial value.
std::ostringstream oss(" ");
oss << "Attempt to access "<<m<<" number "<<tried
<< ", range is "<<min<<" to "<<max;
return oss.str();
}
ImageBoundsError::ImageBoundsError(
const std::string& m, const int min, const int max, const int tried) :
ImageError(MakeErrorMessage(m,min,max,tried))
{}
std::string MakeErrorMessage(const int x, const int y, const Bounds<int> b)
{
std::ostringstream oss(" ");
bool found=false;
if (x<b.getXMin() || x>b.getXMax()) {
oss << "Attempt to access column number "<<x
<< ", range is "<<b.getXMin()<<" to "<<b.getXMax();
found = true;
}
if (y<b.getYMin() || y>b.getYMax()) {
if (found) oss << " and ";
oss << "Attempt to access row number "<<y
<< ", range is "<<b.getYMin()<<" to "<<b.getYMax();
found = true;
}
if (!found) return "Cannot find bounds violation ???";
else return oss.str();
}
ImageBoundsError::ImageBoundsError(const int x, const int y, const Bounds<int> b) :
ImageError(MakeErrorMessage(x,y,b))
{}
/////////////////////////////////////////////////////////////////////
//// Constructor (and related helpers) for the various Image classes
///////////////////////////////////////////////////////////////////////
namespace {
template <typename T>
class ArrayDeleter {
public:
void operator()(T * p) const { delete [] p; }
};
} // anonymous
template <typename T>
BaseImage<T>::BaseImage(const Bounds<int>& b) :
AssignableToImage<T>(b), _owner(), _data(0), _nElements(0), _stride(0)
{
if (this->_bounds.isDefined()) allocateMem();
// Else _data is left as 0, stride = 0.
}
template <typename T>
void BaseImage<T>::allocateMem()
{
// Note: this version always does the memory (re-)allocation.
// So the various functions that call this should do their (different) checks
// for whether this is necessary.
_stride = this->_bounds.getXMax() - this->_bounds.getXMin() + 1;
_nElements = _stride * (this->_bounds.getYMax() - this->_bounds.getYMin() + 1);
if (_stride <= 0 || _nElements <= 0) {
FormatAndThrow<ImageError>() <<
"Attempt to create an Image with defined but invalid Bounds ("<<this->_bounds<<")";
}
// The ArrayDeleter is because we use "new T[]" rather than an normal new.
// Without ArrayDeleter, shared_ptr would just use a regular delete, rather
// than the required "delete []".
_owner.reset(new T[_nElements], ArrayDeleter<T>());
_data = _owner.get();
}
template <typename T>
ImageAlloc<T>::ImageAlloc(int ncol, int nrow, T init_value) :
BaseImage<T>(Bounds<int>(1,ncol,1,nrow))
{
if (ncol <= 0 || nrow <= 0) {
std::ostringstream oss(" ");
if (ncol <= 0) {
if (nrow <= 0) {
oss << "Attempt to create an Image with non-positive ncol ("<<
ncol<<") and nrow ("<<nrow<<")";
} else {
oss << "Attempt to create an Image with non-positive ncol ("<<
ncol<<")";
}
} else {
oss << "Attempt to create an Image with non-positive nrow ("<<
nrow<<")";
}
throw ImageError(oss.str());
}
fill(init_value);
}
template <typename T>
ImageAlloc<T>::ImageAlloc(const Bounds<int>& bounds, const T init_value) :
BaseImage<T>(bounds)
{
fill(init_value);
}
template <typename T>
void ImageAlloc<T>::resize(const Bounds<int>& new_bounds)
{
if (!new_bounds.isDefined()) {
// Then this is really a deallocation. Clear out the existing memory.
this->_bounds = new_bounds;
this->_owner.reset();
this->_data = 0;
this->_nElements = 0;
this->_stride = 0;
} else if (this->_bounds.isDefined() &&
new_bounds.area() <= this->_nElements &&
this->_owner.unique()) {
// Then safe to keep existing memory allocation.
// Just redefine the bounds and stride.
this->_bounds = new_bounds;
this->_stride = new_bounds.getXMax() - new_bounds.getXMin() + 1;
} else {
// Then we want to do the reallocation.
this->_bounds = new_bounds;
this->allocateMem();
}
}
/////////////////////////////////////////////////////////////////////
//// Access methods
///////////////////////////////////////////////////////////////////////
template <typename T>
const T& BaseImage<T>::at(const int xpos, const int ypos) const
{
if (!_data) throw ImageError("Attempt to access values of an undefined image");
if (!this->_bounds.includes(xpos, ypos)) throw ImageBoundsError(xpos, ypos, this->_bounds);
return _data[addressPixel(xpos, ypos)];
}
template <typename T>
T& ImageView<T>::at(const int xpos, const int ypos) const
{
if (!this->_data) throw ImageError("Attempt to access values of an undefined image");
if (!this->_bounds.includes(xpos, ypos)) throw ImageBoundsError(xpos, ypos, this->_bounds);
return this->_data[this->addressPixel(xpos, ypos)];
}
template <typename T>
T& ImageAlloc<T>::at(const int xpos, const int ypos)
{
if (!this->_data) throw ImageError("Attempt to access values of an undefined image");
if (!this->_bounds.includes(xpos, ypos)) throw ImageBoundsError(xpos, ypos, this->_bounds);
return this->_data[this->addressPixel(xpos, ypos)];
}
template <typename T>
const T& ImageAlloc<T>::at(const int xpos, const int ypos) const
{
if (!this->_data) throw ImageError("Attempt to access values of an undefined image");
if (!this->_bounds.includes(xpos, ypos)) throw ImageBoundsError(xpos, ypos, this->_bounds);
return this->_data[this->addressPixel(xpos, ypos)];
}
template <typename T>
ConstImageView<T> BaseImage<T>::subImage(const Bounds<int>& bounds) const
{
if (!_data) throw ImageError("Attempt to make subImage of an undefined image");
if (!this->_bounds.includes(bounds)) {
FormatAndThrow<ImageError>() <<
"Subimage bounds (" << bounds << ") are outside original image bounds (" <<
this->_bounds << ")";
}
T* newdata = _data
+ (bounds.getYMin() - this->_bounds.getYMin()) * _stride
+ (bounds.getXMin() - this->_bounds.getXMin());
return ConstImageView<T>(newdata,_owner,_stride,bounds);
}
template <typename T>
ImageView<T> ImageView<T>::subImage(const Bounds<int>& bounds) const
{
if (!this->_data) throw ImageError("Attempt to make subImage of an undefined image");
if (!this->_bounds.includes(bounds)) {
FormatAndThrow<ImageError>() <<
"Subimage bounds (" << bounds << ") are outside original image bounds (" <<
this->_bounds << ")";
}
T* newdata = this->_data
+ (bounds.getYMin() - this->_bounds.getYMin()) * this->_stride
+ (bounds.getXMin() - this->_bounds.getXMin());
return ImageView<T>(newdata,this->_owner,this->_stride,bounds);
}
namespace {
template <typename T>
class ConstReturn
{
public:
ConstReturn(const T v): val(v) {}
T operator()(const T ) const { return val; }
private:
T val;
};
template <typename T>
class ReturnInverse
{
public:
double operator()(const T val) const { return val==T(0) ? 0. : 1./double(val); }
};
template <typename T>
class ReturnSecond
{
public:
T operator()(T, T v) const { return v; }
};
} // anonymous
template <typename T>
void ImageView<T>::fill(T x) const
{
transform_pixel(*this, ConstReturn<T>(x));
}
template <typename T>
void ImageView<T>::invertSelf() const
{
transform_pixel(*this, ReturnInverse<T>());
}
template <typename T>
void ImageView<T>::copyFrom(const BaseImage<T>& rhs) const
{
if (!this->_bounds.isSameShapeAs(rhs.getBounds()))
throw ImageError("Attempt im1 = im2, but bounds not the same shape");
transform_pixel(*this, rhs, ReturnSecond<T>());
}
// instantiate for expected types
template class BaseImage<double>;
template class BaseImage<float>;
template class BaseImage<int32_t>;
template class BaseImage<int16_t>;
template class ImageAlloc<double>;
template class ImageAlloc<float>;
template class ImageAlloc<int32_t>;
template class ImageAlloc<int16_t>;
template class ImageView<double>;
template class ImageView<float>;
template class ImageView<int32_t>;
template class ImageView<int16_t>;
template class ConstImageView<double>;
template class ConstImageView<float>;
template class ConstImageView<int32_t>;
template class ConstImageView<int16_t>;
} // namespace galsim