-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath-filling3.c++
329 lines (277 loc) · 7.76 KB
/
path-filling3.c++
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL2_rotozoom.h>
#include <vector>
#include <sys/time.h>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <unistd.h>
#define STITCH_COST 10
#define MINSTITCH 20
#define MAXSTITCH 70
#define MAXSTITCHERRORSQR 9
#define MINAREA 2
// #define MAXSTITCHERRORSQR 20
int SCALE = 3;
int DEFAULT_STITCH = 10;
using namespace std;
struct Path {
struct Step {
int x, y;
};
std::vector<Step> steps;
unsigned int r, g, b;
void render(SDL_Surface *s, SDL_Renderer *rnd) {
SDL_SetRenderDrawColor(rnd, r, g, b, 255);
int x = s->w / 2;
int y = s->h / 2;
for(Step &s: steps) {
SDL_RenderDrawLine(rnd, x, y, x + s.x, y + s.y);
x += s.x;
y += s.y;
}
}
};
uint32_t &pixel(SDL_Surface *s, int x, int y) {
return *reinterpret_cast<uint32_t *>(reinterpret_cast<uint8_t *>(s->pixels) + s->pitch * y + 4 * x);
}
uint64_t time() {
struct timeval tv;
gettimeofday(&tv, nullptr);
return tv.tv_sec * 1000000ull + tv.tv_usec;
}
int remainingAreaRec(SDL_Surface *img, int x, int y, int area) {
if(area <= 0) return 0;
pixel(img, x, y) |= 0x0100;
--area;
for(int dy = -1; dy < 2; ++dy) {
for(int dx = -1; dx < 2; ++dx) {
if(y + dy < 0 || y + dy >= img->h || x + dx < 0 || x + dx >= img->w) continue;
if(pixel(img, x + dx, y + dy) & 0xff && !(pixel(img, x + dx, y + dy) & 0xff00)) {
area = remainingAreaRec(img, x + dx, y + dy, area);
}
}
}
return area;
}
bool hasAreaAbove(SDL_Surface *img, int x, int y, int area) {
for(int y = 0; y < img->h; ++y) {
for(int x = 0; x < img->w; ++x) {
pixel(img, x, y) &= 0xffff00fful;
}
}
return remainingAreaRec(img, x, y, area) == 0;
}
void moveToTopLeftRec(SDL_Surface *img, int &x, int &y, int sx, int sy) {
pixel(img, sx, sy) |= 0x0100;
if(sy < y || (sy == y && sx < x)) {
y = sy;
x = sx;
}
for(int dy = -1; dy < 2; ++dy) {
for(int dx = -1; dx < 2; ++dx) {
if(sy + dy < 0 || sy + dy >= img->h || sx + dx < 0 || sx + dx >= img->w) continue;
if(pixel(img, sx + dx, sy + dy) & 0xff && !(pixel(img, sx + dx, sy + dy) & 0xff00)) {
moveToTopLeftRec(img, x, y, sx + dx, sy + dy);
}
}
}
}
void moveToTopLeft(SDL_Surface *img, int &x, int &y) {
for(int sy = 0; sy < img->h; ++sy) {
for(int sx = 0; sx < img->w; ++sx) {
pixel(img, sx, sy) &= 0xffff00fful;
}
}
moveToTopLeftRec(img, x, y, x, y);
}
bool findStart(SDL_Surface *img, int &cx, int &cy, int &ox, int &oy, Path &path) {
int bestX = -1;
int bestY = -1;
int bestDist = 999999999;
for(int y = 0; y < img->h; ++y) {
for(int x = 0; x < img->w; ++x) {
if(pixel(img, x, y) & 0xff) {
int dist = (cx - x) * (cx - x) + (cy - y) * (cy - y);
if(dist < bestDist) {
bestX = x;
bestY = y;
bestDist = dist;
}
}
}
}
if(bestX < 0) return false;
std::cerr << "before: " << bestX << "," << bestY << std::endl;
moveToTopLeft(img, bestX, bestY);
std::cerr << "after: " << bestX << "," << bestY << std::endl;
if(!(pixel(img, bestX, bestY) & 0xff)) {
std::cerr << "wrong" << pixel(img, bestX, bestY) << std::endl;
}
cx = bestX;
cy = bestY;
if(ox > 0) path.steps.push_back({cx - ox, cy - oy});
ox = cx;
oy = cy;
if(oy < 0 || ox < 0) {
ox = cx;
oy = cy;
}
return true;
}
int main(int argc, const char *const argv[]) {
if(argc != 3) {
cerr << "usage: ./path-filling <input image> <output.p>" << endl;
return 1;
}
istringstream default_stitch(argv[1]);
default_stitch >> DEFAULT_STITCH;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
cerr << "Could not init SDL: " << SDL_GetError() << endl;
return 1;
}
const int allFormats = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF;
if(!IMG_Init(allFormats)) {
cerr << "Could not init SDL_Image" << endl;
return 1;
}
SDL_Surface *const rawImg = IMG_Load(argv[1]);
if(!rawImg) {
cerr << "Could not load image" << endl;
return 1;
}
SDL_Surface *const img = SDL_CreateRGBSurface(0, rawImg->w, rawImg->h, 32, 0xff, 0xff00, 0xff0000, 0xff000000u);
if(!img || !rawImg) {
cerr << "Could not load image" << endl;
return 1;
}
SDL_BlitSurface(rawImg, 0, img, 0);
SDL_Window *const win = SDL_CreateWindow("path-guessing", 0, 0, img->w, img->h, 0);
if(!win) {
cerr << "Could not open window" << endl;
return 1;
}
SDL_Surface *const screen = SDL_GetWindowSurface(win);
if(!screen) {
cerr << "Could not get screen surface" << endl;
return 1;
}
atexit(SDL_Quit);
Path path;
SDL_Event event;
uint64_t last = time();
int cx = 0;
int cy = 0;
int ox = -1;
int oy = -1;
#define MODE_RIGHT 0
#define MODE_RIGHTOVERSCAN 1
#define MODE_RIGHTBACKSCAN 2
#define MODE_LEFT 3
#define MODE_LEFTOVERSCAN 4
#define MODE_LEFTBACKSCAN 5
int mode = 0;
bool running = findStart(img, cx, cy, ox, oy, path);
while(running) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT: running = false;
}
}
int maxStitch = (rand() % (MAXSTITCH - MINSTITCH)) + MINSTITCH;
for(int i = 0; i < maxStitch; ++i) {
switch(mode) {
case MODE_RIGHT:
pixel(img, cx, cy) = 0xff800000ul;
if(pixel(img, cx + 1, cy) & 0xff) {
++cx;
} else {
++cy;
if(pixel(img, cx, cy) & 0xff) {
mode = MODE_RIGHTOVERSCAN;
} else {
mode = MODE_RIGHTBACKSCAN;
}
}
break;
case MODE_RIGHTOVERSCAN:
if(pixel(img, cx + 1, cy) & 0xff) {
++cx;
} else {
i = maxStitch;
mode = MODE_LEFT;
}
break;
case MODE_RIGHTBACKSCAN:
if(pixel(img, cx - 1, cy) & 0xff) {
--cx;
i = maxStitch;
mode = MODE_LEFT;
} else if(pixel(img, cx - 1, cy - 1) & 0xff0000) {
--cx;
} else {
running = findStart(img, cx, cy, ox, oy, path);
mode = MODE_RIGHT;
}
break;
case MODE_LEFT:
pixel(img, cx, cy) = 0xff800000ul;
if(pixel(img, cx - 1, cy) & 0xff) {
--cx;
} else {
++cy;
if(pixel(img, cx, cy) & 0xff) {
mode = MODE_LEFTOVERSCAN;
} else {
mode = MODE_LEFTBACKSCAN;
}
}
break;
case MODE_LEFTOVERSCAN:
if(pixel(img, cx - 1, cy) & 0xff) {
--cx;
} else {
i = maxStitch;
mode = MODE_RIGHT;
}
break;
case MODE_LEFTBACKSCAN:
if(pixel(img, cx + 1, cy) & 0xff) {
++cx;
i = maxStitch;
mode = MODE_RIGHT;
} else if(pixel(img, cx + 1, cy - 1) & 0xff0000) {
++cx;
} else {
running = findStart(img, cx, cy, ox, oy, path);
mode = MODE_RIGHT;
}
break;
}
}
path.steps.push_back({cx - ox, cy - oy});
ox = cx;
oy = cy;
// RGBW n = rgbw;
// n.mutate(tmp, stage);
// ++stage;
// n.render(tmp);
// SDL_Surface *zoomed = simulateRGBView(tmp);
// int64_t quality2 = rate(zoomedImg, zoomed) - rgbw.stitchCount() * STITCH_COST;
if(time() > last + 1000000ull) {
// SDL_BlitSurface(zoomedImg, 0, screen, 0);
SDL_BlitSurface(img, 0, screen, 0);
SDL_UpdateWindowSurface(win);
last = time();
}
}
ofstream vp3(argv[2]);
vp3 << "White" << endl;
for(Path::Step &s: path.steps) vp3 << s.x << " " << s.y << endl;
SDL_BlitSurface(img, 0, screen, 0);
SDL_UpdateWindowSurface(win);
std::cout << "Total stitch count: " << path.steps.size() << std::endl;
sleep(5);
}