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