File: image.cpp

package info (click to toggle)
wsclean 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,196 kB
  • sloc: cpp: 34,504; ansic: 234; python: 174; makefile: 10
file content (235 lines) | stat: -rw-r--r-- 5,605 bytes parent folder | download
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
#include "image.h"

#include <algorithm>
#include <cmath>

Image::Image(size_t width, size_t height, ImageBufferAllocator& allocator) :
	_data(allocator.Allocate(width*height)),
	_width(width), _height(height), _allocator(&allocator)
{
}

Image::Image(size_t width, size_t height, double initialValue, ImageBufferAllocator& allocator) :
	_data(allocator.Allocate(width*height)),
	_width(width), _height(height), _allocator(&allocator)
{
	std::fill(_data, _data+_width*_height, initialValue);
}

Image::~Image()
{
	if(_allocator != nullptr)
		_allocator->Free(_data);
}

Image::Image(const Image& source) :
	_data((source._allocator==nullptr) ? nullptr : source._allocator->Allocate(source._width * source._height)),
	_width(source._width),
	_height(source._height),
	_allocator(source._allocator)
{
	std::copy(source._data, source._data + _width*_height, _data);
}

Image& Image::operator=(const Image& source)
{
	if(source._allocator == nullptr)
	{
		reset();
	}
	else {
		if(_allocator == nullptr)
		{
			_allocator = source._allocator;
			_data = _allocator->Allocate(source._width * source._height);
		}
		else if(_width * _height != source._width * source._height || source._allocator != _allocator)
		{
			_allocator->Free(_data);
			_allocator = source._allocator;
			_data = _allocator->Allocate(source._width * source._height);
		}
		_width = source._width;
		_height = source._height;
		std::copy(source._data, source._data + _width*_height, _data);
	}
	return *this;
}

Image& Image::operator=(double value)
{
	for(double& v : *this)
		v = value;
	return *this;
}

Image::Image(Image&& source) :
	_data(source._data),
	_width(source._width),
	_height(source._height),
	_allocator(source._allocator)
{
	source._width = 0;
	source._height = 0;
	source._data = nullptr;
	source._allocator = nullptr;
}

Image& Image::operator=(Image&& source)
{
	std::swap(_data, source._data);
	std::swap(_width, source._width);
	std::swap(_height, source._height);
	std::swap(_allocator, source._allocator);
	return *this;
}

void Image::reset()
{
	if(_allocator != nullptr)
		_allocator->Free(_data);
	_data = nullptr;
	_width = 0;
	_height = 0;
	_allocator = nullptr;
}

Image& Image::operator+=(const Image& other)
{
	for(size_t i=0; i!=_width*_height; ++i)
		_data[i] += other[i];
	return *this;
}

Image& Image::operator*=(double factor)
{
	for(size_t i=0; i!=_width*_height; ++i)
		_data[i] *= factor;
	return *this;
}

Image& Image::operator*=(const Image& other)
{
	for(size_t i=0; i!=_width*_height; ++i)
		_data[i] *= other[i];
	return *this;
}

// Cut-off the borders of an image.
// @param outWidth Should be <= inWidth.
// @param outHeight Should be <= inHeight.
void Image::Trim(double* output, size_t outWidth, size_t outHeight, const double* input, size_t inWidth, size_t inHeight)
{
	size_t startX = (inWidth - outWidth) / 2;
	size_t startY = (inHeight - outHeight) / 2;
	size_t endY = (inHeight + outHeight) / 2;
	for(size_t y=startY; y!=endY; ++y)
	{
		memcpy(&output[(y-startY)*outWidth], &input[y*inWidth + startX], outWidth*sizeof(double));
	}
}

/** Extend an image with zeros, complement of Trim.
	* @param outWidth Should be &gt;= inWidth.
	* @param outHeight Should be &gt;= inHeight.
	*/
void Image::Untrim(double* output, size_t outWidth, size_t outHeight, const double* input, size_t inWidth, size_t inHeight)
{
	size_t startX = (outWidth - inWidth) / 2;
	size_t endX = (outWidth + inWidth) / 2;
	size_t startY = (outHeight - inHeight) / 2;
	size_t endY = (outHeight + inHeight) / 2;
	for(size_t y=0; y!=startY; ++y)
	{
		double* ptr = &output[y*outWidth];
		for(size_t x=0; x!=outWidth; ++x)
			ptr[x] = 0.0;
	}
	for(size_t y=startY; y!=endY; ++y)
	{
		double* ptr = &output[y*outWidth];
		for(size_t x=0; x!=startX; ++x)
			ptr[x] = 0.0;
		memcpy(&output[y*outWidth + startX], &input[(y-startY)*inWidth], inWidth*sizeof(double));
		for(size_t x=endX; x!=outWidth; ++x)
			ptr[x] = 0.0;
	}
	for(size_t y=endY; y!=outHeight; ++y)
	{
		double* ptr = &output[y*outWidth];
		for(size_t x=0; x!=outWidth; ++x)
			ptr[x] = 0.0;
	}
}

double Image::Sum() const
{
	double sum = 0.0;
	for(const double& v : *this)
		sum += v;
	return sum;
}

double Image::Average() const
{
	return Sum() / size();
}

double Image::Min() const
{
	return *std::min_element(begin(), end());
}

double Image::Max() const
{
	return *std::max_element(begin(), end());
}

double Image::median_with_copy(const double* data, size_t size, ao::uvector<double>& copy)
{
	copy.reserve(size);
	for(const double* i=data ; i!=data+size; ++i)
	{
		if(std::isfinite(*i))
			copy.push_back(*i);
	}
	if(copy.empty())
		return 0.0;
	else {
		bool even = (copy.size()%2) == 0;
		ao::uvector<double>::iterator mid = copy.begin()+(copy.size()-1)/2;
		std::nth_element(copy.begin(), mid, copy.end());
		double median = *mid;
		if(even)
		{
			std::nth_element(mid, mid+1, copy.end());
			median = (median + *(mid+1)) * 0.5;
		}
		return median;
	}
}

double Image::MAD(const double* data, size_t size)
{
	ao::uvector<double> copy;
	double median = median_with_copy(data, size, copy);
	if(copy.empty())
		return 0.0;
		
	// Replace all values by the difference from the mean
	ao::uvector<double>::iterator mid = copy.begin()+(copy.size()-1)/2;
	for(ao::uvector<double>::iterator i=copy.begin(); i!=mid+1; ++i)
		*i = median - *i;
	for(ao::uvector<double>::iterator i=mid+1; i!=copy.end(); ++i)
		*i = *i - median;
	
	std::nth_element(copy.begin(), mid, copy.end());
	median = *mid;
	bool even = (copy.size()%2) == 0;
	if(even)
	{
		std::nth_element(mid, mid+1, copy.end());
		median = (median + *(mid+1)) * 0.5;
	}
	return median;
}