vips.c 18 KB

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