您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

141 行
4.1 KiB

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