1 | /* |
---|
2 | * slashdot.c: decode Slashdot captchas |
---|
3 | * $Id: slashdot.c 411 2005-01-04 17:00:50Z sam $ |
---|
4 | * |
---|
5 | * Copyright: (c) 2004 Sam Hocevar <sam@zoy.org> |
---|
6 | * This program is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
8 | * Public License as published by Banlu Kemiyatorn. See |
---|
9 | * http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <stdlib.h> |
---|
14 | #include <string.h> |
---|
15 | #include <limits.h> |
---|
16 | #include <math.h> |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | /* Our macros */ |
---|
22 | #define FACTOR 1 |
---|
23 | #define FONTNAME "share/font_slashdot.png" // use with FACTOR = 1 |
---|
24 | //#define FONTNAME "share/font.png" // use with FACTOR = 2 |
---|
25 | //#define FONTNAME "share/font_dilated.png" // use with FACTOR = 2 |
---|
26 | |
---|
27 | static struct image *count_objects(struct image *img); |
---|
28 | static struct image *rotate(struct image *img); |
---|
29 | static struct image *cut_cells(struct image *img); |
---|
30 | static struct image *find_glyphs(struct image *img); |
---|
31 | |
---|
32 | /* Global stuff */ |
---|
33 | struct { int xmin, ymin, xmax, ymax; } objlist[100]; |
---|
34 | int objects, first, last; |
---|
35 | char *result; |
---|
36 | |
---|
37 | /* Main function */ |
---|
38 | char *decode_slashdot(struct image *img) |
---|
39 | { |
---|
40 | struct image *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7; |
---|
41 | |
---|
42 | /* Initialise local data */ |
---|
43 | objects = 0; |
---|
44 | first = -1; |
---|
45 | last = -1; |
---|
46 | |
---|
47 | /* Slashdot captchas have 7 characters */ |
---|
48 | result = malloc(8 * sizeof(char)); |
---|
49 | strcpy(result, " "); |
---|
50 | |
---|
51 | /* Clean image a bit */ |
---|
52 | tmp1 = filter_detect_lines(img); |
---|
53 | tmp2 = filter_fill_holes(tmp1); |
---|
54 | |
---|
55 | /* Detect small objects to guess image orientation */ |
---|
56 | tmp3 = filter_median(tmp2); |
---|
57 | tmp4 = filter_equalize(tmp3, 200); |
---|
58 | count_objects(tmp4); |
---|
59 | |
---|
60 | /* Invert rotation and find glyphs */ |
---|
61 | tmp5 = rotate(tmp2); |
---|
62 | tmp6 = filter_median(tmp5); |
---|
63 | tmp7 = find_glyphs(tmp6); |
---|
64 | |
---|
65 | /* Clean up our mess */ |
---|
66 | image_free(tmp1); |
---|
67 | image_free(tmp2); |
---|
68 | image_free(tmp3); |
---|
69 | image_free(tmp4); |
---|
70 | image_free(tmp5); |
---|
71 | image_free(tmp6); |
---|
72 | image_free(tmp7); |
---|
73 | |
---|
74 | /* aaaaaaa means decoding failed */ |
---|
75 | if(!strcmp(result, "aaaaaaa")) |
---|
76 | result[0] = '\0'; |
---|
77 | |
---|
78 | return result; |
---|
79 | } |
---|
80 | |
---|
81 | /* The following functions are local */ |
---|
82 | |
---|
83 | static struct image *count_objects(struct image *img) |
---|
84 | { |
---|
85 | struct image *dst; |
---|
86 | int gotblack = 1; |
---|
87 | int x, y, i; |
---|
88 | int r, g, b; |
---|
89 | |
---|
90 | dst = image_new(img->width, img->height); |
---|
91 | |
---|
92 | for(y = 0; y < img->height; y++) |
---|
93 | for(x = 0; x < img->width; x++) |
---|
94 | { |
---|
95 | getpixel(img, x, y, &r, &g, &b); |
---|
96 | setpixel(dst, x, y, r, g, b); |
---|
97 | } |
---|
98 | |
---|
99 | while(gotblack) |
---|
100 | { |
---|
101 | gotblack = 0; |
---|
102 | for(y = 0; y < dst->height; y++) |
---|
103 | for(x = 0; x < dst->width; x++) |
---|
104 | { |
---|
105 | getpixel(dst, x, y, &r, &g, &b); |
---|
106 | if(r == 0 && g == 0 && b == 0) |
---|
107 | { |
---|
108 | gotblack = 1; |
---|
109 | filter_flood_fill(dst, x, y, 254 - objects, 0, 0); |
---|
110 | objects++; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | //printf("%i objects\n", objects); |
---|
116 | |
---|
117 | for(i = 0; i < objects; i++) |
---|
118 | { |
---|
119 | objlist[i].ymin = dst->height; |
---|
120 | objlist[i].ymax = 0; |
---|
121 | |
---|
122 | for(y = 0; y < dst->height; y++) |
---|
123 | for(x = 0; x < dst->width; x++) |
---|
124 | { |
---|
125 | getpixel(dst, x, y, &r, &g, &b); |
---|
126 | if(r == 255 - i && g == 0 && b == 0) |
---|
127 | { |
---|
128 | if(y < objlist[i].ymin) { objlist[i].ymin = y; objlist[i].xmin = x; } |
---|
129 | if(y > objlist[i].ymax) { objlist[i].ymax = y; objlist[i].xmax = x; } |
---|
130 | } |
---|
131 | } |
---|
132 | //printf("y min-max: %i %i (size %i)\n", objlist[i].ymin, objlist[i].ymax, objlist[i].ymax - objlist[i].ymin + 1); |
---|
133 | if(objlist[i].ymax - objlist[i].ymin > 18 && objlist[i].ymax - objlist[i].ymin < 27) |
---|
134 | { |
---|
135 | if(first == -1) |
---|
136 | first = i; |
---|
137 | last = i; |
---|
138 | filter_flood_fill(dst, objlist[i].xmin, objlist[i].ymin, 0, 0, 255); |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | #if 0 |
---|
143 | { CvPoint A, B; |
---|
144 | A.x = (objlist[first].xmin + objlist[first].xmax) / 2; |
---|
145 | A.y = (objlist[first].ymin + objlist[first].ymax) / 2; |
---|
146 | B.x = (objlist[last].xmin + objlist[last].xmax) / 2; |
---|
147 | B.y = (objlist[last].ymin + objlist[last].ymax) / 2; |
---|
148 | cvLine(dst, A, B, 0, 2.0, 0); |
---|
149 | } |
---|
150 | #endif |
---|
151 | |
---|
152 | return dst; |
---|
153 | } |
---|
154 | |
---|
155 | static struct image *rotate(struct image *img) |
---|
156 | { |
---|
157 | struct image *dst; |
---|
158 | int x, y, xdest, ydest; |
---|
159 | int r, g, b; |
---|
160 | //int R, G, B; |
---|
161 | int X = objlist[first].xmin - objlist[last].xmin; |
---|
162 | int Y = objlist[first].ymin - objlist[last].ymin; |
---|
163 | float xtmp, ytmp; |
---|
164 | float sina = (1.0 * Y) / sqrt(1.0 * X * X + Y * Y); |
---|
165 | float cosa = (1.0 * X) / sqrt(1.0 * X * X + Y * Y); |
---|
166 | if(sina * cosa > 0) |
---|
167 | { |
---|
168 | sina = -sina; |
---|
169 | cosa = -cosa; |
---|
170 | } |
---|
171 | |
---|
172 | dst = image_new(img->width * FACTOR, img->height * FACTOR); |
---|
173 | |
---|
174 | for(y = 0; y < img->height * FACTOR; y++) |
---|
175 | for(x = 0; x < img->width * FACTOR; x++) |
---|
176 | { |
---|
177 | xtmp = 1.0 * (x - img->width * FACTOR / 2) / FACTOR; |
---|
178 | ytmp = 1.0 * (y - img->height * FACTOR / 2) / FACTOR; |
---|
179 | xdest = xtmp * cosa - ytmp * sina + 0.5 * img->width; |
---|
180 | ydest = ytmp * cosa + xtmp * sina + 0.5 * img->height; |
---|
181 | //R = G = B = 0; |
---|
182 | getpixel(img, xdest, ydest, &r, &g, &b); |
---|
183 | //R += r; G += g; B += b; |
---|
184 | //getpixel(img, xdest+1, ydest, &r, &g, &b); |
---|
185 | //R += r; G += g; B += b; |
---|
186 | //getpixel(img, xdest, ydest+1, &r, &g, &b); |
---|
187 | //R += r; G += g; B += b; |
---|
188 | //getpixel(img, xdest+1, ydest+1, &r, &g, &b); |
---|
189 | //R += r; G += g; B += b; |
---|
190 | //r = R / 4; g = G / 4; b = B / 4; |
---|
191 | if(r == 255 && g == 0 && b == 255) |
---|
192 | g = 255; |
---|
193 | setpixel(dst, x, y, r, g, b); |
---|
194 | } |
---|
195 | |
---|
196 | return dst; |
---|
197 | } |
---|
198 | |
---|
199 | static struct image *cut_cells(struct image *img) |
---|
200 | { |
---|
201 | struct image *dst; |
---|
202 | int x, y; |
---|
203 | int r, g, b; |
---|
204 | |
---|
205 | dst = image_new(img->width, img->height); |
---|
206 | |
---|
207 | for(y = 0; y < img->height; y++) |
---|
208 | for(x = 0; x < img->width; x++) |
---|
209 | { |
---|
210 | getpixel(img, x, y, &r, &g, &b); |
---|
211 | setpixel(dst, x, y, r, g, b); |
---|
212 | } |
---|
213 | |
---|
214 | for(x = 0; x < img->width; x++) |
---|
215 | { |
---|
216 | setpixel(dst, x, 0, 255, 255, 255); |
---|
217 | setpixel(dst, x, img->height - 1, 255, 255, 255); |
---|
218 | } |
---|
219 | |
---|
220 | for(y = 0; y < img->height; y++) |
---|
221 | for(x = 0; x < 7; x++) |
---|
222 | { |
---|
223 | setpixel(dst, x * img->width / 7, y, 255, 255, 255); |
---|
224 | setpixel(dst, (x + 1) * img->width / 7 - 1, y, 255, 255, 255); |
---|
225 | } |
---|
226 | |
---|
227 | return dst; |
---|
228 | } |
---|
229 | |
---|
230 | static struct image *find_glyphs(struct image *img) |
---|
231 | { |
---|
232 | char all[] = "abcdefgijkmnpqrstvwxyz"; |
---|
233 | struct |
---|
234 | { |
---|
235 | int xmin, xmax, ymin, ymax; |
---|
236 | int count; |
---|
237 | } |
---|
238 | glyphs[22]; |
---|
239 | struct image *dst; |
---|
240 | struct image *font = image_load(FONTNAME); |
---|
241 | int x, y, i = 0; |
---|
242 | int r, g, b; |
---|
243 | int xmin, xmax, ymin, ymax, incell = 0, count = 0, startx = 0, cur = 0; |
---|
244 | int distmin, distx, disty, distch; |
---|
245 | |
---|
246 | if(!font) |
---|
247 | { |
---|
248 | fprintf(stderr, "cannot load font %s\n", FONTNAME); |
---|
249 | exit(-1); |
---|
250 | } |
---|
251 | |
---|
252 | dst = image_new(img->width, img->height); |
---|
253 | |
---|
254 | for(y = 0; y < img->height; y++) |
---|
255 | for(x = 0; x < img->width; x++) |
---|
256 | { |
---|
257 | getpixel(img, x, y, &r, &g, &b); |
---|
258 | setpixel(dst, x, y, 255, g, 255); |
---|
259 | } |
---|
260 | |
---|
261 | for(x = 0; x < font->width; x++) |
---|
262 | { |
---|
263 | int found = 0; |
---|
264 | for(y = 0; y < font->height; y++) |
---|
265 | { |
---|
266 | getpixel(font, x, y, &r, &g, &b); |
---|
267 | if(r < 128) |
---|
268 | { |
---|
269 | found = 1; |
---|
270 | count += (255 - r); |
---|
271 | } |
---|
272 | } |
---|
273 | if(found && !incell) |
---|
274 | { |
---|
275 | incell = 1; |
---|
276 | xmin = x; |
---|
277 | } |
---|
278 | else if(!found && incell) |
---|
279 | { |
---|
280 | incell = 0; |
---|
281 | xmax = x; |
---|
282 | #if 0 |
---|
283 | ymin = font->height; |
---|
284 | ymax = 0; |
---|
285 | for(y = 0; y < font->height; y++) |
---|
286 | { |
---|
287 | int newx; |
---|
288 | int gotit = 0; |
---|
289 | for(newx = xmin; newx < xmax; newx++) |
---|
290 | { |
---|
291 | getpixel(font, newx, y, &r, &g, &b); |
---|
292 | if(r < 128) |
---|
293 | { |
---|
294 | gotit = 1; |
---|
295 | break; |
---|
296 | } |
---|
297 | } |
---|
298 | if(gotit) |
---|
299 | { |
---|
300 | if(ymin > y) ymin = y; |
---|
301 | if(ymax <= y) ymax = y + 1; |
---|
302 | } |
---|
303 | } |
---|
304 | #else |
---|
305 | ymin = 0; |
---|
306 | ymax = font->height; |
---|
307 | #endif |
---|
308 | glyphs[i].xmin = xmin; |
---|
309 | glyphs[i].xmax = xmax; |
---|
310 | glyphs[i].ymin = ymin; |
---|
311 | glyphs[i].ymax = ymax; |
---|
312 | glyphs[i].count = count; |
---|
313 | count = 0; |
---|
314 | i++; |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | if(i != 22) |
---|
319 | { |
---|
320 | printf("error: could not find 22 glyphs in font\n"); |
---|
321 | exit(-1); |
---|
322 | } |
---|
323 | |
---|
324 | while(cur < 7) |
---|
325 | { |
---|
326 | /* Try to find 1st letter */ |
---|
327 | distmin = INT_MAX; |
---|
328 | for(i = 0; i < 22; i++) |
---|
329 | { |
---|
330 | int localmin = INT_MAX, localx, localy; |
---|
331 | //if(all[i] == 'i') continue; |
---|
332 | xmin = glyphs[i].xmin; |
---|
333 | ymin = glyphs[i].ymin; |
---|
334 | xmax = glyphs[i].xmax; |
---|
335 | ymax = glyphs[i].ymax; |
---|
336 | //printf("trying to find %c (%i×%i) - ", all[i], xmax - xmin, ymax - ymin); |
---|
337 | for(y = -5 * FACTOR; y < 5 * FACTOR; y++) |
---|
338 | for(x = startx - 5 * FACTOR; x < startx + 5 * FACTOR; x++) |
---|
339 | { |
---|
340 | int z, t, dist; |
---|
341 | dist = 0; |
---|
342 | for(t = 0; t < ymax - ymin; t++) |
---|
343 | for(z = 0; z < xmax - xmin; z++) |
---|
344 | { |
---|
345 | int r2; |
---|
346 | getgray(font, xmin + z, ymin + t, &r); |
---|
347 | getgray(img, x + z, y + t, &r2); |
---|
348 | dist += abs(r - r2); |
---|
349 | } |
---|
350 | // printf("%i %i -> %i\n", x, y, dist); |
---|
351 | //dist /= sqrt(xmax - xmin); |
---|
352 | dist = dist * 128 / glyphs[i].count; |
---|
353 | if(dist < localmin) |
---|
354 | { |
---|
355 | localmin = dist; |
---|
356 | localx = x; |
---|
357 | localy = y; |
---|
358 | } |
---|
359 | } |
---|
360 | //fprintf(stderr, "%i (%i,%i)\n", localmin, localx - startx, localy); |
---|
361 | if(localmin < distmin) |
---|
362 | { |
---|
363 | distmin = localmin; |
---|
364 | distx = localx; |
---|
365 | disty = localy; |
---|
366 | distch = i; |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | //fprintf(stderr, "%i (%i,%i)\n", distmin, distx - startx, disty); |
---|
371 | //printf("min diff: %c - %i (%i, %i)\n", all[distch], distmin, distx, disty); |
---|
372 | |
---|
373 | /* Print min glyph */ |
---|
374 | xmin = glyphs[distch].xmin; |
---|
375 | ymin = glyphs[distch].ymin; |
---|
376 | xmax = glyphs[distch].xmax; |
---|
377 | ymax = glyphs[distch].ymax; |
---|
378 | for(y = 0; y < ymax - ymin; y++) |
---|
379 | for(x = 0; x < xmax - xmin; x++) |
---|
380 | { |
---|
381 | getpixel(font, xmin + x, ymin + y, &r, &g, &b); |
---|
382 | if(r > 128) continue; |
---|
383 | setpixel(dst, distx + x, disty + y, r, g, b); |
---|
384 | } |
---|
385 | |
---|
386 | startx = distx + xmax - xmin; |
---|
387 | result[cur++] = all[distch]; |
---|
388 | } |
---|
389 | |
---|
390 | return dst; |
---|
391 | } |
---|
392 | |
---|