image_basic.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. * Author: g0tsu
  12. * Email: g0tsu at dnmx.0rg
  13. */
  14. #include <stdio.h>
  15. #include <assert.h>
  16. #include <libcaptcha.h>
  17. #include <errno.h>
  18. #define STB_IMAGE_WRITE_IMPLEMENTATION
  19. #include "stb_image_write.h"
  20. static int create_bmp() {
  21. char * str = "😒 test WTF??";
  22. char * fontfile = "../ttf/dejavu.ttf";
  23. lc_fontBuffer *font = lc_create_font(fontfile);
  24. lc_arrGlyph *arr;
  25. lc_bmp *bmp;
  26. if (!font) {
  27. perror("lc_create_font()");
  28. return 1;
  29. }
  30. arr = lc_str_to_arr(font, str, 50, 0);
  31. if (!arr) {
  32. perror("lc_str_to_arr()");
  33. return 1;
  34. }
  35. bmp = lc_arr_to_bmp(arr);
  36. /* stbi_write_png("/tmp/glyph.png", bmp->w, bmp->h, 1, bmp->buffer, bmp->w);*/
  37. lc_free(bmp);
  38. lc_free(arr);
  39. lc_free(font);
  40. return 0;
  41. }
  42. static int create_randomized_bmp() {
  43. char * str = "LIBCAPTCHA";
  44. char * fontfile = "../ttf/dejavu.ttf";
  45. /* char * fontfile = "../ttf/cmunrm/cmunrm.ttf";*/
  46. lc_fontBuffer *font = lc_create_font(fontfile);
  47. lc_arrGlyph *arr;
  48. lc_bmp *bmp;
  49. if (!font) {
  50. perror("lc_create_font()");
  51. return 1;
  52. }
  53. arr = lc_str_to_arr(font, str, 40, 70);
  54. if (!arr) {
  55. perror("lc_str_to_arr()");
  56. return 1;
  57. }
  58. lc_randomize_arr_rotation_180(arr);
  59. lc_randomize_arr_x(arr, 10);
  60. lc_randomize_arr_y(arr, 20);
  61. bmp = lc_arr_to_bmp(arr);
  62. /* lc_save_png("/tmp/out.png", bmp);*/
  63. lc_free(bmp);
  64. lc_free(arr);
  65. lc_free(font);
  66. return 0;
  67. }
  68. static int create_randomized_x() {
  69. char * str = "randomiz";
  70. char * fontfile = "../ttf/dejavu.ttf";
  71. lc_fontBuffer *font = lc_create_font(fontfile);
  72. lc_arrGlyph *arr;
  73. lc_bmp *bmp;
  74. if (!font) {
  75. perror("lc_create_font()");
  76. return 1;
  77. }
  78. arr = lc_str_to_arr(font, str, 40, 0);
  79. if (!arr) {
  80. perror("lc_str_to_arr()");
  81. return 1;
  82. }
  83. lc_randomize_arr_x(arr, 30);
  84. /* lc_randomize_arr_y(arr, 40);*/
  85. bmp = lc_arr_to_bmp(arr);
  86. /* stbi_write_png("/tmp/glyph.png", bmp->w, bmp->h, 1, bmp->buffer, bmp->w);*/
  87. lc_free(bmp);
  88. lc_free(arr);
  89. lc_free(font);
  90. return 0;
  91. }
  92. int main() {
  93. assert(!create_bmp());
  94. assert(!create_randomized_bmp());
  95. assert(!create_randomized_x());
  96. return 0;
  97. }