vips.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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, int pages, VipsImage **out)
  54. {
  55. return vips_jxlload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, 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. VipsImage *base = vips_image_new();
  361. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  362. int has_alpha_16 = FALSE;
  363. /* RGB16 and GREY16 images have max alpha 65535, but this is not handled by
  364. * vips_icc_import. We need to extract the alpha channel and convert it to 0-255
  365. */
  366. if ((in->Type == VIPS_INTERPRETATION_RGB16 && in->Bands > 3) ||
  367. (in->Type == VIPS_INTERPRETATION_GREY16 && in->Bands > 1)) {
  368. int bands = in->Type == VIPS_INTERPRETATION_RGB16 ? 3 : 1;
  369. if (vips_extract_band(in, &t[0], 0, "n", bands, NULL) ||
  370. vips_extract_band(in, &t[1], bands, "n", 1, NULL)) {
  371. clear_image(&base);
  372. return 1;
  373. }
  374. in = t[0];
  375. has_alpha_16 = TRUE;
  376. }
  377. if (vips_icc_import(in, out, "embedded", TRUE, "pcs", vips_icc_get_pcs(in), NULL)) {
  378. clear_image(&base);
  379. return 1;
  380. }
  381. /* Convert 16-bit alpha channel to 0-255 range and join it back to the image
  382. */
  383. if (has_alpha_16) {
  384. t[2] = *out;
  385. *out = NULL;
  386. if (vips_cast(t[1], &t[3], t[2]->BandFmt, NULL) ||
  387. vips_linear1(t[3], &t[4], 1.0 / 255.0, 0, NULL) ||
  388. vips_bandjoin2(t[2], t[4], out, NULL)) {
  389. clear_image(&base);
  390. return 1;
  391. }
  392. }
  393. vips_image_set_int(*out, "imgproxy-icc-imported", 1);
  394. clear_image(&base);
  395. return 0;
  396. }
  397. int
  398. vips_icc_export_go(VipsImage *in, VipsImage **out)
  399. {
  400. return vips_icc_export(in, out, "pcs", vips_icc_get_pcs(in), NULL);
  401. }
  402. int
  403. vips_icc_export_srgb(VipsImage *in, VipsImage **out)
  404. {
  405. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", vips_icc_get_pcs(in), NULL);
  406. }
  407. int
  408. vips_icc_transform_go(VipsImage *in, VipsImage **out)
  409. {
  410. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", vips_icc_get_pcs(in), NULL);
  411. }
  412. int
  413. vips_icc_remove(VipsImage *in, VipsImage **out)
  414. {
  415. if (vips_copy(in, out, NULL))
  416. return 1;
  417. vips_image_remove(*out, VIPS_META_ICC_NAME);
  418. vips_image_remove(*out, "exif-ifd0-WhitePoint");
  419. vips_image_remove(*out, "exif-ifd0-PrimaryChromaticities");
  420. vips_image_remove(*out, "exif-ifd2-ColorSpace");
  421. return 0;
  422. }
  423. int
  424. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs)
  425. {
  426. return vips_colourspace(in, out, cs, NULL);
  427. }
  428. int
  429. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle)
  430. {
  431. return vips_rot(in, out, angle, NULL);
  432. }
  433. int
  434. vips_flip_horizontal_go(VipsImage *in, VipsImage **out)
  435. {
  436. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  437. }
  438. int
  439. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height)
  440. {
  441. return vips_smartcrop(in, out, width, height, NULL);
  442. }
  443. int
  444. vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
  445. double sharp_sigma, int pixelate_pixels)
  446. {
  447. VipsImage *base = vips_image_new();
  448. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
  449. VipsInterpretation interpretation = in->Type;
  450. VipsBandFormat format = in->BandFmt;
  451. gboolean premultiplied = FALSE;
  452. if ((blur_sigma > 0 || sharp_sigma > 0) && vips_image_hasalpha(in)) {
  453. if (
  454. vips_premultiply(in, &t[0], NULL) ||
  455. vips_cast(t[0], &t[1], format, NULL)) {
  456. clear_image(&base);
  457. return 1;
  458. }
  459. in = t[0];
  460. premultiplied = TRUE;
  461. }
  462. if (blur_sigma > 0.0) {
  463. if (vips_gaussblur(in, &t[2], blur_sigma, NULL)) {
  464. clear_image(&base);
  465. return 1;
  466. }
  467. in = t[2];
  468. }
  469. if (sharp_sigma > 0.0) {
  470. if (vips_sharpen(in, &t[3], "sigma", sharp_sigma, NULL)) {
  471. clear_image(&base);
  472. return 1;
  473. }
  474. in = t[3];
  475. }
  476. pixelate_pixels = VIPS_MIN(pixelate_pixels, VIPS_MAX(in->Xsize, in->Ysize));
  477. if (pixelate_pixels > 1) {
  478. int w, h, tw, th;
  479. w = in->Xsize;
  480. h = in->Ysize;
  481. tw = (int) (VIPS_CEIL((double) w / pixelate_pixels)) * pixelate_pixels;
  482. th = (int) (VIPS_CEIL((double) h / pixelate_pixels)) * pixelate_pixels;
  483. if (tw > w || th > h) {
  484. if (vips_embed(in, &t[4], 0, 0, tw, th, "extend", VIPS_EXTEND_MIRROR, NULL)) {
  485. clear_image(&base);
  486. return 1;
  487. }
  488. in = t[4];
  489. }
  490. if (
  491. vips_shrink(in, &t[5], pixelate_pixels, pixelate_pixels, NULL) ||
  492. vips_zoom(t[5], &t[6], pixelate_pixels, pixelate_pixels, NULL)) {
  493. clear_image(&base);
  494. return 1;
  495. }
  496. in = t[6];
  497. if (tw > w || th > h) {
  498. if (vips_extract_area(in, &t[7], 0, 0, w, h, NULL)) {
  499. clear_image(&base);
  500. return 1;
  501. }
  502. in = t[7];
  503. }
  504. }
  505. if (premultiplied) {
  506. if (vips_unpremultiply(in, &t[8], NULL)) {
  507. clear_image(&base);
  508. return 1;
  509. }
  510. in = t[8];
  511. }
  512. int res =
  513. vips_colourspace(in, &t[9], interpretation, NULL) ||
  514. vips_cast(t[9], out, format, NULL);
  515. clear_image(&base);
  516. return res;
  517. }
  518. int
  519. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b)
  520. {
  521. if (!vips_image_hasalpha(in))
  522. return vips_copy(in, out, NULL);
  523. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  524. int res = vips_flatten(in, out, "background", bg, NULL);
  525. vips_area_unref((VipsArea *) bg);
  526. return res;
  527. }
  528. int
  529. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height)
  530. {
  531. return vips_extract_area(in, out, left, top, width, height, NULL);
  532. }
  533. int
  534. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  535. gboolean smart, double r, double g, double b,
  536. gboolean equal_hor, gboolean equal_ver)
  537. {
  538. VipsImage *base = vips_image_new();
  539. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  540. VipsImage *tmp = in;
  541. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  542. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  543. clear_image(&base);
  544. return 1;
  545. }
  546. tmp = t[0];
  547. }
  548. if (vips_image_hasalpha(tmp)) {
  549. if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
  550. clear_image(&base);
  551. return 1;
  552. }
  553. tmp = t[1];
  554. }
  555. double *bg = NULL;
  556. int bgn;
  557. VipsArrayDouble *bga;
  558. if (smart) {
  559. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  560. clear_image(&base);
  561. return 1;
  562. }
  563. bga = vips_array_double_new(bg, bgn);
  564. }
  565. else {
  566. bga = vips_array_double_newv(3, r, g, b);
  567. }
  568. int left, right, top, bot, width, height, diff;
  569. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  570. clear_image(&base);
  571. vips_area_unref((VipsArea *) bga);
  572. g_free(bg);
  573. if (res) {
  574. return 1;
  575. }
  576. if (equal_hor) {
  577. right = in->Xsize - left - width;
  578. diff = right - left;
  579. if (diff > 0) {
  580. width += diff;
  581. }
  582. else if (diff < 0) {
  583. left = right;
  584. width -= diff;
  585. }
  586. }
  587. if (equal_ver) {
  588. bot = in->Ysize - top - height;
  589. diff = bot - top;
  590. if (diff > 0) {
  591. height += diff;
  592. }
  593. else if (diff < 0) {
  594. top = bot;
  595. height -= diff;
  596. }
  597. }
  598. if (width == 0 || height == 0) {
  599. return vips_copy(in, out, NULL);
  600. }
  601. return vips_extract_area(in, out, left, top, width, height, NULL);
  602. }
  603. int
  604. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height, int centered)
  605. {
  606. VipsImage *tmp;
  607. int across = VIPS_CEIL((double) width / in->Xsize);
  608. int down = VIPS_CEIL((double) height / in->Ysize);
  609. if (centered) {
  610. if (across % 2 == 0)
  611. across++;
  612. if (down % 2 == 0)
  613. down++;
  614. }
  615. if (vips_replicate(in, &tmp, across, down, NULL))
  616. return 1;
  617. const int left = centered ? (tmp->Xsize - width) / 2 : 0;
  618. const int top = centered ? (tmp->Ysize - height) / 2 : 0;
  619. if (vips_extract_area(tmp, out, left, top, width, height, NULL)) {
  620. clear_image(&tmp);
  621. return 1;
  622. }
  623. clear_image(&tmp);
  624. return 0;
  625. }
  626. int
  627. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height)
  628. {
  629. VipsImage *tmp = NULL;
  630. if (!vips_image_hasalpha(in)) {
  631. if (vips_addalpha(in, &tmp, NULL))
  632. return 1;
  633. in = tmp;
  634. }
  635. int ret =
  636. vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  637. if (tmp)
  638. clear_image(&tmp);
  639. return ret;
  640. }
  641. int
  642. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top, double opacity)
  643. {
  644. VipsImage *base = vips_image_new();
  645. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  646. if (!vips_image_hasalpha(watermark)) {
  647. if (vips_addalpha(watermark, &t[0], NULL))
  648. return 1;
  649. watermark = t[0];
  650. }
  651. if (opacity < 1) {
  652. if (
  653. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  654. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  655. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  656. vips_bandjoin2(t[1], t[3], &t[4], NULL)) {
  657. clear_image(&base);
  658. return 1;
  659. }
  660. watermark = t[4];
  661. }
  662. int had_alpha = vips_image_hasalpha(in);
  663. if (
  664. vips_composite2(
  665. in, watermark, &t[5], VIPS_BLEND_MODE_OVER,
  666. "x", left, "y", top, "compositing_space", in->Type,
  667. NULL) ||
  668. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)) {
  669. clear_image(&base);
  670. return 1;
  671. }
  672. int res;
  673. if (!had_alpha && vips_image_hasalpha(t[6])) {
  674. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  675. }
  676. else {
  677. res = vips_copy(t[6], out, NULL);
  678. }
  679. clear_image(&base);
  680. return res;
  681. }
  682. int
  683. vips_linecache_seq(VipsImage *in, VipsImage **out, int tile_height)
  684. {
  685. return vips_linecache(in, out, "tile_height", tile_height, "access", VIPS_ACCESS_SEQUENTIAL,
  686. NULL);
  687. }
  688. int
  689. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n)
  690. {
  691. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  692. }
  693. typedef struct {
  694. int strip_all;
  695. int keep_exif_copyright;
  696. int keep_animation;
  697. } VipsStripOptions;
  698. void *
  699. vips_strip_fn(VipsImage *in, const char *name, GValue *value, void *a)
  700. {
  701. VipsStripOptions *opts = (VipsStripOptions *) a;
  702. if (strcmp(name, "vips-sequential") == 0)
  703. return NULL;
  704. if (!opts->strip_all) {
  705. if ((strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  706. #ifdef VIPS_META_BITS_PER_SAMPLE
  707. (strcmp(name, VIPS_META_BITS_PER_SAMPLE) == 0) ||
  708. #endif
  709. #ifdef VIPS_META_PALETTE
  710. (strcmp(name, VIPS_META_PALETTE) == 0) ||
  711. #endif
  712. (strcmp(name, VIPS_META_PALETTE_BITS_DEPTH) == 0) ||
  713. (strcmp(name, "background") == 0) ||
  714. (strcmp(name, "vips-loader") == 0) ||
  715. (vips_isprefix("imgproxy-", name)))
  716. return NULL;
  717. if (opts->keep_exif_copyright)
  718. if ((strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  719. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  720. (strcmp(name, "exif-ifd0-Artist") == 0))
  721. return NULL;
  722. if (opts->keep_animation)
  723. if ((strcmp(name, "page-height") == 0) ||
  724. (strcmp(name, "delay") == 0) ||
  725. (strcmp(name, "loop") == 0) ||
  726. (strcmp(name, "n-pages") == 0))
  727. return NULL;
  728. }
  729. vips_image_remove(in, name);
  730. return NULL;
  731. }
  732. int
  733. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright)
  734. {
  735. static double default_resolution = 72.0 / 25.4;
  736. VipsStripOptions opts = {
  737. .strip_all = 0,
  738. .keep_exif_copyright = FALSE,
  739. .keep_animation = FALSE,
  740. };
  741. if (vips_image_get_typeof(in, "imgproxy-is-animated") &&
  742. vips_image_get_int(in, "imgproxy-is-animated", &opts.keep_animation))
  743. opts.keep_animation = FALSE;
  744. if (vips_copy(
  745. in, out,
  746. "xres", default_resolution,
  747. "yres", default_resolution,
  748. NULL))
  749. return 1;
  750. vips_image_map(*out, vips_strip_fn, &opts);
  751. return 0;
  752. }
  753. int
  754. vips_strip_all(VipsImage *in, VipsImage **out)
  755. {
  756. VipsStripOptions opts = {
  757. .strip_all = TRUE,
  758. .keep_exif_copyright = FALSE,
  759. .keep_animation = FALSE,
  760. };
  761. if (vips_copy(in, out, NULL))
  762. return 1;
  763. vips_image_map(*out, vips_strip_fn, &opts);
  764. /* vips doesn't include "palette-bit-depth" to the map of fields
  765. */
  766. vips_image_remove(*out, VIPS_META_PALETTE_BITS_DEPTH);
  767. return 0;
  768. }
  769. int
  770. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace)
  771. {
  772. return vips_jpegsave_buffer(
  773. in, buf, len,
  774. "Q", quality,
  775. "optimize_coding", TRUE,
  776. "interlace", interlace,
  777. NULL);
  778. }
  779. int
  780. vips_jxlsave_go(VipsImage *in, void **buf, size_t *len, int quality, int effort)
  781. {
  782. return vips_jxlsave_buffer(
  783. in, buf, len,
  784. "Q", quality,
  785. "effort", effort,
  786. NULL);
  787. }
  788. int
  789. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors)
  790. {
  791. int bitdepth;
  792. if (quantize) {
  793. bitdepth = 1;
  794. if (colors > 16)
  795. bitdepth = 8;
  796. else if (colors > 4)
  797. bitdepth = 4;
  798. else if (colors > 2)
  799. bitdepth = 2;
  800. }
  801. else {
  802. bitdepth = vips_get_palette_bit_depth(in);
  803. if (bitdepth && bitdepth <= 8) {
  804. if (bitdepth > 4)
  805. bitdepth = 8;
  806. else if (bitdepth > 2)
  807. bitdepth = 4;
  808. quantize = 1;
  809. colors = 1 << bitdepth;
  810. }
  811. }
  812. if (!quantize)
  813. return vips_pngsave_buffer(
  814. in, buf, len,
  815. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  816. "interlace", interlace,
  817. NULL);
  818. return vips_pngsave_buffer(
  819. in, buf, len,
  820. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  821. "interlace", interlace,
  822. "palette", quantize,
  823. "bitdepth", bitdepth,
  824. NULL);
  825. }
  826. int
  827. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  828. {
  829. return vips_webpsave_buffer(
  830. in, buf, len,
  831. "Q", quality,
  832. NULL);
  833. }
  834. int
  835. vips_gifsave_go(VipsImage *in, void **buf, size_t *len)
  836. {
  837. int bitdepth = vips_get_palette_bit_depth(in);
  838. if (bitdepth <= 0 || bitdepth > 8)
  839. bitdepth = 8;
  840. return vips_gifsave_buffer(in, buf, len, "bitdepth", bitdepth, NULL);
  841. }
  842. int
  843. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  844. {
  845. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  846. }
  847. int
  848. vips_heifsave_go(VipsImage *in, void **buf, size_t *len, int quality)
  849. {
  850. return vips_heifsave_buffer(
  851. in, buf, len,
  852. "Q", quality,
  853. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_HEVC,
  854. NULL);
  855. }
  856. int
  857. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed)
  858. {
  859. return vips_heifsave_buffer(
  860. in, buf, len,
  861. "Q", quality,
  862. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  863. "effort", 9 - speed,
  864. NULL);
  865. }
  866. void
  867. vips_cleanup()
  868. {
  869. vips_error_clear();
  870. vips_thread_shutdown();
  871. }