| [389] | 1 | /* |
|---|
| 2 | * phpbb.c: decode phpBB captchas |
|---|
| 3 | * $Id$ |
|---|
| 4 | * |
|---|
| 5 | * Copyright: (c) 2005 Sam Hocevar <sam@zoy.org> |
|---|
| [2315] | 6 | * This program is free software. It comes without any warranty, to |
|---|
| 7 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 8 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 9 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| [389] | 11 | */ |
|---|
| 12 | |
|---|
| 13 | #include <stdio.h> |
|---|
| 14 | #include <stdlib.h> |
|---|
| 15 | #include <string.h> |
|---|
| [411] | 16 | #include <limits.h> |
|---|
| [389] | 17 | |
|---|
| 18 | #include "config.h" |
|---|
| 19 | #include "common.h" |
|---|
| 20 | |
|---|
| 21 | /* Main function */ |
|---|
| 22 | char *decode_phpbb(struct image *img) |
|---|
| 23 | { |
|---|
| [448] | 24 | static struct font *font = NULL; |
|---|
| [418] | 25 | char *result; |
|---|
| [445] | 26 | struct image *tmp1, *tmp2; |
|---|
| [389] | 27 | int x, y, i = 0; |
|---|
| 28 | int r, g, b; |
|---|
| 29 | int xmin, xmax, ymin, ymax, cur, offset = -1; |
|---|
| 30 | int distmin, distx, disty, distch; |
|---|
| 31 | |
|---|
| 32 | if(!font) |
|---|
| 33 | { |
|---|
| [448] | 34 | font = font_load_fixed("font_phpbb.png", |
|---|
| 35 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"); |
|---|
| [421] | 36 | if(!font) |
|---|
| 37 | exit(-1); |
|---|
| [389] | 38 | } |
|---|
| 39 | |
|---|
| [418] | 40 | /* phpBB captchas have 6 characters */ |
|---|
| 41 | result = malloc(7 * sizeof(char)); |
|---|
| 42 | strcpy(result, " "); |
|---|
| 43 | |
|---|
| [445] | 44 | tmp1 = image_dup(img); |
|---|
| 45 | tmp2 = image_new(img->width, img->height); |
|---|
| [389] | 46 | |
|---|
| [445] | 47 | filter_smooth(tmp1); |
|---|
| [482] | 48 | filter_threshold(tmp1, 128); |
|---|
| [445] | 49 | |
|---|
| [389] | 50 | for(x = 0; x < img->width; x++) |
|---|
| 51 | for(y = 0; y < img->height; y++) |
|---|
| 52 | { |
|---|
| [445] | 53 | getpixel(tmp1, x, y, &r, &g, &b); |
|---|
| [389] | 54 | if(r == 0 && offset == -1) |
|---|
| 55 | offset = x; |
|---|
| 56 | getpixel(img, x, y, &r, &g, &b); |
|---|
| [445] | 57 | setpixel(tmp2, x, y, 255, g, 255); |
|---|
| [389] | 58 | } |
|---|
| 59 | |
|---|
| 60 | for(cur = 0; cur < 6; cur++) |
|---|
| 61 | { |
|---|
| 62 | /* Try to find 1st letter */ |
|---|
| [411] | 63 | distmin = INT_MAX; |
|---|
| [448] | 64 | for(i = 0; i < font->size; i++) |
|---|
| [389] | 65 | { |
|---|
| [415] | 66 | int localmin = INT_MAX, localx, localy; |
|---|
| [448] | 67 | xmin = font->glyphs[i].xmin; |
|---|
| 68 | ymin = font->glyphs[i].ymin; |
|---|
| 69 | xmax = font->glyphs[i].xmax; |
|---|
| 70 | ymax = font->glyphs[i].ymax; |
|---|
| 71 | for(y = 0; y < img->height - (ymax - ymin); y++) |
|---|
| [389] | 72 | { |
|---|
| 73 | x = offset - 3; |
|---|
| 74 | if(cur == 0) |
|---|
| 75 | x -= 10; |
|---|
| 76 | if(x < 0) |
|---|
| 77 | x = 0; |
|---|
| 78 | for(; x < offset + 3; x++) |
|---|
| 79 | { |
|---|
| 80 | int z, t, dist; |
|---|
| 81 | dist = 0; |
|---|
| 82 | for(t = 0; t < ymax - ymin; t++) |
|---|
| 83 | for(z = 0; z < xmax - xmin; z++) |
|---|
| 84 | { |
|---|
| 85 | int r2; |
|---|
| [448] | 86 | getgray(font->img, xmin + z, ymin + t, &r); |
|---|
| [445] | 87 | getgray(tmp1, x + z, y + t, &r2); |
|---|
| [428] | 88 | if(r > r2) |
|---|
| 89 | dist += r - r2; |
|---|
| 90 | else |
|---|
| 91 | dist += (r2 - r) * 3 / 4; |
|---|
| [389] | 92 | } |
|---|
| 93 | if(dist < localmin) |
|---|
| 94 | { |
|---|
| 95 | localmin = dist; |
|---|
| 96 | localx = x; |
|---|
| 97 | localy = y; |
|---|
| 98 | } |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | if(localmin < distmin) |
|---|
| 102 | { |
|---|
| 103 | distmin = localmin; |
|---|
| 104 | distx = localx; |
|---|
| 105 | disty = localy; |
|---|
| 106 | distch = i; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /* Print min glyph (debug) */ |
|---|
| [448] | 111 | xmin = font->glyphs[distch].xmin; |
|---|
| 112 | ymin = font->glyphs[distch].ymin; |
|---|
| 113 | xmax = font->glyphs[distch].xmax; |
|---|
| 114 | ymax = font->glyphs[distch].ymax; |
|---|
| [389] | 115 | for(y = 0; y < ymax - ymin; y++) |
|---|
| 116 | for(x = 0; x < xmax - xmin; x++) |
|---|
| 117 | { |
|---|
| 118 | int r2; |
|---|
| [448] | 119 | getpixel(font->img, xmin + x, ymin + y, &r2, &g, &b); |
|---|
| 120 | if(r2 > 128) |
|---|
| 121 | continue; |
|---|
| [445] | 122 | getpixel(tmp2, distx + x, disty + y, &r, &g, &b); |
|---|
| 123 | setpixel(tmp2, distx + x, disty + y, r2, g, b); |
|---|
| [389] | 124 | } |
|---|
| 125 | |
|---|
| 126 | offset = distx + xmax - xmin; |
|---|
| [448] | 127 | result[cur] = font->glyphs[distch].c; |
|---|
| [389] | 128 | } |
|---|
| 129 | |
|---|
| 130 | image_free(tmp1); |
|---|
| 131 | image_free(tmp2); |
|---|
| 132 | |
|---|
| 133 | return result; |
|---|
| 134 | } |
|---|
| 135 | |
|---|