| 1 | /* |
|---|
| 2 | * slashdot.c: decode Slashdot captchas |
|---|
| 3 | * $Id$ |
|---|
| 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 | static void count_objects(struct image *img); |
|---|
| 22 | static void rotate(struct image *img); |
|---|
| 23 | static void cut_cells(struct image *img); |
|---|
| 24 | static void find_glyphs(struct image *img); |
|---|
| 25 | |
|---|
| 26 | /* Our macros */ |
|---|
| 27 | #define FONTNAME "font_slashdot.png" |
|---|
| 28 | |
|---|
| 29 | struct font font; |
|---|
| 30 | struct glyph glyphs[22]; |
|---|
| 31 | |
|---|
| 32 | /* Global stuff */ |
|---|
| 33 | struct { int xmin, ymin, xmax, ymax; } objlist[100]; |
|---|
| 34 | int objects, first, last; |
|---|
| 35 | char *result; |
|---|
| 36 | |
|---|
| 37 | /* Main function */ |
|---|
| 38 | char *decode_slashdot(struct image *img) |
|---|
| 39 | { |
|---|
| 40 | struct image *tmp1, *tmp2; |
|---|
| 41 | |
|---|
| 42 | /* Initialise local data */ |
|---|
| 43 | objects = 0; |
|---|
| 44 | first = -1; |
|---|
| 45 | last = -1; |
|---|
| 46 | |
|---|
| 47 | /* Slashdot captchas have 7 characters */ |
|---|
| 48 | result = malloc(8 * sizeof(char)); |
|---|
| 49 | strcpy(result, " "); |
|---|
| 50 | |
|---|
| 51 | /* Clean image a bit */ |
|---|
| 52 | tmp1 = image_dup(img); |
|---|
| 53 | filter_detect_lines(tmp1); |
|---|
| 54 | filter_fill_holes(tmp1); |
|---|
| 55 | |
|---|
| 56 | /* Detect small objects to guess image orientation */ |
|---|
| 57 | tmp2 = image_dup(tmp1); |
|---|
| 58 | filter_median(tmp2); |
|---|
| 59 | filter_equalize(tmp2, 200); |
|---|
| 60 | count_objects(tmp2); |
|---|
| 61 | |
|---|
| 62 | /* Invert rotation and find glyphs */ |
|---|
| 63 | rotate(tmp1); |
|---|
| 64 | filter_median(tmp1); |
|---|
| 65 | find_glyphs(tmp1); |
|---|
| 66 | |
|---|
| 67 | /* Clean up our mess */ |
|---|
| 68 | image_free(tmp1); |
|---|
| 69 | image_free(tmp2); |
|---|
| 70 | |
|---|
| 71 | /* aaaaaaa means decoding failed */ |
|---|
| 72 | if(!strcmp(result, "aaaaaaa")) |
|---|
| 73 | result[0] = '\0'; |
|---|
| 74 | |
|---|
| 75 | return result; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | /* The following functions are local */ |
|---|
| 79 | |
|---|
| 80 | static void count_objects(struct image *img) |
|---|
| 81 | { |
|---|
| 82 | struct image *tmp; |
|---|
| 83 | int gotblack = 1; |
|---|
| 84 | int x, y, i; |
|---|
| 85 | int r, g, b; |
|---|
| 86 | |
|---|
| 87 | tmp = image_new(img->width, img->height); |
|---|
| 88 | |
|---|
| 89 | for(y = 0; y < img->height; y++) |
|---|
| 90 | for(x = 0; x < img->width; x++) |
|---|
| 91 | { |
|---|
| 92 | getpixel(img, x, y, &r, &g, &b); |
|---|
| 93 | setpixel(tmp, x, y, r, g, b); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | while(gotblack) |
|---|
| 97 | { |
|---|
| 98 | gotblack = 0; |
|---|
| 99 | for(y = 0; y < tmp->height; y++) |
|---|
| 100 | for(x = 0; x < tmp->width; x++) |
|---|
| 101 | { |
|---|
| 102 | getpixel(tmp, x, y, &r, &g, &b); |
|---|
| 103 | if(r == 0 && g == 0 && b == 0) |
|---|
| 104 | { |
|---|
| 105 | gotblack = 1; |
|---|
| 106 | filter_flood_fill(tmp, x, y, 254 - objects, 0, 0); |
|---|
| 107 | objects++; |
|---|
| 108 | } |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | //printf("%i objects\n", objects); |
|---|
| 113 | |
|---|
| 114 | for(i = 0; i < objects; i++) |
|---|
| 115 | { |
|---|
| 116 | objlist[i].ymin = tmp->height; |
|---|
| 117 | objlist[i].ymax = 0; |
|---|
| 118 | |
|---|
| 119 | for(y = 0; y < tmp->height; y++) |
|---|
| 120 | for(x = 0; x < tmp->width; x++) |
|---|
| 121 | { |
|---|
| 122 | getpixel(tmp, x, y, &r, &g, &b); |
|---|
| 123 | if(r == 255 - i && g == 0 && b == 0) |
|---|
| 124 | { |
|---|
| 125 | if(y < objlist[i].ymin) { objlist[i].ymin = y; objlist[i].xmin = x; } |
|---|
| 126 | if(y > objlist[i].ymax) { objlist[i].ymax = y; objlist[i].xmax = x; } |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | //printf("y min-max: %i %i (size %i)\n", objlist[i].ymin, objlist[i].ymax, objlist[i].ymax - objlist[i].ymin + 1); |
|---|
| 130 | if(objlist[i].ymax - objlist[i].ymin > 18 && objlist[i].ymax - objlist[i].ymin < 27) |
|---|
| 131 | { |
|---|
| 132 | if(first == -1) |
|---|
| 133 | first = i; |
|---|
| 134 | last = i; |
|---|
| 135 | filter_flood_fill(tmp, objlist[i].xmin, objlist[i].ymin, 0, 0, 255); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | #if 0 |
|---|
| 140 | { CvPoint A, B; |
|---|
| 141 | A.x = (objlist[first].xmin + objlist[first].xmax) / 2; |
|---|
| 142 | A.y = (objlist[first].ymin + objlist[first].ymax) / 2; |
|---|
| 143 | B.x = (objlist[last].xmin + objlist[last].xmax) / 2; |
|---|
| 144 | B.y = (objlist[last].ymin + objlist[last].ymax) / 2; |
|---|
| 145 | cvLine(tmp, A, B, 0, 2.0, 0); |
|---|
| 146 | } |
|---|
| 147 | #endif |
|---|
| 148 | |
|---|
| 149 | image_swap(img, tmp); |
|---|
| 150 | image_free(tmp); |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | static void rotate(struct image *img) |
|---|
| 154 | { |
|---|
| 155 | struct image *tmp; |
|---|
| 156 | int x, y, xdest, ydest; |
|---|
| 157 | int r, g, b; |
|---|
| 158 | //int R, G, B; |
|---|
| 159 | int X = objlist[first].xmin - objlist[last].xmin; |
|---|
| 160 | int Y = objlist[first].ymin - objlist[last].ymin; |
|---|
| 161 | float xtmp, ytmp; |
|---|
| 162 | float sina = (1.0 * Y) / sqrt(1.0 * X * X + Y * Y); |
|---|
| 163 | float cosa = (1.0 * X) / sqrt(1.0 * X * X + Y * Y); |
|---|
| 164 | if(sina * cosa > 0) |
|---|
| 165 | { |
|---|
| 166 | sina = -sina; |
|---|
| 167 | cosa = -cosa; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | tmp = image_new(img->width, img->height); |
|---|
| 171 | |
|---|
| 172 | for(y = 0; y < img->height; y++) |
|---|
| 173 | for(x = 0; x < img->width; x++) |
|---|
| 174 | { |
|---|
| 175 | xtmp = 1.0 * (x - img->width / 2); |
|---|
| 176 | ytmp = 1.0 * (y - img->height / 2); |
|---|
| 177 | xdest = xtmp * cosa - ytmp * sina + 0.5 * img->width; |
|---|
| 178 | ydest = ytmp * cosa + xtmp * sina + 0.5 * img->height; |
|---|
| 179 | //R = G = B = 0; |
|---|
| 180 | getpixel(img, xdest, ydest, &r, &g, &b); |
|---|
| 181 | //R += r; G += g; B += b; |
|---|
| 182 | //getpixel(img, xdest+1, ydest, &r, &g, &b); |
|---|
| 183 | //R += r; G += g; B += b; |
|---|
| 184 | //getpixel(img, xdest, ydest+1, &r, &g, &b); |
|---|
| 185 | //R += r; G += g; B += b; |
|---|
| 186 | //getpixel(img, xdest+1, ydest+1, &r, &g, &b); |
|---|
| 187 | //R += r; G += g; B += b; |
|---|
| 188 | //r = R / 4; g = G / 4; b = B / 4; |
|---|
| 189 | if(r == 255 && g == 0 && b == 255) |
|---|
| 190 | g = 255; |
|---|
| 191 | setpixel(tmp, x, y, r, g, b); |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | image_swap(img, tmp); |
|---|
| 195 | image_free(tmp); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | static void cut_cells(struct image *img) |
|---|
| 199 | { |
|---|
| 200 | struct image *tmp; |
|---|
| 201 | int x, y; |
|---|
| 202 | int r, g, b; |
|---|
| 203 | |
|---|
| 204 | tmp = image_new(img->width, img->height); |
|---|
| 205 | |
|---|
| 206 | for(y = 0; y < img->height; y++) |
|---|
| 207 | for(x = 0; x < img->width; x++) |
|---|
| 208 | { |
|---|
| 209 | getpixel(img, x, y, &r, &g, &b); |
|---|
| 210 | setpixel(tmp, x, y, r, g, b); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | for(x = 0; x < img->width; x++) |
|---|
| 214 | { |
|---|
| 215 | setpixel(tmp, x, 0, 255, 255, 255); |
|---|
| 216 | setpixel(tmp, x, img->height - 1, 255, 255, 255); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | for(y = 0; y < img->height; y++) |
|---|
| 220 | for(x = 0; x < 7; x++) |
|---|
| 221 | { |
|---|
| 222 | setpixel(tmp, x * img->width / 7, y, 255, 255, 255); |
|---|
| 223 | setpixel(tmp, (x + 1) * img->width / 7 - 1, y, 255, 255, 255); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | image_swap(img, tmp); |
|---|
| 227 | image_free(tmp); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | static void find_glyphs(struct image *img) |
|---|
| 231 | { |
|---|
| 232 | char all[] = "abcdefgijkmnpqrstvwxyz"; |
|---|
| 233 | struct image *tmp; |
|---|
| 234 | int x, y, i = 0; |
|---|
| 235 | int r, g, b; |
|---|
| 236 | int xmin, xmax, ymin, ymax, incell = 0, count = 0, startx = 0, cur = 0; |
|---|
| 237 | int distmin, distx, disty, distch; |
|---|
| 238 | |
|---|
| 239 | if(!font.img) |
|---|
| 240 | { |
|---|
| 241 | char fontname[BUFSIZ]; |
|---|
| 242 | sprintf(fontname, "%s/%s", share, FONTNAME); |
|---|
| 243 | font.img = image_load(fontname); |
|---|
| 244 | if(!font.img) |
|---|
| 245 | { |
|---|
| 246 | fprintf(stderr, "cannot load font %s\n", fontname); |
|---|
| 247 | exit(-1); |
|---|
| 248 | } |
|---|
| 249 | font.glyphs = glyphs; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | tmp = image_new(img->width, img->height); |
|---|
| 253 | |
|---|
| 254 | for(y = 0; y < img->height; y++) |
|---|
| 255 | for(x = 0; x < img->width; x++) |
|---|
| 256 | { |
|---|
| 257 | getpixel(img, x, y, &r, &g, &b); |
|---|
| 258 | setpixel(tmp, x, y, 255, g, 255); |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | for(x = 0; x < font.img->width; x++) |
|---|
| 262 | { |
|---|
| 263 | int found = 0; |
|---|
| 264 | for(y = 0; y < font.img->height; y++) |
|---|
| 265 | { |
|---|
| 266 | getpixel(font.img, x, y, &r, &g, &b); |
|---|
| 267 | if(r < 128) |
|---|
| 268 | { |
|---|
| 269 | found = 1; |
|---|
| 270 | count += (255 - r); |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | if(found && !incell) |
|---|
| 274 | { |
|---|
| 275 | incell = 1; |
|---|
| 276 | xmin = x; |
|---|
| 277 | } |
|---|
| 278 | else if(!found && incell) |
|---|
| 279 | { |
|---|
| 280 | incell = 0; |
|---|
| 281 | xmax = x; |
|---|
| 282 | #if 0 |
|---|
| 283 | ymin = font.img->height; |
|---|
| 284 | ymax = 0; |
|---|
| 285 | for(y = 0; y < font.img->height; y++) |
|---|
| 286 | { |
|---|
| 287 | int newx; |
|---|
| 288 | int gotit = 0; |
|---|
| 289 | for(newx = xmin; newx < xmax; newx++) |
|---|
| 290 | { |
|---|
| 291 | getpixel(font.img, newx, y, &r, &g, &b); |
|---|
| 292 | if(r < 128) |
|---|
| 293 | { |
|---|
| 294 | gotit = 1; |
|---|
| 295 | break; |
|---|
| 296 | } |
|---|
| 297 | } |
|---|
| 298 | if(gotit) |
|---|
| 299 | { |
|---|
| 300 | if(ymin > y) ymin = y; |
|---|
| 301 | if(ymax <= y) ymax = y + 1; |
|---|
| 302 | } |
|---|
| 303 | } |
|---|
| 304 | #else |
|---|
| 305 | ymin = 0; |
|---|
| 306 | ymax = font.img->height; |
|---|
| 307 | #endif |
|---|
| 308 | font.glyphs[i].xmin = xmin; |
|---|
| 309 | font.glyphs[i].xmax = xmax; |
|---|
| 310 | font.glyphs[i].ymin = ymin; |
|---|
| 311 | font.glyphs[i].ymax = ymax; |
|---|
| 312 | font.glyphs[i].count = count; |
|---|
| 313 | count = 0; |
|---|
| 314 | i++; |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | if(i != 22) |
|---|
| 319 | { |
|---|
| 320 | printf("error: could not find 22 glyphs in font\n"); |
|---|
| 321 | exit(-1); |
|---|
| 322 | } |
|---|
| 323 | |
|---|
| 324 | while(cur < 7) |
|---|
| 325 | { |
|---|
| 326 | /* Try to find 1st letter */ |
|---|
| 327 | distmin = INT_MAX; |
|---|
| 328 | for(i = 0; i < 22; i++) |
|---|
| 329 | { |
|---|
| 330 | int localmin = INT_MAX, localx, localy; |
|---|
| 331 | //if(all[i] == 'i') continue; |
|---|
| 332 | xmin = font.glyphs[i].xmin; |
|---|
| 333 | ymin = font.glyphs[i].ymin; |
|---|
| 334 | xmax = font.glyphs[i].xmax; |
|---|
| 335 | ymax = font.glyphs[i].ymax; |
|---|
| 336 | //printf("trying to find %c (%i×%i) - ", all[i], xmax - xmin, ymax - ymin); |
|---|
| 337 | for(y = -5; y < 5; y++) |
|---|
| 338 | for(x = startx - 5; x < startx + 5; x++) |
|---|
| 339 | { |
|---|
| 340 | int z, t, dist; |
|---|
| 341 | dist = 0; |
|---|
| 342 | for(t = 0; t < ymax - ymin; t++) |
|---|
| 343 | for(z = 0; z < xmax - xmin; z++) |
|---|
| 344 | { |
|---|
| 345 | int r2; |
|---|
| 346 | getgray(font.img, xmin + z, ymin + t, &r); |
|---|
| 347 | getgray(img, x + z, y + t, &r2); |
|---|
| 348 | dist += abs(r - r2); |
|---|
| 349 | } |
|---|
| 350 | // printf("%i %i -> %i\n", x, y, dist); |
|---|
| 351 | //dist /= sqrt(xmax - xmin); |
|---|
| 352 | dist = dist * 128 / font.glyphs[i].count; |
|---|
| 353 | if(dist < localmin) |
|---|
| 354 | { |
|---|
| 355 | localmin = dist; |
|---|
| 356 | localx = x; |
|---|
| 357 | localy = y; |
|---|
| 358 | } |
|---|
| 359 | } |
|---|
| 360 | //fprintf(stderr, "%i (%i,%i)\n", localmin, localx - startx, localy); |
|---|
| 361 | if(localmin < distmin) |
|---|
| 362 | { |
|---|
| 363 | distmin = localmin; |
|---|
| 364 | distx = localx; |
|---|
| 365 | disty = localy; |
|---|
| 366 | distch = i; |
|---|
| 367 | } |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | //fprintf(stderr, "%i (%i,%i)\n", distmin, distx - startx, disty); |
|---|
| 371 | //printf("min diff: %c - %i (%i, %i)\n", all[distch], distmin, distx, disty); |
|---|
| 372 | |
|---|
| 373 | /* Print min glyph */ |
|---|
| 374 | xmin = font.glyphs[distch].xmin; |
|---|
| 375 | ymin = font.glyphs[distch].ymin; |
|---|
| 376 | xmax = font.glyphs[distch].xmax; |
|---|
| 377 | ymax = font.glyphs[distch].ymax; |
|---|
| 378 | for(y = 0; y < ymax - ymin; y++) |
|---|
| 379 | for(x = 0; x < xmax - xmin; x++) |
|---|
| 380 | { |
|---|
| 381 | getpixel(font.img, xmin + x, ymin + y, &r, &g, &b); |
|---|
| 382 | if(r > 128) continue; |
|---|
| 383 | setpixel(tmp, distx + x, disty + y, r, g, b); |
|---|
| 384 | } |
|---|
| 385 | |
|---|
| 386 | startx = distx + xmax - xmin; |
|---|
| 387 | result[cur++] = all[distch]; |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | image_swap(img, tmp); |
|---|
| 391 | image_free(tmp); |
|---|
| 392 | } |
|---|
| 393 | |
|---|