1 | /* |
---|
2 | * livejournal.c: decode livejournal captchas |
---|
3 | * $Id: livejournal.c 477 2005-04-27 08:54: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 | static void find_glyphs(struct image *img); |
---|
21 | |
---|
22 | /* Our macros */ |
---|
23 | char *result; |
---|
24 | |
---|
25 | /* Main function */ |
---|
26 | char *decode_livejournal(struct image *img) |
---|
27 | { |
---|
28 | struct image *tmp; |
---|
29 | |
---|
30 | /* livejournal captchas have 7 characters */ |
---|
31 | result = malloc(8 * sizeof(char)); |
---|
32 | strcpy(result, " "); |
---|
33 | |
---|
34 | tmp = image_dup(img); |
---|
35 | filter_detect_lines(tmp); |
---|
36 | filter_fill_holes(tmp); |
---|
37 | filter_median(tmp); |
---|
38 | // filter_smooth(tmp); |
---|
39 | // filter_contrast(tmp); |
---|
40 | filter_equalize(tmp, 128); |
---|
41 | image_save(tmp, "foo.bmp"); |
---|
42 | find_glyphs(tmp); |
---|
43 | |
---|
44 | image_free(tmp); |
---|
45 | |
---|
46 | return result; |
---|
47 | } |
---|
48 | |
---|
49 | static void find_glyphs(struct image *img) |
---|
50 | { |
---|
51 | static struct font *font = NULL; |
---|
52 | struct image *tmp; |
---|
53 | int x, y, i = 0; |
---|
54 | int r, g, b; |
---|
55 | int xmin, xmax, ymin, ymax, startx = 0, cur = 0; |
---|
56 | int distmin, distx, disty, distch; |
---|
57 | |
---|
58 | if(!font) |
---|
59 | { |
---|
60 | font = font_load_variable("x_font_freesansbold_32_09az.bmp", |
---|
61 | "0123456789abcdefghijklmnopqrstuvwxyz"); |
---|
62 | if(!font) |
---|
63 | exit(1); |
---|
64 | } |
---|
65 | |
---|
66 | tmp = image_new(img->width, img->height); |
---|
67 | |
---|
68 | for(y = 0; y < img->height; y++) |
---|
69 | for(x = 0; x < img->width; x++) |
---|
70 | { |
---|
71 | getpixel(img, x, y, &r, &g, &b); |
---|
72 | setpixel(tmp, x, y, 255, g, 255); |
---|
73 | } |
---|
74 | |
---|
75 | while(cur < 7) |
---|
76 | { |
---|
77 | /* Try to find 1st letter */ |
---|
78 | distmin = INT_MAX; |
---|
79 | for(i = 0; i < font->size; i++) |
---|
80 | { |
---|
81 | int sqr; |
---|
82 | int localmin = INT_MAX, localx, localy; |
---|
83 | xmin = font->glyphs[i].xmin; |
---|
84 | ymin = font->glyphs[i].ymin; |
---|
85 | xmax = font->glyphs[i].xmax; |
---|
86 | ymax = font->glyphs[i].ymax; |
---|
87 | sqr = sqrt(xmax - xmin); |
---|
88 | for(y = -16; y < 8; y++) |
---|
89 | for(x = 25 * cur; x < 25 * cur + 5; x++) |
---|
90 | { |
---|
91 | int z, t, dist; |
---|
92 | dist = 0; |
---|
93 | for(t = 0; t < ymax - ymin; t++) |
---|
94 | for(z = 0; z < xmax - xmin; z++) |
---|
95 | { |
---|
96 | int r2; |
---|
97 | getgray(font->img, xmin + z, ymin + t, &r); |
---|
98 | getgray(img, x + z, y + t, &r2); |
---|
99 | dist += abs(r - r2); |
---|
100 | } |
---|
101 | //dist = dist * 128 / font->glyphs[i].count; |
---|
102 | dist = dist / (xmax - xmin) / sqr; |
---|
103 | if(dist < localmin) |
---|
104 | { |
---|
105 | localmin = dist; |
---|
106 | localx = x; |
---|
107 | localy = y; |
---|
108 | } |
---|
109 | } |
---|
110 | if(localmin < distmin) |
---|
111 | { |
---|
112 | distmin = localmin; |
---|
113 | distx = localx; |
---|
114 | disty = localy; |
---|
115 | distch = i; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /* Print min glyph */ |
---|
120 | xmin = font->glyphs[distch].xmin; |
---|
121 | ymin = font->glyphs[distch].ymin; |
---|
122 | xmax = font->glyphs[distch].xmax; |
---|
123 | ymax = font->glyphs[distch].ymax; |
---|
124 | for(y = 0; y < ymax - ymin; y++) |
---|
125 | for(x = 0; x < xmax - xmin; x++) |
---|
126 | { |
---|
127 | getpixel(font->img, xmin + x, ymin + y, &r, &g, &b); |
---|
128 | if(r > 128) |
---|
129 | continue; |
---|
130 | setpixel(tmp, distx + x, disty + y, r, g, b); |
---|
131 | } |
---|
132 | |
---|
133 | startx = distx + 20; |
---|
134 | result[cur++] = font->glyphs[distch].c; |
---|
135 | } |
---|
136 | |
---|
137 | image_save(tmp, "foo2.bmp"); |
---|
138 | image_free(tmp); |
---|
139 | } |
---|
140 | |
---|