選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

88 行
2.3 KiB

  1. /*
  2. * authimage.c: decode authimage 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 <math.h>
  16. #include "config.h"
  17. #include "common.h"
  18. #define FONTNAME "font_authimage.png"
  19. static struct image *font = NULL;
  20. /* Main function */
  21. char *decode_authimage(struct image *img)
  22. {
  23. char *all = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  24. char *result;
  25. struct image *tmp;
  26. int x, y, r, g, b, i;
  27. if(!font)
  28. {
  29. char fontname[BUFSIZ];
  30. sprintf(fontname, "%s/%s", share, FONTNAME);
  31. font = image_load(fontname);
  32. if(!font)
  33. {
  34. fprintf(stderr, "cannot load font %s\n", fontname);
  35. exit(-1);
  36. }
  37. }
  38. /* authimage captchas have 6 characters */
  39. result = malloc(7 * sizeof(char));
  40. memset(result, '\0', 7);
  41. /* half the captchas are inverse video; we set them back to normal */
  42. tmp = image_dup(img);
  43. filter_scale(tmp, 2.0);
  44. getpixel(tmp, 0, 0, &r, &g, &b);
  45. filter_equalize(tmp, r * 3 / 4);
  46. filter_smooth(tmp);
  47. for(i = 0; i < 6; i++)
  48. {
  49. int mindiff = INT_MAX, minch = -1, ch;
  50. for(ch = 0; ch < 36; ch++)
  51. {
  52. int diff = 0;
  53. for(y = 0; y < 7; y++)
  54. {
  55. for(x = 0; x < 5; x++)
  56. {
  57. int newx, newy, r2;
  58. newx = 35.0 + (x + 6 * i) * 218.0 / 34.0 + y * 5.0 / 6.0 + 0.5;
  59. newy = 33.0 - (x + 6 * i) * 18.0 / 34.0 + y * 42.0 / 6.0 + 0.5;
  60. getpixel(tmp, newx, newy, &r, &g, &b);
  61. getpixel(font, x + 6 * ch, y, &r2, &g, &b);
  62. r = (r < 220) ? 0 : 255;
  63. diff += (r - r2) * (r - r2);
  64. }
  65. }
  66. if(diff < mindiff)
  67. {
  68. mindiff = diff;
  69. minch = ch;
  70. }
  71. }
  72. result[i] = all[minch];
  73. }
  74. image_free(tmp);
  75. return result;
  76. }