forked from DeviceFarmer/minicap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProjection.hpp
More file actions
253 lines (236 loc) · 6.04 KB
/
Copy pathProjection.hpp
File metadata and controls
253 lines (236 loc) · 6.04 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
#ifndef MINICAP_PROJECTION_HPP
#define MINICAP_PROJECTION_HPP
#include <cmath>
#include <ostream>
class Projection {
public:
class Parser {
public:
Parser(): mState(real_width_start) {
}
bool
parse(Projection& proj, const char* lo, const char* hi) {
consume: while (lo < hi) {
switch (mState) {
case real_width_start:
if (isDigit(*lo)) {
proj.realWidth += (*lo - 48);
mState = real_width_continued;
lo += 1;
goto consume;
}
return false;
case real_width_continued:
if (isDigit(*lo)) {
proj.realWidth *= 10;
proj.realWidth += (*lo - 48);
lo += 1;
goto consume;
}
if (*lo == 'x') {
mState = real_height_start;
lo += 1;
goto consume;
}
return false;
case real_height_start:
if (isDigit(*lo)) {
proj.realHeight += (*lo - 48);
mState = real_height_continued;
lo += 1;
goto consume;
}
return false;
case real_height_continued:
if (isDigit(*lo)) {
proj.realHeight *= 10;
proj.realHeight += (*lo - 48);
lo += 1;
goto consume;
}
if (*lo == '@') {
mState = virtual_width_start;
lo += 1;
goto consume;
}
return false;
case virtual_width_start:
if (isDigit(*lo)) {
proj.virtualWidth += (*lo - 48);
mState = virtual_width_continued;
lo += 1;
goto consume;
}
return false;
case virtual_width_continued:
if (isDigit(*lo)) {
proj.virtualWidth *= 10;
proj.virtualWidth += (*lo - 48);
lo += 1;
goto consume;
}
if (*lo == 'x') {
mState = virtual_height_start;
lo += 1;
goto consume;
}
return false;
case virtual_height_start:
if (isDigit(*lo)) {
proj.virtualHeight += (*lo - 48);
mState = virtual_height_continued;
lo += 1;
goto consume;
}
return false;
case virtual_height_continued:
if (isDigit(*lo)) {
proj.virtualHeight *= 10;
proj.virtualHeight += (*lo - 48);
lo += 1;
goto consume;
}
if (*lo == '/') {
mState = rotation_start;
lo += 1;
goto consume;
}
return false;
case rotation_start:
if (*lo == '0') {
proj.rotation = 0;
mState = satisfied;
lo += 1;
goto consume;
}
if (*lo == '9') {
mState = rotation_90_2;
lo += 1;
goto consume;
}
if (*lo == '1') {
mState = rotation_180_2;
lo += 1;
goto consume;
}
if (*lo == '2') {
mState = rotation_270_2;
lo += 1;
goto consume;
}
return false;
case rotation_90_2:
if (*lo == '0') {
proj.rotation = 1;
mState = satisfied;
lo += 1;
goto consume;
}
return false;
case rotation_180_2:
if (*lo == '8') {
mState = rotation_180_3;
lo += 1;
goto consume;
}
return false;
case rotation_180_3:
if (*lo == '0') {
proj.rotation = 2;
mState = satisfied;
lo += 1;
goto consume;
}
return false;
case rotation_270_2:
if (*lo == '7') {
mState = rotation_270_3;
lo += 1;
goto consume;
}
return false;
case rotation_270_3:
if (*lo == '0') {
proj.rotation = 3;
mState = satisfied;
lo += 1;
goto consume;
}
return false;
case satisfied:
return false;
}
}
return mState == satisfied;
}
private:
enum State {
real_width_start,
real_width_continued,
real_height_start,
real_height_continued,
virtual_width_start,
virtual_width_continued,
virtual_height_start,
virtual_height_continued,
rotation_start,
rotation_90_2,
rotation_180_2,
rotation_180_3,
rotation_270_2,
rotation_270_3,
satisfied,
};
State mState;
inline bool
isDigit(int input) {
return input >= '0' && input <= '9';
}
};
const uint32_t MAX_WIDTH = 10000;
const uint32_t MAX_HEIGHT = 10000;
uint32_t realWidth;
uint32_t realHeight;
uint32_t virtualWidth;
uint32_t virtualHeight;
uint32_t rotation;
Projection()
: realWidth(0),
realHeight(0),
virtualWidth(0),
virtualHeight(0),
rotation(0) {
}
void
forceMaximumSize() {
if (virtualWidth > realWidth) {
virtualWidth = realWidth;
}
if (virtualHeight > realHeight) {
virtualHeight = realHeight;
}
}
void
forceAspectRatio() {
double aspect = static_cast<double>(realWidth) / static_cast<double>(realHeight);
if (virtualHeight > (uint32_t) (virtualWidth / aspect)) {
virtualHeight = static_cast<uint32_t>(round(virtualWidth / aspect));
}
else {
virtualWidth = static_cast<uint32_t>(round(virtualHeight * aspect));
}
}
bool
valid() {
return realWidth > 0 && realHeight > 0 &&
virtualWidth > 0 && virtualHeight > 0 &&
virtualWidth <= realWidth && virtualHeight <= realHeight;
}
friend std::ostream&
operator<< (std::ostream& stream, const Projection& proj) {
stream << proj.realWidth << 'x' << proj.realHeight << '@'
<< proj.virtualWidth << 'x' << proj.virtualHeight << '/' << proj.rotation;
return stream;
}
};
#endif