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