vips.c 18 KB

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