1
0

vips.c 27 KB

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