Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

128 rindas
3.5 KiB

  1. /*
  2. * lmt.c: decode lmt.lv 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. static void find_glyphs(struct image *img);
  18. /* Our macros */
  19. char *result;
  20. /* Main function */
  21. char *decode_lmt(struct image *img)
  22. {
  23. struct image *tmp;
  24. /* lmt captchas have 3 characters */
  25. result = malloc(4 * sizeof(char));
  26. strcpy(result, " ");
  27. tmp = image_dup(img);
  28. filter_contrast(tmp);
  29. filter_black_stuff(tmp);
  30. find_glyphs(tmp);
  31. image_free(tmp);
  32. return result;
  33. }
  34. static void find_glyphs(struct image *img)
  35. {
  36. #define DELTA 2
  37. static struct font *font;
  38. int x, y, i = 0;
  39. int r, g, b;
  40. int xmin, xmax, ymin, ymax, startx = 0, cur = 0;
  41. int bestdist, bestx, besty, bestch;
  42. if(!font)
  43. {
  44. font = font_load_variable("font_freesans_24_09AZ.bmp",
  45. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  46. if(!font)
  47. exit(1);
  48. }
  49. while(cur < 3)
  50. {
  51. /* Try to find 1st letter */
  52. bestdist = INT_MAX;
  53. for(i = 0; i < font->size; i++)
  54. {
  55. int localmin = INT_MAX, localx, localy;
  56. xmin = font->glyphs[i].xmin - DELTA;
  57. ymin = font->glyphs[i].ymin;
  58. xmax = font->glyphs[i].xmax + DELTA;
  59. ymax = font->glyphs[i].ymax;
  60. for(y = -5; y < 5; y++)
  61. {
  62. for(x = startx; x < startx + 15; x++)
  63. {
  64. int z, t, dist;
  65. dist = 0;
  66. for(t = 0; t < ymax - ymin; t++)
  67. for(z = 0; z < xmax - xmin; z++)
  68. {
  69. int r2;
  70. getgray(font->img, xmin + z, ymin + t, &r);
  71. getgray(img, x + z, y + t, &r2);
  72. dist += (r - r2) * (r - r2);
  73. }
  74. dist = dist / (xmax - xmin - 2 * DELTA);
  75. if(dist < localmin)
  76. {
  77. localmin = dist;
  78. localx = x;
  79. localy = y;
  80. }
  81. }
  82. }
  83. if(localmin < bestdist)
  84. {
  85. bestdist = localmin;
  86. bestx = localx;
  87. besty = localy;
  88. bestch = i;
  89. }
  90. }
  91. /* Print min glyph */
  92. #if 0
  93. xmin = font->glyphs[bestch].xmin - DELTA;
  94. ymin = font->glyphs[bestch].ymin;
  95. xmax = font->glyphs[bestch].xmax + DELTA;
  96. ymax = font->glyphs[bestch].ymax;
  97. for(y = 0; y < ymax - ymin; y++)
  98. for(x = 0; x < xmax - xmin; x++)
  99. {
  100. getpixel(font->img, xmin + x, ymin + y, &r, &g, &b);
  101. if(r > 128)
  102. {
  103. getpixel(img, bestx + x, besty + y, &r, &g, &b);
  104. r = 255;
  105. }
  106. setpixel(img, bestx + x, besty + y, r, g, b);
  107. }
  108. #endif
  109. startx = bestx + font->glyphs[bestch].xmax - font->glyphs[bestch].xmin;
  110. result[cur++] = font->glyphs[bestch].c;
  111. }
  112. }