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