example5.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <stdio.h>
  2. #include <libcaptcha.h>
  3. lc_bmp *load_pattern(char *filename, int x, int y, int w, int h) {
  4. lc_bmp *img = lc_load_png(filename);
  5. lc_bmp *cropped;
  6. if (!img) {
  7. perror("lc_load_png()");
  8. return NULL;
  9. }
  10. cropped = lc_crop_bmp(img, x, y, w, h);
  11. if (!cropped) {
  12. perror("lc_crop_bmp()");
  13. return NULL;
  14. }
  15. lc_free(img);
  16. return cropped;
  17. }
  18. int main() {
  19. char * str = "withpattern";
  20. char * fontfile = "../ttf/dejavu.ttf";
  21. lc_fontBuffer *font = lc_create_font(fontfile);
  22. lc_arrGlyph *arr;
  23. lc_bmp * text;
  24. lc_bmp * pattern;
  25. lc_bmp * bg;
  26. lc_bmp * bmp;
  27. lc_bmp * captcha;
  28. if (!font) {
  29. perror("lc_create_font()");
  30. return 1;
  31. }
  32. arr = lc_str_to_arr(font, str, 50, 70);
  33. if (!arr) {
  34. perror("lc_str_to_arr()");
  35. return 1;
  36. }
  37. lc_randomize_arr_x(arr, 10);
  38. lc_randomize_arr_y(arr, 10);
  39. text = lc_arr_to_bmp(arr);
  40. if (!text) {
  41. perror("lc_arr_to_bmp()");
  42. return 1;
  43. }
  44. pattern = load_pattern("../patterns/96b.png", 400, 200, text->w, text->h);
  45. /* looks great */
  46. /* lc_save_png("./example4.png", bmp);*/
  47. if (!pattern) {
  48. return 1;
  49. }
  50. bg = load_pattern("../patterns/96b.png", 300, 20, pattern->w + 20, pattern->h + 20);
  51. if (!bg) {
  52. return 1;
  53. }
  54. bmp = lc_pattern_apply(pattern, text);
  55. if (!bmp) {
  56. perror("lc_pattern_apply()");
  57. return 1;
  58. }
  59. captcha = lc_pattern_merge(bg, bmp, 10, 10);
  60. if (!captcha) {
  61. perror("lc_pattern_merge()");
  62. return 1;
  63. }
  64. lc_save_png("./example5.png", captcha);
  65. lc_free(captcha);
  66. lc_free(bg);
  67. lc_free(text);
  68. lc_free(pattern);
  69. lc_free(arr);
  70. lc_free(bmp);
  71. lc_free(font);
  72. return 0;
  73. }