source: pwntcha/trunk/src/test.c @ 392

Last change on this file since 392 was 389, checked in by Sam Hocevar, 18 years ago
  • use OpenCV rather than Imlib2 if both are available.
  • cleaned up the slashdot code.
  • decode phpBB captchas.
  • added filter_contrast.
  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1/*
2 * test.c: test captchas
3 * $Id: test.c 389 2005-01-03 21:48:54Z sam $
4 *
5 * Copyright: (c) 2004 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 <math.h>
16
17#include "config.h"
18#include "common.h"
19
20/* Our macros */
21#define FONTNAME "share/font_phpbb.png"
22
23static struct image *find_glyphs(struct image *img);
24
25/* Global stuff */
26struct { int xmin, ymin, xmax, ymax; } objlist[100];
27int objects, first, last;
28char *result;
29
30/* Main function */
31char * decode_test(struct image *img)
32{
33    struct image *tmp1, *tmp2, *tmp3, *tmp4, *tmp5, *tmp6, *tmp7;
34
35    /* Initialise local data */
36    objects = 0;
37    first = -1;
38    last = -1;
39
40    /* phpBB captchas have 6 characters */
41    result = malloc(7 * sizeof(char));
42
43    tmp1 = filter_smooth(img);
44    tmp2 = filter_median(tmp1);
45    tmp3 = filter_equalize(tmp2, 130);
46    tmp4 = filter_median(tmp3);
47    tmp5 = find_glyphs(tmp3);
48
49    image_free(tmp1);
50    image_free(tmp2);
51    image_free(tmp3);
52    image_free(tmp4);
53    image_free(tmp5);
54
55    return result;
56}
57
58/* The following functions are local */
59
60static struct image *find_glyphs(struct image *img)
61{
62    char all[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
63    struct image *dst;
64    struct image *font = image_load(FONTNAME);
65    int x, y, i = 0;
66    int r, g, b;
67    int xmin, xmax, ymin, ymax, incell = 0, count = 0, cur = 0, offset = -1;
68    int distmin, distx, disty, distch;
69
70    if(!font)
71    {
72        fprintf(stderr, "cannot load font %s\n", FONTNAME);
73        exit(-1);
74    }
75
76    dst = image_new(img->width, img->height);
77
78    for(x = 0; x < img->width; x++)
79        for(y = 0; y < img->height; y++)
80        {
81            getpixel(img, x, y, &r, &g, &b);
82            setpixel(dst, x, y, 255, g, 255);
83            if(r == 0 && offset == -1)
84                offset = x;
85        }
86
87    strcpy(result, "       ");
88
89    while(cur < 6)
90    {
91        /* Try to find 1st letter */
92        distmin = 999999999;
93        for(i = 0; i < 35; i++)
94        {
95            int localmin = 99999999, localx, localy;
96            xmin = i * 40;
97            ymin = 0;
98            xmax = i * 40 + 40;
99            ymax = 40;
100            for(y = 0; y < img->height - 40; y++)
101            {
102                x = offset - 5;
103                if(cur == 0)
104                    x -= 15;
105                if(x < 0)
106                    x = 0;
107                for(; x < offset + 10; x++)
108                {
109                    int z, t, dist;
110                    dist = 0;
111                    for(t = 0; t < ymax - ymin; t++)
112                        for(z = 0; z < xmax - xmin; z++)
113                        {
114                            int r2;
115                            getgray(font, xmin + z, ymin + t, &r);
116                            getgray(img, x + z, y + t, &r2);
117                            dist += abs(r - r2);
118                        }
119                    if(dist < localmin)
120                    {
121                        localmin = dist;
122                        localx = x;
123                        localy = y;
124                    }
125                }
126            }
127            if(localmin < distmin)
128            {
129                distmin = localmin;
130                distx = localx;
131                disty = localy;
132                distch = i;
133            }
134        }
135
136        /* Print min glyph (debug) */
137        xmin = distch * 40;
138        ymin = 0;
139        xmax = distch * 40 + 40;
140        ymax = 40;
141        for(y = 0; y < ymax - ymin; y++)
142            for(x = 0; x < xmax - xmin; x++)
143            {
144                getpixel(font, xmin + x, ymin + y, &r, &g, &b);
145                if(r > 128) continue;
146                setpixel(dst, distx + x, disty + y, r, g, b);
147            }
148
149        offset = distx + xmax - xmin;
150        result[cur++] = all[distch];
151    }
152
153    return dst;
154}
155
Note: See TracBrowser for help on using the repository browser.