1 | /* |
---|
2 | * phpbb.c: decode phpBB captchas |
---|
3 | * $Id: phpbb.c 418 2005-01-04 23:04:30Z sam $ |
---|
4 | * |
---|
5 | * Copyright: (c) 2005 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 | |
---|
17 | #include "config.h" |
---|
18 | #include "common.h" |
---|
19 | |
---|
20 | /* Our macros */ |
---|
21 | #define FONTNAME "share/font_phpbb.png" |
---|
22 | |
---|
23 | /* Main function */ |
---|
24 | char *decode_phpbb(struct image *img) |
---|
25 | { |
---|
26 | char all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; |
---|
27 | char *result; |
---|
28 | struct image *tmp1, *tmp2, *tmp3; |
---|
29 | struct image *font = image_load(FONTNAME); |
---|
30 | int x, y, i = 0; |
---|
31 | int r, g, b; |
---|
32 | int xmin, xmax, ymin, ymax, cur, offset = -1; |
---|
33 | int distmin, distx, disty, distch; |
---|
34 | |
---|
35 | if(!font) |
---|
36 | { |
---|
37 | fprintf(stderr, "cannot load font %s\n", FONTNAME); |
---|
38 | exit(-1); |
---|
39 | } |
---|
40 | |
---|
41 | /* phpBB captchas have 6 characters */ |
---|
42 | result = malloc(7 * sizeof(char)); |
---|
43 | strcpy(result, " "); |
---|
44 | |
---|
45 | tmp1 = filter_smooth(img); |
---|
46 | tmp2 = filter_equalize(tmp1, 128); |
---|
47 | tmp3 = image_new(img->width, img->height); |
---|
48 | |
---|
49 | for(x = 0; x < img->width; x++) |
---|
50 | for(y = 0; y < img->height; y++) |
---|
51 | { |
---|
52 | getpixel(tmp2, x, y, &r, &g, &b); |
---|
53 | if(r == 0 && offset == -1) |
---|
54 | offset = x; |
---|
55 | getpixel(img, x, y, &r, &g, &b); |
---|
56 | setpixel(tmp3, x, y, 255, g, 255); |
---|
57 | } |
---|
58 | |
---|
59 | for(cur = 0; cur < 6; cur++) |
---|
60 | { |
---|
61 | /* Try to find 1st letter */ |
---|
62 | distmin = INT_MAX; |
---|
63 | for(i = 0; i < 35; i++) |
---|
64 | { |
---|
65 | int localmin = INT_MAX, localx, localy; |
---|
66 | xmin = i * 40; |
---|
67 | ymin = 0; |
---|
68 | xmax = i * 40 + 40; |
---|
69 | ymax = 40; |
---|
70 | for(y = 0; y < img->height - 40; y++) |
---|
71 | { |
---|
72 | x = offset - 3; |
---|
73 | if(cur == 0) |
---|
74 | x -= 10; |
---|
75 | if(x < 0) |
---|
76 | x = 0; |
---|
77 | for(; x < offset + 3; x++) |
---|
78 | { |
---|
79 | int z, t, dist; |
---|
80 | dist = 0; |
---|
81 | for(t = 0; t < ymax - ymin; t++) |
---|
82 | for(z = 0; z < xmax - xmin; z++) |
---|
83 | { |
---|
84 | int r2; |
---|
85 | getgray(font, xmin + z, ymin + t, &r); |
---|
86 | getgray(tmp2, x + z, y + t, &r2); |
---|
87 | dist += (r - r2) * (r - r2); |
---|
88 | } |
---|
89 | if(dist < localmin) |
---|
90 | { |
---|
91 | localmin = dist; |
---|
92 | localx = x; |
---|
93 | localy = y; |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | if(localmin < distmin) |
---|
98 | { |
---|
99 | distmin = localmin; |
---|
100 | distx = localx; |
---|
101 | disty = localy; |
---|
102 | distch = i; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | /* Print min glyph (debug) */ |
---|
107 | xmin = distch * 40; |
---|
108 | ymin = 0; |
---|
109 | xmax = distch * 40 + 40; |
---|
110 | ymax = 40; |
---|
111 | for(y = 0; y < ymax - ymin; y++) |
---|
112 | for(x = 0; x < xmax - xmin; x++) |
---|
113 | { |
---|
114 | int r2; |
---|
115 | getpixel(font, xmin + x, ymin + y, &r2, &g, &b); |
---|
116 | if(r2 > 128) continue; |
---|
117 | getpixel(tmp3, distx + x, disty + y, &r, &g, &b); |
---|
118 | setpixel(tmp3, distx + x, disty + y, r2, g, b); |
---|
119 | } |
---|
120 | |
---|
121 | offset = distx + xmax - xmin; |
---|
122 | result[cur] = all[distch]; |
---|
123 | } |
---|
124 | |
---|
125 | image_free(tmp1); |
---|
126 | image_free(tmp2); |
---|
127 | image_free(tmp3); |
---|
128 | image_free(font); |
---|
129 | |
---|
130 | return result; |
---|
131 | } |
---|
132 | |
---|