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