vips.c 21 KB

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