vips.c 27 KB

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