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