example4.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <stdio.h>
  2. #include <libcaptcha.h>
  3. /*
  4. * Lets apply some pattern from png image to text
  5. */
  6. lc_bmp *load_pattern(char *filename, int x, int y, int w, int h) {
  7. lc_bmp *img = lc_load_png(filename);
  8. lc_bmp *cropped;
  9. if (!img) {
  10. perror("lc_load_png()");
  11. return NULL;
  12. }
  13. cropped = lc_crop_bmp(img, x, y, w, h);
  14. if (!cropped) {
  15. perror("lc_crop_bmp()");
  16. return NULL;
  17. }
  18. lc_free(img);
  19. return cropped;
  20. }
  21. int main() {
  22. char * str = "usepattern";
  23. char * fontfile = "../ttf/dejavu.ttf";
  24. lc_fontBuffer *font = lc_create_font(fontfile);
  25. lc_arrGlyph *arr;
  26. lc_bmp * text;
  27. lc_bmp * pattern;
  28. lc_bmp * bmp;
  29. if (!font) {
  30. perror("lc_create_font()");
  31. return 1;
  32. }
  33. arr = lc_str_to_arr(font, str, 38, 0);
  34. if (!arr) {
  35. perror("lc_str_to_arr()");
  36. return 1;
  37. }
  38. text = lc_arr_to_bmp(arr);
  39. if (!text) {
  40. perror("lc_arr_to_bmp()");
  41. return 1;
  42. }
  43. pattern = load_pattern("../patterns/96b.png", 300, 20, 240, 80);
  44. /* looks great */
  45. /* lc_save_png("./example4.png", bmp);*/
  46. if (!pattern) {
  47. return 1;
  48. }
  49. bmp = lc_pattern_apply(pattern, text);
  50. if (!bmp) {
  51. perror("lc_pattern_apply()");
  52. return 1;
  53. }
  54. lc_save_png("./example4.png", bmp);
  55. lc_free(text);
  56. lc_free(pattern);
  57. lc_free(arr);
  58. lc_free(bmp);
  59. lc_free(font);
  60. return 0;
  61. }