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