1 | /* |
---|
2 | * java.c: decode java captchas I forgot about |
---|
3 | * $Id: decoder.c 2317 2008-04-26 08:41:35Z 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 | /* Main function */ |
---|
22 | char *decode_java(struct image *img) |
---|
23 | { |
---|
24 | struct image *tmp; |
---|
25 | int x, y, dx, dy, best = 0, bestx, besty; |
---|
26 | int r, g, b, r2, g2, b2, r3, g3, b3, r4, g4, b4, i, j, c; |
---|
27 | |
---|
28 | tmp = image_dup(img); |
---|
29 | filter_threshold(tmp, 245); |
---|
30 | |
---|
31 | for(dy = 0; dy < 20; dy++) |
---|
32 | { |
---|
33 | if(dy > -5 && dy < 5) |
---|
34 | continue; |
---|
35 | |
---|
36 | for(dx = -20; dx < 20; dx++) |
---|
37 | { |
---|
38 | int good = 0; |
---|
39 | |
---|
40 | if(dx > -5 && dx < 5) |
---|
41 | continue; |
---|
42 | |
---|
43 | for(y = 0; y < tmp->height - dy; y++) |
---|
44 | { |
---|
45 | for(x = 0; x < tmp->width; x++) |
---|
46 | { |
---|
47 | getpixel(tmp, x, y, &r, &g, &b); |
---|
48 | getpixel(tmp, x + dx, y + dy, &r2, &g2, &b2); |
---|
49 | |
---|
50 | if(r && r2) |
---|
51 | good++; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | if(good > best) |
---|
56 | { |
---|
57 | best = good; |
---|
58 | bestx = dx; |
---|
59 | besty = dy; |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | for(y = 0; y < tmp->height - besty; y++) |
---|
65 | { |
---|
66 | for(x = 0; x < tmp->width; x++) |
---|
67 | { |
---|
68 | getpixel(tmp, x, y, &r, &g, &b); |
---|
69 | getpixel(tmp, x + bestx, y + besty, &r2, &g2, &b2); |
---|
70 | getpixel(tmp, x - bestx, y - besty, &r3, &g3, &b3); |
---|
71 | getpixel(tmp, x + 2 * bestx, y + 2 * besty, &r4, &g4, &b4); |
---|
72 | |
---|
73 | if(r && r2) |
---|
74 | { |
---|
75 | if(r3 && r4) |
---|
76 | setpixel(img, x, y, 0, 127, 0); |
---|
77 | else |
---|
78 | setpixel(img, x, y, 0, 255, 255); |
---|
79 | } |
---|
80 | else if(r) |
---|
81 | setpixel(img, x, y, 0, 0, 127); |
---|
82 | else |
---|
83 | setpixel(img, x, y, 0, 0, 0); |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | image_save(img, "test.bmp"); |
---|
88 | image_free(tmp); |
---|
89 | |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | |
---|