vips.h 8.5 KB

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