vips.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. if (imgtype == ICO) {
  62. return vips_type_find("VipsOperation", "magicksave_buffer");
  63. }
  64. return 0;
  65. }
  66. int
  67. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  68. if (shrink > 1) {
  69. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  70. }
  71. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  72. }
  73. int
  74. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  75. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  76. }
  77. int
  78. vips_webpload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  79. if (shrink > 1) {
  80. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  81. }
  82. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  83. }
  84. int
  85. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  86. #if VIPS_SUPPORT_GIF
  87. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  88. #else
  89. vips_error("vips_gifload_go", "Loading GIF is not supported");
  90. return 1;
  91. #endif
  92. }
  93. int
  94. vips_get_exif_orientation(VipsImage *image) {
  95. const char *orientation;
  96. if (
  97. vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
  98. vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
  99. ) return atoi(&orientation[0]);
  100. return 1;
  101. }
  102. int
  103. vips_support_smartcrop() {
  104. #if VIPS_SUPPORT_SMARTCROP
  105. return 1;
  106. #else
  107. return 0;
  108. #endif
  109. }
  110. VipsBandFormat
  111. vips_band_format(VipsImage *in) {
  112. return in->BandFmt;
  113. }
  114. gboolean
  115. vips_image_hasalpha_go(VipsImage * in) {
  116. #if VIPS_SUPPORT_HASALPHA
  117. return vips_image_hasalpha(in);
  118. #else
  119. return( in->Bands == 2 ||
  120. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  121. in->Bands > 4 );
  122. #endif
  123. }
  124. int
  125. vips_premultiply_go(VipsImage *in, VipsImage **out) {
  126. return vips_premultiply(in, out, NULL);
  127. }
  128. int
  129. vips_unpremultiply_go(VipsImage *in, VipsImage **out) {
  130. return vips_unpremultiply(in, out, NULL);
  131. }
  132. int
  133. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  134. return vips_cast(in, out, format, NULL);
  135. }
  136. int
  137. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  138. return vips_resize(in, out, scale, NULL);
  139. }
  140. int
  141. vips_need_icc_import(VipsImage *in) {
  142. return in->Type == VIPS_INTERPRETATION_CMYK;
  143. }
  144. int
  145. vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
  146. return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  147. }
  148. int
  149. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  150. return vips_colourspace(in, out, cs, NULL);
  151. }
  152. int
  153. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  154. return vips_rot(in, out, angle, NULL);
  155. }
  156. int
  157. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  158. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  159. }
  160. int
  161. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  162. #if VIPS_SUPPORT_SMARTCROP
  163. return vips_smartcrop(in, out, width, height, NULL);
  164. #else
  165. vips_error("vips_smartcrop_go", "Smart crop is not supported");
  166. return 1;
  167. #endif
  168. }
  169. int
  170. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  171. return vips_gaussblur(in, out, sigma, NULL);
  172. }
  173. int
  174. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  175. return vips_sharpen(in, out, "sigma", sigma, NULL);
  176. }
  177. int
  178. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  179. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  180. int res = vips_flatten(in, out, "background", bg, NULL);
  181. vips_area_unref((VipsArea *)bg);
  182. return res;
  183. }
  184. int
  185. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  186. return vips_extract_area(in, out, left, top, width, height, NULL);
  187. }
  188. int
  189. vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down) {
  190. return vips_replicate(in, out, across, down, NULL);
  191. }
  192. int
  193. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  194. return vips_embed(in, out, x, y, width, height, NULL);
  195. }
  196. int
  197. vips_extract_band_go(VipsImage *in, VipsImage **out, int band, int band_num) {
  198. return vips_extract_band(in, out, band, "n", band_num, NULL);
  199. }
  200. int
  201. vips_bandjoin_go (VipsImage *in1, VipsImage *in2, VipsImage **out) {
  202. return vips_bandjoin2(in1, in2, out, NULL);
  203. }
  204. int
  205. vips_bandjoin_const_go (VipsImage *in, VipsImage **out, double c) {
  206. return vips_bandjoin_const1(in, out, c, NULL);
  207. }
  208. int
  209. vips_linear_go (VipsImage *in, VipsImage **out, double a, double b) {
  210. return vips_linear1(in, out, a, b, NULL);
  211. }
  212. int
  213. vips_ifthenelse_go(VipsImage *cond, VipsImage *in1, VipsImage *in2, VipsImage **out) {
  214. return vips_ifthenelse(cond, in1, in2, out, "blend", TRUE, NULL);
  215. }
  216. int
  217. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  218. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  219. }
  220. int
  221. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  222. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  223. }
  224. int
  225. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int embed_profile) {
  226. if (embed_profile) {
  227. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  228. }
  229. return vips_pngsave_buffer(in, buf, len, "profile", "none", "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  230. }
  231. int
  232. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  233. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  234. }
  235. int
  236. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  237. #if VIPS_SUPPORT_MAGICK
  238. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  239. #else
  240. vips_error("vips_gifsave_go", "Saving GIF is not supported");
  241. return 1;
  242. #endif
  243. }
  244. int
  245. vips_icosave_go(VipsImage *in, void **buf, size_t *len) {
  246. #if VIPS_SUPPORT_MAGICK
  247. return vips_magicksave_buffer(in, buf, len, "format", "ico", NULL);
  248. #else
  249. vips_error("vips_icosave_go", "Saving ICO is not supported");
  250. return 1;
  251. #endif
  252. }
  253. void
  254. vips_cleanup() {
  255. vips_thread_shutdown();
  256. vips_error_clear();
  257. }