vips.h 4.1 KB

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