1 | /* |
---|
2 | * paypal.c: decode Paypal captchas |
---|
3 | * $Id: decoder.c 2318 2008-04-26 08:41:43Z 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 | "stencil_23_AZ.bmp", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
---|
51 | "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(DECODER, |
---|
63 | files[f * 2], files[f * 2 + 1]); |
---|
64 | if(!fonts[f]) |
---|
65 | exit(1); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | while(cur < 8) |
---|
70 | { |
---|
71 | /* Try to find 1st letter */ |
---|
72 | bestdist = INT_MAX; |
---|
73 | for(f = 0; f < FONTS; f++) for(i = 0; i < fonts[f]->size; i++) |
---|
74 | { |
---|
75 | int localmin = INT_MAX, localx, localy; |
---|
76 | xmin = fonts[f]->glyphs[i].xmin - DELTA; |
---|
77 | ymin = fonts[f]->glyphs[i].ymin; |
---|
78 | xmax = fonts[f]->glyphs[i].xmax + DELTA; |
---|
79 | ymax = fonts[f]->glyphs[i].ymax; |
---|
80 | for(y = -3; y < 1; y++) |
---|
81 | { |
---|
82 | for(x = startx; x < startx + 15; x++) |
---|
83 | { |
---|
84 | int z, t, dist; |
---|
85 | dist = 0; |
---|
86 | for(t = 0; t < ymax - ymin; t++) |
---|
87 | for(z = 0; z < xmax - xmin; z++) |
---|
88 | { |
---|
89 | int r2; |
---|
90 | getgray(fonts[f]->img, xmin + z, ymin + t, &r); |
---|
91 | getgray(img, x + z, y + t, &r2); |
---|
92 | if(r < r2) |
---|
93 | dist += (r - r2) * (r - r2); |
---|
94 | else |
---|
95 | dist += (r - r2) * (r - r2) / 2; |
---|
96 | } |
---|
97 | //dist = dist * 128 / fonts[f]->glyphs[i].count; |
---|
98 | dist = dist / (xmax - xmin - 2 * DELTA) / (xmax - xmin - 2 * DELTA); |
---|
99 | if(dist < localmin) |
---|
100 | { |
---|
101 | localmin = dist; |
---|
102 | localx = x; |
---|
103 | localy = y; |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | if(localmin < bestdist) |
---|
108 | { |
---|
109 | bestdist = localmin; |
---|
110 | bestfont = f; |
---|
111 | bestx = localx; |
---|
112 | besty = localy; |
---|
113 | bestch = i; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | /* Print min glyph */ |
---|
118 | #if 0 |
---|
119 | xmin = fonts[bestfont]->glyphs[bestch].xmin - DELTA; |
---|
120 | ymin = fonts[bestfont]->glyphs[bestch].ymin; |
---|
121 | xmax = fonts[bestfont]->glyphs[bestch].xmax + DELTA; |
---|
122 | ymax = fonts[bestfont]->glyphs[bestch].ymax; |
---|
123 | for(y = 0; y < ymax - ymin; y++) |
---|
124 | for(x = 0; x < xmax - xmin; x++) |
---|
125 | { |
---|
126 | getpixel(fonts[bestfont]->img, xmin + x, ymin + y, &r, &g, &b); |
---|
127 | if(r > 128) |
---|
128 | { |
---|
129 | getpixel(img, bestx + x, besty + y, &r, &g, &b); |
---|
130 | r = 255; |
---|
131 | } |
---|
132 | setpixel(img, bestx + x, besty + y, r, g, b); |
---|
133 | } |
---|
134 | #endif |
---|
135 | |
---|
136 | startx = bestx + fonts[bestfont]->glyphs[bestch].xmax - fonts[bestfont]->glyphs[bestch].xmin; |
---|
137 | result[cur++] = fonts[bestfont]->glyphs[bestch].c; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|