vips.c 23 KB

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