vips.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. #include "vips.h"
  2. #include <string.h>
  3. #define VIPS_SUPPORT_SMARTCROP \
  4. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  5. #define VIPS_SUPPORT_HASALPHA \
  6. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  7. #define VIPS_SUPPORT_GIF \
  8. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
  9. #define VIPS_SUPPORT_SVG \
  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 VIPS_SUPPORT_PNG_QUANTIZATION \
  14. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
  15. #define VIPS_SUPPORT_WEBP_SCALE_ON_LOAD \
  16. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  17. #define VIPS_SUPPORT_WEBP_ANIMATION \
  18. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  19. #define VIPS_SUPPORT_HEIF \
  20. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  21. #define VIPS_SUPPORT_BUILTIN_ICC \
  22. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  23. #define VIPS_SUPPORT_COMPOSITE \
  24. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 6))
  25. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  26. #if (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  27. #define VIPS_BLOB_DATA_TYPE const void *
  28. #else
  29. #define VIPS_BLOB_DATA_TYPE void *
  30. #endif
  31. int
  32. vips_initialize() {
  33. return vips_init("imgproxy");
  34. }
  35. void
  36. clear_image(VipsImage **in) {
  37. if (G_IS_OBJECT(*in)) g_clear_object(in);
  38. }
  39. void
  40. g_free_go(void **buf) {
  41. g_free(*buf);
  42. }
  43. void
  44. swap_and_clear(VipsImage **in, VipsImage *out) {
  45. clear_image(in);
  46. *in = out;
  47. }
  48. int
  49. vips_type_find_load_go(int imgtype) {
  50. switch (imgtype)
  51. {
  52. case (JPEG):
  53. return vips_type_find("VipsOperation", "jpegload_buffer");
  54. case (PNG):
  55. return vips_type_find("VipsOperation", "pngload_buffer");
  56. case (WEBP):
  57. return vips_type_find("VipsOperation", "webpload_buffer");
  58. case (GIF):
  59. return vips_type_find("VipsOperation", "gifload_buffer");
  60. case (SVG):
  61. return vips_type_find("VipsOperation", "svgload_buffer");
  62. case (HEIC):
  63. return vips_type_find("VipsOperation", "heifload_buffer");
  64. }
  65. return 0;
  66. }
  67. int
  68. vips_type_find_save_go(int imgtype) {
  69. switch (imgtype)
  70. {
  71. case (JPEG):
  72. return vips_type_find("VipsOperation", "jpegsave_buffer");
  73. case (PNG):
  74. return vips_type_find("VipsOperation", "pngsave_buffer");
  75. case (WEBP):
  76. return vips_type_find("VipsOperation", "webpsave_buffer");
  77. case (GIF):
  78. return vips_type_find("VipsOperation", "magicksave_buffer");
  79. case (ICO):
  80. return vips_type_find("VipsOperation", "magicksave_buffer");
  81. case (HEIC):
  82. return vips_type_find("VipsOperation", "heifsave_buffer");
  83. }
  84. return 0;
  85. }
  86. int
  87. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  88. if (shrink > 1)
  89. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  90. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  91. }
  92. int
  93. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  94. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  95. }
  96. int
  97. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out) {
  98. return vips_webpload_buffer(
  99. buf, len, out,
  100. "access", VIPS_ACCESS_SEQUENTIAL,
  101. #if VIPS_SUPPORT_WEBP_SCALE_ON_LOAD
  102. "scale", scale,
  103. #else
  104. "shrink", (int)(1.0 / scale),
  105. #endif
  106. #if VIPS_SUPPORT_WEBP_ANIMATION
  107. "n", pages,
  108. #endif
  109. NULL
  110. );
  111. }
  112. int
  113. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  114. #if VIPS_SUPPORT_GIF
  115. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  116. #else
  117. vips_error("vips_gifload_go", "Loading GIF is not supported (libvips 8.3+ reuired)");
  118. return 1;
  119. #endif
  120. }
  121. int
  122. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
  123. #if VIPS_SUPPORT_SVG
  124. return vips_svgload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "scale", scale, NULL);
  125. #else
  126. vips_error("vips_svgload_go", "Loading SVG is not supported (libvips 8.5+ reuired)");
  127. return 1;
  128. #endif
  129. }
  130. int
  131. vips_heifload_go(void *buf, size_t len, VipsImage **out) {
  132. #if VIPS_SUPPORT_HEIF
  133. return vips_heifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  134. #else
  135. vips_error("vips_heifload_go", "Loading HEIF is not supported (libvips 8.8+ reuired)");
  136. return 1;
  137. #endif
  138. }
  139. int
  140. vips_get_orientation(VipsImage *image) {
  141. #ifdef VIPS_META_ORIENTATION
  142. int orientation;
  143. if (vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0)
  144. return orientation;
  145. #else
  146. const char *orientation;
  147. if (
  148. vips_image_get_typeof(image, EXIF_ORIENTATION) == VIPS_TYPE_REF_STRING &&
  149. vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
  150. ) return atoi(orientation);
  151. #endif
  152. return 1;
  153. }
  154. int
  155. vips_support_smartcrop() {
  156. return VIPS_SUPPORT_SMARTCROP;
  157. }
  158. VipsBandFormat
  159. vips_band_format(VipsImage *in) {
  160. return in->BandFmt;
  161. }
  162. gboolean
  163. vips_support_webp_animation() {
  164. return VIPS_SUPPORT_WEBP_ANIMATION;
  165. }
  166. gboolean
  167. vips_is_animated(VipsImage * in) {
  168. return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
  169. vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
  170. vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
  171. }
  172. gboolean
  173. vips_image_hasalpha_go(VipsImage * in) {
  174. #if VIPS_SUPPORT_HASALPHA
  175. return vips_image_hasalpha(in);
  176. #else
  177. return( in->Bands == 2 ||
  178. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  179. in->Bands > 4 );
  180. #endif
  181. }
  182. int
  183. vips_copy_go(VipsImage *in, VipsImage **out) {
  184. return vips_copy(in, out, NULL);
  185. }
  186. int
  187. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  188. return vips_cast(in, out, format, NULL);
  189. }
  190. int
  191. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  192. return vips_rad2float(in, out, NULL);
  193. }
  194. int
  195. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  196. return vips_resize(in, out, scale, NULL);
  197. }
  198. int
  199. vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale) {
  200. VipsBandFormat format;
  201. VipsImage *tmp1, *tmp2;
  202. format = vips_band_format(in);
  203. if (vips_premultiply(in, &tmp1, NULL))
  204. return 1;
  205. if (vips_resize(tmp1, &tmp2, scale, NULL)) {
  206. clear_image(&tmp1);
  207. return 1;
  208. }
  209. swap_and_clear(&tmp1, tmp2);
  210. if (vips_unpremultiply(tmp1, &tmp2, NULL)) {
  211. clear_image(&tmp1);
  212. return 1;
  213. }
  214. swap_and_clear(&tmp1, tmp2);
  215. if (vips_cast(tmp1, out, format, NULL)) {
  216. clear_image(&tmp1);
  217. return 1;
  218. }
  219. clear_image(&tmp1);
  220. return 0;
  221. }
  222. int
  223. vips_icc_is_srgb_iec61966(VipsImage *in) {
  224. VIPS_BLOB_DATA_TYPE data;
  225. size_t data_len;
  226. // 1998-12-01
  227. static char date[] = { 7, 206, 0, 2, 0, 9 };
  228. // 2.1
  229. static char version[] = { 2, 16, 0, 0 };
  230. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  231. return FALSE;
  232. // Less than header size
  233. if (data_len < 128)
  234. return FALSE;
  235. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  236. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  237. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  238. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  239. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  240. (memcmp(data + 8, version, 4) == 0)); // Version
  241. }
  242. int
  243. vips_has_embedded_icc(VipsImage *in) {
  244. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  245. }
  246. int
  247. vips_support_builtin_icc() {
  248. return VIPS_SUPPORT_BUILTIN_ICC;
  249. }
  250. int
  251. vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
  252. return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  253. }
  254. int
  255. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  256. return vips_colourspace(in, out, cs, NULL);
  257. }
  258. int
  259. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  260. return vips_rot(in, out, angle, NULL);
  261. }
  262. int
  263. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  264. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  265. }
  266. int
  267. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  268. #if VIPS_SUPPORT_SMARTCROP
  269. return vips_smartcrop(in, out, width, height, NULL);
  270. #else
  271. vips_error("vips_smartcrop_go", "Smart crop is not supported (libvips 8.5+ reuired)");
  272. return 1;
  273. #endif
  274. }
  275. int
  276. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  277. return vips_gaussblur(in, out, sigma, NULL);
  278. }
  279. int
  280. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  281. return vips_sharpen(in, out, "sigma", sigma, NULL);
  282. }
  283. int
  284. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  285. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  286. int res = vips_flatten(in, out, "background", bg, NULL);
  287. vips_area_unref((VipsArea *)bg);
  288. return res;
  289. }
  290. int
  291. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  292. return vips_extract_area(in, out, left, top, width, height, NULL);
  293. }
  294. int
  295. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  296. VipsImage *tmp;
  297. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  298. return 1;
  299. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  300. clear_image(&tmp);
  301. return 1;
  302. }
  303. clear_image(&tmp);
  304. return 0;
  305. }
  306. int
  307. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn) {
  308. VipsArrayDouble *bga = vips_array_double_new(bg, bgn);
  309. int ret = vips_embed(
  310. in, out, x, y, width, height,
  311. "extend", VIPS_EXTEND_BACKGROUND,
  312. "background", bga,
  313. NULL
  314. );
  315. vips_area_unref((VipsArea *)bga);
  316. return ret;
  317. }
  318. int
  319. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  320. if (vips_image_hasalpha_go(in)) {
  321. return vips_copy(in, out, NULL);
  322. }
  323. return vips_bandjoin_const1(in, out, 255, NULL);
  324. }
  325. int
  326. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  327. #if VIPS_SUPPORT_COMPOSITE
  328. VipsImage *base = vips_image_new();
  329. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  330. if (opacity < 1) {
  331. if (
  332. vips_extract_band(watermark, &t[0], 0, "n", watermark->Bands - 1, NULL) ||
  333. vips_extract_band(watermark, &t[1], watermark->Bands - 1, "n", 1, NULL) ||
  334. vips_linear1(t[1], &t[2], opacity, 0, NULL) ||
  335. vips_bandjoin2(t[0], t[2], &t[3], NULL)
  336. ) {
  337. clear_image(&base);
  338. return 1;
  339. }
  340. } else {
  341. if (vips_copy(watermark, &t[3], NULL)) {
  342. clear_image(&base);
  343. return 1;
  344. }
  345. }
  346. int res =
  347. vips_composite2(in, t[3], &t[4], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  348. vips_cast(t[4], out, vips_image_get_format(in), NULL);
  349. clear_image(&base);
  350. return res;
  351. #else
  352. vips_error("vips_apply_watermark", "Watermarking is not supported (libvips 8.6+ reuired)");
  353. return 1;
  354. #endif
  355. }
  356. int
  357. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  358. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  359. }
  360. int
  361. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  362. return vips_jpegsave_buffer(in, buf, len, "profile", "none", "Q", quality, "strip", TRUE, "optimize_coding", TRUE, "interlace", interlace, NULL);
  363. }
  364. int
  365. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  366. return vips_pngsave_buffer(
  367. in, buf, len,
  368. "profile", "none",
  369. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  370. "interlace", interlace,
  371. #if VIPS_SUPPORT_PNG_QUANTIZATION
  372. "palette", quantize,
  373. "colours", colors,
  374. #endif // VIPS_SUPPORT_PNG_QUANTIZATION
  375. NULL);
  376. }
  377. int
  378. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  379. return vips_webpsave_buffer(in, buf, len, "Q", quality, "strip", TRUE, NULL);
  380. }
  381. int
  382. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  383. #if VIPS_SUPPORT_MAGICK
  384. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  385. #else
  386. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.7+ reuired)");
  387. return 1;
  388. #endif
  389. }
  390. int
  391. vips_icosave_go(VipsImage *in, void **buf, size_t *len) {
  392. #if VIPS_SUPPORT_MAGICK
  393. return vips_magicksave_buffer(in, buf, len, "format", "ico", NULL);
  394. #else
  395. vips_error("vips_icosave_go", "Saving ICO is not supported (libvips 8.7+ reuired)");
  396. return 1;
  397. #endif
  398. }
  399. int
  400. vips_heifsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  401. #if VIPS_SUPPORT_HEIF
  402. return vips_heifsave_buffer(in, buf, len, "Q", quality, NULL);
  403. #else
  404. vips_error("vips_heifsave_go", "Saving HEIF is not supported");
  405. return 1;
  406. #endif
  407. }
  408. void
  409. vips_cleanup() {
  410. vips_error_clear();
  411. vips_thread_shutdown();
  412. }