pattern_texturize.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. lc_bmp * create_glyphs(char *str, int line_height) {
  21. char * fontfile = "../ttf/dejavu.ttf";
  22. lc_fontBuffer *font = lc_create_font(fontfile);
  23. lc_arrGlyph *arr;
  24. lc_bmp *bmp;
  25. if (!font) {
  26. perror("lc_create_font()");
  27. return NULL;
  28. }
  29. arr = lc_str_to_arr(font, str, line_height, 0);
  30. if (!arr) {
  31. perror("lc_str_to_arr()");
  32. return NULL;
  33. }
  34. bmp = lc_create_image(arr);
  35. if (!bmp) {
  36. perror("lc_create_image()");
  37. return NULL;
  38. }
  39. lc_free(font);
  40. lc_free(arr);
  41. return bmp;
  42. }
  43. lc_bmp * create_glyphs_pattern(const char *filename, int x, int y, int w, int h) {
  44. lc_bmp *img = lc_load_png(filename);
  45. lc_bmp *cropped;
  46. if (!img) {
  47. perror("lc_load_png()");
  48. return NULL;
  49. }
  50. cropped = lc_crop_bmp(img, x, y, w, h);
  51. if (!cropped) {
  52. perror("lc_crop_bmp()");
  53. return NULL;
  54. }
  55. lc_free(img);
  56. return cropped;
  57. }
  58. static int texturize_glyphs() {
  59. char * fap = "../patterns/96c.png";
  60. char * str = "(◕‿◕)funnyFACE";
  61. lc_bmp *text = create_glyphs(str, 30);
  62. lc_bmp *gpt;
  63. lc_bmp *gpat;
  64. if (!text) {
  65. return 1;
  66. }
  67. gpt = create_glyphs_pattern(fap, 200, 80, 230, 120);
  68. if (!gpt) {
  69. return 1;
  70. }
  71. gpat = lc_pattern_apply(gpt, text);
  72. if (!gpat) {
  73. perror("lc_pattern_apply()");
  74. return 1;
  75. }
  76. /* lc_save_png("/tmp/glyph.png", gpat);*/
  77. /* lc_save_png("/tmp/glyph.png", text);*/
  78. lc_free(text);
  79. lc_free(gpat);
  80. lc_free(gpt);
  81. return 0;
  82. }
  83. static int texturize_background() {
  84. char * fap = "../patterns/96b.png";
  85. char * fbp = "../patterns/96b.png";
  86. char * str = "LIBCAPTCHA";
  87. lc_bmp *text = create_glyphs(str, 30);
  88. lc_bmp *bg;
  89. lc_bmp *gpt;
  90. lc_bmp *gpat;
  91. lc_bmp *cp;
  92. if (!text) {
  93. return 1;
  94. }
  95. gpt = create_glyphs_pattern(fap, 400, 200, 200, 120);
  96. if (!gpt) {
  97. return 1;
  98. }
  99. bg = create_glyphs_pattern(fbp, 20, 180, 180, 60);
  100. if (!bg) {
  101. return 1;
  102. }
  103. gpat = lc_pattern_apply(gpt, text);
  104. if (!gpat) {
  105. perror("lc_pattern_apply()");
  106. return 1;
  107. }
  108. cp = lc_pattern_merge(bg, gpat, 24, 20);
  109. /* lc_save_png("/tmp/glyph.png", gpat);*/
  110. /* lc_save_png("/tmp/glyph.png", bg);*/
  111. /* lc_save_png("/tmp/glyph.png", cp);*/
  112. lc_free(cp);
  113. lc_free(text);
  114. lc_free(gpat);
  115. lc_free(bg);
  116. lc_free(gpt);
  117. return 0;
  118. }
  119. int main() {
  120. assert(!texturize_glyphs());
  121. assert(!texturize_background());
  122. return 0;
  123. }