vips.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  9. enum types {
  10. JPEG = 0,
  11. PNG,
  12. WEBP,
  13. GIF
  14. };
  15. int
  16. vips_initialize() {
  17. return vips_init("imgproxy");
  18. }
  19. void
  20. clear_image(VipsImage **in) {
  21. if (G_IS_OBJECT(*in)) g_clear_object(in);
  22. }
  23. void
  24. g_free_go(void **buf) {
  25. g_free(*buf);
  26. }
  27. VipsAccess
  28. access_mode(int random) {
  29. if (random > 0) return VIPS_ACCESS_RANDOM;
  30. return VIPS_ACCESS_SEQUENTIAL;
  31. }
  32. void
  33. swap_and_clear(VipsImage **in, VipsImage *out) {
  34. clear_image(in);
  35. *in = out;
  36. }
  37. int
  38. vips_type_find_load_go(int imgtype) {
  39. if (imgtype == JPEG) {
  40. return vips_type_find("VipsOperation", "jpegload");
  41. }
  42. if (imgtype == PNG) {
  43. return vips_type_find("VipsOperation", "pngload");
  44. }
  45. if (imgtype == WEBP) {
  46. return vips_type_find("VipsOperation", "webpload");
  47. }
  48. if (imgtype == GIF) {
  49. return vips_type_find("VipsOperation", "gifload");
  50. }
  51. return 0;
  52. }
  53. int
  54. vips_type_find_save_go(int imgtype) {
  55. if (imgtype == JPEG) {
  56. return vips_type_find("VipsOperation", "jpegsave_buffer");
  57. }
  58. if (imgtype == PNG) {
  59. return vips_type_find("VipsOperation", "pngsave_buffer");
  60. }
  61. if (imgtype == WEBP) {
  62. return vips_type_find("VipsOperation", "webpsave_buffer");
  63. }
  64. return 0;
  65. }
  66. int
  67. vips_jpegload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  68. return vips_jpegload_buffer(buf, len, out, "access", access_mode(random), NULL);
  69. }
  70. int
  71. vips_pngload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  72. return vips_pngload_buffer(buf, len, out, "access", access_mode(random), NULL);
  73. }
  74. int
  75. vips_gifload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  76. #if VIPS_SUPPORT_GIF
  77. return vips_gifload_buffer(buf, len, out, "access", access_mode(random), NULL);
  78. #else
  79. return 0;
  80. #endif
  81. }
  82. int
  83. vips_webpload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
  84. return vips_webpload_buffer(buf, len, out, "access", access_mode(random), NULL);
  85. }
  86. int
  87. vips_get_exif_orientation(VipsImage *image) {
  88. const char *orientation;
  89. if (
  90. vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
  91. !vips_image_get_string(image, EXIF_ORIENTATION, &orientation)
  92. ) return atoi(&orientation[0]);
  93. return 1;
  94. }
  95. int
  96. vips_exif_rotate(VipsImage **img, int orientation) {
  97. int err;
  98. int angle = VIPS_ANGLE_D0;
  99. gboolean flip = FALSE;
  100. VipsImage *tmp;
  101. if (orientation == 3 || orientation == 4) angle = VIPS_ANGLE_D180;
  102. if (orientation == 5 || orientation == 6) angle = VIPS_ANGLE_D90;
  103. if (orientation == 7 || orientation == 8) angle = VIPS_ANGLE_D270;
  104. if (orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7) {
  105. flip = TRUE;
  106. }
  107. err = vips_rot(*img, &tmp, angle, NULL);
  108. swap_and_clear(img, tmp);
  109. if (err > 0) { return err; }
  110. if (flip) {
  111. err = vips_flip(*img, &tmp, VIPS_DIRECTION_HORIZONTAL, NULL);
  112. swap_and_clear(img, tmp);
  113. if (err > 0) { return err; }
  114. }
  115. return 0;
  116. }
  117. int
  118. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  119. return vips_resize(in, out, scale, NULL);
  120. }
  121. int
  122. vips_support_smartcrop() {
  123. #if VIPS_SUPPORT_SMARTCROP
  124. return 1;
  125. #else
  126. return 0;
  127. #endif
  128. }
  129. int
  130. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  131. #if VIPS_SUPPORT_SMARTCROP
  132. return vips_smartcrop(in, out, width, height, NULL);
  133. #else
  134. return 0;
  135. #endif
  136. }
  137. int
  138. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation space) {
  139. return vips_colourspace(in, out, space, NULL);
  140. }
  141. int
  142. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  143. return vips_extract_area(in, out, left, top, width, height, NULL);
  144. }
  145. int
  146. vips_process_image(VipsImage **img, int resize, double scale, int crop, int smart, int left, int top, int width, int height) {
  147. VipsImage *tmp;
  148. int err;
  149. int exif_orientation = vips_get_exif_orientation(*img);
  150. if (exif_orientation > 1) {
  151. vips_exif_rotate(img, exif_orientation);
  152. if (err > 0) { return 1; }
  153. }
  154. if (resize > 0) {
  155. err = vips_resize_go(*img, &tmp, scale);
  156. swap_and_clear(img, tmp);
  157. if (err > 0) { return 1; }
  158. }
  159. if (crop > 0) {
  160. if (smart > 0) {
  161. err = vips_smartcrop_go(*img, &tmp, width, height);
  162. swap_and_clear(img, tmp);
  163. if (err > 0) { return 1; }
  164. } else {
  165. vips_extract_area_go(*img, &tmp, left, top, width, height);
  166. swap_and_clear(img, tmp);
  167. if (err > 0) { return 1; }
  168. }
  169. }
  170. if (vips_image_guess_interpretation(*img) != VIPS_INTERPRETATION_sRGB) {
  171. err = vips_colourspace_go(*img, &tmp, VIPS_INTERPRETATION_sRGB);
  172. swap_and_clear(img, tmp);
  173. }
  174. return err;
  175. }
  176. int
  177. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  178. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  179. }
  180. int
  181. vips_pngsave_go(VipsImage *in, void **buf, size_t *len) {
  182. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
  183. }
  184. int
  185. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  186. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  187. }
  188. void
  189. vips_cleanup() {
  190. vips_thread_shutdown();
  191. vips_error_clear();
  192. }