25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

81 lines
2.1 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. /* Main function */
  19. char *decode_authimage(struct image *img)
  20. {
  21. static struct font *font = NULL;
  22. char *result;
  23. struct image *tmp;
  24. int x, y, r, g, b, i;
  25. if(!font)
  26. {
  27. font = font_load_fixed("font_authimage.png",
  28. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  29. if(!font)
  30. exit(-1);
  31. }
  32. /* authimage captchas have 6 characters */
  33. result = malloc(7 * sizeof(char));
  34. memset(result, '\0', 7);
  35. /* double the captcha size for better accuracy in the rotation */
  36. tmp = image_dup(img);
  37. filter_scale(tmp, 2.0);
  38. getpixel(tmp, 0, 0, &r, &g, &b);
  39. filter_threshold(tmp, r * 3 / 4);
  40. filter_smooth(tmp);
  41. filter_threshold(tmp, 220);
  42. for(i = 0; i < 6; i++)
  43. {
  44. int mindiff = INT_MAX, minch = -1, ch;
  45. for(ch = 0; ch < font->size; ch++)
  46. {
  47. int diff = 0;
  48. for(y = 0; y < 7; y++)
  49. {
  50. for(x = 0; x < 5; x++)
  51. {
  52. int newx, newy, r2;
  53. newx = 35.0 + (x + 6 * i) * 218.0 / 34.0 + y * 5.0 / 6.0 + 0.5;
  54. newy = 33.0 - (x + 6 * i) * 18.0 / 34.0 + y * 42.0 / 6.0 + 0.5;
  55. getpixel(tmp, newx, newy, &r, &g, &b);
  56. getpixel(font->img, x + 6 * ch, y, &r2, &g, &b);
  57. diff += (r - r2) * (r - r2);
  58. }
  59. }
  60. if(diff < mindiff)
  61. {
  62. mindiff = diff;
  63. minch = ch;
  64. }
  65. }
  66. result[i] = font->glyphs[minch].c;
  67. }
  68. image_free(tmp);
  69. return result;
  70. }