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