vips.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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_has_embedded_icc(VipsImage *in) {
  300. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  301. }
  302. int
  303. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  304. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  305. }
  306. int
  307. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  308. return vips_icc_export(in, out, NULL);
  309. }
  310. int
  311. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  312. return vips_icc_export(in, out, "output_profile", "sRGB", NULL);
  313. }
  314. int
  315. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  316. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  317. }
  318. int
  319. vips_icc_remove(VipsImage *in, VipsImage **out) {
  320. if (vips_copy(in, out, NULL)) return 1;
  321. vips_image_remove(*out, VIPS_META_ICC_NAME);
  322. return 0;
  323. }
  324. int
  325. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  326. return vips_colourspace(in, out, cs, NULL);
  327. }
  328. int
  329. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  330. return vips_rot(in, out, angle, NULL);
  331. }
  332. int
  333. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  334. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  335. }
  336. int
  337. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  338. #if VIPS_SUPPORT_SMARTCROP
  339. return vips_smartcrop(in, out, width, height, NULL);
  340. #else
  341. vips_error("vips_smartcrop_go", "Smart crop is not supported (libvips 8.5+ reuired)");
  342. return 1;
  343. #endif
  344. }
  345. int
  346. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  347. return vips_gaussblur(in, out, sigma, NULL);
  348. }
  349. int
  350. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  351. return vips_sharpen(in, out, "sigma", sigma, NULL);
  352. }
  353. int
  354. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  355. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  356. int res = vips_flatten(in, out, "background", bg, NULL);
  357. vips_area_unref((VipsArea *)bg);
  358. return res;
  359. }
  360. int
  361. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  362. return vips_extract_area(in, out, left, top, width, height, NULL);
  363. }
  364. int
  365. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  366. gboolean smart, double r, double g, double b,
  367. gboolean equal_hor, gboolean equal_ver) {
  368. #if VIPS_SUPPORT_FIND_TRIM
  369. VipsImage *tmp;
  370. if (vips_image_hasalpha(in)) {
  371. if (vips_flatten_go(in, &tmp, 255.0, 0, 255.0))
  372. return 1;
  373. } else {
  374. if (vips_copy(in, &tmp, NULL))
  375. return 1;
  376. }
  377. double *bg;
  378. int bgn;
  379. VipsArrayDouble *bga;
  380. if (smart) {
  381. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  382. clear_image(&tmp);
  383. return 1;
  384. }
  385. bga = vips_array_double_new(bg, bgn);
  386. } else {
  387. bga = vips_array_double_newv(3, r, g, b);
  388. bg = 0;
  389. }
  390. int left, right, top, bot, width, height, diff;
  391. if (vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL)) {
  392. clear_image(&tmp);
  393. vips_area_unref((VipsArea *)bga);
  394. g_free(bg);
  395. return 1;
  396. }
  397. if (equal_hor) {
  398. right = in->Xsize - left - width;
  399. diff = right - left;
  400. if (diff > 0) {
  401. width += diff;
  402. } else if (diff < 0) {
  403. left = right;
  404. width -= diff;
  405. }
  406. }
  407. if (equal_ver) {
  408. bot = in->Ysize - top - height;
  409. diff = bot - top;
  410. if (diff > 0) {
  411. height += diff;
  412. } else if (diff < 0) {
  413. top = bot;
  414. height -= diff;
  415. }
  416. }
  417. clear_image(&tmp);
  418. vips_area_unref((VipsArea *)bga);
  419. g_free(bg);
  420. if (width == 0 || height == 0) {
  421. return vips_copy(in, out, NULL);
  422. }
  423. return vips_extract_area(in, out, left, top, width, height, NULL);
  424. #else
  425. vips_error("vips_trim", "Trim is not supported (libvips 8.6+ reuired)");
  426. return 1;
  427. #endif
  428. }
  429. int
  430. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  431. VipsImage *tmp;
  432. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  433. return 1;
  434. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  435. clear_image(&tmp);
  436. return 1;
  437. }
  438. clear_image(&tmp);
  439. return 0;
  440. }
  441. int
  442. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn) {
  443. VipsArrayDouble *bga = vips_array_double_new(bg, bgn);
  444. int ret = vips_embed(
  445. in, out, x, y, width, height,
  446. "extend", VIPS_EXTEND_BACKGROUND,
  447. "background", bga,
  448. NULL
  449. );
  450. vips_area_unref((VipsArea *)bga);
  451. return ret;
  452. }
  453. int
  454. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  455. if (vips_image_hasalpha_go(in)) {
  456. return vips_copy(in, out, NULL);
  457. }
  458. return vips_bandjoin_const1(in, out, 255, NULL);
  459. }
  460. int
  461. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  462. #if VIPS_SUPPORT_COMPOSITE
  463. VipsImage *base = vips_image_new();
  464. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  465. if (opacity < 1) {
  466. if (
  467. vips_extract_band(watermark, &t[0], 0, "n", watermark->Bands - 1, NULL) ||
  468. vips_extract_band(watermark, &t[1], watermark->Bands - 1, "n", 1, NULL) ||
  469. vips_linear1(t[1], &t[2], opacity, 0, NULL) ||
  470. vips_bandjoin2(t[0], t[2], &t[3], NULL)
  471. ) {
  472. clear_image(&base);
  473. return 1;
  474. }
  475. } else {
  476. if (vips_copy(watermark, &t[3], NULL)) {
  477. clear_image(&base);
  478. return 1;
  479. }
  480. }
  481. int res =
  482. vips_composite2(in, t[3], &t[4], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  483. vips_cast(t[4], out, vips_image_get_format(in), NULL);
  484. clear_image(&base);
  485. return res;
  486. #else
  487. vips_error("vips_apply_watermark", "Watermarking is not supported (libvips 8.6+ reuired)");
  488. return 1;
  489. #endif
  490. }
  491. int
  492. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  493. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  494. }
  495. int
  496. vips_strip(VipsImage *in, VipsImage **out) {
  497. if (vips_copy(in, out, NULL)) return 1;
  498. gchar **fields = vips_image_get_fields(in);
  499. for (int i = 0; fields[i] != NULL; i++) {
  500. gchar *name = fields[i];
  501. if (strcmp(name, VIPS_META_ICC_NAME) == 0) continue;
  502. vips_image_remove(*out, name);
  503. }
  504. g_strfreev(fields);
  505. return 0;
  506. }
  507. int
  508. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  509. return vips_jpegsave_buffer(
  510. in, buf, len,
  511. "Q", quality,
  512. "optimize_coding", TRUE,
  513. "interlace", interlace,
  514. NULL
  515. );
  516. }
  517. int
  518. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  519. if (!quantize)
  520. return vips_pngsave_buffer(
  521. in, buf, len,
  522. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  523. "interlace", interlace,
  524. NULL
  525. );
  526. int bitdepth = 1;
  527. if (colors > 16) bitdepth = 8;
  528. else if (colors > 4) bitdepth = 4;
  529. else if (colors > 2) bitdepth = 2;
  530. return vips_pngsave_buffer(
  531. in, buf, len,
  532. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  533. "interlace", interlace,
  534. #if VIPS_SUPPORT_PNG_BITDEPTH
  535. "palette", quantize,
  536. "bitdepth", bitdepth,
  537. #elif VIPS_SUPPORT_PNG_QUANTIZATION // VIPS_SUPPORT_PNG_BITDEPTH
  538. "palette", quantize,
  539. "colours", colors,
  540. #endif // VIPS_SUPPORT_PNG_QUANTIZATION
  541. NULL
  542. );
  543. }
  544. int
  545. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  546. return vips_webpsave_buffer(
  547. in, buf, len,
  548. "Q", quality,
  549. NULL
  550. );
  551. }
  552. int
  553. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  554. #if VIPS_SUPPORT_MAGICK
  555. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  556. #else
  557. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.7+ reuired)");
  558. return 1;
  559. #endif
  560. }
  561. int
  562. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  563. #if VIPS_SUPPORT_TIFF
  564. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  565. #else
  566. vips_error("vips_tiffsave_go", "Saving TIFF is not supported (libvips 8.6+ reuired)");
  567. return 1;
  568. #endif
  569. }
  570. int
  571. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  572. #if VIPS_SUPPORT_AVIF
  573. return vips_heifsave_buffer(
  574. in, buf, len,
  575. "Q", quality,
  576. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  577. #if VIPS_SUPPORT_AVIF_SPEED
  578. "speed", speed,
  579. #endif
  580. NULL);
  581. #else
  582. vips_error("vips_avifsave_go", "Saving AVIF is not supported (libvips 8.9+ reuired)");
  583. return 1;
  584. #endif
  585. }
  586. int
  587. vips_bmpsave_go(VipsImage *in, void **buf, size_t *len) {
  588. #if VIPS_SUPPORT_MAGICK
  589. return vips_magicksave_buffer(in, buf, len, "format", "bmp", NULL);
  590. #else
  591. vips_error("vips_bmpsave_go", "Saving BMP is not supported");
  592. return 1;
  593. #endif
  594. }
  595. void
  596. vips_cleanup() {
  597. vips_error_clear();
  598. vips_thread_shutdown();
  599. }