vips.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #include <stdlib.h>
  2. #include <vips/vips.h>
  3. #include <vips/vips7compat.h>
  4. #include "image_types.h"
  5. #define VIPS_SUPPORT_SMARTCROP \
  6. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  7. #define VIPS_SUPPORT_HASALPHA \
  8. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  9. #define VIPS_SUPPORT_GIF \
  10. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
  11. #define VIPS_SUPPORT_MAGICK \
  12. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
  13. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  14. int
  15. vips_initialize() {
  16. return vips_init("imgproxy");
  17. }
  18. void
  19. clear_image(VipsImage **in) {
  20. if (G_IS_OBJECT(*in)) g_clear_object(in);
  21. }
  22. void
  23. g_free_go(void **buf) {
  24. g_free(*buf);
  25. }
  26. void
  27. swap_and_clear(VipsImage **in, VipsImage *out) {
  28. clear_image(in);
  29. *in = out;
  30. }
  31. int
  32. vips_type_find_load_go(int imgtype) {
  33. if (imgtype == JPEG) {
  34. return vips_type_find("VipsOperation", "jpegload_buffer");
  35. }
  36. if (imgtype == PNG) {
  37. return vips_type_find("VipsOperation", "pngload_buffer");
  38. }
  39. if (imgtype == WEBP) {
  40. return vips_type_find("VipsOperation", "webpload_buffer");
  41. }
  42. if (imgtype == GIF) {
  43. return vips_type_find("VipsOperation", "gifload_buffer");
  44. }
  45. return 0;
  46. }
  47. int
  48. vips_type_find_save_go(int imgtype) {
  49. if (imgtype == JPEG) {
  50. return vips_type_find("VipsOperation", "jpegsave_buffer");
  51. }
  52. if (imgtype == PNG) {
  53. return vips_type_find("VipsOperation", "pngsave_buffer");
  54. }
  55. if (imgtype == WEBP) {
  56. return vips_type_find("VipsOperation", "webpsave_buffer");
  57. }
  58. if (imgtype == GIF) {
  59. return vips_type_find("VipsOperation", "magicksave_buffer");
  60. }
  61. return 0;
  62. }
  63. int
  64. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  65. if (shrink > 1) {
  66. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  67. }
  68. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  69. }
  70. int
  71. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  72. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  73. }
  74. int
  75. vips_webpload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  76. if (shrink > 1) {
  77. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  78. }
  79. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  80. }
  81. int
  82. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  83. #if VIPS_SUPPORT_GIF
  84. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  85. #else
  86. vips_error("vips_gifload_go", "Loading GIF is not supported");
  87. return 1;
  88. #endif
  89. }
  90. int
  91. vips_get_exif_orientation(VipsImage *image) {
  92. const char *orientation;
  93. if (
  94. vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
  95. vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
  96. ) return atoi(&orientation[0]);
  97. return 1;
  98. }
  99. int
  100. vips_support_smartcrop() {
  101. #if VIPS_SUPPORT_SMARTCROP
  102. return 1;
  103. #else
  104. return 0;
  105. #endif
  106. }
  107. VipsBandFormat
  108. vips_band_format(VipsImage *in) {
  109. return in->BandFmt;
  110. }
  111. gboolean
  112. vips_image_hasalpha_go(VipsImage * in) {
  113. #if VIPS_SUPPORT_HASALPHA
  114. return vips_image_hasalpha(in);
  115. #else
  116. return( in->Bands == 2 ||
  117. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  118. in->Bands > 4 );
  119. #endif
  120. }
  121. int
  122. vips_premultiply_go(VipsImage *in, VipsImage **out) {
  123. return vips_premultiply(in, out, NULL);
  124. }
  125. int
  126. vips_unpremultiply_go(VipsImage *in, VipsImage **out) {
  127. return vips_unpremultiply(in, out, NULL);
  128. }
  129. int
  130. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  131. return vips_cast(in, out, format, NULL);
  132. }
  133. int
  134. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  135. return vips_resize(in, out, scale, NULL);
  136. }
  137. int
  138. vips_need_icc_import(VipsImage *in) {
  139. return in->Type == VIPS_INTERPRETATION_CMYK;
  140. }
  141. int
  142. vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
  143. return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  144. }
  145. int
  146. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  147. return vips_colourspace(in, out, cs, NULL);
  148. }
  149. int
  150. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  151. return vips_rot(in, out, angle, NULL);
  152. }
  153. int
  154. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  155. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  156. }
  157. int
  158. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  159. #if VIPS_SUPPORT_SMARTCROP
  160. return vips_smartcrop(in, out, width, height, NULL);
  161. #else
  162. vips_error("vips_smartcrop_go", "Smart crop is not supported");
  163. return 1;
  164. #endif
  165. }
  166. int
  167. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  168. return vips_gaussblur(in, out, sigma, NULL);
  169. }
  170. int
  171. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  172. return vips_sharpen(in, out, "sigma", sigma, NULL);
  173. }
  174. int
  175. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  176. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  177. int res = vips_flatten(in, out, "background", bg, NULL);
  178. vips_area_unref((VipsArea *)bg);
  179. return res;
  180. }
  181. int
  182. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  183. return vips_extract_area(in, out, left, top, width, height, NULL);
  184. }
  185. int
  186. vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down) {
  187. return vips_replicate(in, out, across, down, NULL);
  188. }
  189. int
  190. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  191. return vips_embed(in, out, x, y, width, height, NULL);
  192. }
  193. int
  194. vips_extract_band_go(VipsImage *in, VipsImage **out, int band, int band_num) {
  195. return vips_extract_band(in, out, band, "n", band_num, NULL);
  196. }
  197. int
  198. vips_bandjoin_go (VipsImage *in1, VipsImage *in2, VipsImage **out) {
  199. return vips_bandjoin2(in1, in2, out, NULL);
  200. }
  201. int
  202. vips_bandjoin_const_go (VipsImage *in, VipsImage **out, double c) {
  203. return vips_bandjoin_const1(in, out, c, NULL);
  204. }
  205. int
  206. vips_linear_go (VipsImage *in, VipsImage **out, double a, double b) {
  207. return vips_linear1(in, out, a, b, NULL);
  208. }
  209. int
  210. vips_ifthenelse_go(VipsImage *cond, VipsImage *in1, VipsImage *in2, VipsImage **out) {
  211. return vips_ifthenelse(cond, in1, in2, out, "blend", TRUE, NULL);
  212. }
  213. int
  214. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  215. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  216. }
  217. int
  218. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  219. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  220. }
  221. int
  222. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int embed_profile) {
  223. if (embed_profile) {
  224. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  225. }
  226. return vips_pngsave_buffer(in, buf, len, "profile", "none", "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  227. }
  228. int
  229. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  230. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  231. }
  232. int
  233. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  234. #if VIPS_SUPPORT_MAGICK
  235. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  236. #else
  237. vips_error("vips_gifsave_go", "Saving GIF is not supported");
  238. return 1;
  239. #endif
  240. }
  241. void
  242. vips_cleanup() {
  243. vips_thread_shutdown();
  244. vips_error_clear();
  245. }