1 | /* |
---|
2 | * phpbb.c: decode phpBB captchas |
---|
3 | * $Id: phpbb.c 415 2005-01-04 17:10:03Z 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 | |
---|
23 | static struct image *find_glyphs(struct image *img); |
---|
24 | |
---|
25 | /* Global stuff */ |
---|
26 | char *result; |
---|
27 | |
---|
28 | /* Main function */ |
---|
29 | char *decode_phpbb(struct image *img) |
---|
30 | { |
---|
31 | char all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; |
---|
32 | struct image *tmp1, *tmp2, *tmp3; |
---|
33 | struct image *font = image_load(FONTNAME); |
---|
34 | int x, y, i = 0; |
---|
35 | int r, g, b; |
---|
36 | int xmin, xmax, ymin, ymax, cur, offset = -1; |
---|
37 | int distmin, distx, disty, distch; |
---|
38 | |
---|
39 | /* phpBB captchas have 6 characters */ |
---|
40 | result = malloc(7 * sizeof(char)); |
---|
41 | strcpy(result, " "); |
---|
42 | |
---|
43 | if(!font) |
---|
44 | { |
---|
45 | fprintf(stderr, "cannot load font %s\n", FONTNAME); |
---|
46 | exit(-1); |
---|
47 | } |
---|
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 | dist += (r - r2) * (r - r2); |
---|
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 = distch * 40; |
---|
112 | ymin = 0; |
---|
113 | xmax = distch * 40 + 40; |
---|
114 | ymax = 40; |
---|
115 | for(y = 0; y < ymax - ymin; y++) |
---|
116 | for(x = 0; x < xmax - xmin; x++) |
---|
117 | { |
---|
118 | int r2; |
---|
119 | getpixel(font, xmin + x, ymin + y, &r2, &g, &b); |
---|
120 | if(r2 > 128) continue; |
---|
121 | getpixel(tmp3, distx + x, disty + y, &r, &g, &b); |
---|
122 | setpixel(tmp3, distx + x, disty + y, r2, g, b); |
---|
123 | } |
---|
124 | |
---|
125 | offset = distx + xmax - xmin; |
---|
126 | result[cur] = all[distch]; |
---|
127 | } |
---|
128 | |
---|
129 | image_free(tmp1); |
---|
130 | image_free(tmp2); |
---|
131 | image_free(tmp3); |
---|
132 | image_free(font); |
---|
133 | |
---|
134 | return result; |
---|
135 | } |
---|
136 | |
---|