File: image.h

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 (158 lines) | stat: -rw-r--r-- 4,505 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
#ifndef IMAGE_H
#define IMAGE_H

#include <cstring>

#include "wsclean/imagebufferallocator.h"

#include "uvector.h"

class Image
{
public:
	typedef double* iterator;
	typedef const double* const_iterator;
	
	Image() : _data(nullptr), _width(0), _height(0), _allocator(nullptr) { }
	Image(size_t width, size_t height, class ImageBufferAllocator& allocator);
	Image(size_t width, size_t height, double initialValue, ImageBufferAllocator& allocator);
	
	~Image();
	
	Image(const Image&);
	Image& operator=(const Image&);
	Image& operator=(double value);
	
	Image(Image&& source);
	Image& operator=(Image&& source);
	
	double* data() { return _data; }
	const double* data() const { return _data; }
	
	size_t Width() const { return _width; }
	size_t Height() const { return _height; }
	size_t size() const { return _width * _height; }
	bool empty() const { return _width == 0 || _height == 0; }
	ImageBufferAllocator& Allocator() const { return *_allocator; }
	
	iterator begin() { return _data; }
	const_iterator begin() const { return _data; }
	
	iterator end() { return _data + _width*_height; }
	const_iterator end() const { return _data + _width*_height; }
	
	const double& operator[](size_t index) const { return _data[index]; }
	double& operator[](size_t index) { return _data[index]; }
	
	Image& operator+=(const Image& other);
	
	Image& operator*=(double factor);
	Image& operator*=(const Image& other);
	
	void reset();
	
	/** Cut-off the borders of an image.
	 * @param outWidth Should be &lt;= inWidth.
	 * @param outHeight Should be &lt;= inHeight.
	 */
	static void Trim(double* output, size_t outWidth, size_t outHeight, const double* input, size_t inWidth, size_t inHeight);
	
	Image Trim(size_t outWidth, size_t outHeight) const
	{
		Image image(outWidth, outHeight, *_allocator);
		Trim(image.data(), outWidth, outHeight, data(), Width(), Height());
		return image;
	}
	
	Image TrimBox(size_t x1, size_t y1, size_t boxWidth, size_t boxHeight) const
	{
		Image image(boxWidth, boxHeight, *_allocator);
		TrimBox(image.data(), x1, y1, boxWidth, boxHeight, data(), Width(), Height());
		return image;
	}
	
	template<typename T>
	static void TrimBox(T* output, size_t x1, size_t y1, size_t boxWidth, size_t boxHeight, const T* input, size_t inWidth, size_t inHeight);
	
	template<typename T>
	static void CopyMasked(T* to, size_t toX, size_t toY, size_t toWidth, const T* from, size_t fromWidth, size_t fromHeight, const bool* fromMask);
	
	/** Extend an image with zeros, complement of Trim.
	 * @param outWidth Should be &gt;= inWidth.
	 * @param outHeight Should be &gt;= inHeight.
	 */
	static void Untrim(double* output, size_t outWidth, size_t outHeight, const double* input, size_t inWidth, size_t inHeight);
	
	Image Untrim(size_t outWidth, size_t outHeight) const
	{
		Image image(outWidth, outHeight, *_allocator);
		Untrim(image.data(), outWidth, outHeight, data(), Width(), Height());
		return image;
	}
	
	static double Median(const double* data, size_t size)
	{
		ao::uvector<double> copy;
		return median_with_copy(data, size, copy);
	}
	
	static double MAD(const double* data, size_t size);
	
	double Sum() const;
	double Average() const;
	
	double Min() const;
	double Max() const;
	
	double StdDevFromMAD() const { return StdDevFromMAD(_data, _width*_height); }
	static double StdDevFromMAD(const double* data, size_t size)
	{
		// norminv(0.75) x MAD
		return 1.48260221850560 * MAD(data, size);
	}
	
	static double RMS(const double* data, size_t size)
	{
		double sum = 0.0;
		for(size_t i=0; i!=size; ++i)
			sum += data[i]*data[i];
		return sqrt(sum/size);
	}
	
	void Negate()
	{
		for(double& d : *this)
			d = -d;
	}
private:
	double* _data;
	size_t _width, _height;
	ImageBufferAllocator* _allocator;
	
	static double median_with_copy(const double* data, size_t size, ao::uvector<double>& copy);
};

template<typename T>
void Image::TrimBox(T* output, size_t x1, size_t y1, size_t boxWidth, size_t boxHeight, const T* input, size_t inWidth, size_t /*inHeight*/)
{
	size_t endY = y1 + boxHeight;
	for(size_t y=y1; y!=endY; ++y)
	{
		std::copy_n(&input[y*inWidth + x1], boxWidth, &output[(y-y1)*boxWidth]);
	}
}

template<typename T>
void Image::CopyMasked(T* to, size_t toX, size_t toY, size_t toWidth, const T* from, size_t fromWidth, size_t fromHeight, const bool* fromMask)
{
	for(size_t y=0; y!=fromHeight; ++y)
	{
		for(size_t x=0; x!=fromWidth; ++x)
		{
			if(fromMask[y*fromWidth + x])
				to[toX + (toY+y) * toWidth + x] = from[y*fromWidth + x];
		}
	}
}

#endif