1 | /* |
---|
2 | * phpbb.c: decode phpBB captchas |
---|
3 | * $Id: phpbb.c 482 2005-05-30 15:22:02Z 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 | /* Main function */ |
---|
21 | char *decode_phpbb(struct image *img) |
---|
22 | { |
---|
23 | static struct font *font = NULL; |
---|
24 | char *result; |
---|
25 | struct image *tmp1, *tmp2; |
---|
26 | int x, y, i = 0; |
---|
27 | int r, g, b; |
---|
28 | int xmin, xmax, ymin, ymax, cur, offset = -1; |
---|
29 | int distmin, distx, disty, distch; |
---|
30 | |
---|
31 | if(!font) |
---|
32 | { |
---|
33 | font = font_load_fixed("font_phpbb.png", |
---|
34 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"); |
---|
35 | if(!font) |
---|
36 | exit(-1); |
---|
37 | } |
---|
38 | |
---|
39 | /* phpBB captchas have 6 characters */ |
---|
40 | result = malloc(7 * sizeof(char)); |
---|
41 | strcpy(result, " "); |
---|
42 | |
---|
43 | tmp1 = image_dup(img); |
---|
44 | tmp2 = image_new(img->width, img->height); |
---|
45 | |
---|
46 | filter_smooth(tmp1); |
---|
47 | filter_threshold(tmp1, 128); |
---|
48 | |
---|
49 | for(x = 0; x < img->width; x++) |
---|
50 | for(y = 0; y < img->height; y++) |
---|
51 | { |
---|
52 | getpixel(tmp1, x, y, &r, &g, &b); |
---|
53 | if(r == 0 && offset == -1) |
---|
54 | offset = x; |
---|
55 | getpixel(img, x, y, &r, &g, &b); |
---|
56 | setpixel(tmp2, x, y, 255, g, 255); |
---|
57 | } |
---|
58 | |
---|
59 | for(cur = 0; cur < 6; cur++) |
---|
60 | { |
---|
61 | /* Try to find 1st letter */ |
---|
62 | distmin = INT_MAX; |
---|
63 | for(i = 0; i < font->size; i++) |
---|
64 | { |
---|
65 | int localmin = INT_MAX, localx, localy; |
---|
66 | xmin = font->glyphs[i].xmin; |
---|
67 | ymin = font->glyphs[i].ymin; |
---|
68 | xmax = font->glyphs[i].xmax; |
---|
69 | ymax = font->glyphs[i].ymax; |
---|
70 | for(y = 0; y < img->height - (ymax - ymin); y++) |
---|
71 | { |
---|
72 | x = offset - 3; |
---|
73 | if(cur == 0) |
---|
74 | x -= 10; |
---|
75 | if(x < 0) |
---|
76 | x = 0; |
---|
77 | for(; x < offset + 3; x++) |
---|
78 | { |
---|
79 | int z, t, dist; |
---|
80 | dist = 0; |
---|
81 | for(t = 0; t < ymax - ymin; t++) |
---|
82 | for(z = 0; z < xmax - xmin; z++) |
---|
83 | { |
---|
84 | int r2; |
---|
85 | getgray(font->img, xmin + z, ymin + t, &r); |
---|
86 | getgray(tmp1, x + z, y + t, &r2); |
---|
87 | if(r > r2) |
---|
88 | dist += r - r2; |
---|
89 | else |
---|
90 | dist += (r2 - r) * 3 / 4; |
---|
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 = font->glyphs[distch].xmin; |
---|
111 | ymin = font->glyphs[distch].ymin; |
---|
112 | xmax = font->glyphs[distch].xmax; |
---|
113 | ymax = font->glyphs[distch].ymax; |
---|
114 | for(y = 0; y < ymax - ymin; y++) |
---|
115 | for(x = 0; x < xmax - xmin; x++) |
---|
116 | { |
---|
117 | int r2; |
---|
118 | getpixel(font->img, xmin + x, ymin + y, &r2, &g, &b); |
---|
119 | if(r2 > 128) |
---|
120 | continue; |
---|
121 | getpixel(tmp2, distx + x, disty + y, &r, &g, &b); |
---|
122 | setpixel(tmp2, distx + x, disty + y, r2, g, b); |
---|
123 | } |
---|
124 | |
---|
125 | offset = distx + xmax - xmin; |
---|
126 | result[cur] = font->glyphs[distch].c; |
---|
127 | } |
---|
128 | |
---|
129 | image_free(tmp1); |
---|
130 | image_free(tmp2); |
---|
131 | |
---|
132 | return result; |
---|
133 | } |
---|
134 | |
---|