vips.c 22 KB

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