1
0

vips.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. #include "vips.h"
  2. #include <string.h>
  3. #define VIPS_SUPPORT_SMARTCROP \
  4. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  5. #define VIPS_SUPPORT_HASALPHA \
  6. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  7. #define VIPS_SUPPORT_GIF \
  8. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
  9. #define VIPS_SUPPORT_SVG \
  10. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3))
  11. #define VIPS_SUPPORT_TIFF \
  12. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 6))
  13. #define VIPS_SUPPORT_MAGICK \
  14. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
  15. #define VIPS_SUPPORT_PNG_QUANTIZATION \
  16. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 7))
  17. #define VIPS_SUPPORT_WEBP_SCALE_ON_LOAD \
  18. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  19. #define VIPS_SUPPORT_WEBP_ANIMATION \
  20. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  21. #define VIPS_SUPPORT_ARRAY_HEADERS \
  22. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 9))
  23. #define VIPS_SUPPORT_HEIF \
  24. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  25. #define VIPS_SUPPORT_AVIF \
  26. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 9))
  27. #define VIPS_SUPPORT_AVIF_SPEED \
  28. (VIPS_MAJOR_VERSION > 8 || \
  29. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION > 10) || \
  30. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10 && VIPS_MICRO_VERSION >= 2))
  31. #define VIPS_SUPPORT_COMPOSITE \
  32. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 6))
  33. #define VIPS_SUPPORT_FIND_TRIM \
  34. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 6))
  35. #define VIPS_SUPPORT_PNG_BITDEPTH \
  36. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10))
  37. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  38. #if (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 8))
  39. #define VIPS_BLOB_DATA_TYPE const void *
  40. #else
  41. #define VIPS_BLOB_DATA_TYPE void *
  42. #endif
  43. int
  44. vips_initialize() {
  45. return vips_init("imgproxy");
  46. }
  47. void
  48. clear_image(VipsImage **in) {
  49. if (G_IS_OBJECT(*in)) g_clear_object(in);
  50. }
  51. void
  52. g_free_go(void **buf) {
  53. g_free(*buf);
  54. }
  55. void
  56. swap_and_clear(VipsImage **in, VipsImage *out) {
  57. clear_image(in);
  58. *in = out;
  59. }
  60. int
  61. vips_type_find_load_go(int imgtype) {
  62. switch (imgtype)
  63. {
  64. case (JPEG):
  65. return vips_type_find("VipsOperation", "jpegload_buffer");
  66. case (PNG):
  67. return vips_type_find("VipsOperation", "pngload_buffer");
  68. case (WEBP):
  69. return vips_type_find("VipsOperation", "webpload_buffer");
  70. case (GIF):
  71. return vips_type_find("VipsOperation", "gifload_buffer");
  72. case (SVG):
  73. return vips_type_find("VipsOperation", "svgload_buffer");
  74. case (HEIC):
  75. return vips_type_find("VipsOperation", "heifload_buffer");
  76. case (AVIF):
  77. return vips_type_find("VipsOperation", "heifload_buffer");
  78. case (BMP):
  79. return vips_type_find("VipsOperation", "magickload_buffer");
  80. case (TIFF):
  81. return vips_type_find("VipsOperation", "tiffload_buffer");
  82. }
  83. return 0;
  84. }
  85. int
  86. vips_type_find_save_go(int imgtype) {
  87. switch (imgtype)
  88. {
  89. case (JPEG):
  90. return vips_type_find("VipsOperation", "jpegsave_buffer");
  91. case (PNG):
  92. return vips_type_find("VipsOperation", "pngsave_buffer");
  93. case (WEBP):
  94. return vips_type_find("VipsOperation", "webpsave_buffer");
  95. case (GIF):
  96. return vips_type_find("VipsOperation", "magicksave_buffer");
  97. #if VIPS_SUPPORT_AVIF
  98. case (AVIF):
  99. return vips_type_find("VipsOperation", "heifsave_buffer");
  100. #endif
  101. case (ICO):
  102. return vips_type_find("VipsOperation", "pngsave_buffer");
  103. case (BMP):
  104. return vips_type_find("VipsOperation", "magicksave_buffer");
  105. case (TIFF):
  106. return vips_type_find("VipsOperation", "tiffsave_buffer");
  107. }
  108. return 0;
  109. }
  110. int
  111. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  112. if (shrink > 1)
  113. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  114. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  115. }
  116. int
  117. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  118. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  119. }
  120. int
  121. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out) {
  122. return vips_webpload_buffer(
  123. buf, len, out,
  124. "access", VIPS_ACCESS_SEQUENTIAL,
  125. #if VIPS_SUPPORT_WEBP_SCALE_ON_LOAD
  126. "scale", scale,
  127. #else
  128. "shrink", (int)(1.0 / scale),
  129. #endif
  130. #if VIPS_SUPPORT_WEBP_ANIMATION
  131. "n", pages,
  132. #endif
  133. NULL
  134. );
  135. }
  136. int
  137. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  138. #if VIPS_SUPPORT_GIF
  139. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  140. #else
  141. vips_error("vips_gifload_go", "Loading GIF is not supported (libvips 8.3+ reuired)");
  142. return 1;
  143. #endif
  144. }
  145. int
  146. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
  147. #if VIPS_SUPPORT_SVG
  148. // libvips limits the minimal scale to 0.001, so we have to scale down dpi
  149. // for lower scale values
  150. double dpi = 72.0;
  151. if (scale < 0.001) {
  152. dpi *= VIPS_MAX(scale / 0.001, 0.001);
  153. scale = 0.001;
  154. }
  155. return vips_svgload_buffer(
  156. buf, len, out,
  157. "access", VIPS_ACCESS_SEQUENTIAL,
  158. "scale", scale,
  159. "dpi", dpi,
  160. NULL
  161. );
  162. #else
  163. vips_error("vips_svgload_go", "Loading SVG is not supported (libvips 8.5+ reuired)");
  164. return 1;
  165. #endif
  166. }
  167. int
  168. vips_heifload_go(void *buf, size_t len, VipsImage **out) {
  169. #if VIPS_SUPPORT_HEIF
  170. return vips_heifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  171. #else
  172. vips_error("vips_heifload_go", "Loading HEIF is not supported (libvips 8.8+ reuired)");
  173. return 1;
  174. #endif
  175. }
  176. int
  177. vips_bmpload_go(void *buf, size_t len, VipsImage **out) {
  178. #if VIPS_SUPPORT_MAGICK
  179. return vips_magickload_buffer(buf, len, out, NULL);
  180. #else
  181. vips_error("vips_bmpload_go", "Loading BMP is not supported");
  182. return 1;
  183. #endif
  184. }
  185. int
  186. vips_tiffload_go(void *buf, size_t len, VipsImage **out) {
  187. #if VIPS_SUPPORT_TIFF
  188. return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  189. #else
  190. vips_error("vips_tiffload_go", "Loading TIFF is not supported (libvips 8.6+ reuired)");
  191. return 1;
  192. #endif
  193. }
  194. int
  195. vips_get_orientation(VipsImage *image) {
  196. #ifdef VIPS_META_ORIENTATION
  197. int orientation;
  198. if (
  199. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  200. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0
  201. ) return orientation;
  202. #else
  203. const char *orientation;
  204. if (
  205. vips_image_get_typeof(image, EXIF_ORIENTATION) == VIPS_TYPE_REF_STRING &&
  206. vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
  207. ) return atoi(orientation);
  208. #endif
  209. return 1;
  210. }
  211. int
  212. vips_support_smartcrop() {
  213. return VIPS_SUPPORT_SMARTCROP;
  214. }
  215. VipsBandFormat
  216. vips_band_format(VipsImage *in) {
  217. return in->BandFmt;
  218. }
  219. gboolean
  220. vips_support_webp_animation() {
  221. return VIPS_SUPPORT_WEBP_ANIMATION;
  222. }
  223. gboolean
  224. vips_is_animated(VipsImage * in) {
  225. return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
  226. vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
  227. vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
  228. }
  229. int
  230. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
  231. #if VIPS_SUPPORT_ARRAY_HEADERS
  232. return vips_image_get_array_int(image, name, out, n);
  233. #else
  234. vips_error("vips_image_get_array_int_go", "Array headers are not supported (libvips 8.9+ reuired)");
  235. return 1;
  236. #endif
  237. }
  238. void
  239. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
  240. #if VIPS_SUPPORT_ARRAY_HEADERS
  241. vips_image_set_array_int(image, name, array, n);
  242. #endif
  243. }
  244. gboolean
  245. vips_image_hasalpha_go(VipsImage * in) {
  246. #if VIPS_SUPPORT_HASALPHA
  247. return vips_image_hasalpha(in);
  248. #else
  249. return( in->Bands == 2 ||
  250. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  251. in->Bands > 4 );
  252. #endif
  253. }
  254. int
  255. vips_addalpha_go(VipsImage *in, VipsImage **out) {
  256. return vips_addalpha(in, out, NULL);
  257. }
  258. int
  259. vips_copy_go(VipsImage *in, VipsImage **out) {
  260. return vips_copy(in, out, NULL);
  261. }
  262. int
  263. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  264. return vips_cast(in, out, format, NULL);
  265. }
  266. int
  267. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  268. return vips_rad2float(in, out, NULL);
  269. }
  270. int
  271. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  272. return vips_resize(in, out, scale, NULL);
  273. }
  274. int
  275. vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale) {
  276. VipsBandFormat format;
  277. VipsImage *tmp1, *tmp2;
  278. format = vips_band_format(in);
  279. if (vips_premultiply(in, &tmp1, NULL))
  280. return 1;
  281. if (vips_resize(tmp1, &tmp2, scale, NULL)) {
  282. clear_image(&tmp1);
  283. return 1;
  284. }
  285. swap_and_clear(&tmp1, tmp2);
  286. if (vips_unpremultiply(tmp1, &tmp2, NULL)) {
  287. clear_image(&tmp1);
  288. return 1;
  289. }
  290. swap_and_clear(&tmp1, tmp2);
  291. if (vips_cast(tmp1, out, format, NULL)) {
  292. clear_image(&tmp1);
  293. return 1;
  294. }
  295. clear_image(&tmp1);
  296. return 0;
  297. }
  298. int
  299. vips_icc_is_srgb_iec61966(VipsImage *in) {
  300. VIPS_BLOB_DATA_TYPE data;
  301. size_t data_len;
  302. // 1998-12-01
  303. static char date[] = { 7, 206, 0, 2, 0, 9 };
  304. // 2.1
  305. static char version[] = { 2, 16, 0, 0 };
  306. // The image had no profile and built-in CMYK was imported.
  307. // Vips gives us an invalid data pointer when the built-in profile was imported,
  308. // so we check this mark before receiving an actual profile.
  309. // if (vips_image_get_typeof(in, "icc-cmyk-no-profile"))
  310. // return FALSE;
  311. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  312. return FALSE;
  313. // Less than header size
  314. if (data_len < 128)
  315. return FALSE;
  316. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  317. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  318. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  319. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  320. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  321. (memcmp(data + 8, version, 4) == 0)); // Version
  322. }
  323. int
  324. vips_has_embedded_icc(VipsImage *in) {
  325. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  326. }
  327. int
  328. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  329. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  330. }
  331. int
  332. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  333. return vips_icc_export(in, out, NULL);
  334. }
  335. int
  336. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  337. return vips_icc_export(in, out, "output_profile", "sRGB", NULL);
  338. }
  339. int
  340. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  341. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  342. }
  343. int
  344. vips_icc_remove(VipsImage *in, VipsImage **out) {
  345. if (vips_copy(in, out, NULL)) return 1;
  346. vips_image_remove(*out, VIPS_META_ICC_NAME);
  347. return 0;
  348. }
  349. int
  350. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  351. return vips_colourspace(in, out, cs, NULL);
  352. }
  353. int
  354. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  355. return vips_rot(in, out, angle, NULL);
  356. }
  357. int
  358. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  359. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  360. }
  361. int
  362. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  363. #if VIPS_SUPPORT_SMARTCROP
  364. return vips_smartcrop(in, out, width, height, NULL);
  365. #else
  366. vips_error("vips_smartcrop_go", "Smart crop is not supported (libvips 8.5+ reuired)");
  367. return 1;
  368. #endif
  369. }
  370. int
  371. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  372. return vips_gaussblur(in, out, sigma, NULL);
  373. }
  374. int
  375. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  376. return vips_sharpen(in, out, "sigma", sigma, NULL);
  377. }
  378. int
  379. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  380. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  381. int res = vips_flatten(in, out, "background", bg, NULL);
  382. vips_area_unref((VipsArea *)bg);
  383. return res;
  384. }
  385. int
  386. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  387. return vips_extract_area(in, out, left, top, width, height, NULL);
  388. }
  389. int
  390. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  391. gboolean smart, double r, double g, double b,
  392. gboolean equal_hor, gboolean equal_ver) {
  393. #if VIPS_SUPPORT_FIND_TRIM
  394. VipsImage *tmp;
  395. if (vips_image_hasalpha(in)) {
  396. if (vips_flatten_go(in, &tmp, 255.0, 0, 255.0))
  397. return 1;
  398. } else {
  399. if (vips_copy(in, &tmp, NULL))
  400. return 1;
  401. }
  402. double *bg;
  403. int bgn;
  404. VipsArrayDouble *bga;
  405. if (smart) {
  406. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  407. clear_image(&tmp);
  408. return 1;
  409. }
  410. bga = vips_array_double_new(bg, bgn);
  411. } else {
  412. bga = vips_array_double_newv(3, r, g, b);
  413. bg = 0;
  414. }
  415. int left, right, top, bot, width, height, diff;
  416. if (vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL)) {
  417. clear_image(&tmp);
  418. vips_area_unref((VipsArea *)bga);
  419. g_free(bg);
  420. return 1;
  421. }
  422. if (equal_hor) {
  423. right = in->Xsize - left - width;
  424. diff = right - left;
  425. if (diff > 0) {
  426. width += diff;
  427. } else if (diff < 0) {
  428. left = right;
  429. width -= diff;
  430. }
  431. }
  432. if (equal_ver) {
  433. bot = in->Ysize - top - height;
  434. diff = bot - top;
  435. if (diff > 0) {
  436. height += diff;
  437. } else if (diff < 0) {
  438. top = bot;
  439. height -= diff;
  440. }
  441. }
  442. clear_image(&tmp);
  443. vips_area_unref((VipsArea *)bga);
  444. g_free(bg);
  445. if (width == 0 || height == 0) {
  446. return vips_copy(in, out, NULL);
  447. }
  448. return vips_extract_area(in, out, left, top, width, height, NULL);
  449. #else
  450. vips_error("vips_trim", "Trim is not supported (libvips 8.6+ reuired)");
  451. return 1;
  452. #endif
  453. }
  454. int
  455. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  456. VipsImage *tmp;
  457. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  458. return 1;
  459. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  460. clear_image(&tmp);
  461. return 1;
  462. }
  463. clear_image(&tmp);
  464. return 0;
  465. }
  466. int
  467. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn) {
  468. VipsArrayDouble *bga = vips_array_double_new(bg, bgn);
  469. int ret = vips_embed(
  470. in, out, x, y, width, height,
  471. "extend", VIPS_EXTEND_BACKGROUND,
  472. "background", bga,
  473. NULL
  474. );
  475. vips_area_unref((VipsArea *)bga);
  476. return ret;
  477. }
  478. int
  479. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  480. if (vips_image_hasalpha_go(in)) {
  481. return vips_copy(in, out, NULL);
  482. }
  483. return vips_bandjoin_const1(in, out, 255, NULL);
  484. }
  485. int
  486. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  487. #if VIPS_SUPPORT_COMPOSITE
  488. VipsImage *base = vips_image_new();
  489. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  490. if (opacity < 1) {
  491. if (
  492. vips_extract_band(watermark, &t[0], 0, "n", watermark->Bands - 1, NULL) ||
  493. vips_extract_band(watermark, &t[1], watermark->Bands - 1, "n", 1, NULL) ||
  494. vips_linear1(t[1], &t[2], opacity, 0, NULL) ||
  495. vips_bandjoin2(t[0], t[2], &t[3], NULL)
  496. ) {
  497. clear_image(&base);
  498. return 1;
  499. }
  500. } else {
  501. if (vips_copy(watermark, &t[3], NULL)) {
  502. clear_image(&base);
  503. return 1;
  504. }
  505. }
  506. int res =
  507. vips_composite2(in, t[3], &t[4], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  508. vips_cast(t[4], out, vips_image_get_format(in), NULL);
  509. clear_image(&base);
  510. return res;
  511. #else
  512. vips_error("vips_apply_watermark", "Watermarking is not supported (libvips 8.6+ reuired)");
  513. return 1;
  514. #endif
  515. }
  516. int
  517. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  518. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  519. }
  520. int
  521. vips_strip(VipsImage *in, VipsImage **out) {
  522. static double default_resolution = 72.0 / 25.4;
  523. if (vips_copy(
  524. in, out,
  525. "xres", default_resolution,
  526. "yres", default_resolution,
  527. NULL
  528. )) return 1;
  529. gchar **fields = vips_image_get_fields(in);
  530. for (int i = 0; fields[i] != NULL; i++) {
  531. gchar *name = fields[i];
  532. if (strcmp(name, VIPS_META_ICC_NAME) == 0) continue;
  533. vips_image_remove(*out, name);
  534. }
  535. g_strfreev(fields);
  536. return 0;
  537. }
  538. int
  539. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  540. return vips_jpegsave_buffer(
  541. in, buf, len,
  542. "Q", quality,
  543. "optimize_coding", TRUE,
  544. "interlace", interlace,
  545. NULL
  546. );
  547. }
  548. int
  549. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  550. if (!quantize)
  551. return vips_pngsave_buffer(
  552. in, buf, len,
  553. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  554. "interlace", interlace,
  555. NULL
  556. );
  557. int bitdepth = 1;
  558. if (colors > 16) bitdepth = 8;
  559. else if (colors > 4) bitdepth = 4;
  560. else if (colors > 2) bitdepth = 2;
  561. return vips_pngsave_buffer(
  562. in, buf, len,
  563. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  564. "interlace", interlace,
  565. #if VIPS_SUPPORT_PNG_BITDEPTH
  566. "palette", quantize,
  567. "bitdepth", bitdepth,
  568. #elif VIPS_SUPPORT_PNG_QUANTIZATION // VIPS_SUPPORT_PNG_BITDEPTH
  569. "palette", quantize,
  570. "colours", colors,
  571. #endif // VIPS_SUPPORT_PNG_QUANTIZATION
  572. NULL
  573. );
  574. }
  575. int
  576. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  577. return vips_webpsave_buffer(
  578. in, buf, len,
  579. "Q", quality,
  580. NULL
  581. );
  582. }
  583. int
  584. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  585. #if VIPS_SUPPORT_MAGICK
  586. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  587. #else
  588. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.7+ reuired)");
  589. return 1;
  590. #endif
  591. }
  592. int
  593. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  594. #if VIPS_SUPPORT_TIFF
  595. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  596. #else
  597. vips_error("vips_tiffsave_go", "Saving TIFF is not supported (libvips 8.6+ reuired)");
  598. return 1;
  599. #endif
  600. }
  601. int
  602. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  603. #if VIPS_SUPPORT_AVIF
  604. return vips_heifsave_buffer(
  605. in, buf, len,
  606. "Q", quality,
  607. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  608. #if VIPS_SUPPORT_AVIF_SPEED
  609. "speed", speed,
  610. #endif
  611. NULL);
  612. #else
  613. vips_error("vips_avifsave_go", "Saving AVIF is not supported (libvips 8.9+ reuired)");
  614. return 1;
  615. #endif
  616. }
  617. int
  618. vips_bmpsave_go(VipsImage *in, void **buf, size_t *len) {
  619. #if VIPS_SUPPORT_MAGICK
  620. return vips_magicksave_buffer(in, buf, len, "format", "bmp", NULL);
  621. #else
  622. vips_error("vips_bmpsave_go", "Saving BMP is not supported");
  623. return 1;
  624. #endif
  625. }
  626. void
  627. vips_cleanup() {
  628. vips_error_clear();
  629. vips_thread_shutdown();
  630. }