-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageAlgorithms.h
More file actions
163 lines (147 loc) · 5.02 KB
/
ImageAlgorithms.h
File metadata and controls
163 lines (147 loc) · 5.02 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
// Define algorithm templates which iterate over pixels in images.
// Used for example to define image arithmetic.
#ifndef IMAGE_ALGORITHM_H
#define IMAGE_ALGORITHM_H
namespace img {
// Execute function on each pixel value
template <class Img, class Op>
Op for_each_pixel(Img I, Op f) {
for (int i=I.yMin(); i<=I.yMax(); i++)
f=for_each(I.rowBegin(i), I.rowEnd(i), f);
return f;
}
// Execute function on a range of pixels
template <class Img, class Op>
Op for_each_pixel(Img I, Bounds<int> b, Op f) {
if (!I.getBounds().includes(b))
throw ImageError("for_each_pixel range exceeds image range");
for (int i=b.getYMin(); i<=b.getYMax(); i++)
f=for_each(I.getIterator(b.getXMin(),i),
I.getIterator(b.getXMax()+1,i), f);
return f;
}
// Replace image with function of itself
template <class Img, class Op>
Op transform_pixel(Img I, Op f) {
for (int y=I.yMin(); y<=I.yMax(); y++) {
typename Img::iterator ee=I.rowEnd(y);
for (typename Img::iterator it=I.rowBegin(y);
it!=ee;
++it)
*it=f(*it);
}
return f;
}
// Replace image with function of itself over range
template <class Img, class Op>
Op transform_pixel(Img I, Bounds<int> b, Op f) {
if (!I.getBounds().includes(b))
throw ImageError("transform_pixel range exceeds image range");
for (int y=b.getYMin(); y<=b.getYMax(); y++) {
typename Img::iterator ee=I.getIterator(b.getXMax()+1,y);
for (typename Img::iterator it=I.getIterator(b.getXMin(),y);
it!=ee;
++it)
*it=f(*it);
}
return f;
}
// Add function of pixel coords to image
template <class Img, class Op>
Op add_function_pixel(Img I, Op f) {
for (int y=I.yMin(); y<=I.yMax(); y++) {
int x=I.xMin();
typename Img::iterator ee=I.rowEnd(y);
for (typename Img::iterator it=I.rowBegin(y);
it!=ee;
++it, ++x)
*it+=f(x,y);
}
return f;
}
// Add function of pixel coords to image over a range
template <class Img, class Op>
Op add_function_pixel(Img I, Bounds<int> b, Op f) {
if (b && !I.getBounds().includes(b))
throw ImageError("add_function_pixel range exceeds image range");
for (int y=b.getYMin(); y<=b.getYMax(); y++) {
int x=b.getXMin();
typename Img::iterator ee=I.getIterator(b.getXMax()+1,y);
for (typename Img::iterator it=I.getIterator(b.getXMin(),y);
it!=ee;
++it, ++x)
*it+=f(x,y);
}
return f;
}
// Replace image with function of pixel coords
template <class Img, class Op>
Op fill_pixel(Img I, Op f) {
for (int y=I.yMin(); y<=I.yMax(); y++) {
int x=I.xMin();
typename Img::iterator ee=I.rowEnd(y);
for (typename Img::iterator it=I.rowBegin(y);
it!=ee;
++it, ++x)
*it=f(x,y);
}
return f;
}
// Replace image with function of pixel coords, over specified bounds
template <class Img, class Op>
Op fill_pixel(Img I, Bounds<int> b, Op f) {
if (!I.getBounds().includes(b))
throw ImageError("add_function_pixel range exceeds image range");
for (int y=b.getYMin(); y<=b.getYMax(); y++) {
int x=b.getXMin();
typename Img::iterator ee=I.getIterator(b.getXMax()+1,y);
for (typename Img::iterator it=I.getIterator(b.getXMin(),y);
it!=ee;
++it, ++x)
*it=f(x,y);
}
return f;
}
// Assign function of 2 images to 1st. Pixel range comes from 2nd image
template <class Img1, class Img2, class Op>
Op transform_pixel(Img1 I1, const Img2 I2, Op f) {
for (int y=I2.yMin(); y<=I2.yMax(); y++) {
typename Img1::iterator it1=I1.getIterator(I2.xMin(),y);
typename Img2::const_iterator ee=I2.rowEnd(y);
for (typename Img2::const_iterator it2=I2.rowBegin(y);
it2!=ee; ++it1, ++it2) *it1=f(*it1,*it2);
}
return f;
}
// Assign function of Img2 & Img3 to Img1
template <class Img1, class Img2, class Img3, class Op>
Op transform_pixel(Img1 I1, const Img2 I2, const Img3 I3, Op f) {
for (int y=I1.yMin(); y<=I1.yMax(); y++) {
typename Img2::const_iterator it2=I2.getIterator(I1.xMin(),y);
typename Img3::const_iterator it3=I3.getIterator(I1.xMin(),y);
typename Img1::iterator ee=I1.rowEnd(y);
for (typename Img1::iterator it1=I1.rowBegin(y);
it1!=ee;
++it1, ++it2, ++it3)
*it1=f(*it2,*it3);
}
return f;
}
// Assign function of 2 images to 1st over bounds
template <class Img1, class Img2, class Op>
Op transform_pixel(Img1 I1, const Img2 I2, Op f, Bounds<int> b) {
if (!I1.getBounds().includes(b) || !I2.getBounds().includes(b))
throw ImageError("transform_pixel range exceeds image range");
for (int y=b.getYMin(); y<=b.getYMax(); y++) {
int x=b.getXMin();
typename Img1::iterator ee=I1.getIterator(b.getXMax()+1,y);
typename Img2::const_iterator it2=I2.getIterator(b.getXMin(),y);
for (typename Img1::iterator it1=I1.getIterator(b.getXMin(),y);
it1!=ee;
++it1, ++it2)
*it1=f(*it1,*it2);
}
return f;
}
} // end namespace img
#endif // IMAGE_ALGORITHM_H