vips.c 26 KB

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