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

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