1 | /* |
---|
2 | * vbulletin.c: decode vbulletin captchas |
---|
3 | * $Id: vbulletin.c 430 2005-01-06 00:51:07Z 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 | #define FONTNAME "font_vbulletin.png" |
---|
21 | static struct image *font = NULL; |
---|
22 | |
---|
23 | /* Main function */ |
---|
24 | char *decode_vbulletin(struct image *img) |
---|
25 | { |
---|
26 | char all[] = "2346789ABCDEFGHJKLMNPRTWXYZ"; |
---|
27 | char *result; |
---|
28 | struct image *tmp1, *tmp2, *tmp3; |
---|
29 | int limits[6] = { 26, 53, 80, 107, 134, 160 }; |
---|
30 | int x, y, r, g, b, i, j; |
---|
31 | |
---|
32 | if(!font) |
---|
33 | { |
---|
34 | char fontname[BUFSIZ]; |
---|
35 | sprintf(fontname, "%s/%s", share, FONTNAME); |
---|
36 | font = image_load(fontname); |
---|
37 | if(!font) |
---|
38 | { |
---|
39 | fprintf(stderr, "cannot load font %s\n", fontname); |
---|
40 | exit(-1); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | /* vBulletin captchas have 6 characters */ |
---|
45 | result = malloc(7 * sizeof(char)); |
---|
46 | strcpy(result, " "); |
---|
47 | |
---|
48 | /* half the captchas are inverse video; we set them back to normal */ |
---|
49 | getpixel(img, 0, 0, &r, &g, &b); |
---|
50 | if(r < 50) |
---|
51 | tmp1 = filter_equalize(img, 128); |
---|
52 | else |
---|
53 | tmp1 = filter_equalize(img, -128); |
---|
54 | |
---|
55 | /* Remove garbage around the cells */ |
---|
56 | for(x = 0; x < img->width; x++) |
---|
57 | { |
---|
58 | for(y = 0; y < 15; y++) |
---|
59 | setpixel(tmp1, x, y, 255, 255, 255); |
---|
60 | for(y = 45; y < img->height; y++) |
---|
61 | setpixel(tmp1, x, y, 255, 255, 255); |
---|
62 | } |
---|
63 | |
---|
64 | for(x = 0; x < img->width; x++) |
---|
65 | { |
---|
66 | for(i = 0; i < 6; i++) |
---|
67 | if(x == limits[i]) |
---|
68 | break; |
---|
69 | if(i == 6) |
---|
70 | for(y = 15; y < 45; y++) |
---|
71 | setpixel(tmp1, x, y, 255, 255, 255); |
---|
72 | else |
---|
73 | x += 11; |
---|
74 | } |
---|
75 | |
---|
76 | tmp2 = filter_black_stuff(tmp1); |
---|
77 | tmp3 = filter_black_stuff(tmp2); |
---|
78 | |
---|
79 | /* Fill letters in gray */ |
---|
80 | for(x = 26; x < 172; x++) |
---|
81 | { |
---|
82 | getpixel(tmp3, x, 15, &r, &g, &b); |
---|
83 | if(r == 0) |
---|
84 | filter_flood_fill(tmp3, x, 15, 127, 0, 255); |
---|
85 | } |
---|
86 | |
---|
87 | /* Find remaining black parts and remove them */ |
---|
88 | for(x = 26; x < 172; x++) |
---|
89 | for(y = 15; y < 45; y++) |
---|
90 | { |
---|
91 | getpixel(tmp3, x, y, &r, &g, &b); |
---|
92 | if(r == 0) |
---|
93 | filter_flood_fill(tmp3, x, y, 255, 255, 255); |
---|
94 | } |
---|
95 | |
---|
96 | /* Fill letters in black */ |
---|
97 | for(x = 26; x < 172; x++) |
---|
98 | { |
---|
99 | getpixel(tmp3, x, 44, &r, &g, &b); |
---|
100 | if(r == 127) |
---|
101 | filter_flood_fill(tmp3, x, 44, 0, 0, 0); |
---|
102 | } |
---|
103 | |
---|
104 | /* Find remaining gray parts and remove them */ |
---|
105 | for(x = 26; x < 172; x++) |
---|
106 | for(y = 15; y < 45; y++) |
---|
107 | { |
---|
108 | getpixel(tmp3, x, y, &r, &g, &b); |
---|
109 | if(r == 127) |
---|
110 | filter_flood_fill(tmp3, x, y, 255, 255, 255); |
---|
111 | } |
---|
112 | |
---|
113 | /* Guess all glyphs */ |
---|
114 | for(i = 0; i < 6; i++) |
---|
115 | { |
---|
116 | struct image *new; |
---|
117 | int mindist = INT_MAX, min = -1; |
---|
118 | new = filter_crop(tmp3, limits[i], 15, limits[i] + 11, 45); |
---|
119 | for(j = 0; j < 27; j++) |
---|
120 | { |
---|
121 | int dist = 0; |
---|
122 | for(y = 0; y < new->height; y++) |
---|
123 | for(x = 0; x < new->width; x++) |
---|
124 | { |
---|
125 | int r2, g2, b2; |
---|
126 | getpixel(font, 12 * j + x, y, &r, &g, &b); |
---|
127 | getpixel(new, x, y, &r2, &g2, &b2); |
---|
128 | dist += (r - r2) * (r - r2); |
---|
129 | } |
---|
130 | if(dist < mindist) |
---|
131 | { |
---|
132 | mindist = dist; |
---|
133 | min = j; |
---|
134 | } |
---|
135 | } |
---|
136 | image_free(new); |
---|
137 | result[i] = all[min]; |
---|
138 | } |
---|
139 | |
---|
140 | image_free(tmp1); |
---|
141 | image_free(tmp2); |
---|
142 | image_free(tmp3); |
---|
143 | |
---|
144 | return result; |
---|
145 | } |
---|
146 | |
---|