vips.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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), 4);
  164. int res =
  165. vips_premultiply(in, &t[0], NULL) ||
  166. vips_cast(t[0], &t[1], format, NULL) ||
  167. vips_resize(t[1], &t[2], wscale, "vscale", hscale, NULL) ||
  168. vips_unpremultiply(t[2], &t[3], NULL) ||
  169. vips_cast(t[3], out, format, NULL);
  170. clear_image(&base);
  171. return 0;
  172. }
  173. int
  174. vips_icc_is_srgb_iec61966(VipsImage *in) {
  175. const void *data;
  176. size_t data_len;
  177. // 1998-12-01
  178. static char date[] = { 7, 206, 0, 2, 0, 9 };
  179. // 2.1
  180. static char version[] = { 2, 16, 0, 0 };
  181. // The image had no profile and built-in CMYK was imported.
  182. // Vips gives us an invalid data pointer when the built-in profile was imported,
  183. // so we check this mark before receiving an actual profile.
  184. // if (vips_image_get_typeof(in, "icc-cmyk-no-profile"))
  185. // return FALSE;
  186. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  187. return FALSE;
  188. // Less than header size
  189. if (data_len < 128)
  190. return FALSE;
  191. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  192. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  193. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  194. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  195. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  196. (memcmp(data + 8, version, 4) == 0)); // Version
  197. }
  198. int
  199. vips_has_embedded_icc(VipsImage *in) {
  200. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  201. }
  202. int
  203. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  204. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  205. }
  206. int
  207. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  208. return vips_icc_export(in, out, "pcs", VIPS_PCS_LAB, NULL);
  209. }
  210. int
  211. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  212. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", VIPS_PCS_LAB, NULL);
  213. }
  214. int
  215. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  216. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  217. }
  218. int
  219. vips_icc_remove(VipsImage *in, VipsImage **out) {
  220. if (vips_copy(in, out, NULL)) return 1;
  221. vips_image_remove(*out, VIPS_META_ICC_NAME);
  222. vips_image_remove(*out, "exif-ifd0-WhitePoint");
  223. vips_image_remove(*out, "exif-ifd0-PrimaryChromaticities");
  224. vips_image_remove(*out, "exif-ifd2-ColorSpace");
  225. return 0;
  226. }
  227. int
  228. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  229. return vips_colourspace(in, out, cs, NULL);
  230. }
  231. int
  232. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  233. return vips_rot(in, out, angle, NULL);
  234. }
  235. int
  236. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  237. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  238. }
  239. int
  240. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  241. return vips_smartcrop(in, out, width, height, NULL);
  242. }
  243. int
  244. vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
  245. double sharp_sigma, int pixelate_pixels) {
  246. VipsImage *base = vips_image_new();
  247. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
  248. VipsInterpretation interpretation = in->Type;
  249. VipsBandFormat format = in->BandFmt;
  250. gboolean premultiplied = FALSE;
  251. if ((blur_sigma > 0 || sharp_sigma > 0) && vips_image_hasalpha(in)) {
  252. if (
  253. vips_premultiply(in, &t[0], NULL) ||
  254. vips_cast(t[0], &t[1], format, NULL)
  255. ) {
  256. clear_image(&base);
  257. return 1;
  258. }
  259. in = t[0];
  260. premultiplied = TRUE;
  261. }
  262. if (blur_sigma > 0.0) {
  263. if (vips_gaussblur(in, &t[2], blur_sigma, NULL)) {
  264. clear_image(&base);
  265. return 1;
  266. }
  267. in = t[2];
  268. }
  269. if (sharp_sigma > 0.0) {
  270. if (vips_sharpen(in, &t[3], "sigma", sharp_sigma, NULL)) {
  271. clear_image(&base);
  272. return 1;
  273. }
  274. in = t[3];
  275. }
  276. pixelate_pixels = VIPS_MIN(pixelate_pixels, VIPS_MAX(in->Xsize, in->Ysize));
  277. if (pixelate_pixels > 1) {
  278. int w, h, tw, th;
  279. w = in->Xsize;
  280. h = in->Ysize;
  281. tw = (int)(VIPS_CEIL((double)w / pixelate_pixels)) * pixelate_pixels;
  282. th = (int)(VIPS_CEIL((double)h / pixelate_pixels)) * pixelate_pixels;
  283. if (tw > w || th > h) {
  284. if (vips_embed(in, &t[4], 0, 0, tw, th, "extend", VIPS_EXTEND_MIRROR, NULL)) {
  285. clear_image(&base);
  286. return 1;
  287. }
  288. in = t[4];
  289. }
  290. if (
  291. vips_shrink(in, &t[5], pixelate_pixels, pixelate_pixels, NULL) ||
  292. vips_zoom(t[5], &t[6], pixelate_pixels, pixelate_pixels, NULL)
  293. ) {
  294. clear_image(&base);
  295. return 1;
  296. }
  297. in = t[6];
  298. if (tw > w || th > h) {
  299. if (vips_extract_area(in, &t[7], 0, 0, w, h, NULL)) {
  300. clear_image(&base);
  301. return 1;
  302. }
  303. in = t[7];
  304. }
  305. }
  306. if (premultiplied) {
  307. if (vips_unpremultiply(in, &t[8], NULL)) {
  308. clear_image(&base);
  309. return 1;
  310. }
  311. in = t[8];
  312. }
  313. int res =
  314. vips_colourspace(in, &t[9], interpretation, NULL) ||
  315. vips_cast(t[9], out, format, NULL);
  316. clear_image(&base);
  317. return res;
  318. }
  319. int
  320. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  321. if (!vips_image_hasalpha(in))
  322. return vips_copy(in, out, NULL);
  323. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  324. int res = vips_flatten(in, out, "background", bg, NULL);
  325. vips_area_unref((VipsArea *)bg);
  326. return res;
  327. }
  328. int
  329. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  330. return vips_extract_area(in, out, left, top, width, height, NULL);
  331. }
  332. int
  333. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  334. gboolean smart, double r, double g, double b,
  335. gboolean equal_hor, gboolean equal_ver) {
  336. VipsImage *base = vips_image_new();
  337. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  338. VipsImage *tmp = in;
  339. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  340. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  341. clear_image(&base);
  342. return 1;
  343. }
  344. tmp = t[0];
  345. }
  346. if (vips_image_hasalpha(tmp)) {
  347. if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
  348. clear_image(&base);
  349. return 1;
  350. }
  351. tmp = t[1];
  352. }
  353. double *bg = NULL;
  354. int bgn;
  355. VipsArrayDouble *bga;
  356. if (smart) {
  357. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  358. clear_image(&base);
  359. return 1;
  360. }
  361. bga = vips_array_double_new(bg, bgn);
  362. } else {
  363. bga = vips_array_double_newv(3, r, g, b);
  364. }
  365. int left, right, top, bot, width, height, diff;
  366. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  367. clear_image(&base);
  368. vips_area_unref((VipsArea *)bga);
  369. g_free(bg);
  370. if (res) {
  371. return 1;
  372. }
  373. if (equal_hor) {
  374. right = in->Xsize - left - width;
  375. diff = right - left;
  376. if (diff > 0) {
  377. width += diff;
  378. } else if (diff < 0) {
  379. left = right;
  380. width -= diff;
  381. }
  382. }
  383. if (equal_ver) {
  384. bot = in->Ysize - top - height;
  385. diff = bot - top;
  386. if (diff > 0) {
  387. height += diff;
  388. } else if (diff < 0) {
  389. top = bot;
  390. height -= diff;
  391. }
  392. }
  393. if (width == 0 || height == 0) {
  394. return vips_copy(in, out, NULL);
  395. }
  396. return vips_extract_area(in, out, left, top, width, height, NULL);
  397. }
  398. int
  399. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  400. VipsImage *tmp;
  401. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  402. return 1;
  403. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  404. clear_image(&tmp);
  405. return 1;
  406. }
  407. clear_image(&tmp);
  408. return 0;
  409. }
  410. int
  411. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  412. VipsImage *tmp = NULL;
  413. if (!vips_image_hasalpha(in)) {
  414. if (vips_bandjoin_const1(in, &tmp, 255, NULL))
  415. return 1;
  416. in = tmp;
  417. }
  418. int ret =
  419. vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  420. if (tmp) clear_image(&tmp);
  421. return ret;
  422. }
  423. int
  424. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top, double opacity) {
  425. VipsImage *base = vips_image_new();
  426. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  427. if (!vips_image_hasalpha(watermark)) {
  428. if (vips_bandjoin_const1(watermark, &t[0], 255, NULL))
  429. return 1;
  430. watermark = t[0];
  431. }
  432. if (opacity < 1) {
  433. if (
  434. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  435. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  436. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  437. vips_bandjoin2(t[1], t[3], &t[4], NULL)
  438. ) {
  439. clear_image(&base);
  440. return 1;
  441. }
  442. watermark = t[4];
  443. }
  444. int had_alpha = vips_image_hasalpha(in);
  445. if (
  446. vips_composite2(
  447. in, watermark, &t[5], VIPS_BLEND_MODE_OVER,
  448. "x", left, "y", top, "compositing_space", in->Type,
  449. NULL
  450. ) ||
  451. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)
  452. ) {
  453. clear_image(&base);
  454. return 1;
  455. }
  456. int res;
  457. if (!had_alpha && vips_image_hasalpha(t[6])) {
  458. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  459. } else {
  460. res = vips_copy(t[6], out, NULL);
  461. }
  462. clear_image(&base);
  463. return res;
  464. }
  465. int
  466. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  467. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  468. }
  469. int
  470. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright) {
  471. static double default_resolution = 72.0 / 25.4;
  472. if (vips_copy(
  473. in, out,
  474. "xres", default_resolution,
  475. "yres", default_resolution,
  476. NULL
  477. )) return 1;
  478. gchar **fields = vips_image_get_fields(in);
  479. for (int i = 0; fields[i] != NULL; i++) {
  480. gchar *name = fields[i];
  481. if (
  482. (strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  483. (strcmp(name, "palette-bit-depth") == 0) ||
  484. (strcmp(name, "width") == 0) ||
  485. (strcmp(name, "height") == 0) ||
  486. (strcmp(name, "bands") == 0) ||
  487. (strcmp(name, "format") == 0) ||
  488. (strcmp(name, "coding") == 0) ||
  489. (strcmp(name, "interpretation") == 0) ||
  490. (strcmp(name, "xoffset") == 0) ||
  491. (strcmp(name, "yoffset") == 0) ||
  492. (strcmp(name, "xres") == 0) ||
  493. (strcmp(name, "yres") == 0) ||
  494. (strcmp(name, "vips-loader") == 0) ||
  495. (strcmp(name, "background") == 0) ||
  496. (strcmp(name, "vips-sequential") == 0) ||
  497. (strcmp(name, "imgproxy-dpr-scale") == 0)
  498. ) continue;
  499. if (keep_exif_copyright) {
  500. if (
  501. (strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  502. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  503. (strcmp(name, "exif-ifd0-Artist") == 0)
  504. ) continue;
  505. }
  506. vips_image_remove(*out, name);
  507. }
  508. g_strfreev(fields);
  509. return 0;
  510. }
  511. int
  512. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  513. return vips_jpegsave_buffer(
  514. in, buf, len,
  515. "Q", quality,
  516. "optimize_coding", TRUE,
  517. "interlace", interlace,
  518. NULL
  519. );
  520. }
  521. int
  522. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  523. int bitdepth;
  524. if (quantize) {
  525. bitdepth = 1;
  526. if (colors > 16) bitdepth = 8;
  527. else if (colors > 4) bitdepth = 4;
  528. else if (colors > 2) bitdepth = 2;
  529. } else {
  530. bitdepth = vips_get_palette_bit_depth(in);
  531. if (bitdepth) {
  532. if (bitdepth > 4) bitdepth = 8;
  533. else if (bitdepth > 2) bitdepth = 4;
  534. quantize = 1;
  535. colors = 1 << bitdepth;
  536. }
  537. }
  538. if (!quantize)
  539. return vips_pngsave_buffer(
  540. in, buf, len,
  541. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  542. "interlace", interlace,
  543. NULL
  544. );
  545. return vips_pngsave_buffer(
  546. in, buf, len,
  547. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  548. "interlace", interlace,
  549. "palette", quantize,
  550. "bitdepth", bitdepth,
  551. NULL
  552. );
  553. }
  554. int
  555. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  556. return vips_webpsave_buffer(
  557. in, buf, len,
  558. "Q", quality,
  559. NULL
  560. );
  561. }
  562. int
  563. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  564. #if VIPS_SUPPORT_GIFSAVE
  565. return vips_gifsave_buffer(in, buf, len, NULL);
  566. #else
  567. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.12+ reuired)");
  568. return 1;
  569. #endif
  570. }
  571. int
  572. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  573. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  574. }
  575. int
  576. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  577. return vips_heifsave_buffer(
  578. in, buf, len,
  579. "Q", quality,
  580. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  581. #if VIPS_SUPPORT_AVIF_EFFORT
  582. "effort", 9-speed,
  583. #elif VIPS_SUPPORT_AVIF_SPEED
  584. "speed", speed,
  585. #endif
  586. NULL);
  587. }
  588. void
  589. vips_cleanup() {
  590. vips_error_clear();
  591. vips_thread_shutdown();
  592. }