vips.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #include "vips.h"
  2. #include <string.h>
  3. #define VIPS_SUPPORT_AVIF_SPEED \
  4. (VIPS_MAJOR_VERSION > 8 || \
  5. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION > 10) || \
  6. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10 && VIPS_MICRO_VERSION >= 2))
  7. #define VIPS_SUPPORT_AVIF_EFFORT \
  8. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 12))
  9. #define VIPS_SUPPORT_GIFSAVE \
  10. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 12))
  11. #define VIPS_GIF_RESOLUTION_LIMITED \
  12. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION <= 12)
  13. int
  14. vips_initialize() {
  15. return vips_init("imgproxy");
  16. }
  17. void
  18. clear_image(VipsImage **in) {
  19. if (G_IS_OBJECT(*in)) g_clear_object(in);
  20. }
  21. void
  22. g_free_go(void **buf) {
  23. g_free(*buf);
  24. }
  25. void
  26. swap_and_clear(VipsImage **in, VipsImage *out) {
  27. clear_image(in);
  28. *in = out;
  29. }
  30. int
  31. gif_resolution_limit() {
  32. #if VIPS_GIF_RESOLUTION_LIMITED
  33. // https://github.com/libvips/libvips/blob/v8.12.2/libvips/foreign/cgifsave.c#L437-L442
  34. return 2000 * 2000;
  35. #else
  36. return INT_MAX / 4;
  37. #endif
  38. }
  39. int
  40. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  41. if (shrink > 1)
  42. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  43. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  44. }
  45. int
  46. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  47. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  48. }
  49. int
  50. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out) {
  51. return vips_webpload_buffer(
  52. buf, len, out,
  53. "access", VIPS_ACCESS_SEQUENTIAL,
  54. "scale", scale,
  55. "n", pages,
  56. NULL
  57. );
  58. }
  59. int
  60. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  61. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  62. }
  63. int
  64. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
  65. // libvips limits the minimal scale to 0.001, so we have to scale down dpi
  66. // for lower scale values
  67. double dpi = 72.0;
  68. if (scale < 0.001) {
  69. dpi *= VIPS_MAX(scale / 0.001, 0.001);
  70. scale = 0.001;
  71. }
  72. return vips_svgload_buffer(
  73. buf, len, out,
  74. "access", VIPS_ACCESS_SEQUENTIAL,
  75. "scale", scale,
  76. "dpi", dpi,
  77. NULL
  78. );
  79. }
  80. int
  81. vips_heifload_go(void *buf, size_t len, VipsImage **out, int thumbnail) {
  82. return vips_heifload_buffer(
  83. buf, len, out,
  84. "access", VIPS_ACCESS_SEQUENTIAL,
  85. "thumbnail", thumbnail,
  86. NULL
  87. );
  88. }
  89. int
  90. vips_tiffload_go(void *buf, size_t len, VipsImage **out) {
  91. return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  92. }
  93. int
  94. vips_black_go(VipsImage **out, int width, int height, int bands) {
  95. VipsImage *tmp;
  96. int res = vips_black(&tmp, width, height, "bands", bands, NULL) ||
  97. vips_copy(tmp, out, "interpretation", VIPS_INTERPRETATION_sRGB, NULL);
  98. clear_image(&tmp);
  99. return res;
  100. }
  101. int
  102. vips_get_orientation(VipsImage *image) {
  103. int orientation;
  104. if (
  105. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  106. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0
  107. ) return orientation;
  108. return 1;
  109. }
  110. int
  111. vips_get_palette_bit_depth(VipsImage *image) {
  112. int palette_bit_depth;
  113. if (
  114. vips_image_get_typeof(image, "palette-bit-depth") == G_TYPE_INT &&
  115. vips_image_get_int(image, "palette-bit-depth", &palette_bit_depth) == 0
  116. ) return palette_bit_depth;
  117. return 0;
  118. }
  119. VipsBandFormat
  120. vips_band_format(VipsImage *in) {
  121. return in->BandFmt;
  122. }
  123. gboolean
  124. vips_is_animated(VipsImage * in) {
  125. int n_pages;
  126. return( vips_image_get_typeof(in, "delay") != G_TYPE_INVALID &&
  127. vips_image_get_typeof(in, "loop") != G_TYPE_INVALID &&
  128. vips_image_get_typeof(in, "page-height") == G_TYPE_INT &&
  129. vips_image_get_typeof(in, "n-pages") == G_TYPE_INT &&
  130. vips_image_get_int(in, "n-pages", &n_pages) == 0 &&
  131. n_pages > 1 );
  132. }
  133. int
  134. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
  135. return vips_image_get_array_int(image, name, out, n);
  136. }
  137. void
  138. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
  139. vips_image_set_array_int(image, name, array, n);
  140. }
  141. int
  142. vips_addalpha_go(VipsImage *in, VipsImage **out) {
  143. return vips_addalpha(in, out, NULL);
  144. }
  145. int
  146. vips_copy_go(VipsImage *in, VipsImage **out) {
  147. return vips_copy(in, out, NULL);
  148. }
  149. int
  150. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  151. return vips_cast(in, out, format, NULL);
  152. }
  153. int
  154. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  155. return vips_rad2float(in, out, NULL);
  156. }
  157. int
  158. vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale) {
  159. if (!vips_image_hasalpha(in))
  160. return vips_resize(in, out, wscale, "vscale", hscale, NULL);
  161. VipsBandFormat format = vips_band_format(in);
  162. VipsImage *base = vips_image_new();
  163. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 3);
  164. int res =
  165. vips_premultiply(in, &t[0], NULL) ||
  166. vips_resize(t[0], &t[1], wscale, "vscale", hscale, NULL) ||
  167. vips_unpremultiply(t[1], &t[2], NULL) ||
  168. vips_cast(t[2], out, format, NULL);
  169. clear_image(&base);
  170. return 0;
  171. }
  172. int
  173. vips_icc_is_srgb_iec61966(VipsImage *in) {
  174. const void *data;
  175. size_t data_len;
  176. // 1998-12-01
  177. static char date[] = { 7, 206, 0, 2, 0, 9 };
  178. // 2.1
  179. static char version[] = { 2, 16, 0, 0 };
  180. // The image had no profile and built-in CMYK was imported.
  181. // Vips gives us an invalid data pointer when the built-in profile was imported,
  182. // so we check this mark before receiving an actual profile.
  183. // if (vips_image_get_typeof(in, "icc-cmyk-no-profile"))
  184. // return FALSE;
  185. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  186. return FALSE;
  187. // Less than header size
  188. if (data_len < 128)
  189. return FALSE;
  190. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  191. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  192. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  193. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  194. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  195. (memcmp(data + 8, version, 4) == 0)); // Version
  196. }
  197. int
  198. vips_has_embedded_icc(VipsImage *in) {
  199. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  200. }
  201. int
  202. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  203. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  204. }
  205. int
  206. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  207. return vips_icc_export(in, out, "pcs", VIPS_PCS_LAB, NULL);
  208. }
  209. int
  210. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  211. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", VIPS_PCS_LAB, NULL);
  212. }
  213. int
  214. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  215. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  216. }
  217. int
  218. vips_icc_remove(VipsImage *in, VipsImage **out) {
  219. if (vips_copy(in, out, NULL)) return 1;
  220. vips_image_remove(*out, VIPS_META_ICC_NAME);
  221. vips_image_remove(*out, "exif-ifd0-WhitePoint");
  222. vips_image_remove(*out, "exif-ifd0-PrimaryChromaticities");
  223. vips_image_remove(*out, "exif-ifd2-ColorSpace");
  224. return 0;
  225. }
  226. int
  227. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  228. return vips_colourspace(in, out, cs, NULL);
  229. }
  230. int
  231. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  232. return vips_rot(in, out, angle, NULL);
  233. }
  234. int
  235. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  236. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  237. }
  238. int
  239. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  240. return vips_smartcrop(in, out, width, height, NULL);
  241. }
  242. int
  243. vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
  244. double sharp_sigma, int pixelate_pixels) {
  245. VipsImage *base = vips_image_new();
  246. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 9);
  247. VipsInterpretation interpretation = in->Type;
  248. VipsBandFormat format = in->BandFmt;
  249. gboolean premultiplied = FALSE;
  250. if ((blur_sigma > 0 || sharp_sigma > 0) && vips_image_hasalpha(in)) {
  251. if (vips_premultiply(in, &t[0], NULL)) {
  252. clear_image(&base);
  253. return 1;
  254. }
  255. in = t[0];
  256. premultiplied = TRUE;
  257. }
  258. if (blur_sigma > 0.0) {
  259. if (vips_gaussblur(in, &t[1], blur_sigma, NULL)) {
  260. clear_image(&base);
  261. return 1;
  262. }
  263. in = t[1];
  264. }
  265. if (sharp_sigma > 0.0) {
  266. if (vips_sharpen(in, &t[2], "sigma", sharp_sigma, NULL)) {
  267. clear_image(&base);
  268. return 1;
  269. }
  270. in = t[2];
  271. }
  272. pixelate_pixels = VIPS_MIN(pixelate_pixels, VIPS_MAX(in->Xsize, in->Ysize));
  273. if (pixelate_pixels > 1) {
  274. int w, h, tw, th;
  275. w = in->Xsize;
  276. h = in->Ysize;
  277. tw = (int)(VIPS_CEIL((double)w / pixelate_pixels)) * pixelate_pixels;
  278. th = (int)(VIPS_CEIL((double)h / pixelate_pixels)) * pixelate_pixels;
  279. if (tw > w || th > h) {
  280. if (vips_embed(in, &t[3], 0, 0, tw, th, "extend", VIPS_EXTEND_MIRROR, NULL)) {
  281. clear_image(&base);
  282. return 1;
  283. }
  284. in = t[3];
  285. }
  286. if (
  287. vips_shrink(in, &t[4], pixelate_pixels, pixelate_pixels, NULL) ||
  288. vips_zoom(t[4], &t[5], pixelate_pixels, pixelate_pixels, NULL)
  289. ) {
  290. clear_image(&base);
  291. return 1;
  292. }
  293. in = t[5];
  294. if (tw > w || th > h) {
  295. if (vips_extract_area(in, &t[6], 0, 0, w, h, NULL)) {
  296. clear_image(&base);
  297. return 1;
  298. }
  299. in = t[6];
  300. }
  301. }
  302. if (premultiplied) {
  303. if (vips_unpremultiply(in, &t[7], NULL)) {
  304. clear_image(&base);
  305. return 1;
  306. }
  307. in = t[7];
  308. }
  309. int res =
  310. vips_colourspace(in, &t[8], interpretation, NULL) ||
  311. vips_cast(t[8], out, format, NULL);
  312. clear_image(&base);
  313. return res;
  314. }
  315. int
  316. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  317. if (!vips_image_hasalpha(in))
  318. return vips_copy(in, out, NULL);
  319. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  320. int res = vips_flatten(in, out, "background", bg, NULL);
  321. vips_area_unref((VipsArea *)bg);
  322. return res;
  323. }
  324. int
  325. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  326. return vips_extract_area(in, out, left, top, width, height, NULL);
  327. }
  328. int
  329. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  330. gboolean smart, double r, double g, double b,
  331. gboolean equal_hor, gboolean equal_ver) {
  332. VipsImage *base = vips_image_new();
  333. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  334. VipsImage *tmp = in;
  335. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  336. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  337. clear_image(&base);
  338. return 1;
  339. }
  340. tmp = t[0];
  341. }
  342. if (vips_image_hasalpha(tmp)) {
  343. if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
  344. clear_image(&base);
  345. return 1;
  346. }
  347. tmp = t[1];
  348. }
  349. double *bg = NULL;
  350. int bgn;
  351. VipsArrayDouble *bga;
  352. if (smart) {
  353. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  354. clear_image(&base);
  355. return 1;
  356. }
  357. bga = vips_array_double_new(bg, bgn);
  358. } else {
  359. bga = vips_array_double_newv(3, r, g, b);
  360. }
  361. int left, right, top, bot, width, height, diff;
  362. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  363. clear_image(&base);
  364. vips_area_unref((VipsArea *)bga);
  365. g_free(bg);
  366. if (res) {
  367. return 1;
  368. }
  369. if (equal_hor) {
  370. right = in->Xsize - left - width;
  371. diff = right - left;
  372. if (diff > 0) {
  373. width += diff;
  374. } else if (diff < 0) {
  375. left = right;
  376. width -= diff;
  377. }
  378. }
  379. if (equal_ver) {
  380. bot = in->Ysize - top - height;
  381. diff = bot - top;
  382. if (diff > 0) {
  383. height += diff;
  384. } else if (diff < 0) {
  385. top = bot;
  386. height -= diff;
  387. }
  388. }
  389. if (width == 0 || height == 0) {
  390. return vips_copy(in, out, NULL);
  391. }
  392. return vips_extract_area(in, out, left, top, width, height, NULL);
  393. }
  394. int
  395. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  396. VipsImage *tmp;
  397. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  398. return 1;
  399. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  400. clear_image(&tmp);
  401. return 1;
  402. }
  403. clear_image(&tmp);
  404. return 0;
  405. }
  406. int
  407. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  408. VipsImage *tmp = NULL;
  409. if (!vips_image_hasalpha(in)) {
  410. if (vips_bandjoin_const1(in, &tmp, 255, NULL))
  411. return 1;
  412. in = tmp;
  413. }
  414. int ret =
  415. vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  416. if (tmp) clear_image(&tmp);
  417. return ret;
  418. }
  419. int
  420. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  421. VipsImage *base = vips_image_new();
  422. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  423. if (!vips_image_hasalpha(watermark)) {
  424. if (vips_bandjoin_const1(watermark, &t[0], 255, NULL))
  425. return 1;
  426. watermark = t[0];
  427. }
  428. if (opacity < 1) {
  429. if (
  430. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  431. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  432. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  433. vips_bandjoin2(t[1], t[3], &t[4], NULL)
  434. ) {
  435. clear_image(&base);
  436. return 1;
  437. }
  438. watermark = t[4];
  439. }
  440. int had_alpha = vips_image_hasalpha(in);
  441. if (
  442. vips_composite2(in, watermark, &t[5], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  443. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)
  444. ) {
  445. clear_image(&base);
  446. return 1;
  447. }
  448. int res;
  449. if (!had_alpha && vips_image_hasalpha(t[6])) {
  450. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  451. } else {
  452. res = vips_copy(t[6], out, NULL);
  453. }
  454. clear_image(&base);
  455. return res;
  456. }
  457. int
  458. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  459. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  460. }
  461. int
  462. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright) {
  463. static double default_resolution = 72.0 / 25.4;
  464. if (vips_copy(
  465. in, out,
  466. "xres", default_resolution,
  467. "yres", default_resolution,
  468. NULL
  469. )) return 1;
  470. gchar **fields = vips_image_get_fields(in);
  471. for (int i = 0; fields[i] != NULL; i++) {
  472. gchar *name = fields[i];
  473. if (
  474. (strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  475. (strcmp(name, "palette-bit-depth") == 0) ||
  476. (strcmp(name, "width") == 0) ||
  477. (strcmp(name, "height") == 0) ||
  478. (strcmp(name, "bands") == 0) ||
  479. (strcmp(name, "format") == 0) ||
  480. (strcmp(name, "coding") == 0) ||
  481. (strcmp(name, "interpretation") == 0) ||
  482. (strcmp(name, "xoffset") == 0) ||
  483. (strcmp(name, "yoffset") == 0) ||
  484. (strcmp(name, "xres") == 0) ||
  485. (strcmp(name, "yres") == 0) ||
  486. (strcmp(name, "vips-loader") == 0) ||
  487. (strcmp(name, "background") == 0) ||
  488. (strcmp(name, "vips-sequential") == 0)
  489. ) continue;
  490. if (keep_exif_copyright) {
  491. if (
  492. (strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  493. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  494. (strcmp(name, "exif-ifd0-Artist") == 0)
  495. ) continue;
  496. }
  497. vips_image_remove(*out, name);
  498. }
  499. g_strfreev(fields);
  500. return 0;
  501. }
  502. int
  503. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  504. return vips_jpegsave_buffer(
  505. in, buf, len,
  506. "Q", quality,
  507. "optimize_coding", TRUE,
  508. "interlace", interlace,
  509. NULL
  510. );
  511. }
  512. int
  513. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  514. int bitdepth;
  515. if (quantize) {
  516. bitdepth = 1;
  517. if (colors > 16) bitdepth = 8;
  518. else if (colors > 4) bitdepth = 4;
  519. else if (colors > 2) bitdepth = 2;
  520. } else {
  521. bitdepth = vips_get_palette_bit_depth(in);
  522. if (bitdepth) {
  523. if (bitdepth > 4) bitdepth = 8;
  524. else if (bitdepth > 2) bitdepth = 4;
  525. quantize = 1;
  526. colors = 1 << bitdepth;
  527. }
  528. }
  529. if (!quantize)
  530. return vips_pngsave_buffer(
  531. in, buf, len,
  532. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  533. "interlace", interlace,
  534. NULL
  535. );
  536. return vips_pngsave_buffer(
  537. in, buf, len,
  538. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  539. "interlace", interlace,
  540. "palette", quantize,
  541. "bitdepth", bitdepth,
  542. NULL
  543. );
  544. }
  545. int
  546. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  547. return vips_webpsave_buffer(
  548. in, buf, len,
  549. "Q", quality,
  550. NULL
  551. );
  552. }
  553. int
  554. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  555. #if VIPS_SUPPORT_GIFSAVE
  556. return vips_gifsave_buffer(in, buf, len, NULL);
  557. #else
  558. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.12+ reuired)");
  559. return 1;
  560. #endif
  561. }
  562. int
  563. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  564. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  565. }
  566. int
  567. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  568. return vips_heifsave_buffer(
  569. in, buf, len,
  570. "Q", quality,
  571. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  572. #if VIPS_SUPPORT_AVIF_EFFORT
  573. "effort", 9-speed,
  574. #elif VIPS_SUPPORT_AVIF_SPEED
  575. "speed", speed,
  576. #endif
  577. NULL);
  578. }
  579. void
  580. vips_cleanup() {
  581. vips_error_clear();
  582. vips_thread_shutdown();
  583. }