source: pwntcha/trunk/src/authimage/decoder.c @ 2318

Last change on this file since 2318 was 2318, checked in by Sam Hocevar, 12 years ago
  • Step 2 of the code reorganisation: move each font to its corresponding decoder's subdirectory.
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1/*
2 * authimage.c: decode authimage captchas
3 * $Id: decoder.c 2318 2008-04-26 08:41:43Z 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#include <math.h>
18
19#include "config.h"
20#include "common.h"
21
22/* Main function */
23char *decode_authimage(struct image *img)
24{
25    static struct font *font = NULL;
26    char *result;
27    struct image *tmp;
28    int x, y, r, g, b, i;
29
30    if(!font)
31    {
32        font = font_load_fixed(DECODER, "font.png",
33                               "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
34        if(!font)
35            exit(-1);
36    }
37
38    /* authimage captchas have 6 characters */
39    result = malloc(7 * sizeof(char));
40    memset(result, '\0', 7);
41
42    /* double the captcha size for better accuracy in the rotation */
43    tmp = image_dup(img);
44    filter_scale(tmp, 2.0);
45    getpixel(tmp, 0, 0, &r, &g, &b);
46    filter_threshold(tmp, r * 3 / 4);
47    filter_smooth(tmp);
48    filter_threshold(tmp, 220);
49
50    for(i = 0; i < 6; i++)
51    {
52        int mindiff = INT_MAX, minch = -1, ch;
53        for(ch = 0; ch < font->size; ch++)
54        {
55            int diff = 0;
56            for(y = 0; y < 7; y++)
57            {
58                for(x = 0; x < 5; x++)
59                {
60                    int newx, newy, r2;
61                    newx = 35.0 + (x + 6 * i) * 218.0 / 34.0 + y * 5.0 / 6.0 + 0.5;
62                    newy = 33.0 - (x + 6 * i) * 18.0 / 34.0 + y * 42.0 / 6.0 + 0.5;
63                    getpixel(tmp, newx, newy, &r, &g, &b);
64                    getpixel(font->img, x + 6 * ch, y, &r2, &g, &b);
65                    diff += (r - r2) * (r - r2);
66                }
67            }
68            if(diff < mindiff)
69            {
70                mindiff = diff;
71                minch = ch;
72            }
73        }
74        result[i] = font->glyphs[minch].c;
75    }
76
77    image_free(tmp);
78
79    return result;
80}
81
Note: See TracBrowser for help on using the repository browser.