1 | /* |
---|
2 | * test.c: test captchas |
---|
3 | * $Id: test.c 430 2005-01-06 00:51:07Z sam $ |
---|
4 | * |
---|
5 | * Copyright: (c) 2004 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 | #include <math.h> |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | /* Our macros */ |
---|
22 | #define FONTNAME "font_phpbb.png" |
---|
23 | |
---|
24 | static struct image *find_glyphs(struct image *img); |
---|
25 | |
---|
26 | /* Global stuff */ |
---|
27 | struct { int xmin, ymin, xmax, ymax; } objlist[100]; |
---|
28 | int objects, first, last; |
---|
29 | char *result; |
---|
30 | |
---|
31 | /* Main function */ |
---|
32 | char *decode_test(struct image *img) |
---|
33 | { |
---|
34 | struct image *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7; |
---|
35 | |
---|
36 | /* Initialise local data */ |
---|
37 | objects = 0; |
---|
38 | first = -1; |
---|
39 | last = -1; |
---|
40 | |
---|
41 | /* phpBB captchas have 6 characters */ |
---|
42 | result = malloc(7 * sizeof(char)); |
---|
43 | |
---|
44 | tmp1 = filter_smooth(img); |
---|
45 | tmp2 = filter_median(tmp1); |
---|
46 | tmp3 = filter_equalize(tmp2, 130); |
---|
47 | tmp4 = filter_median(tmp3); |
---|
48 | tmp5 = find_glyphs(tmp3); |
---|
49 | |
---|
50 | image_free(tmp1); |
---|
51 | image_free(tmp2); |
---|
52 | image_free(tmp3); |
---|
53 | image_free(tmp4); |
---|
54 | image_free(tmp5); |
---|
55 | |
---|
56 | return result; |
---|
57 | } |
---|
58 | |
---|
59 | /* The following functions are local */ |
---|
60 | |
---|
61 | static struct image *find_glyphs(struct image *img) |
---|
62 | { |
---|
63 | char all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; |
---|
64 | struct image *dst, *font; |
---|
65 | int x, y, i = 0; |
---|
66 | int r, g, b; |
---|
67 | int xmin, xmax, ymin, ymax, incell = 0, count = 0, cur = 0, offset = -1; |
---|
68 | int distmin, distx, disty, distch; |
---|
69 | |
---|
70 | if(!font) |
---|
71 | { |
---|
72 | char fontname[BUFSIZ]; |
---|
73 | sprintf(fontname, "%s/%s", share, FONTNAME); |
---|
74 | font = image_load(fontname); |
---|
75 | if(!font) |
---|
76 | { |
---|
77 | fprintf(stderr, "cannot load font %s\n", fontname); |
---|
78 | exit(-1); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | dst = image_new(img->width, img->height); |
---|
83 | |
---|
84 | for(x = 0; x < img->width; x++) |
---|
85 | for(y = 0; y < img->height; y++) |
---|
86 | { |
---|
87 | getpixel(img, x, y, &r, &g, &b); |
---|
88 | setpixel(dst, x, y, 255, g, 255); |
---|
89 | if(r == 0 && offset == -1) |
---|
90 | offset = x; |
---|
91 | } |
---|
92 | |
---|
93 | strcpy(result, " "); |
---|
94 | |
---|
95 | while(cur < 6) |
---|
96 | { |
---|
97 | /* Try to find 1st letter */ |
---|
98 | distmin = INT_MAX; |
---|
99 | for(i = 0; i < 35; i++) |
---|
100 | { |
---|
101 | int localmin = INT_MAX, localx, localy; |
---|
102 | xmin = i * 40; |
---|
103 | ymin = 0; |
---|
104 | xmax = i * 40 + 40; |
---|
105 | ymax = 40; |
---|
106 | for(y = 0; y < img->height - 40; y++) |
---|
107 | { |
---|
108 | x = offset - 5; |
---|
109 | if(cur == 0) |
---|
110 | x -= 15; |
---|
111 | if(x < 0) |
---|
112 | x = 0; |
---|
113 | for(; x < offset + 10; x++) |
---|
114 | { |
---|
115 | int z, t, dist; |
---|
116 | dist = 0; |
---|
117 | for(t = 0; t < ymax - ymin; t++) |
---|
118 | for(z = 0; z < xmax - xmin; z++) |
---|
119 | { |
---|
120 | int r2; |
---|
121 | getgray(font, xmin + z, ymin + t, &r); |
---|
122 | getgray(img, x + z, y + t, &r2); |
---|
123 | dist += abs(r - r2); |
---|
124 | } |
---|
125 | if(dist < localmin) |
---|
126 | { |
---|
127 | localmin = dist; |
---|
128 | localx = x; |
---|
129 | localy = y; |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | if(localmin < distmin) |
---|
134 | { |
---|
135 | distmin = localmin; |
---|
136 | distx = localx; |
---|
137 | disty = localy; |
---|
138 | distch = i; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | /* Print min glyph (debug) */ |
---|
143 | xmin = distch * 40; |
---|
144 | ymin = 0; |
---|
145 | xmax = distch * 40 + 40; |
---|
146 | ymax = 40; |
---|
147 | for(y = 0; y < ymax - ymin; y++) |
---|
148 | for(x = 0; x < xmax - xmin; x++) |
---|
149 | { |
---|
150 | getpixel(font, xmin + x, ymin + y, &r, &g, &b); |
---|
151 | if(r > 128) continue; |
---|
152 | setpixel(dst, distx + x, disty + y, r, g, b); |
---|
153 | } |
---|
154 | |
---|
155 | offset = distx + xmax - xmin; |
---|
156 | result[cur++] = all[distch]; |
---|
157 | } |
---|
158 | |
---|
159 | return dst; |
---|
160 | } |
---|
161 | |
---|