| 1 | /* |
|---|
| 2 | * paypal.c: decode Paypal 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 | |
|---|
| 17 | #include "config.h" |
|---|
| 18 | #include "common.h" |
|---|
| 19 | |
|---|
| 20 | static void find_glyphs(struct image *img); |
|---|
| 21 | |
|---|
| 22 | /* Our macros */ |
|---|
| 23 | char *result; |
|---|
| 24 | |
|---|
| 25 | /* Main function */ |
|---|
| 26 | char *decode_paypal(struct image *img) |
|---|
| 27 | { |
|---|
| 28 | struct image *tmp; |
|---|
| 29 | |
|---|
| 30 | /* paypal captchas have 8 characters */ |
|---|
| 31 | result = malloc(9 * sizeof(char)); |
|---|
| 32 | strcpy(result, " "); |
|---|
| 33 | |
|---|
| 34 | tmp = image_dup(img); |
|---|
| 35 | find_glyphs(tmp); |
|---|
| 36 | |
|---|
| 37 | image_free(tmp); |
|---|
| 38 | |
|---|
| 39 | return result; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | static void find_glyphs(struct image *img) |
|---|
| 43 | { |
|---|
| 44 | #define DELTA 2 |
|---|
| 45 | #define FONTS 2 |
|---|
| 46 | static struct font *fonts[FONTS]; |
|---|
| 47 | static char *files[] = |
|---|
| 48 | { |
|---|
| 49 | "font_stencil_23_AZ.bmp", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
|---|
| 50 | "font_stencil_24_AZ.bmp", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", |
|---|
| 51 | }; |
|---|
| 52 | int x, y, i = 0, f; |
|---|
| 53 | int r, g, b; |
|---|
| 54 | int xmin, xmax, ymin, ymax, startx = 0, cur = 0; |
|---|
| 55 | int bestdist, bestfont, bestx, besty, bestch; |
|---|
| 56 | |
|---|
| 57 | for(f = 0; f < FONTS; f++) |
|---|
| 58 | { |
|---|
| 59 | if(!fonts[f]) |
|---|
| 60 | { |
|---|
| 61 | fonts[f] = font_load_variable(files[f * 2], files[f * 2 + 1]); |
|---|
| 62 | if(!fonts[f]) |
|---|
| 63 | exit(1); |
|---|
| 64 | } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | while(cur < 8) |
|---|
| 68 | { |
|---|
| 69 | /* Try to find 1st letter */ |
|---|
| 70 | bestdist = INT_MAX; |
|---|
| 71 | for(f = 0; f < FONTS; f++) for(i = 0; i < fonts[f]->size; i++) |
|---|
| 72 | { |
|---|
| 73 | int localmin = INT_MAX, localx, localy; |
|---|
| 74 | xmin = fonts[f]->glyphs[i].xmin - DELTA; |
|---|
| 75 | ymin = fonts[f]->glyphs[i].ymin; |
|---|
| 76 | xmax = fonts[f]->glyphs[i].xmax + DELTA; |
|---|
| 77 | ymax = fonts[f]->glyphs[i].ymax; |
|---|
| 78 | for(y = -3; y < 1; y++) |
|---|
| 79 | { |
|---|
| 80 | for(x = startx; x < startx + 15; x++) |
|---|
| 81 | { |
|---|
| 82 | int z, t, dist; |
|---|
| 83 | dist = 0; |
|---|
| 84 | for(t = 0; t < ymax - ymin; t++) |
|---|
| 85 | for(z = 0; z < xmax - xmin; z++) |
|---|
| 86 | { |
|---|
| 87 | int r2; |
|---|
| 88 | getgray(fonts[f]->img, xmin + z, ymin + t, &r); |
|---|
| 89 | getgray(img, x + z, y + t, &r2); |
|---|
| 90 | if(r < r2) |
|---|
| 91 | dist += (r - r2) * (r - r2); |
|---|
| 92 | else |
|---|
| 93 | dist += (r - r2) * (r - r2) / 2; |
|---|
| 94 | } |
|---|
| 95 | //dist = dist * 128 / fonts[f]->glyphs[i].count; |
|---|
| 96 | dist = dist / (xmax - xmin - 2 * DELTA) / (xmax - xmin - 2 * DELTA); |
|---|
| 97 | if(dist < localmin) |
|---|
| 98 | { |
|---|
| 99 | localmin = dist; |
|---|
| 100 | localx = x; |
|---|
| 101 | localy = y; |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | if(localmin < bestdist) |
|---|
| 106 | { |
|---|
| 107 | bestdist = localmin; |
|---|
| 108 | bestfont = f; |
|---|
| 109 | bestx = localx; |
|---|
| 110 | besty = localy; |
|---|
| 111 | bestch = i; |
|---|
| 112 | } |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | /* Print min glyph */ |
|---|
| 116 | #if 0 |
|---|
| 117 | xmin = fonts[bestfont]->glyphs[bestch].xmin - DELTA; |
|---|
| 118 | ymin = fonts[bestfont]->glyphs[bestch].ymin; |
|---|
| 119 | xmax = fonts[bestfont]->glyphs[bestch].xmax + DELTA; |
|---|
| 120 | ymax = fonts[bestfont]->glyphs[bestch].ymax; |
|---|
| 121 | for(y = 0; y < ymax - ymin; y++) |
|---|
| 122 | for(x = 0; x < xmax - xmin; x++) |
|---|
| 123 | { |
|---|
| 124 | getpixel(fonts[bestfont]->img, xmin + x, ymin + y, &r, &g, &b); |
|---|
| 125 | if(r > 128) |
|---|
| 126 | { |
|---|
| 127 | getpixel(img, bestx + x, besty + y, &r, &g, &b); |
|---|
| 128 | r = 255; |
|---|
| 129 | } |
|---|
| 130 | setpixel(img, bestx + x, besty + y, r, g, b); |
|---|
| 131 | } |
|---|
| 132 | #endif |
|---|
| 133 | |
|---|
| 134 | startx = bestx + fonts[bestfont]->glyphs[bestch].xmax - fonts[bestfont]->glyphs[bestch].xmin; |
|---|
| 135 | result[cur++] = fonts[bestfont]->glyphs[bestch].c; |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|