1 | /* |
---|
2 | * paypal.c: decode Paypal captchas |
---|
3 | * $Id: decoder.c 2317 2008-04-26 08:41:35Z sam $ |
---|
4 | * |
---|
5 | * Copyright: (c) 2005 Sam Hocevar <sam@zoy.org> |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <stdio.h> |
---|
14 | #include <stdlib.h> |
---|
15 | #include <string.h> |
---|
16 | #include <limits.h> |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | static void find_glyphs(struct image *img); |
---|
22 | |
---|
23 | /* Our macros */ |
---|
24 | char *result; |
---|
25 | |
---|
26 | /* Main function */ |
---|
27 | char *decode_paypal(struct image *img) |
---|
28 | { |
---|
29 | struct image *tmp; |
---|
30 | |
---|
31 | /* paypal captchas have 8 characters */ |
---|
32 | result = malloc(9 * sizeof(char)); |
---|
33 | strcpy(result, " "); |
---|
34 | |
---|
35 | tmp = image_dup(img); |
---|
36 | find_glyphs(tmp); |
---|
37 | |
---|
38 | image_free(tmp); |
---|
39 | |
---|
40 | return result; |
---|
41 | } |
---|
42 | |
---|
43 | static void find_glyphs(struct image *img) |
---|
44 | { |
---|
45 | #define DELTA 2 |
---|
46 | #define FONTS 2 |
---|
47 | static struct font *fonts[FONTS]; |
---|
48 | static char *files[] = |
---|
49 | { |
---|
50 | "font_stencil_23_AZ.bmp", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
---|
51 | "font_stencil_24_AZ.bmp", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
---|
52 | }; |
---|
53 | int x, y, i = 0, f; |
---|
54 | int r, g, b; |
---|
55 | int xmin, xmax, ymin, ymax, startx = 0, cur = 0; |
---|
56 | int bestdist, bestfont, bestx, besty, bestch; |
---|
57 | |
---|
58 | for(f = 0; f < FONTS; f++) |
---|
59 | { |
---|
60 | if(!fonts[f]) |
---|
61 | { |
---|
62 | fonts[f] = font_load_variable(files[f * 2], files[f * 2 + 1]); |
---|
63 | if(!fonts[f]) |
---|
64 | exit(1); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | while(cur < 8) |
---|
69 | { |
---|
70 | /* Try to find 1st letter */ |
---|
71 | bestdist = INT_MAX; |
---|
72 | for(f = 0; f < FONTS; f++) for(i = 0; i < fonts[f]->size; i++) |
---|
73 | { |
---|
74 | int localmin = INT_MAX, localx, localy; |
---|
75 | xmin = fonts[f]->glyphs[i].xmin - DELTA; |
---|
76 | ymin = fonts[f]->glyphs[i].ymin; |
---|
77 | xmax = fonts[f]->glyphs[i].xmax + DELTA; |
---|
78 | ymax = fonts[f]->glyphs[i].ymax; |
---|
79 | for(y = -3; y < 1; y++) |
---|
80 | { |
---|
81 | for(x = startx; x < startx + 15; 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(fonts[f]->img, xmin + z, ymin + t, &r); |
---|
90 | getgray(img, x + z, y + t, &r2); |
---|
91 | if(r < r2) |
---|
92 | dist += (r - r2) * (r - r2); |
---|
93 | else |
---|
94 | dist += (r - r2) * (r - r2) / 2; |
---|
95 | } |
---|
96 | //dist = dist * 128 / fonts[f]->glyphs[i].count; |
---|
97 | dist = dist / (xmax - xmin - 2 * DELTA) / (xmax - xmin - 2 * DELTA); |
---|
98 | if(dist < localmin) |
---|
99 | { |
---|
100 | localmin = dist; |
---|
101 | localx = x; |
---|
102 | localy = y; |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | if(localmin < bestdist) |
---|
107 | { |
---|
108 | bestdist = localmin; |
---|
109 | bestfont = f; |
---|
110 | bestx = localx; |
---|
111 | besty = localy; |
---|
112 | bestch = i; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | /* Print min glyph */ |
---|
117 | #if 0 |
---|
118 | xmin = fonts[bestfont]->glyphs[bestch].xmin - DELTA; |
---|
119 | ymin = fonts[bestfont]->glyphs[bestch].ymin; |
---|
120 | xmax = fonts[bestfont]->glyphs[bestch].xmax + DELTA; |
---|
121 | ymax = fonts[bestfont]->glyphs[bestch].ymax; |
---|
122 | for(y = 0; y < ymax - ymin; y++) |
---|
123 | for(x = 0; x < xmax - xmin; x++) |
---|
124 | { |
---|
125 | getpixel(fonts[bestfont]->img, xmin + x, ymin + y, &r, &g, &b); |
---|
126 | if(r > 128) |
---|
127 | { |
---|
128 | getpixel(img, bestx + x, besty + y, &r, &g, &b); |
---|
129 | r = 255; |
---|
130 | } |
---|
131 | setpixel(img, bestx + x, besty + y, r, g, b); |
---|
132 | } |
---|
133 | #endif |
---|
134 | |
---|
135 | startx = bestx + fonts[bestfont]->glyphs[bestch].xmax - fonts[bestfont]->glyphs[bestch].xmin; |
---|
136 | result[cur++] = fonts[bestfont]->glyphs[bestch].c; |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|