| 1 | /* |
|---|
| 2 | * authimage.c: decode authimage captchas |
|---|
| 3 | * $Id$ |
|---|
| 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 | |
|---|
| 21 | #define FONTNAME "font_authimage.png" |
|---|
| 22 | static struct image *font = NULL; |
|---|
| 23 | |
|---|
| 24 | /* Main function */ |
|---|
| 25 | char *decode_authimage(struct image *img) |
|---|
| 26 | { |
|---|
| 27 | char *all = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|---|
| 28 | char *result; |
|---|
| 29 | struct image *tmp; |
|---|
| 30 | int x, y, r, g, b, i; |
|---|
| 31 | |
|---|
| 32 | if(!font) |
|---|
| 33 | { |
|---|
| 34 | char fontname[BUFSIZ]; |
|---|
| 35 | sprintf(fontname, "%s/%s", share, FONTNAME); |
|---|
| 36 | font = image_load(fontname); |
|---|
| 37 | if(!font) |
|---|
| 38 | { |
|---|
| 39 | fprintf(stderr, "cannot load font %s\n", fontname); |
|---|
| 40 | exit(-1); |
|---|
| 41 | } |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | /* authimage captchas have 6 characters */ |
|---|
| 45 | result = malloc(7 * sizeof(char)); |
|---|
| 46 | memset(result, '\0', 7); |
|---|
| 47 | |
|---|
| 48 | /* half the captchas are inverse video; we set them back to normal */ |
|---|
| 49 | tmp = image_dup(img); |
|---|
| 50 | filter_scale(tmp, 2.0); |
|---|
| 51 | getpixel(tmp, 0, 0, &r, &g, &b); |
|---|
| 52 | filter_equalize(tmp, r * 3 / 4); |
|---|
| 53 | filter_smooth(tmp); |
|---|
| 54 | |
|---|
| 55 | for(i = 0; i < 6; i++) |
|---|
| 56 | { |
|---|
| 57 | int mindiff = INT_MAX, minch = -1, ch; |
|---|
| 58 | for(ch = 0; ch < 36; ch++) |
|---|
| 59 | { |
|---|
| 60 | int diff = 0; |
|---|
| 61 | for(y = 0; y < 7; y++) |
|---|
| 62 | { |
|---|
| 63 | for(x = 0; x < 5; x++) |
|---|
| 64 | { |
|---|
| 65 | int newx, newy, r2; |
|---|
| 66 | newx = 35.0 + (x + 6 * i) * 218.0 / 34.0 + y * 5.0 / 6.0 + 0.5; |
|---|
| 67 | newy = 33.0 - (x + 6 * i) * 18.0 / 34.0 + y * 42.0 / 6.0 + 0.5; |
|---|
| 68 | getpixel(tmp, newx, newy, &r, &g, &b); |
|---|
| 69 | getpixel(font, x + 6 * ch, y, &r2, &g, &b); |
|---|
| 70 | r = (r < 220) ? 0 : 255; |
|---|
| 71 | diff += (r - r2) * (r - r2); |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | if(diff < mindiff) |
|---|
| 75 | { |
|---|
| 76 | mindiff = diff; |
|---|
| 77 | minch = ch; |
|---|
| 78 | } |
|---|
| 79 | } |
|---|
| 80 | result[i] = all[minch]; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | image_free(tmp); |
|---|
| 84 | |
|---|
| 85 | return result; |
|---|
| 86 | } |
|---|
| 87 | |
|---|