Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

101 wiersze
2.8 KiB

  1. /*
  2. * tickets.c: decode tickets.com 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. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <limits.h>
  15. #include "config.h"
  16. #include "common.h"
  17. #define FONTS 8
  18. /* Main function */
  19. char *decode_tickets(struct image *img)
  20. {
  21. static struct font *fonts[FONTS];
  22. char *result;
  23. struct image *tmp;
  24. int x, y, r, g, b, cur, i, j, f;
  25. int curx, bestx, besty;
  26. for(i = 0; i < FONTS; i++)
  27. {
  28. if(!fonts[i])
  29. {
  30. char buf[BUFSIZ];
  31. sprintf(buf, "font_tickets%i.png", i + 1);
  32. fonts[i] = font_load_variable(buf, "0123456789");
  33. if(!fonts[i])
  34. exit(-1);
  35. }
  36. }
  37. /* tickets.com captchas have 7 characters */
  38. result = malloc(8 * sizeof(char));
  39. strcpy(result, " ");
  40. /* captcha is jpeg; threshold the image */
  41. tmp = image_dup(img);
  42. filter_equalize(tmp, 127);
  43. /* Guess all glyphs */
  44. curx = 50;
  45. for(cur = 0; cur < 7; cur++)
  46. {
  47. for(f = 0; f < FONTS; f++)
  48. {
  49. for(x = curx - 5; x < curx + 10; x++)
  50. {
  51. for(y = 5; y < 15; y++)
  52. {
  53. for(i = 0; i < fonts[f]->size; i++)
  54. {
  55. int xmin, xmax, ymin, ymax;
  56. int t, z;
  57. xmin = fonts[f]->glyphs[i].xmin;
  58. ymin = fonts[f]->glyphs[i].ymin;
  59. xmax = fonts[f]->glyphs[i].xmax;
  60. ymax = fonts[f]->glyphs[i].ymax;
  61. for(t = 0; t < ymax - ymin; t++)
  62. {
  63. for(z = 0; z < xmax - xmin; z++)
  64. {
  65. int r, r2;
  66. getgray(fonts[f]->img, xmin + z, ymin + t, &r);
  67. getgray(tmp, x + z, y + t, &r2);
  68. if(r < 127 && r2 > 127)
  69. goto char_failed;
  70. }
  71. }
  72. goto char_ok;
  73. char_failed:
  74. continue;
  75. }
  76. }
  77. }
  78. }
  79. result[cur] = '?';
  80. curx += 10; /* XXX: totally random */
  81. continue;
  82. char_ok:
  83. result[cur] = fonts[f]->glyphs[i].c;
  84. curx = x + fonts[f]->glyphs[i].xmax - fonts[f]->glyphs[i].xmin;
  85. }
  86. image_free(tmp);
  87. return result;
  88. }