vips.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include <stdlib.h>
  2. #include <vips/vips.h>
  3. #include <vips/vips7compat.h>
  4. #define VIPS_SUPPORT_SMARTCROP \
  5. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  6. #define VIPS_SUPPORT_GIF \
  7. VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3)
  8. enum types {
  9. JPEG = 0,
  10. PNG,
  11. WEBP,
  12. GIF
  13. };
  14. int
  15. vips_initialize() {
  16. return vips_init("imgproxy");
  17. }
  18. void
  19. clear_image(VipsImage **in) {
  20. g_clear_object(in);
  21. }
  22. void
  23. g_free_go(void **buf) {
  24. g_free(*buf);
  25. }
  26. VipsAccess
  27. access_mode(int random) {
  28. if (random > 0) return VIPS_ACCESS_RANDOM;
  29. return VIPS_ACCESS_SEQUENTIAL;
  30. }
  31. void
  32. swap_and_clear(VipsImage **in, VipsImage *out) {
  33. clear_image(in);
  34. *in = out;
  35. }
  36. int
  37. vips_type_find_load_go(int imgtype) {
  38. if (imgtype == JPEG) {
  39. return vips_type_find("VipsOperation", "jpegload");
  40. }
  41. if (imgtype == PNG) {
  42. return vips_type_find("VipsOperation", "pngload");
  43. }
  44. if (imgtype == WEBP) {
  45. return vips_type_find("VipsOperation", "webpload");
  46. }
  47. if (imgtype == GIF) {
  48. return vips_type_find("VipsOperation", "gifload");
  49. }
  50. return 0;
  51. }
  52. int
  53. vips_type_find_save_go(int imgtype) {
  54. if (imgtype == JPEG) {
  55. return vips_type_find("VipsOperation", "jpegsave_buffer");
  56. }
  57. if (imgtype == PNG) {
  58. return vips_type_find("VipsOperation", "pngsave_buffer");
  59. }
  60. if (imgtype == WEBP) {
  61. return vips_type_find("VipsOperation", "webpsave_buffer");
  62. }
  63. return 0;
  64. }
  65. int
  66. vips_jpegload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  67. return vips_jpegload_buffer(buf, len, out, "access", access_mode(random), NULL);
  68. }
  69. int
  70. vips_pngload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  71. return vips_pngload_buffer(buf, len, out, "access", access_mode(random), NULL);
  72. }
  73. int
  74. vips_gifload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  75. #if VIPS_SUPPORT_GIF
  76. return vips_gifload_buffer(buf, len, out, "access", access_mode(random), NULL);
  77. #else
  78. return 0;
  79. #endif
  80. }
  81. int
  82. vips_webpload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  83. return vips_webpload_buffer(buf, len, out, "access", access_mode(random), NULL);
  84. }
  85. int
  86. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  87. return vips_resize(in, out, scale, NULL);
  88. }
  89. int
  90. vips_support_smartcrop() {
  91. #if VIPS_SUPPORT_SMARTCROP
  92. return 1;
  93. #else
  94. return 0;
  95. #endif
  96. }
  97. int
  98. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  99. #if VIPS_SUPPORT_SMARTCROP
  100. return vips_smartcrop(in, out, width, height, NULL);
  101. #else
  102. return 0;
  103. #endif
  104. }
  105. int
  106. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation space) {
  107. return vips_colourspace(in, out, space, NULL);
  108. }
  109. int
  110. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  111. return vips_extract_area(in, out, left, top, width, height, NULL);
  112. }
  113. int
  114. vips_process_image(VipsImage **img, int resize, double scale, int crop, int smart, int left, int top, int width, int height) {
  115. VipsImage *tmp;
  116. int err;
  117. if (resize > 0) {
  118. err = vips_resize_go(*img, &tmp, scale);
  119. swap_and_clear(img, tmp);
  120. if (err> 0) { return 1; }
  121. }
  122. if (crop > 0) {
  123. if (smart > 0) {
  124. err = vips_smartcrop_go(*img, &tmp, width, height);
  125. swap_and_clear(img, tmp);
  126. if (err> 0) { return 1; }
  127. } else {
  128. vips_extract_area_go(*img, &tmp, left, top, width, height);
  129. swap_and_clear(img, tmp);
  130. if (err> 0) { return 1; }
  131. }
  132. }
  133. if (vips_image_guess_interpretation(*img) != VIPS_INTERPRETATION_sRGB) {
  134. err = vips_colourspace_go(*img, &tmp, VIPS_INTERPRETATION_sRGB);
  135. swap_and_clear(img, tmp);
  136. }
  137. return err;
  138. }
  139. int
  140. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  141. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  142. }
  143. int
  144. vips_pngsave_go(VipsImage *in, void **buf, size_t *len) {
  145. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
  146. }
  147. int
  148. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  149. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  150. }
  151. void
  152. vips_cleanup() {
  153. vips_thread_shutdown();
  154. vips_error_clear();
  155. }