image_basic.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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_create_image(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_arrGlyph *narr = lc_randomize_arr_rotation(arr, 320);
  59. /* lc_arrGlyph *narr = arr;*/
  60. if (!narr) {
  61. perror("lc_randomize_arr_rotation()");
  62. return 1;
  63. }
  64. lc_randomize_arr_x(narr, 10);
  65. lc_randomize_arr_y(narr, 20);
  66. bmp = lc_create_image(narr);
  67. /* stbi_write_png("/tmp/glyph.png", bmp->w, bmp->h, 1, bmp->buffer, bmp->w);*/
  68. lc_free(bmp);
  69. lc_free(arr);
  70. lc_free(narr);
  71. lc_free(font);
  72. return 0;
  73. }
  74. static int create_randomized_x() {
  75. char * str = "randomiz";
  76. char * fontfile = "../ttf/dejavu.ttf";
  77. lc_fontBuffer *font = lc_create_font(fontfile);
  78. lc_arrGlyph *arr;
  79. lc_bmp *bmp;
  80. if (!font) {
  81. perror("lc_create_font()");
  82. return 1;
  83. }
  84. arr = lc_str_to_arr(font, str, 40, 0);
  85. if (!arr) {
  86. perror("lc_str_to_arr()");
  87. return 1;
  88. }
  89. lc_randomize_arr_x(arr, 30);
  90. /* lc_randomize_arr_y(arr, 40);*/
  91. bmp = lc_create_image(arr);
  92. /* stbi_write_png("/tmp/glyph.png", bmp->w, bmp->h, 1, bmp->buffer, bmp->w);*/
  93. lc_free(bmp);
  94. lc_free(arr);
  95. lc_free(font);
  96. return 0;
  97. }
  98. int main() {
  99. assert(!create_bmp());
  100. assert(!create_randomized_bmp());
  101. assert(!create_randomized_x());
  102. return 0;
  103. }