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