1 | /* |
---|
2 | * xanga.c: decode Xanga captchas |
---|
3 | * $Id: xanga.c 444 2005-01-10 00:43:36Z 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 | #include <math.h> |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.h" |
---|
20 | |
---|
21 | static struct image *fill_white_holes(struct image *img); |
---|
22 | static struct image *rotate(struct image *img); |
---|
23 | static struct image *cut_cells(struct image *img); |
---|
24 | static struct image *find_glyphs(struct image *img); |
---|
25 | |
---|
26 | /* Our macros */ |
---|
27 | #define FACTOR 1 |
---|
28 | #define FONTNAME "font_xanga.png" // use with FACTOR = 1 |
---|
29 | //#define FONTNAME "font.png" // use with FACTOR = 2 |
---|
30 | //#define FONTNAME "font_dilated.png" // use with FACTOR = 2 |
---|
31 | static struct image *font = NULL; |
---|
32 | |
---|
33 | /* Global stuff */ |
---|
34 | struct { int xmin, ymin, xmax, ymax; } objlist[100]; |
---|
35 | int objects, first, last; |
---|
36 | char *result; |
---|
37 | |
---|
38 | /* Main function */ |
---|
39 | char *decode_xanga(struct image *img) |
---|
40 | { |
---|
41 | struct image *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7; |
---|
42 | |
---|
43 | /* Initialise local data */ |
---|
44 | objects = 0; |
---|
45 | first = -1; |
---|
46 | last = -1; |
---|
47 | |
---|
48 | /* Xanga captchas have 7 characters */ |
---|
49 | result = malloc(8 * sizeof(char)); |
---|
50 | strcpy(result, " "); |
---|
51 | |
---|
52 | image_save(img, "xanga1.bmp"); |
---|
53 | /* Clean image a bit */ |
---|
54 | // tmp1 = filter_equalize(img, 200); |
---|
55 | tmp1 = filter_contrast(img); |
---|
56 | //tmp1 = filter_detect_lines(img); |
---|
57 | image_save(tmp1, "xanga2.bmp"); |
---|
58 | tmp2 = fill_white_holes(tmp1); |
---|
59 | // tmp2 = filter_fill_holes(tmp1); |
---|
60 | image_save(tmp2, "xanga3.bmp"); |
---|
61 | //tmp3 = filter_detect_lines(tmp2); |
---|
62 | // tmp3 = filter_median(tmp2); |
---|
63 | //image_save(tmp3, "xanga4.bmp"); |
---|
64 | tmp3 = filter_equalize(tmp2, 128); |
---|
65 | image_save(tmp3, "xanga4.bmp"); |
---|
66 | return NULL; |
---|
67 | |
---|
68 | /* Detect small objects to guess image orientation */ |
---|
69 | tmp3 = filter_median(tmp2); |
---|
70 | tmp4 = filter_equalize(tmp3, 200); |
---|
71 | |
---|
72 | /* Invert rotation and find glyphs */ |
---|
73 | tmp5 = rotate(tmp2); |
---|
74 | tmp6 = filter_median(tmp5); |
---|
75 | |
---|
76 | /* Clean up our mess */ |
---|
77 | image_free(tmp1); |
---|
78 | image_free(tmp2); |
---|
79 | image_free(tmp3); |
---|
80 | image_free(tmp4); |
---|
81 | image_free(tmp5); |
---|
82 | image_free(tmp6); |
---|
83 | image_free(tmp7); |
---|
84 | |
---|
85 | /* aaaaaaa means decoding failed */ |
---|
86 | if(!strcmp(result, "aaaaaaa")) |
---|
87 | result[0] = '\0'; |
---|
88 | |
---|
89 | return result; |
---|
90 | } |
---|
91 | |
---|
92 | /* The following functions are local */ |
---|
93 | |
---|
94 | static struct image *fill_white_holes(struct image *img) |
---|
95 | { |
---|
96 | struct image *dst; |
---|
97 | int x, y, i; |
---|
98 | int r, g, b; |
---|
99 | |
---|
100 | dst = image_new(img->width, img->height); |
---|
101 | |
---|
102 | for(y = 0; y < img->height; y++) |
---|
103 | for(x = 0; x < img->width; x++) |
---|
104 | { |
---|
105 | getpixel(img, x, y, &r, &g, &b); |
---|
106 | setpixel(dst, x, y, r, g, b); |
---|
107 | } |
---|
108 | |
---|
109 | for(y = 1; y < img->height - 1; y++) |
---|
110 | for(x = 1; x < img->width - 1; x++) |
---|
111 | { |
---|
112 | int count = 0; |
---|
113 | getpixel(img, x, y, &r, &g, &b); |
---|
114 | if(r <= 16) |
---|
115 | continue; |
---|
116 | getpixel(img, x + 1, y, &r, &g, &b); |
---|
117 | count += r; |
---|
118 | getpixel(img, x - 1, y, &r, &g, &b); |
---|
119 | count += r; |
---|
120 | getpixel(img, x, y + 1, &r, &g, &b); |
---|
121 | count += r; |
---|
122 | getpixel(img, x, y - 1, &r, &g, &b); |
---|
123 | count += r; |
---|
124 | if(count > 600) |
---|
125 | continue; |
---|
126 | setpixel(dst, x, y, count / 5, count / 5, count / 5); |
---|
127 | } |
---|
128 | |
---|
129 | return dst; |
---|
130 | } |
---|
131 | |
---|