1 | /* |
---|
2 | * lmt.c: decode lmt.lv captchas |
---|
3 | * $Id: lmt.c 459 2005-01-13 15:26:55Z 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 | static void find_glyphs(struct image *img); |
---|
21 | |
---|
22 | /* Our macros */ |
---|
23 | char *result; |
---|
24 | |
---|
25 | /* Main function */ |
---|
26 | char *decode_lmt(struct image *img) |
---|
27 | { |
---|
28 | struct image *tmp; |
---|
29 | |
---|
30 | /* lmt captchas have 3 characters */ |
---|
31 | result = malloc(4 * sizeof(char)); |
---|
32 | strcpy(result, " "); |
---|
33 | |
---|
34 | tmp = image_dup(img); |
---|
35 | filter_contrast(tmp); |
---|
36 | filter_black_stuff(tmp); |
---|
37 | find_glyphs(tmp); |
---|
38 | |
---|
39 | image_free(tmp); |
---|
40 | |
---|
41 | return result; |
---|
42 | } |
---|
43 | |
---|
44 | static void find_glyphs(struct image *img) |
---|
45 | { |
---|
46 | #define DELTA 2 |
---|
47 | static struct font *font; |
---|
48 | int x, y, i = 0; |
---|
49 | int r, g, b; |
---|
50 | int xmin, xmax, ymin, ymax, startx = 0, cur = 0; |
---|
51 | int bestdist, bestx, besty, bestch; |
---|
52 | |
---|
53 | if(!font) |
---|
54 | { |
---|
55 | font = font_load_variable("font_freesans_24_09AZ.bmp", |
---|
56 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"); |
---|
57 | if(!font) |
---|
58 | exit(1); |
---|
59 | } |
---|
60 | |
---|
61 | while(cur < 3) |
---|
62 | { |
---|
63 | /* Try to find 1st letter */ |
---|
64 | bestdist = INT_MAX; |
---|
65 | for(i = 0; i < font->size; i++) |
---|
66 | { |
---|
67 | int localmin = INT_MAX, localx, localy; |
---|
68 | xmin = font->glyphs[i].xmin - DELTA; |
---|
69 | ymin = font->glyphs[i].ymin; |
---|
70 | xmax = font->glyphs[i].xmax + DELTA; |
---|
71 | ymax = font->glyphs[i].ymax; |
---|
72 | for(y = -5; y < 5; y++) |
---|
73 | { |
---|
74 | for(x = startx; x < startx + 15; x++) |
---|
75 | { |
---|
76 | int z, t, dist; |
---|
77 | dist = 0; |
---|
78 | for(t = 0; t < ymax - ymin; t++) |
---|
79 | for(z = 0; z < xmax - xmin; z++) |
---|
80 | { |
---|
81 | int r2; |
---|
82 | getgray(font->img, xmin + z, ymin + t, &r); |
---|
83 | getgray(img, x + z, y + t, &r2); |
---|
84 | dist += (r - r2) * (r - r2); |
---|
85 | } |
---|
86 | dist = dist / (xmax - xmin - 2 * DELTA); |
---|
87 | if(dist < localmin) |
---|
88 | { |
---|
89 | localmin = dist; |
---|
90 | localx = x; |
---|
91 | localy = y; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | if(localmin < bestdist) |
---|
96 | { |
---|
97 | bestdist = localmin; |
---|
98 | bestx = localx; |
---|
99 | besty = localy; |
---|
100 | bestch = i; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | /* Print min glyph */ |
---|
105 | #if 0 |
---|
106 | xmin = font->glyphs[bestch].xmin - DELTA; |
---|
107 | ymin = font->glyphs[bestch].ymin; |
---|
108 | xmax = font->glyphs[bestch].xmax + DELTA; |
---|
109 | ymax = font->glyphs[bestch].ymax; |
---|
110 | for(y = 0; y < ymax - ymin; y++) |
---|
111 | for(x = 0; x < xmax - xmin; x++) |
---|
112 | { |
---|
113 | getpixel(font->img, xmin + x, ymin + y, &r, &g, &b); |
---|
114 | if(r > 128) |
---|
115 | { |
---|
116 | getpixel(img, bestx + x, besty + y, &r, &g, &b); |
---|
117 | r = 255; |
---|
118 | } |
---|
119 | setpixel(img, bestx + x, besty + y, r, g, b); |
---|
120 | } |
---|
121 | #endif |
---|
122 | |
---|
123 | startx = bestx + font->glyphs[bestch].xmax - font->glyphs[bestch].xmin; |
---|
124 | result[cur++] = font->glyphs[bestch].c; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|