1
0

vips.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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_ARRAY_HEADERS \
  22. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 9))
  23. #define VIPS_SUPPORT_HEIF \
  24. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  25. #define VIPS_SUPPORT_AVIF \
  26. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 9))
  27. #define VIPS_SUPPORT_AVIF_SPEED \
  28. (VIPS_MAJOR_VERSION > 8 || \
  29. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION > 10) || \
  30. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10 && VIPS_MICRO_VERSION >= 2))
  31. #define VIPS_SUPPORT_COMPOSITE \
  32. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 6))
  33. #define VIPS_SUPPORT_FIND_TRIM \
  34. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 6))
  35. #define VIPS_SUPPORT_PNG_BITDEPTH \
  36. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10))
  37. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  38. #if (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  39. #define VIPS_BLOB_DATA_TYPE const void *
  40. #else
  41. #define VIPS_BLOB_DATA_TYPE void *
  42. #endif
  43. int
  44. vips_initialize() {
  45. return vips_init("imgproxy");
  46. }
  47. void
  48. clear_image(VipsImage **in) {
  49. if (G_IS_OBJECT(*in)) g_clear_object(in);
  50. }
  51. void
  52. g_free_go(void **buf) {
  53. g_free(*buf);
  54. }
  55. void
  56. swap_and_clear(VipsImage **in, VipsImage *out) {
  57. clear_image(in);
  58. *in = out;
  59. }
  60. int
  61. vips_type_find_load_go(int imgtype) {
  62. switch (imgtype)
  63. {
  64. case (JPEG):
  65. return vips_type_find("VipsOperation", "jpegload_buffer");
  66. case (PNG):
  67. return vips_type_find("VipsOperation", "pngload_buffer");
  68. case (WEBP):
  69. return vips_type_find("VipsOperation", "webpload_buffer");
  70. case (GIF):
  71. return vips_type_find("VipsOperation", "gifload_buffer");
  72. case (SVG):
  73. return vips_type_find("VipsOperation", "svgload_buffer");
  74. case (HEIC):
  75. return vips_type_find("VipsOperation", "heifload_buffer");
  76. case (AVIF):
  77. return vips_type_find("VipsOperation", "heifload_buffer");
  78. case (BMP):
  79. return vips_type_find("VipsOperation", "magickload_buffer");
  80. case (TIFF):
  81. return vips_type_find("VipsOperation", "tiffload_buffer");
  82. }
  83. return 0;
  84. }
  85. int
  86. vips_type_find_save_go(int imgtype) {
  87. switch (imgtype)
  88. {
  89. case (JPEG):
  90. return vips_type_find("VipsOperation", "jpegsave_buffer");
  91. case (PNG):
  92. return vips_type_find("VipsOperation", "pngsave_buffer");
  93. case (WEBP):
  94. return vips_type_find("VipsOperation", "webpsave_buffer");
  95. case (GIF):
  96. return vips_type_find("VipsOperation", "magicksave_buffer");
  97. #if VIPS_SUPPORT_AVIF
  98. case (AVIF):
  99. return vips_type_find("VipsOperation", "heifsave_buffer");
  100. #endif
  101. case (ICO):
  102. return vips_type_find("VipsOperation", "pngsave_buffer");
  103. case (BMP):
  104. return vips_type_find("VipsOperation", "magicksave_buffer");
  105. case (TIFF):
  106. return vips_type_find("VipsOperation", "tiffsave_buffer");
  107. }
  108. return 0;
  109. }
  110. int
  111. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  112. if (shrink > 1)
  113. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  114. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  115. }
  116. int
  117. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  118. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  119. }
  120. int
  121. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out) {
  122. return vips_webpload_buffer(
  123. buf, len, out,
  124. "access", VIPS_ACCESS_SEQUENTIAL,
  125. #if VIPS_SUPPORT_WEBP_SCALE_ON_LOAD
  126. "scale", scale,
  127. #else
  128. "shrink", (int)(1.0 / scale),
  129. #endif
  130. #if VIPS_SUPPORT_WEBP_ANIMATION
  131. "n", pages,
  132. #endif
  133. NULL
  134. );
  135. }
  136. int
  137. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  138. #if VIPS_SUPPORT_GIF
  139. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  140. #else
  141. vips_error("vips_gifload_go", "Loading GIF is not supported (libvips 8.3+ reuired)");
  142. return 1;
  143. #endif
  144. }
  145. int
  146. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
  147. #if VIPS_SUPPORT_SVG
  148. // libvips limits the minimal scale to 0.001, so we have to scale down dpi
  149. // for lower scale values
  150. double dpi = 72.0;
  151. if (scale < 0.001) {
  152. dpi *= VIPS_MAX(scale / 0.001, 0.001);
  153. scale = 0.001;
  154. }
  155. return vips_svgload_buffer(
  156. buf, len, out,
  157. "access", VIPS_ACCESS_SEQUENTIAL,
  158. "scale", scale,
  159. "dpi", dpi,
  160. NULL
  161. );
  162. #else
  163. vips_error("vips_svgload_go", "Loading SVG is not supported (libvips 8.5+ reuired)");
  164. return 1;
  165. #endif
  166. }
  167. int
  168. vips_heifload_go(void *buf, size_t len, VipsImage **out) {
  169. #if VIPS_SUPPORT_HEIF
  170. return vips_heifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  171. #else
  172. vips_error("vips_heifload_go", "Loading HEIF is not supported (libvips 8.8+ reuired)");
  173. return 1;
  174. #endif
  175. }
  176. int
  177. vips_bmpload_go(void *buf, size_t len, VipsImage **out) {
  178. #if VIPS_SUPPORT_MAGICK
  179. return vips_magickload_buffer(buf, len, out, NULL);
  180. #else
  181. vips_error("vips_bmpload_go", "Loading BMP is not supported");
  182. return 1;
  183. #endif
  184. }
  185. int
  186. vips_tiffload_go(void *buf, size_t len, VipsImage **out) {
  187. #if VIPS_SUPPORT_TIFF
  188. return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  189. #else
  190. vips_error("vips_tiffload_go", "Loading TIFF is not supported (libvips 8.6+ reuired)");
  191. return 1;
  192. #endif
  193. }
  194. int
  195. vips_get_orientation(VipsImage *image) {
  196. #ifdef VIPS_META_ORIENTATION
  197. int orientation;
  198. if (
  199. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  200. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0
  201. ) return orientation;
  202. #else
  203. const char *orientation;
  204. if (
  205. vips_image_get_typeof(image, EXIF_ORIENTATION) == VIPS_TYPE_REF_STRING &&
  206. vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
  207. ) return atoi(orientation);
  208. #endif
  209. return 1;
  210. }
  211. int
  212. vips_support_smartcrop() {
  213. return VIPS_SUPPORT_SMARTCROP;
  214. }
  215. VipsBandFormat
  216. vips_band_format(VipsImage *in) {
  217. return in->BandFmt;
  218. }
  219. gboolean
  220. vips_support_webp_animation() {
  221. return VIPS_SUPPORT_WEBP_ANIMATION;
  222. }
  223. gboolean
  224. vips_support_avif_speed() {
  225. return VIPS_SUPPORT_AVIF_SPEED;
  226. }
  227. gboolean
  228. vips_is_animated(VipsImage * in) {
  229. return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
  230. vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
  231. vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
  232. }
  233. int
  234. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
  235. #if VIPS_SUPPORT_ARRAY_HEADERS
  236. return vips_image_get_array_int(image, name, out, n);
  237. #else
  238. vips_error("vips_image_get_array_int_go", "Array headers are not supported (libvips 8.9+ reuired)");
  239. return 1;
  240. #endif
  241. }
  242. void
  243. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
  244. #if VIPS_SUPPORT_ARRAY_HEADERS
  245. vips_image_set_array_int(image, name, array, n);
  246. #endif
  247. }
  248. gboolean
  249. vips_image_hasalpha_go(VipsImage * in) {
  250. #if VIPS_SUPPORT_HASALPHA
  251. return vips_image_hasalpha(in);
  252. #else
  253. return( in->Bands == 2 ||
  254. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  255. in->Bands > 4 );
  256. #endif
  257. }
  258. int
  259. vips_addalpha_go(VipsImage *in, VipsImage **out) {
  260. return vips_addalpha(in, out, NULL);
  261. }
  262. int
  263. vips_copy_go(VipsImage *in, VipsImage **out) {
  264. return vips_copy(in, out, NULL);
  265. }
  266. int
  267. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  268. return vips_cast(in, out, format, NULL);
  269. }
  270. int
  271. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  272. return vips_rad2float(in, out, NULL);
  273. }
  274. int
  275. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  276. return vips_resize(in, out, scale, NULL);
  277. }
  278. int
  279. vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale) {
  280. VipsBandFormat format;
  281. VipsImage *tmp1, *tmp2;
  282. format = vips_band_format(in);
  283. if (vips_premultiply(in, &tmp1, NULL))
  284. return 1;
  285. if (vips_resize(tmp1, &tmp2, scale, NULL)) {
  286. clear_image(&tmp1);
  287. return 1;
  288. }
  289. swap_and_clear(&tmp1, tmp2);
  290. if (vips_unpremultiply(tmp1, &tmp2, NULL)) {
  291. clear_image(&tmp1);
  292. return 1;
  293. }
  294. swap_and_clear(&tmp1, tmp2);
  295. if (vips_cast(tmp1, out, format, NULL)) {
  296. clear_image(&tmp1);
  297. return 1;
  298. }
  299. clear_image(&tmp1);
  300. return 0;
  301. }
  302. int
  303. vips_icc_is_srgb_iec61966(VipsImage *in) {
  304. VIPS_BLOB_DATA_TYPE data;
  305. size_t data_len;
  306. // 1998-12-01
  307. static char date[] = { 7, 206, 0, 2, 0, 9 };
  308. // 2.1
  309. static char version[] = { 2, 16, 0, 0 };
  310. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  311. return FALSE;
  312. // Less than header size
  313. if (data_len < 128)
  314. return FALSE;
  315. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  316. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  317. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  318. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  319. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  320. (memcmp(data + 8, version, 4) == 0)); // Version
  321. }
  322. int
  323. vips_has_embedded_icc(VipsImage *in) {
  324. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  325. }
  326. int
  327. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  328. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  329. }
  330. int
  331. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  332. return vips_icc_export(in, out, NULL);
  333. }
  334. int
  335. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  336. return vips_icc_export(in, out, "output_profile", "sRGB", NULL);
  337. }
  338. int
  339. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  340. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  341. }
  342. int
  343. vips_icc_remove(VipsImage *in, VipsImage **out) {
  344. if (vips_copy(in, out, NULL)) return 1;
  345. vips_image_remove(*out, VIPS_META_ICC_NAME);
  346. return 0;
  347. }
  348. int
  349. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  350. return vips_colourspace(in, out, cs, NULL);
  351. }
  352. int
  353. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  354. return vips_rot(in, out, angle, NULL);
  355. }
  356. int
  357. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  358. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  359. }
  360. int
  361. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  362. #if VIPS_SUPPORT_SMARTCROP
  363. return vips_smartcrop(in, out, width, height, NULL);
  364. #else
  365. vips_error("vips_smartcrop_go", "Smart crop is not supported (libvips 8.5+ reuired)");
  366. return 1;
  367. #endif
  368. }
  369. int
  370. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  371. return vips_gaussblur(in, out, sigma, NULL);
  372. }
  373. int
  374. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  375. return vips_sharpen(in, out, "sigma", sigma, NULL);
  376. }
  377. int
  378. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  379. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  380. int res = vips_flatten(in, out, "background", bg, NULL);
  381. vips_area_unref((VipsArea *)bg);
  382. return res;
  383. }
  384. int
  385. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  386. return vips_extract_area(in, out, left, top, width, height, NULL);
  387. }
  388. int
  389. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  390. gboolean smart, double r, double g, double b,
  391. gboolean equal_hor, gboolean equal_ver) {
  392. #if VIPS_SUPPORT_FIND_TRIM
  393. VipsImage *tmp;
  394. if (vips_image_hasalpha(in)) {
  395. if (vips_flatten(in, &tmp, NULL))
  396. return 1;
  397. } else {
  398. if (vips_copy(in, &tmp, NULL))
  399. return 1;
  400. }
  401. double *bg;
  402. int bgn;
  403. VipsArrayDouble *bga;
  404. if (smart) {
  405. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  406. clear_image(&tmp);
  407. return 1;
  408. }
  409. bga = vips_array_double_new(bg, bgn);
  410. } else {
  411. bga = vips_array_double_newv(3, r, g, b);
  412. bg = 0;
  413. }
  414. int left, right, top, bot, width, height, diff;
  415. if (vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL)) {
  416. clear_image(&tmp);
  417. vips_area_unref((VipsArea *)bga);
  418. g_free(bg);
  419. return 1;
  420. }
  421. if (equal_hor) {
  422. right = in->Xsize - left - width;
  423. diff = right - left;
  424. if (diff > 0) {
  425. width += diff;
  426. } else if (diff < 0) {
  427. left = right;
  428. width -= diff;
  429. }
  430. }
  431. if (equal_ver) {
  432. bot = in->Ysize - top - height;
  433. diff = bot - top;
  434. if (diff > 0) {
  435. height += diff;
  436. } else if (diff < 0) {
  437. top = bot;
  438. height -= diff;
  439. }
  440. }
  441. clear_image(&tmp);
  442. vips_area_unref((VipsArea *)bga);
  443. g_free(bg);
  444. if (width == 0 || height == 0) {
  445. return vips_copy(in, out, NULL);
  446. }
  447. return vips_extract_area(in, out, left, top, width, height, NULL);
  448. #else
  449. vips_error("vips_trim", "Trim is not supported (libvips 8.6+ reuired)");
  450. return 1;
  451. #endif
  452. }
  453. int
  454. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  455. VipsImage *tmp;
  456. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  457. return 1;
  458. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  459. clear_image(&tmp);
  460. return 1;
  461. }
  462. clear_image(&tmp);
  463. return 0;
  464. }
  465. int
  466. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn) {
  467. VipsArrayDouble *bga = vips_array_double_new(bg, bgn);
  468. int ret = vips_embed(
  469. in, out, x, y, width, height,
  470. "extend", VIPS_EXTEND_BACKGROUND,
  471. "background", bga,
  472. NULL
  473. );
  474. vips_area_unref((VipsArea *)bga);
  475. return ret;
  476. }
  477. int
  478. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  479. if (vips_image_hasalpha_go(in)) {
  480. return vips_copy(in, out, NULL);
  481. }
  482. return vips_bandjoin_const1(in, out, 255, NULL);
  483. }
  484. int
  485. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  486. #if VIPS_SUPPORT_COMPOSITE
  487. VipsImage *base = vips_image_new();
  488. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  489. if (opacity < 1) {
  490. if (
  491. vips_extract_band(watermark, &t[0], 0, "n", watermark->Bands - 1, NULL) ||
  492. vips_extract_band(watermark, &t[1], watermark->Bands - 1, "n", 1, NULL) ||
  493. vips_linear1(t[1], &t[2], opacity, 0, NULL) ||
  494. vips_bandjoin2(t[0], t[2], &t[3], NULL)
  495. ) {
  496. clear_image(&base);
  497. return 1;
  498. }
  499. } else {
  500. if (vips_copy(watermark, &t[3], NULL)) {
  501. clear_image(&base);
  502. return 1;
  503. }
  504. }
  505. int res =
  506. vips_composite2(in, t[3], &t[4], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  507. vips_cast(t[4], out, vips_image_get_format(in), NULL);
  508. clear_image(&base);
  509. return res;
  510. #else
  511. vips_error("vips_apply_watermark", "Watermarking is not supported (libvips 8.6+ reuired)");
  512. return 1;
  513. #endif
  514. }
  515. int
  516. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  517. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  518. }
  519. int
  520. vips_strip(VipsImage *in, VipsImage **out) {
  521. if (vips_copy(in, out, NULL)) return 1;
  522. gchar **fields = vips_image_get_fields(in);
  523. for (int i = 0; fields[i] != NULL; i++) {
  524. gchar *name = fields[i];
  525. if (strcmp(name, VIPS_META_ICC_NAME) == 0) continue;
  526. vips_image_remove(*out, name);
  527. }
  528. g_strfreev(fields);
  529. return 0;
  530. }
  531. int
  532. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  533. return vips_jpegsave_buffer(
  534. in, buf, len,
  535. "Q", quality,
  536. "optimize_coding", TRUE,
  537. "interlace", interlace,
  538. NULL
  539. );
  540. }
  541. int
  542. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  543. if (!quantize)
  544. return vips_pngsave_buffer(
  545. in, buf, len,
  546. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  547. "interlace", interlace,
  548. NULL
  549. );
  550. int bitdepth = 1;
  551. if (colors > 16) bitdepth = 8;
  552. else if (colors > 4) bitdepth = 4;
  553. else if (colors > 2) bitdepth = 2;
  554. return vips_pngsave_buffer(
  555. in, buf, len,
  556. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  557. "interlace", interlace,
  558. #if VIPS_SUPPORT_PNG_BITDEPTH
  559. "palette", quantize,
  560. "bitdepth", bitdepth,
  561. #elif VIPS_SUPPORT_PNG_QUANTIZATION // VIPS_SUPPORT_PNG_BITDEPTH
  562. "palette", quantize,
  563. "colours", colors,
  564. #endif // VIPS_SUPPORT_PNG_QUANTIZATION
  565. NULL
  566. );
  567. }
  568. int
  569. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  570. return vips_webpsave_buffer(
  571. in, buf, len,
  572. "Q", quality,
  573. NULL
  574. );
  575. }
  576. int
  577. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  578. #if VIPS_SUPPORT_MAGICK
  579. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  580. #else
  581. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.7+ reuired)");
  582. return 1;
  583. #endif
  584. }
  585. int
  586. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  587. #if VIPS_SUPPORT_TIFF
  588. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  589. #else
  590. vips_error("vips_tiffsave_go", "Saving TIFF is not supported (libvips 8.6+ reuired)");
  591. return 1;
  592. #endif
  593. }
  594. int
  595. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  596. #if VIPS_SUPPORT_AVIF
  597. #if VIPS_SUPPORT_AVIF_SPEED
  598. return vips_heifsave_buffer(in, buf, len, "Q", quality, "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1, "speed", speed, NULL);
  599. #else
  600. return vips_heifsave_buffer(in, buf, len, "Q", quality, "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1, NULL);
  601. #endif
  602. #else
  603. vips_error("vips_avifsave_go", "Saving AVIF is not supported (libvips 8.9+ reuired)");
  604. return 1;
  605. #endif
  606. }
  607. int
  608. vips_bmpsave_go(VipsImage *in, void **buf, size_t *len) {
  609. #if VIPS_SUPPORT_MAGICK
  610. return vips_magicksave_buffer(in, buf, len, "format", "bmp", NULL);
  611. #else
  612. vips_error("vips_bmpsave_go", "Saving BMP is not supported");
  613. return 1;
  614. #endif
  615. }
  616. void
  617. vips_cleanup() {
  618. vips_error_clear();
  619. vips_thread_shutdown();
  620. }