-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRotator.cpp
More file actions
218 lines (180 loc) · 4.2 KB
/
Rotator.cpp
File metadata and controls
218 lines (180 loc) · 4.2 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
#include <Magick++.h>
#include <iostream>
#include <string>
#include <stdint.h>
#include <cassert>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
using namespace Magick;
int usage(string argv0) {
cerr << "USAGE: " << argv0 << " <input_image> <output_image> [maxangle] [intervals]" << endl;
return 1;
}
class ImageWrapper {
const PixelPacket *pp;
const int colorShift;
const int width, height;
public:
ImageWrapper(const Image &image)
:
colorShift(8 * (sizeof(pp->green) - sizeof(uint8_t))),
width(image.size().width()),
height(image.size().height())
{
pp = image.getConstPixels(0, 0, width, height);
}
uint8_t gray(int x, int y) const {
int v = (*this)(x, y)->green >> colorShift;
assert(v < 256);
assert(v >= 0);
return v;
}
const PixelPacket *operator()(int x, int y) const {
if (x < 0)
x += width;
if (y < 0)
y += height;
if (x >= width)
x -= width;
if (y >= height)
y -= height;
assert(x >= 0 && y >= 0 && x < width && y < height);
return pp + y * width + x;
}
int isblack(int x, int y) const {
return gray(x, y) < 128;
}
int getColorShift() const {
return colorShift;
}
};
int lineScore(const Image &input, int deltay) {
Geometry g = input.size();
const int width = g.width();
const int height = g.height();
ImageWrapper iw(input);
std::vector<int> lineTotal(height, 0);
int dir = deltay > 0 ? 1 : (deltay < 0 ? -1 : 0);
int deltaerr = deltay * dir;
int cnt = 0;
for (int y0 = 0; y0 < height; y0++) {
int y = y0;
int error = 0;
lineTotal[y0] = 0;
for (int x = 0; x < width; x++) {
lineTotal[y0] += iw.isblack(x, y);
error += deltaerr;
if (2 * error >= width) {
y += dir;
error -= width;
}
}
if (lineTotal[y0])
cnt++;
}
return cnt;
}
void tilt(const Image &input, Image &output, int deltay) {
Geometry g = input.size();
const int width = g.width();
const int height = g.height();
ImageWrapper iw(input);
int dir = deltay > 0 ? 1 : (deltay < 0 ? -1 : 0);
int deltaerr = deltay * dir;
for (int y0 = 0; y0 < height; y0++) {
int y = y0;
int error = 0;
for (int x = 0; x < width; x++) {
*output.getPixels(x, y0, 1, 1) = *iw(x, y);
error += deltaerr;
if (2 * error >= width) {
y += dir;
error -= width;
}
}
}
}
int main(int argc, char **argv)
{
if (argc < 3 || argc > 5)
return usage(argv[0]);
int cols = 21;
double maxangle = 10;
if (argc > 3)
maxangle = atof(argv[3]);
if (argc > 4)
cols = atoi(argv[4]);
cout << "Processing `" << argv[1] << "' into `" << argv[2] << "'" << " with angular range from -" << maxangle << " to " << maxangle
<< " degrees and " << cols << " intervals" << endl;
InitializeMagick(*argv);
Image input;
Color black("black");
try {
input.read(argv[1]);
input.quantizeColorSpace(GRAYColorspace);
input.quantizeColors(256);
input.quantize();
std::vector<int> dy(cols);
std::vector<int> score(cols);
Image output(input.size(), "white");
int width = input.size().width();
int minpos = 0;
double range = tan(maxangle / 45 * atan(1.));
for (int i = 0; i < cols; i++) {
dy[i] = range * width * (2 * i - cols + 1) / (cols - 1);
score[i] = lineScore(input, dy[i]);
if (score[i] < score[minpos])
minpos = i;
}
if (minpos == 0 || minpos == (cols - 1)) {
cerr << "Angle range was too narrow" << endl;
return 2;
}
int a = dy[minpos - 1];
int b = dy[minpos + 1];
double w = 0.381966;
int c = a + w * (b - a);
int d = a + b - c;
int fc = lineScore(input, c);
int fd = lineScore(input, d);
while (b > d && d > c && c > a) {
if (fd > fc) {
b = d;
d = c;
c = a + b - d;
fd = fc;
fc = lineScore(input, c);
} else {
a = c;
c = d;
d = a + b - c;
fc = fd;
fd = lineScore(input, d);
}
}
int fa;
if (fd < fc) {
minpos = d;
fa = fd;
} else {
minpos = c;
fa = fc;
}
for (int i = std::min(d, c); i <= std::max(d, c) + 1; i++) {
int v = lineScore(input, i);
if (v < fa) {
fa = v;
minpos = i;
}
}
cout << "Optimum tilt = " << minpos << "px" << endl;
tilt(input, output, minpos);
output.write(argv[2]);
} catch (Exception &e) {
cerr << "Caught exception: " << e.what() << endl;
return 1;
}
return 0;
}