1 | /* |
---|

2 | * java.c: decode java captchas I forgot about |
---|

3 | * $Id: java.c 1000 2006-06-04 00:58:45Z 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 | /* Main function */ |
---|

21 | char *decode_java(struct image *img) |
---|

22 | { |
---|

23 | struct image *tmp; |
---|

24 | int x, y, dx, dy, best = 0, bestx, besty; |
---|

25 | int r, g, b, r2, g2, b2, r3, g3, b3, r4, g4, b4, i, j, c; |
---|

26 | |
---|

27 | tmp = image_dup(img); |
---|

28 | filter_threshold(tmp, 245); |
---|

29 | |
---|

30 | for(dy = 0; dy < 20; dy++) |
---|

31 | { |
---|

32 | if(dy > -5 && dy < 5) |
---|

33 | continue; |
---|

34 | |
---|

35 | for(dx = -20; dx < 20; dx++) |
---|

36 | { |
---|

37 | int good = 0; |
---|

38 | |
---|

39 | if(dx > -5 && dx < 5) |
---|

40 | continue; |
---|

41 | |
---|

42 | for(y = 0; y < tmp->height - dy; y++) |
---|

43 | { |
---|

44 | for(x = 0; x < tmp->width; x++) |
---|

45 | { |
---|

46 | getpixel(tmp, x, y, &r, &g, &b); |
---|

47 | getpixel(tmp, x + dx, y + dy, &r2, &g2, &b2); |
---|

48 | |
---|

49 | if(r && r2) |
---|

50 | good++; |
---|

51 | } |
---|

52 | } |
---|

53 | |
---|

54 | if(good > best) |
---|

55 | { |
---|

56 | best = good; |
---|

57 | bestx = dx; |
---|

58 | besty = dy; |
---|

59 | } |
---|

60 | } |
---|

61 | } |
---|

62 | |
---|

63 | for(y = 0; y < tmp->height - besty; y++) |
---|

64 | { |
---|

65 | for(x = 0; x < tmp->width; x++) |
---|

66 | { |
---|

67 | getpixel(tmp, x, y, &r, &g, &b); |
---|

68 | getpixel(tmp, x + bestx, y + besty, &r2, &g2, &b2); |
---|

69 | getpixel(tmp, x - bestx, y - besty, &r3, &g3, &b3); |
---|

70 | getpixel(tmp, x + 2 * bestx, y + 2 * besty, &r4, &g4, &b4); |
---|

71 | |
---|

72 | if(r && r2) |
---|

73 | { |
---|

74 | if(r3 && r4) |
---|

75 | setpixel(img, x, y, 0, 127, 0); |
---|

76 | else |
---|

77 | setpixel(img, x, y, 0, 255, 255); |
---|

78 | } |
---|

79 | else if(r) |
---|

80 | setpixel(img, x, y, 0, 0, 127); |
---|

81 | else |
---|

82 | setpixel(img, x, y, 0, 0, 0); |
---|

83 | } |
---|

84 | } |
---|

85 | |
---|

86 | image_save(img, "test.bmp"); |
---|

87 | image_free(tmp); |
---|

88 | |
---|

89 | return NULL; |
---|

90 | } |
---|

91 | |
---|