vips.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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_is_animated(VipsImage * in) {
  225. return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
  226. vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
  227. vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
  228. }
  229. int
  230. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
  231. #if VIPS_SUPPORT_ARRAY_HEADERS
  232. return vips_image_get_array_int(image, name, out, n);
  233. #else
  234. vips_error("vips_image_get_array_int_go", "Array headers are not supported (libvips 8.9+ reuired)");
  235. return 1;
  236. #endif
  237. }
  238. void
  239. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
  240. #if VIPS_SUPPORT_ARRAY_HEADERS
  241. vips_image_set_array_int(image, name, array, n);
  242. #endif
  243. }
  244. gboolean
  245. vips_image_hasalpha_go(VipsImage * in) {
  246. #if VIPS_SUPPORT_HASALPHA
  247. return vips_image_hasalpha(in);
  248. #else
  249. return( in->Bands == 2 ||
  250. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  251. in->Bands > 4 );
  252. #endif
  253. }
  254. int
  255. vips_addalpha_go(VipsImage *in, VipsImage **out) {
  256. return vips_addalpha(in, out, NULL);
  257. }
  258. int
  259. vips_copy_go(VipsImage *in, VipsImage **out) {
  260. return vips_copy(in, out, NULL);
  261. }
  262. int
  263. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  264. return vips_cast(in, out, format, NULL);
  265. }
  266. int
  267. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  268. return vips_rad2float(in, out, NULL);
  269. }
  270. int
  271. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  272. return vips_resize(in, out, scale, NULL);
  273. }
  274. int
  275. vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale) {
  276. VipsBandFormat format;
  277. VipsImage *tmp1, *tmp2;
  278. format = vips_band_format(in);
  279. if (vips_premultiply(in, &tmp1, NULL))
  280. return 1;
  281. if (vips_resize(tmp1, &tmp2, scale, NULL)) {
  282. clear_image(&tmp1);
  283. return 1;
  284. }
  285. swap_and_clear(&tmp1, tmp2);
  286. if (vips_unpremultiply(tmp1, &tmp2, NULL)) {
  287. clear_image(&tmp1);
  288. return 1;
  289. }
  290. swap_and_clear(&tmp1, tmp2);
  291. if (vips_cast(tmp1, out, format, NULL)) {
  292. clear_image(&tmp1);
  293. return 1;
  294. }
  295. clear_image(&tmp1);
  296. return 0;
  297. }
  298. int
  299. vips_icc_is_srgb_iec61966(VipsImage *in) {
  300. VIPS_BLOB_DATA_TYPE data;
  301. size_t data_len;
  302. // 1998-12-01
  303. static char date[] = { 7, 206, 0, 2, 0, 9 };
  304. // 2.1
  305. static char version[] = { 2, 16, 0, 0 };
  306. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  307. return FALSE;
  308. // Less than header size
  309. if (data_len < 128)
  310. return FALSE;
  311. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  312. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  313. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  314. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  315. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  316. (memcmp(data + 8, version, 4) == 0)); // Version
  317. }
  318. int
  319. vips_has_embedded_icc(VipsImage *in) {
  320. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  321. }
  322. int
  323. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  324. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  325. }
  326. int
  327. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  328. return vips_icc_export(in, out, NULL);
  329. }
  330. int
  331. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  332. return vips_icc_export(in, out, "output_profile", "sRGB", NULL);
  333. }
  334. int
  335. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  336. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  337. }
  338. int
  339. vips_icc_remove(VipsImage *in, VipsImage **out) {
  340. if (vips_copy(in, out, NULL)) return 1;
  341. vips_image_remove(*out, VIPS_META_ICC_NAME);
  342. return 0;
  343. }
  344. int
  345. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  346. return vips_colourspace(in, out, cs, NULL);
  347. }
  348. int
  349. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  350. return vips_rot(in, out, angle, NULL);
  351. }
  352. int
  353. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  354. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  355. }
  356. int
  357. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  358. #if VIPS_SUPPORT_SMARTCROP
  359. return vips_smartcrop(in, out, width, height, NULL);
  360. #else
  361. vips_error("vips_smartcrop_go", "Smart crop is not supported (libvips 8.5+ reuired)");
  362. return 1;
  363. #endif
  364. }
  365. int
  366. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  367. return vips_gaussblur(in, out, sigma, NULL);
  368. }
  369. int
  370. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  371. return vips_sharpen(in, out, "sigma", sigma, NULL);
  372. }
  373. int
  374. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  375. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  376. int res = vips_flatten(in, out, "background", bg, NULL);
  377. vips_area_unref((VipsArea *)bg);
  378. return res;
  379. }
  380. int
  381. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  382. return vips_extract_area(in, out, left, top, width, height, NULL);
  383. }
  384. int
  385. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  386. gboolean smart, double r, double g, double b,
  387. gboolean equal_hor, gboolean equal_ver) {
  388. #if VIPS_SUPPORT_FIND_TRIM
  389. VipsImage *tmp;
  390. if (vips_image_hasalpha(in)) {
  391. if (vips_flatten_go(in, &tmp, 255.0, 0, 255.0))
  392. return 1;
  393. } else {
  394. if (vips_copy(in, &tmp, NULL))
  395. return 1;
  396. }
  397. double *bg;
  398. int bgn;
  399. VipsArrayDouble *bga;
  400. if (smart) {
  401. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  402. clear_image(&tmp);
  403. return 1;
  404. }
  405. bga = vips_array_double_new(bg, bgn);
  406. } else {
  407. bga = vips_array_double_newv(3, r, g, b);
  408. bg = 0;
  409. }
  410. int left, right, top, bot, width, height, diff;
  411. if (vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL)) {
  412. clear_image(&tmp);
  413. vips_area_unref((VipsArea *)bga);
  414. g_free(bg);
  415. return 1;
  416. }
  417. if (equal_hor) {
  418. right = in->Xsize - left - width;
  419. diff = right - left;
  420. if (diff > 0) {
  421. width += diff;
  422. } else if (diff < 0) {
  423. left = right;
  424. width -= diff;
  425. }
  426. }
  427. if (equal_ver) {
  428. bot = in->Ysize - top - height;
  429. diff = bot - top;
  430. if (diff > 0) {
  431. height += diff;
  432. } else if (diff < 0) {
  433. top = bot;
  434. height -= diff;
  435. }
  436. }
  437. clear_image(&tmp);
  438. vips_area_unref((VipsArea *)bga);
  439. g_free(bg);
  440. if (width == 0 || height == 0) {
  441. return vips_copy(in, out, NULL);
  442. }
  443. return vips_extract_area(in, out, left, top, width, height, NULL);
  444. #else
  445. vips_error("vips_trim", "Trim is not supported (libvips 8.6+ reuired)");
  446. return 1;
  447. #endif
  448. }
  449. int
  450. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  451. VipsImage *tmp;
  452. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  453. return 1;
  454. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  455. clear_image(&tmp);
  456. return 1;
  457. }
  458. clear_image(&tmp);
  459. return 0;
  460. }
  461. int
  462. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn) {
  463. VipsArrayDouble *bga = vips_array_double_new(bg, bgn);
  464. int ret = vips_embed(
  465. in, out, x, y, width, height,
  466. "extend", VIPS_EXTEND_BACKGROUND,
  467. "background", bga,
  468. NULL
  469. );
  470. vips_area_unref((VipsArea *)bga);
  471. return ret;
  472. }
  473. int
  474. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  475. if (vips_image_hasalpha_go(in)) {
  476. return vips_copy(in, out, NULL);
  477. }
  478. return vips_bandjoin_const1(in, out, 255, NULL);
  479. }
  480. int
  481. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  482. #if VIPS_SUPPORT_COMPOSITE
  483. VipsImage *base = vips_image_new();
  484. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  485. if (opacity < 1) {
  486. if (
  487. vips_extract_band(watermark, &t[0], 0, "n", watermark->Bands - 1, NULL) ||
  488. vips_extract_band(watermark, &t[1], watermark->Bands - 1, "n", 1, NULL) ||
  489. vips_linear1(t[1], &t[2], opacity, 0, NULL) ||
  490. vips_bandjoin2(t[0], t[2], &t[3], NULL)
  491. ) {
  492. clear_image(&base);
  493. return 1;
  494. }
  495. } else {
  496. if (vips_copy(watermark, &t[3], NULL)) {
  497. clear_image(&base);
  498. return 1;
  499. }
  500. }
  501. int res =
  502. vips_composite2(in, t[3], &t[4], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  503. vips_cast(t[4], out, vips_image_get_format(in), NULL);
  504. clear_image(&base);
  505. return res;
  506. #else
  507. vips_error("vips_apply_watermark", "Watermarking is not supported (libvips 8.6+ reuired)");
  508. return 1;
  509. #endif
  510. }
  511. int
  512. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  513. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  514. }
  515. int
  516. vips_strip(VipsImage *in, VipsImage **out) {
  517. if (vips_copy(in, out, NULL)) return 1;
  518. gchar **fields = vips_image_get_fields(in);
  519. for (int i = 0; fields[i] != NULL; i++) {
  520. gchar *name = fields[i];
  521. if (strcmp(name, VIPS_META_ICC_NAME) == 0) continue;
  522. vips_image_remove(*out, name);
  523. }
  524. g_strfreev(fields);
  525. return 0;
  526. }
  527. int
  528. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  529. return vips_jpegsave_buffer(
  530. in, buf, len,
  531. "Q", quality,
  532. "optimize_coding", TRUE,
  533. "interlace", interlace,
  534. NULL
  535. );
  536. }
  537. int
  538. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  539. if (!quantize)
  540. return vips_pngsave_buffer(
  541. in, buf, len,
  542. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  543. "interlace", interlace,
  544. NULL
  545. );
  546. int bitdepth = 1;
  547. if (colors > 16) bitdepth = 8;
  548. else if (colors > 4) bitdepth = 4;
  549. else if (colors > 2) bitdepth = 2;
  550. return vips_pngsave_buffer(
  551. in, buf, len,
  552. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  553. "interlace", interlace,
  554. #if VIPS_SUPPORT_PNG_BITDEPTH
  555. "palette", quantize,
  556. "bitdepth", bitdepth,
  557. #elif VIPS_SUPPORT_PNG_QUANTIZATION // VIPS_SUPPORT_PNG_BITDEPTH
  558. "palette", quantize,
  559. "colours", colors,
  560. #endif // VIPS_SUPPORT_PNG_QUANTIZATION
  561. NULL
  562. );
  563. }
  564. int
  565. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  566. return vips_webpsave_buffer(
  567. in, buf, len,
  568. "Q", quality,
  569. NULL
  570. );
  571. }
  572. int
  573. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  574. #if VIPS_SUPPORT_MAGICK
  575. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  576. #else
  577. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.7+ reuired)");
  578. return 1;
  579. #endif
  580. }
  581. int
  582. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  583. #if VIPS_SUPPORT_TIFF
  584. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  585. #else
  586. vips_error("vips_tiffsave_go", "Saving TIFF is not supported (libvips 8.6+ reuired)");
  587. return 1;
  588. #endif
  589. }
  590. int
  591. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  592. #if VIPS_SUPPORT_AVIF
  593. return vips_heifsave_buffer(
  594. in, buf, len,
  595. "Q", quality,
  596. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  597. #if VIPS_SUPPORT_AVIF_SPEED
  598. "speed", speed,
  599. #endif
  600. NULL);
  601. #else
  602. vips_error("vips_avifsave_go", "Saving AVIF is not supported (libvips 8.9+ reuired)");
  603. return 1;
  604. #endif
  605. }
  606. int
  607. vips_bmpsave_go(VipsImage *in, void **buf, size_t *len) {
  608. #if VIPS_SUPPORT_MAGICK
  609. return vips_magicksave_buffer(in, buf, len, "format", "bmp", NULL);
  610. #else
  611. vips_error("vips_bmpsave_go", "Saving BMP is not supported");
  612. return 1;
  613. #endif
  614. }
  615. void
  616. vips_cleanup() {
  617. vips_error_clear();
  618. vips_thread_shutdown();
  619. }