vips.c 26 KB

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