1 | /* |
---|
2 | * xanga.c: decode Xanga captchas |
---|
3 | * $Id: decoder.c 2318 2008-04-26 08:41:43Z sam $ |
---|
4 | * |
---|
5 | * Copyright: (c) 2005 Sam Hocevar <sam@zoy.org> |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <string.h> |
---|
16 | #include <limits.h> |
---|
17 | #include <math.h> |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | #include "common.h" |
---|
21 | |
---|
22 | static void fill_white_holes(struct image *img); |
---|
23 | static void find_glyphs(struct image *img); |
---|
24 | |
---|
25 | static char *result; |
---|
26 | |
---|
27 | /* Main function */ |
---|
28 | char *decode_xanga(struct image *img) |
---|
29 | { |
---|
30 | struct image *tmp; |
---|
31 | |
---|
32 | /* Xanga captchas have 6 characters */ |
---|
33 | result = malloc(7 * sizeof(char)); |
---|
34 | strcpy(result, " "); |
---|
35 | |
---|
36 | tmp = image_dup(img); |
---|
37 | image_save(tmp, "xanga1.bmp"); |
---|
38 | /* Clean image a bit */ |
---|
39 | // filter_threshold(tmp, 200); |
---|
40 | filter_contrast(tmp); |
---|
41 | //filter_detect_lines(tmp); |
---|
42 | image_save(tmp, "xanga2.bmp"); |
---|
43 | fill_white_holes(tmp); |
---|
44 | // filter_fill_holes(tmp); |
---|
45 | filter_smooth(tmp); |
---|
46 | //filter_median(tmp); |
---|
47 | image_save(tmp, "xanga3.bmp"); |
---|
48 | //filter_detect_lines(tmp); |
---|
49 | // filter_median(tmp); |
---|
50 | //image_save(tmp, "xanga4.bmp"); |
---|
51 | // filter_threshold(tmp, 128); |
---|
52 | filter_contrast(tmp); |
---|
53 | image_save(tmp, "xanga4.bmp"); |
---|
54 | |
---|
55 | #if 0 |
---|
56 | /* Detect small objects to guess image orientation */ |
---|
57 | filter_median(tmp); |
---|
58 | filter_threshold(tmp, 200); |
---|
59 | |
---|
60 | /* Invert rotation and find glyphs */ |
---|
61 | filter_median(tmp); |
---|
62 | #endif |
---|
63 | find_glyphs(tmp); |
---|
64 | image_save(tmp, "xanga5.bmp"); |
---|
65 | |
---|
66 | /* Clean up our mess */ |
---|
67 | image_free(tmp); |
---|
68 | |
---|
69 | /* aaaaaaa means decoding failed */ |
---|
70 | if(!strcmp(result, "aaaaaaa")) |
---|
71 | result[0] = '\0'; |
---|
72 | |
---|
73 | return result; |
---|
74 | } |
---|
75 | |
---|
76 | /* The following functions are local */ |
---|
77 | |
---|
78 | static void fill_white_holes(struct image *img) |
---|
79 | { |
---|
80 | struct image *tmp; |
---|
81 | int x, y; |
---|
82 | int r, g, b; |
---|
83 | |
---|
84 | tmp = image_new(img->width, img->height); |
---|
85 | |
---|
86 | for(y = 0; y < img->height; y++) |
---|
87 | for(x = 0; x < img->width; x++) |
---|
88 | { |
---|
89 | getpixel(img, x, y, &r, &g, &b); |
---|
90 | setpixel(tmp, x, y, r, g, b); |
---|
91 | } |
---|
92 | |
---|
93 | for(y = 1; y < img->height - 1; y++) |
---|
94 | for(x = 1; x < img->width - 1; x++) |
---|
95 | { |
---|
96 | int count = 0; |
---|
97 | getpixel(img, x, y, &r, &g, &b); |
---|
98 | if(r <= 16) |
---|
99 | continue; |
---|
100 | getpixel(img, x + 1, y, &r, &g, &b); |
---|
101 | count += r; |
---|
102 | getpixel(img, x - 1, y, &r, &g, &b); |
---|
103 | count += r; |
---|
104 | getpixel(img, x, y + 1, &r, &g, &b); |
---|
105 | count += r; |
---|
106 | getpixel(img, x, y - 1, &r, &g, &b); |
---|
107 | count += r; |
---|
108 | if(count > 600) |
---|
109 | continue; |
---|
110 | setpixel(tmp, x, y, count / 5, count / 5, count / 5); |
---|
111 | } |
---|
112 | |
---|
113 | image_swap(tmp, img); |
---|
114 | image_free(tmp); |
---|
115 | } |
---|
116 | |
---|
117 | static void find_glyphs(struct image *img) |
---|
118 | { |
---|
119 | #define FONTS 6 |
---|
120 | static struct font *fonts[FONTS]; |
---|
121 | static char *files[] = |
---|
122 | { |
---|
123 | "x_freemonobold_32_az.bmp", "abcdefghijklmnopqrstuvwxyz", |
---|
124 | "x_freemonobold_24_az.bmp", "abcdefghijklmnopqrstuvwxyz", |
---|
125 | "x_freesansbold_32_az.bmp", "abcdefghijklmnopqrstuvwxyz", |
---|
126 | //"x_freeserifbold_32_az.bmp", "abcdefghijklmnopqrstuvwxyz", |
---|
127 | "x_comic_32_az.bmp", "abcdefghijklmnopqrstuvwxyz", |
---|
128 | "x_comic_24_az_messed.bmp", "abcdefghijklmnopqrstuvwxyz", |
---|
129 | "x_freesansbold_36_az_messed.bmp", "abcdefghijklmnopqrstuvwxyz", |
---|
130 | }; |
---|
131 | struct image *tmp; |
---|
132 | int x, y, i = 0, f; |
---|
133 | int r, g, b; |
---|
134 | int xmin, xmax, ymin, ymax, cur = 0; |
---|
135 | int bestdist, bestfont, bestx, besty, bestch; |
---|
136 | |
---|
137 | for(f = 0; f < FONTS; f++) |
---|
138 | { |
---|
139 | if(!fonts[f]) |
---|
140 | { |
---|
141 | fonts[f] = font_load_variable(DECODER, |
---|
142 | files[f * 2], files[f * 2 + 1]); |
---|
143 | if(!fonts[f]) |
---|
144 | exit(1); |
---|
145 | //filter_smooth(fonts[f]->img); |
---|
146 | //filter_contrast(fonts[f]->img); |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | tmp = image_new(img->width, img->height); |
---|
151 | |
---|
152 | for(y = 0; y < img->height; y++) |
---|
153 | for(x = 0; x < img->width; x++) |
---|
154 | { |
---|
155 | getpixel(img, x, y, &r, &g, &b); |
---|
156 | setpixel(tmp, x, y, 255, g, 255); |
---|
157 | } |
---|
158 | |
---|
159 | while(cur < 6) |
---|
160 | { |
---|
161 | /* Try to find 1st letter */ |
---|
162 | bestdist = INT_MAX; |
---|
163 | for(f = 0; f < FONTS; f++) for(i = 0; i < fonts[f]->size; i++) |
---|
164 | { |
---|
165 | int localmin = INT_MAX, localx, localy; |
---|
166 | int sqr; |
---|
167 | if(fonts[f]->glyphs[i].c == 'l' || fonts[f]->glyphs[i].c == 'z') |
---|
168 | continue; |
---|
169 | xmin = fonts[f]->glyphs[i].xmin - 5; |
---|
170 | ymin = fonts[f]->glyphs[i].ymin - 3; |
---|
171 | xmax = fonts[f]->glyphs[i].xmax + 5; |
---|
172 | ymax = fonts[f]->glyphs[i].ymax + 3; |
---|
173 | sqr = sqrt(xmax - xmin); |
---|
174 | for(y = -15; y < 15; y++) |
---|
175 | for(x = 22 - (xmax - xmin) / 2 + 25 * cur; x < 28 - (xmax - xmin) / 2 + 25 * cur; x++) |
---|
176 | { |
---|
177 | int z, t, dist; |
---|
178 | dist = 0; |
---|
179 | for(t = 0; t < ymax - ymin; t++) |
---|
180 | for(z = 0; z < xmax - xmin; z++) |
---|
181 | { |
---|
182 | int r2; |
---|
183 | getgray(fonts[f]->img, xmin + z, ymin + t, &r); |
---|
184 | getgray(img, x + z, y + t, &r2); |
---|
185 | if(r < r2) |
---|
186 | dist += (r - r2) * (r - r2); |
---|
187 | else |
---|
188 | dist += (r - r2) * (r - r2) * 3 / 4; |
---|
189 | } |
---|
190 | // printf("%i %i -> %i\n", x, y, dist); |
---|
191 | // dist /= (xmax - xmin); |
---|
192 | // dist = dist / sqrt((ymax - ymin) * (xmax - xmin)) / (xmax - xmin); |
---|
193 | dist = dist / (xmax - xmin) / sqr; |
---|
194 | // dist = dist * 128 / fonts[f]->glyphs[i].count; |
---|
195 | if(dist < localmin) |
---|
196 | { |
---|
197 | localmin = dist; |
---|
198 | localx = x; |
---|
199 | localy = y; |
---|
200 | } |
---|
201 | } |
---|
202 | if(localmin < bestdist) |
---|
203 | { |
---|
204 | //printf(" bestch is now %i (%c) in font %i\n", i, fonts[f]->glyphs[i].c, f); |
---|
205 | bestdist = localmin; |
---|
206 | bestfont = f; |
---|
207 | bestx = localx; |
---|
208 | besty = localy; |
---|
209 | bestch = i; |
---|
210 | } |
---|
211 | } |
---|
212 | //printf("%i (%c) in font %i\n", i, fonts[bestfont]->glyphs[bestch].c, bestfont); |
---|
213 | //printf("%i < %i < %i\n", 10 + 25 * cur, bestx, 30 + 25 * cur); |
---|
214 | |
---|
215 | /* Draw best glyph in picture (debugging purposes) */ |
---|
216 | xmin = fonts[bestfont]->glyphs[bestch].xmin - 5; |
---|
217 | ymin = fonts[bestfont]->glyphs[bestch].ymin - 3; |
---|
218 | xmax = fonts[bestfont]->glyphs[bestch].xmax + 5; |
---|
219 | ymax = fonts[bestfont]->glyphs[bestch].ymax + 3; |
---|
220 | for(y = 0; y < ymax - ymin; y++) |
---|
221 | for(x = 0; x < xmax - xmin; x++) |
---|
222 | { |
---|
223 | getpixel(fonts[bestfont]->img, xmin + x, ymin + y, &r, &g, &b); |
---|
224 | if(r > 128) continue; |
---|
225 | setpixel(tmp, bestx + x, besty + y, r, g, b); |
---|
226 | } |
---|
227 | |
---|
228 | result[cur++] = fonts[bestfont]->glyphs[bestch].c; |
---|
229 | } |
---|
230 | |
---|
231 | image_swap(img, tmp); |
---|
232 | image_free(tmp); |
---|
233 | } |
---|
234 | |
---|