1
0

vips.c 26 KB

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