1
0

vips.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. #include "vips.h"
  2. #include <string.h>
  3. #define VIPS_SCRGB_ALPHA_FIXED \
  4. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 15))
  5. #define VIPS_META_PALETTE_BITS_DEPTH "palette-bit-depth"
  6. int
  7. vips_initialize()
  8. {
  9. return vips_init("imgproxy");
  10. }
  11. void
  12. clear_image(VipsImage **in)
  13. {
  14. if (G_IS_OBJECT(*in))
  15. g_clear_object(in);
  16. }
  17. void
  18. g_free_go(void **buf)
  19. {
  20. g_free(*buf);
  21. }
  22. void
  23. swap_and_clear(VipsImage **in, VipsImage *out)
  24. {
  25. clear_image(in);
  26. *in = out;
  27. }
  28. int
  29. gif_resolution_limit()
  30. {
  31. return INT_MAX / 4;
  32. }
  33. // Just create and destroy a tiny image to ensure vips is operational
  34. int
  35. vips_health()
  36. {
  37. VipsImage *base = vips_image_new();
  38. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  39. int res = vips_black(&t[0], 4, 4, "bands", 4, NULL) ||
  40. !(t[1] = vips_image_copy_memory(t[0]));
  41. clear_image(&base);
  42. return res;
  43. }
  44. int
  45. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out)
  46. {
  47. if (shrink > 1)
  48. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink,
  49. NULL);
  50. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  51. }
  52. int
  53. vips_pngload_go(void *buf, size_t len, VipsImage **out)
  54. {
  55. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  56. }
  57. int
  58. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out)
  59. {
  60. return vips_webpload_buffer(
  61. buf, len, out,
  62. "access", VIPS_ACCESS_SEQUENTIAL,
  63. "scale", scale,
  64. "n", pages,
  65. NULL);
  66. }
  67. int
  68. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out)
  69. {
  70. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  71. }
  72. int
  73. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out)
  74. {
  75. // libvips limits the minimal scale to 0.001, so we have to scale down dpi
  76. // for lower scale values
  77. double dpi = 72.0;
  78. if (scale < 0.001) {
  79. dpi *= VIPS_MAX(scale / 0.001, 0.001);
  80. scale = 0.001;
  81. }
  82. return vips_svgload_buffer(
  83. buf, len, out,
  84. "access", VIPS_ACCESS_SEQUENTIAL,
  85. "scale", scale,
  86. "dpi", dpi,
  87. NULL);
  88. }
  89. int
  90. vips_heifload_go(void *buf, size_t len, VipsImage **out, int thumbnail)
  91. {
  92. return vips_heifload_buffer(
  93. buf, len, out,
  94. "access", VIPS_ACCESS_SEQUENTIAL,
  95. "thumbnail", thumbnail,
  96. NULL);
  97. }
  98. int
  99. vips_tiffload_go(void *buf, size_t len, VipsImage **out)
  100. {
  101. return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  102. }
  103. int
  104. vips_black_go(VipsImage **out, int width, int height, int bands)
  105. {
  106. VipsImage *tmp;
  107. int res = vips_black(&tmp, width, height, "bands", bands, NULL) ||
  108. vips_copy(tmp, out, "interpretation", VIPS_INTERPRETATION_sRGB, NULL);
  109. clear_image(&tmp);
  110. return res;
  111. }
  112. int
  113. vips_fix_scRGB_alpha_tiff(VipsImage *in, VipsImage **out)
  114. {
  115. #if VIPS_SCRGB_ALPHA_FIXED
  116. /* Vips 8.15+ uses 0.0-1.0 range for linear alpha, so we don't need a fix.
  117. */
  118. return vips_copy(in, out, NULL);
  119. #else
  120. /* Vips prior to 8.14 loads linear alpha in the 0.0-1.0 range but uses the 0.0-255.0 range.
  121. */
  122. VipsImage *base = vips_image_new();
  123. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 4);
  124. int res =
  125. vips_extract_band(in, &t[0], 0, "n", 3, NULL) ||
  126. vips_extract_band(in, &t[1], 3, "n", in->Bands - 3, NULL) ||
  127. vips_linear1(t[1], &t[2], 255.0, 0, NULL) ||
  128. vips_cast(t[2], &t[3], in->BandFmt, NULL) ||
  129. vips_bandjoin2(t[0], t[3], out, NULL);
  130. clear_image(&base);
  131. return res;
  132. #endif
  133. }
  134. /* Vips loads linear BW TIFFs as VIPS_INTERPRETATION_B_W or VIPS_INTERPRETATION_GREY16
  135. * but these colourspaces are not linear. We should properly convert them to
  136. * VIPS_INTERPRETATION_GREY16
  137. */
  138. int
  139. vips_fix_BW_float_tiff(VipsImage *in, VipsImage **out)
  140. {
  141. VipsImage *base = vips_image_new();
  142. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 8);
  143. VipsImage *color = in;
  144. VipsImage *alpha = NULL;
  145. /* Extract and fix alpha. Float WB TIFF uses the 0.0-1.0 range but we need
  146. * the 0.0-65535.0 range
  147. */
  148. if (in->Bands > 1) {
  149. if (
  150. vips_extract_band(in, &t[0], 0, NULL) ||
  151. vips_extract_band(in, &t[1], 1, "n", in->Bands - 1, NULL) ||
  152. vips_linear1(t[1], &t[2], 65535.0, 0, NULL) ||
  153. vips_cast_ushort(t[2], &t[3], NULL) ||
  154. vips_copy(t[3], &t[4], "interpretation", VIPS_INTERPRETATION_GREY16, NULL)) {
  155. clear_image(&base);
  156. return 1;
  157. }
  158. color = t[0];
  159. alpha = t[4];
  160. }
  161. /* Craft an scRGB image and convert it back to GREY16 to apply a gamma
  162. * correction
  163. */
  164. VipsImage *rgb[3] = { color, color, color };
  165. if (
  166. vips_bandjoin(rgb, &t[5], 3, NULL) ||
  167. vips_colourspace(t[5], &t[6], VIPS_INTERPRETATION_GREY16,
  168. "source_space", VIPS_INTERPRETATION_scRGB, NULL)) {
  169. clear_image(&base);
  170. return 1;
  171. }
  172. int res;
  173. if (alpha)
  174. res =
  175. vips_bandjoin2(t[6], alpha, &t[7], NULL) ||
  176. vips_icc_remove(t[7], out);
  177. else
  178. res = vips_icc_remove(t[6], out);
  179. clear_image(&base);
  180. return res;
  181. }
  182. int
  183. vips_fix_float_tiff(VipsImage *in, VipsImage **out)
  184. {
  185. /* Vips loads linear alpha in the 0.0-1.0 range but uses the 0.0-255.0 range.
  186. * https://github.com/libvips/libvips/pull/3627 fixes this behavior
  187. */
  188. if (in->Type == VIPS_INTERPRETATION_scRGB && in->Bands > 3)
  189. return vips_fix_scRGB_alpha_tiff(in, out);
  190. /* Vips loads linear BW TIFFs as VIPS_INTERPRETATION_B_W or VIPS_INTERPRETATION_GREY16
  191. * but these colourspaces are not linear. We should properly convert them to
  192. * VIPS_INTERPRETATION_GREY16
  193. */
  194. if (
  195. (in->Type == VIPS_INTERPRETATION_B_W || in->Type == VIPS_INTERPRETATION_GREY16) &&
  196. (in->BandFmt == VIPS_FORMAT_FLOAT || in->BandFmt == VIPS_FORMAT_DOUBLE))
  197. return vips_fix_BW_float_tiff(in, out);
  198. return vips_copy(in, out, NULL);
  199. }
  200. int
  201. vips_get_orientation(VipsImage *image)
  202. {
  203. int orientation;
  204. if (
  205. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  206. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0)
  207. return orientation;
  208. return 1;
  209. }
  210. int
  211. vips_get_palette_bit_depth(VipsImage *image)
  212. {
  213. int palette_bit_depth;
  214. if (
  215. vips_image_get_typeof(image, VIPS_META_PALETTE_BITS_DEPTH) == G_TYPE_INT &&
  216. vips_image_get_int(image, VIPS_META_PALETTE_BITS_DEPTH, &palette_bit_depth) == 0)
  217. return palette_bit_depth;
  218. return 0;
  219. }
  220. void
  221. vips_remove_palette_bit_depth(VipsImage *image)
  222. {
  223. vips_image_remove(image, VIPS_META_PALETTE_BITS_DEPTH);
  224. }
  225. VipsBandFormat
  226. vips_band_format(VipsImage *in)
  227. {
  228. return in->BandFmt;
  229. }
  230. gboolean
  231. vips_is_animated(VipsImage *in)
  232. {
  233. int n_pages;
  234. return (vips_image_get_typeof(in, "delay") != G_TYPE_INVALID &&
  235. vips_image_get_typeof(in, "loop") != G_TYPE_INVALID &&
  236. vips_image_get_typeof(in, "page-height") == G_TYPE_INT &&
  237. vips_image_get_typeof(in, "n-pages") == G_TYPE_INT &&
  238. vips_image_get_int(in, "n-pages", &n_pages) == 0 &&
  239. n_pages > 1);
  240. }
  241. int
  242. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n)
  243. {
  244. return vips_image_get_array_int(image, name, out, n);
  245. }
  246. void
  247. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n)
  248. {
  249. vips_image_set_array_int(image, name, array, n);
  250. }
  251. int
  252. vips_addalpha_go(VipsImage *in, VipsImage **out)
  253. {
  254. return vips_addalpha(in, out, NULL);
  255. }
  256. int
  257. vips_copy_go(VipsImage *in, VipsImage **out)
  258. {
  259. return vips_copy(in, out, NULL);
  260. }
  261. int
  262. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format)
  263. {
  264. return vips_cast(in, out, format, NULL);
  265. }
  266. int
  267. vips_rad2float_go(VipsImage *in, VipsImage **out)
  268. {
  269. return vips_rad2float(in, out, NULL);
  270. }
  271. int
  272. vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale)
  273. {
  274. if (!vips_image_hasalpha(in))
  275. return vips_resize(in, out, wscale, "vscale", hscale, NULL);
  276. VipsBandFormat format = vips_band_format(in);
  277. VipsImage *base = vips_image_new();
  278. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 4);
  279. int res =
  280. vips_premultiply(in, &t[0], NULL) ||
  281. vips_cast(t[0], &t[1], format, NULL) ||
  282. vips_resize(t[1], &t[2], wscale, "vscale", hscale, NULL) ||
  283. vips_unpremultiply(t[2], &t[3], NULL) ||
  284. vips_cast(t[3], out, format, NULL);
  285. clear_image(&base);
  286. return res;
  287. }
  288. int
  289. vips_icc_is_srgb_iec61966(VipsImage *in)
  290. {
  291. const void *data;
  292. size_t data_len;
  293. // 1998-12-01
  294. static char date[] = { 7, 206, 0, 2, 0, 9 };
  295. // 2.1
  296. static char version[] = { 2, 16, 0, 0 };
  297. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  298. return FALSE;
  299. /* Less than header size
  300. */
  301. if (data_len < 128)
  302. return FALSE;
  303. /* Predict it is sRGB IEC61966 2.1 by checking some header fields
  304. */
  305. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  306. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  307. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  308. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  309. (memcmp(data + 8, version, 4) == 0)); // Version
  310. }
  311. int
  312. vips_has_embedded_icc(VipsImage *in)
  313. {
  314. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  315. }
  316. int
  317. vips_icc_import_go(VipsImage *in, VipsImage **out)
  318. {
  319. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  320. }
  321. int
  322. vips_icc_export_go(VipsImage *in, VipsImage **out)
  323. {
  324. return vips_icc_export(in, out, "pcs", VIPS_PCS_LAB, NULL);
  325. }
  326. int
  327. vips_icc_export_srgb(VipsImage *in, VipsImage **out)
  328. {
  329. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", VIPS_PCS_LAB, NULL);
  330. }
  331. int
  332. vips_icc_transform_go(VipsImage *in, VipsImage **out)
  333. {
  334. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  335. }
  336. int
  337. vips_icc_remove(VipsImage *in, VipsImage **out)
  338. {
  339. if (vips_copy(in, out, NULL))
  340. return 1;
  341. vips_image_remove(*out, VIPS_META_ICC_NAME);
  342. vips_image_remove(*out, "exif-ifd0-WhitePoint");
  343. vips_image_remove(*out, "exif-ifd0-PrimaryChromaticities");
  344. vips_image_remove(*out, "exif-ifd2-ColorSpace");
  345. return 0;
  346. }
  347. int
  348. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs)
  349. {
  350. return vips_colourspace(in, out, cs, NULL);
  351. }
  352. int
  353. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle)
  354. {
  355. return vips_rot(in, out, angle, NULL);
  356. }
  357. int
  358. vips_flip_horizontal_go(VipsImage *in, VipsImage **out)
  359. {
  360. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  361. }
  362. int
  363. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height)
  364. {
  365. return vips_smartcrop(in, out, width, height, NULL);
  366. }
  367. int
  368. vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
  369. double sharp_sigma, int pixelate_pixels)
  370. {
  371. VipsImage *base = vips_image_new();
  372. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
  373. VipsInterpretation interpretation = in->Type;
  374. VipsBandFormat format = in->BandFmt;
  375. gboolean premultiplied = FALSE;
  376. if ((blur_sigma > 0 || sharp_sigma > 0) && vips_image_hasalpha(in)) {
  377. if (
  378. vips_premultiply(in, &t[0], NULL) ||
  379. vips_cast(t[0], &t[1], format, NULL)) {
  380. clear_image(&base);
  381. return 1;
  382. }
  383. in = t[0];
  384. premultiplied = TRUE;
  385. }
  386. if (blur_sigma > 0.0) {
  387. if (vips_gaussblur(in, &t[2], blur_sigma, NULL)) {
  388. clear_image(&base);
  389. return 1;
  390. }
  391. in = t[2];
  392. }
  393. if (sharp_sigma > 0.0) {
  394. if (vips_sharpen(in, &t[3], "sigma", sharp_sigma, NULL)) {
  395. clear_image(&base);
  396. return 1;
  397. }
  398. in = t[3];
  399. }
  400. pixelate_pixels = VIPS_MIN(pixelate_pixels, VIPS_MAX(in->Xsize, in->Ysize));
  401. if (pixelate_pixels > 1) {
  402. int w, h, tw, th;
  403. w = in->Xsize;
  404. h = in->Ysize;
  405. tw = (int) (VIPS_CEIL((double) w / pixelate_pixels)) * pixelate_pixels;
  406. th = (int) (VIPS_CEIL((double) h / pixelate_pixels)) * pixelate_pixels;
  407. if (tw > w || th > h) {
  408. if (vips_embed(in, &t[4], 0, 0, tw, th, "extend", VIPS_EXTEND_MIRROR, NULL)) {
  409. clear_image(&base);
  410. return 1;
  411. }
  412. in = t[4];
  413. }
  414. if (
  415. vips_shrink(in, &t[5], pixelate_pixels, pixelate_pixels, NULL) ||
  416. vips_zoom(t[5], &t[6], pixelate_pixels, pixelate_pixels, NULL)) {
  417. clear_image(&base);
  418. return 1;
  419. }
  420. in = t[6];
  421. if (tw > w || th > h) {
  422. if (vips_extract_area(in, &t[7], 0, 0, w, h, NULL)) {
  423. clear_image(&base);
  424. return 1;
  425. }
  426. in = t[7];
  427. }
  428. }
  429. if (premultiplied) {
  430. if (vips_unpremultiply(in, &t[8], NULL)) {
  431. clear_image(&base);
  432. return 1;
  433. }
  434. in = t[8];
  435. }
  436. int res =
  437. vips_colourspace(in, &t[9], interpretation, NULL) ||
  438. vips_cast(t[9], out, format, NULL);
  439. clear_image(&base);
  440. return res;
  441. }
  442. int
  443. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b)
  444. {
  445. if (!vips_image_hasalpha(in))
  446. return vips_copy(in, out, NULL);
  447. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  448. int res = vips_flatten(in, out, "background", bg, NULL);
  449. vips_area_unref((VipsArea *) bg);
  450. return res;
  451. }
  452. int
  453. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height)
  454. {
  455. return vips_extract_area(in, out, left, top, width, height, NULL);
  456. }
  457. int
  458. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  459. gboolean smart, double r, double g, double b,
  460. gboolean equal_hor, gboolean equal_ver)
  461. {
  462. VipsImage *base = vips_image_new();
  463. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  464. VipsImage *tmp = in;
  465. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  466. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  467. clear_image(&base);
  468. return 1;
  469. }
  470. tmp = t[0];
  471. }
  472. if (vips_image_hasalpha(tmp)) {
  473. if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
  474. clear_image(&base);
  475. return 1;
  476. }
  477. tmp = t[1];
  478. }
  479. double *bg = NULL;
  480. int bgn;
  481. VipsArrayDouble *bga;
  482. if (smart) {
  483. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  484. clear_image(&base);
  485. return 1;
  486. }
  487. bga = vips_array_double_new(bg, bgn);
  488. }
  489. else {
  490. bga = vips_array_double_newv(3, r, g, b);
  491. }
  492. int left, right, top, bot, width, height, diff;
  493. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  494. clear_image(&base);
  495. vips_area_unref((VipsArea *) bga);
  496. g_free(bg);
  497. if (res) {
  498. return 1;
  499. }
  500. if (equal_hor) {
  501. right = in->Xsize - left - width;
  502. diff = right - left;
  503. if (diff > 0) {
  504. width += diff;
  505. }
  506. else if (diff < 0) {
  507. left = right;
  508. width -= diff;
  509. }
  510. }
  511. if (equal_ver) {
  512. bot = in->Ysize - top - height;
  513. diff = bot - top;
  514. if (diff > 0) {
  515. height += diff;
  516. }
  517. else if (diff < 0) {
  518. top = bot;
  519. height -= diff;
  520. }
  521. }
  522. if (width == 0 || height == 0) {
  523. return vips_copy(in, out, NULL);
  524. }
  525. return vips_extract_area(in, out, left, top, width, height, NULL);
  526. }
  527. int
  528. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height)
  529. {
  530. VipsImage *tmp;
  531. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  532. return 1;
  533. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  534. clear_image(&tmp);
  535. return 1;
  536. }
  537. clear_image(&tmp);
  538. return 0;
  539. }
  540. int
  541. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height)
  542. {
  543. VipsImage *tmp = NULL;
  544. if (!vips_image_hasalpha(in)) {
  545. if (vips_addalpha(in, &tmp, NULL))
  546. return 1;
  547. in = tmp;
  548. }
  549. int ret =
  550. vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  551. if (tmp)
  552. clear_image(&tmp);
  553. return ret;
  554. }
  555. int
  556. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top, double opacity)
  557. {
  558. VipsImage *base = vips_image_new();
  559. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  560. if (!vips_image_hasalpha(watermark)) {
  561. if (vips_addalpha(watermark, &t[0], NULL))
  562. return 1;
  563. watermark = t[0];
  564. }
  565. if (opacity < 1) {
  566. if (
  567. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  568. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  569. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  570. vips_bandjoin2(t[1], t[3], &t[4], NULL)) {
  571. clear_image(&base);
  572. return 1;
  573. }
  574. watermark = t[4];
  575. }
  576. int had_alpha = vips_image_hasalpha(in);
  577. if (
  578. vips_composite2(
  579. in, watermark, &t[5], VIPS_BLEND_MODE_OVER,
  580. "x", left, "y", top, "compositing_space", in->Type,
  581. NULL) ||
  582. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)) {
  583. clear_image(&base);
  584. return 1;
  585. }
  586. int res;
  587. if (!had_alpha && vips_image_hasalpha(t[6])) {
  588. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  589. }
  590. else {
  591. res = vips_copy(t[6], out, NULL);
  592. }
  593. clear_image(&base);
  594. return res;
  595. }
  596. int
  597. vips_linecache_seq(VipsImage *in, VipsImage **out, int tile_height)
  598. {
  599. return vips_linecache(in, out, "tile_height", tile_height, "access", VIPS_ACCESS_SEQUENTIAL,
  600. NULL);
  601. }
  602. int
  603. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n)
  604. {
  605. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  606. }
  607. int
  608. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright)
  609. {
  610. static double default_resolution = 72.0 / 25.4;
  611. if (vips_copy(
  612. in, out,
  613. "xres", default_resolution,
  614. "yres", default_resolution,
  615. NULL))
  616. return 1;
  617. gchar **fields = vips_image_get_fields(in);
  618. for (int i = 0; fields[i] != NULL; i++) {
  619. gchar *name = fields[i];
  620. if (
  621. (strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  622. #ifdef VIPS_META_BITS_PER_SAMPLE
  623. (strcmp(name, VIPS_META_BITS_PER_SAMPLE) == 0) ||
  624. #endif
  625. (strcmp(name, VIPS_META_PALETTE_BITS_DEPTH) == 0) ||
  626. (strcmp(name, "width") == 0) ||
  627. (strcmp(name, "height") == 0) ||
  628. (strcmp(name, "bands") == 0) ||
  629. (strcmp(name, "format") == 0) ||
  630. (strcmp(name, "coding") == 0) ||
  631. (strcmp(name, "interpretation") == 0) ||
  632. (strcmp(name, "xoffset") == 0) ||
  633. (strcmp(name, "yoffset") == 0) ||
  634. (strcmp(name, "xres") == 0) ||
  635. (strcmp(name, "yres") == 0) ||
  636. (strcmp(name, "vips-loader") == 0) ||
  637. (strcmp(name, "background") == 0) ||
  638. (strcmp(name, "vips-sequential") == 0) ||
  639. (strcmp(name, "imgproxy-dpr-scale") == 0))
  640. continue;
  641. if (keep_exif_copyright) {
  642. if (
  643. (strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  644. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  645. (strcmp(name, "exif-ifd0-Artist") == 0))
  646. continue;
  647. }
  648. vips_image_remove(*out, name);
  649. }
  650. g_strfreev(fields);
  651. return 0;
  652. }
  653. int
  654. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace)
  655. {
  656. return vips_jpegsave_buffer(
  657. in, buf, len,
  658. "Q", quality,
  659. "optimize_coding", TRUE,
  660. "interlace", interlace,
  661. NULL);
  662. }
  663. int
  664. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors)
  665. {
  666. int bitdepth;
  667. if (quantize) {
  668. bitdepth = 1;
  669. if (colors > 16)
  670. bitdepth = 8;
  671. else if (colors > 4)
  672. bitdepth = 4;
  673. else if (colors > 2)
  674. bitdepth = 2;
  675. }
  676. else {
  677. bitdepth = vips_get_palette_bit_depth(in);
  678. if (bitdepth && bitdepth <= 8) {
  679. if (bitdepth > 4)
  680. bitdepth = 8;
  681. else if (bitdepth > 2)
  682. bitdepth = 4;
  683. quantize = 1;
  684. colors = 1 << bitdepth;
  685. }
  686. }
  687. if (!quantize)
  688. return vips_pngsave_buffer(
  689. in, buf, len,
  690. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  691. "interlace", interlace,
  692. NULL);
  693. return vips_pngsave_buffer(
  694. in, buf, len,
  695. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  696. "interlace", interlace,
  697. "palette", quantize,
  698. "bitdepth", bitdepth,
  699. NULL);
  700. }
  701. int
  702. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  703. {
  704. return vips_webpsave_buffer(
  705. in, buf, len,
  706. "Q", quality,
  707. NULL);
  708. }
  709. int
  710. vips_gifsave_go(VipsImage *in, void **buf, size_t *len)
  711. {
  712. int bitdepth = vips_get_palette_bit_depth(in);
  713. if (bitdepth <= 0 || bitdepth > 8)
  714. bitdepth = 8;
  715. return vips_gifsave_buffer(in, buf, len, "bitdepth", bitdepth, NULL);
  716. }
  717. int
  718. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  719. {
  720. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  721. }
  722. int
  723. vips_heifsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  724. {
  725. return vips_heifsave_buffer(
  726. in, buf, len,
  727. "Q", quality,
  728. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_HEVC,
  729. NULL);
  730. }
  731. int
  732. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed)
  733. {
  734. return vips_heifsave_buffer(
  735. in, buf, len,
  736. "Q", quality,
  737. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  738. "effort", 9 - speed,
  739. NULL);
  740. }
  741. void
  742. vips_cleanup()
  743. {
  744. vips_error_clear();
  745. vips_thread_shutdown();
  746. }