vips.c 13 KB

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