source: pwntcha/trunk/src/xanga.c @ 448

Last change on this file since 448 was 448, checked in by Sam Hocevar, 18 years ago
  • Use font_load_* for all decoders.
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1/*
2 * xanga.c: decode Xanga captchas
3 * $Id: xanga.c 448 2005-01-10 15:31: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#include <math.h>
17
18#include "config.h"
19#include "common.h"
20
21static void fill_white_holes(struct image *img);
22
23/* Main function */
24char *decode_xanga(struct image *img)
25{
26    static struct font *font1 = NULL, *font2 = NULL, *font3 = NULL;
27    struct image *tmp;
28    char *result;
29
30    if(!font1)
31    {
32        font1 = font_load_variable("font_freemonobold_32_az.bmp",
33                                   "abcdefghijklmnopqrstuvwxyz");
34        if(!font1)
35            exit(1);
36    }
37
38    if(!font2)
39    {
40        font2 = font_load_variable("font_freemonobold_32_az.bmp",
41                                   "abcdefghijklmnopqrstuvwxyz");
42        if(!font2)
43            exit(1);
44    }
45
46    if(!font3)
47    {
48        font3 = font_load_variable("font_freemonobold_32_az.bmp",
49                                   "abcdefghijklmnopqrstuvwxyz");
50        if(!font3)
51            exit(1);
52    }
53
54    /* Xanga captchas have 7 characters */
55    result = malloc(8 * sizeof(char));
56    strcpy(result, "       ");
57
58    tmp = image_dup(img);
59image_save(tmp, "xanga1.bmp");
60    /* Clean image a bit */
61//    filter_equalize(tmp, 200);
62    filter_contrast(tmp);
63    //filter_detect_lines(tmp);
64image_save(tmp, "xanga2.bmp");
65    fill_white_holes(tmp);
66//    filter_fill_holes(tmp);
67image_save(tmp, "xanga3.bmp");
68    //filter_detect_lines(tmp);
69//    filter_median(tmp);
70//image_save(tmp, "xanga4.bmp");
71    filter_equalize(tmp, 128);
72image_save(tmp, "xanga4.bmp");
73return NULL;
74
75    /* Detect small objects to guess image orientation */
76    filter_median(tmp);
77    filter_equalize(tmp, 200);
78
79    /* Invert rotation and find glyphs */
80    filter_median(tmp);
81
82    /* Clean up our mess */
83    image_free(tmp);
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
94static void fill_white_holes(struct image *img)
95{
96    struct image *tmp;
97    int x, y;
98    int r, g, b;
99
100    tmp = 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(tmp, 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(tmp, x, y, count / 5, count / 5, count / 5);
127        }
128
129    image_swap(tmp, img);
130    image_free(tmp);
131}
132
Note: See TracBrowser for help on using the repository browser.