1
0

vips.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. #include "vips.h"
  2. #include <string.h>
  3. #define VIPS_SUPPORT_AVIF_SPEED \
  4. (VIPS_MAJOR_VERSION > 8 || \
  5. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION > 10) || \
  6. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 10 && VIPS_MICRO_VERSION >= 2))
  7. #define VIPS_SUPPORT_AVIF_EFFORT \
  8. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 12))
  9. #define VIPS_SUPPORT_GIFSAVE \
  10. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 12))
  11. #define VIPS_GIF_RESOLUTION_LIMITED \
  12. (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION <= 12)
  13. #define VIPS_SCRGB_ALPHA_FIXED \
  14. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 15))
  15. #ifndef VIPS_META_BITS_PER_SAMPLE
  16. #define VIPS_META_BITS_PER_SAMPLE "palette-bit-depth"
  17. #endif
  18. int
  19. vips_initialize() {
  20. return vips_init("imgproxy");
  21. }
  22. void
  23. clear_image(VipsImage **in) {
  24. if (G_IS_OBJECT(*in)) g_clear_object(in);
  25. }
  26. void
  27. g_free_go(void **buf) {
  28. g_free(*buf);
  29. }
  30. void
  31. swap_and_clear(VipsImage **in, VipsImage *out) {
  32. clear_image(in);
  33. *in = out;
  34. }
  35. int
  36. gif_resolution_limit() {
  37. #if VIPS_GIF_RESOLUTION_LIMITED
  38. // https://github.com/libvips/libvips/blob/v8.12.2/libvips/foreign/cgifsave.c#L437-L442
  39. return 2000 * 2000;
  40. #else
  41. return INT_MAX / 4;
  42. #endif
  43. }
  44. // Just create and destroy a tiny image to ensure vips is operational
  45. int
  46. vips_health() {
  47. VipsImage *base = vips_image_new();
  48. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  49. int res = vips_black(&t[0], 4, 4, "bands", 4, NULL) ||
  50. !(t[1] = vips_image_copy_memory(t[0]));
  51. clear_image(&base);
  52. return res;
  53. }
  54. int
  55. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  56. if (shrink > 1)
  57. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink,
  58. NULL);
  59. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  60. }
  61. int
  62. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  63. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  64. }
  65. int
  66. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out) {
  67. return vips_webpload_buffer(
  68. buf, len, out,
  69. "access", VIPS_ACCESS_SEQUENTIAL,
  70. "scale", scale,
  71. "n", pages,
  72. NULL
  73. );
  74. }
  75. int
  76. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  77. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  78. }
  79. int
  80. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
  81. // libvips limits the minimal scale to 0.001, so we have to scale down dpi
  82. // for lower scale values
  83. double dpi = 72.0;
  84. if (scale < 0.001) {
  85. dpi *= VIPS_MAX(scale / 0.001, 0.001);
  86. scale = 0.001;
  87. }
  88. return vips_svgload_buffer(
  89. buf, len, out,
  90. "access", VIPS_ACCESS_SEQUENTIAL,
  91. "scale", scale,
  92. "dpi", dpi,
  93. NULL
  94. );
  95. }
  96. int
  97. vips_heifload_go(void *buf, size_t len, VipsImage **out, int thumbnail) {
  98. return vips_heifload_buffer(
  99. buf, len, out,
  100. "access", VIPS_ACCESS_SEQUENTIAL,
  101. "thumbnail", thumbnail,
  102. NULL
  103. );
  104. }
  105. int
  106. vips_tiffload_go(void *buf, size_t len, VipsImage **out) {
  107. return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  108. }
  109. int
  110. vips_black_go(VipsImage **out, int width, int height, int bands) {
  111. VipsImage *tmp;
  112. int res = vips_black(&tmp, width, height, "bands", bands, NULL) ||
  113. vips_copy(tmp, out, "interpretation", VIPS_INTERPRETATION_sRGB, NULL);
  114. clear_image(&tmp);
  115. return res;
  116. }
  117. /* Vips loads linear alpha in the 0.0-1.0 range but uses the 0.0-255.0 range.
  118. * https://github.com/libvips/libvips/pull/3627 fixes this behavior
  119. */
  120. int
  121. vips_fix_scRGB_alpha_tiff(VipsImage *in, VipsImage **out) {
  122. #if VIPS_SCRGB_ALPHA_FIXED
  123. #warning Revise vips_fix_scRGB_tiff
  124. return vips_copy(in, out, NULL);
  125. #else
  126. VipsImage *base = vips_image_new();
  127. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 4);
  128. int res =
  129. vips_extract_band(in, &t[0], 0, "n", 3, NULL) ||
  130. vips_extract_band(in, &t[1], 3, "n", in->Bands - 3, NULL) ||
  131. vips_linear1(t[1], &t[2], 255.0, 0, NULL) ||
  132. vips_cast(t[2], &t[3], in->BandFmt, NULL) ||
  133. vips_bandjoin2(t[0], t[3], out, NULL);
  134. clear_image(&base);
  135. return res;
  136. #endif
  137. }
  138. /* Vips loads linear BW TIFFs as VIPS_INTERPRETATION_B_W or VIPS_INTERPRETATION_GREY16
  139. * but these colourspaces are not linear. We should properly convert them to
  140. * VIPS_INTERPRETATION_GREY16
  141. */
  142. int
  143. vips_fix_BW_float_tiff(VipsImage *in, VipsImage **out) {
  144. VipsImage *base = vips_image_new();
  145. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 8);
  146. VipsImage *color = in;
  147. VipsImage *alpha = NULL;
  148. /* Extract and fix alpha. Float WB TIFF uses the 0.0-1.0 range but we need
  149. * the 0.0-65535.0 range
  150. */
  151. if (in->Bands > 1) {
  152. if (
  153. vips_extract_band(in, &t[0], 0, NULL) ||
  154. vips_extract_band(in, &t[1], 1, "n", in->Bands - 1, NULL) ||
  155. vips_linear1(t[1], &t[2], 65535.0, 0, NULL) ||
  156. vips_cast_ushort(t[2], &t[3], NULL) ||
  157. vips_copy(t[3], &t[4], "interpretation", VIPS_INTERPRETATION_GREY16, NULL)
  158. ) {
  159. clear_image(&base);
  160. return 1;
  161. }
  162. color = t[0];
  163. alpha = t[4];
  164. }
  165. /* Craft an scRGB image and convert it back to GREY16 to apply a gamma
  166. * correction
  167. */
  168. VipsImage *rgb[3] = { color, color, color };
  169. if (
  170. vips_bandjoin(rgb, &t[5], 3, NULL) ||
  171. vips_colourspace(t[5], &t[6], VIPS_INTERPRETATION_GREY16,
  172. "source_space", VIPS_INTERPRETATION_scRGB, NULL)
  173. ) {
  174. clear_image(&base);
  175. return 1;
  176. }
  177. int res;
  178. if (alpha)
  179. res =
  180. vips_bandjoin2(t[6], alpha, &t[7], NULL) ||
  181. vips_icc_remove(t[7], out);
  182. else
  183. res = vips_icc_remove(t[6], out);
  184. clear_image(&base);
  185. return res;
  186. }
  187. int
  188. vips_fix_float_tiff(VipsImage *in, VipsImage **out) {
  189. /* Vips loads linear alpha in the 0.0-1.0 range but uses the 0.0-255.0 range.
  190. * https://github.com/libvips/libvips/pull/3627 fixes this behavior
  191. */
  192. if (in->Type == VIPS_INTERPRETATION_scRGB && in->Bands > 3)
  193. return vips_fix_scRGB_alpha_tiff(in, out);
  194. /* Vips loads linear BW TIFFs as VIPS_INTERPRETATION_B_W or VIPS_INTERPRETATION_GREY16
  195. * but these colourspaces are not linear. We should properly convert them to
  196. * VIPS_INTERPRETATION_GREY16
  197. */
  198. if (
  199. (in->Type == VIPS_INTERPRETATION_B_W || in->Type == VIPS_INTERPRETATION_GREY16) &&
  200. (in->BandFmt == VIPS_FORMAT_FLOAT || in->BandFmt == VIPS_FORMAT_DOUBLE)
  201. )
  202. return vips_fix_BW_float_tiff(in, out);
  203. return vips_copy(in, out, NULL);
  204. }
  205. int
  206. vips_get_orientation(VipsImage *image) {
  207. int orientation;
  208. if (
  209. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  210. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0
  211. ) return orientation;
  212. return 1;
  213. }
  214. int
  215. vips_get_bits_per_sample(VipsImage *image) {
  216. int bits_per_sample;
  217. if (
  218. vips_image_get_typeof(image, VIPS_META_BITS_PER_SAMPLE) == G_TYPE_INT &&
  219. vips_image_get_int(image, VIPS_META_BITS_PER_SAMPLE, &bits_per_sample) == 0
  220. ) return bits_per_sample;
  221. return 0;
  222. }
  223. void
  224. vips_remove_bits_per_sample(VipsImage *image) {
  225. vips_image_remove(image, VIPS_META_BITS_PER_SAMPLE);
  226. }
  227. VipsBandFormat
  228. vips_band_format(VipsImage *in) {
  229. return in->BandFmt;
  230. }
  231. gboolean
  232. vips_is_animated(VipsImage * in) {
  233. int n_pages;
  234. return( vips_image_get_typeof(in, "delay") != G_TYPE_INVALID &&
  235. vips_image_get_typeof(in, "loop") != G_TYPE_INVALID &&
  236. vips_image_get_typeof(in, "page-height") == G_TYPE_INT &&
  237. vips_image_get_typeof(in, "n-pages") == G_TYPE_INT &&
  238. vips_image_get_int(in, "n-pages", &n_pages) == 0 &&
  239. n_pages > 1 );
  240. }
  241. int
  242. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
  243. return vips_image_get_array_int(image, name, out, n);
  244. }
  245. void
  246. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
  247. vips_image_set_array_int(image, name, array, n);
  248. }
  249. int
  250. vips_addalpha_go(VipsImage *in, VipsImage **out) {
  251. return vips_addalpha(in, out, NULL);
  252. }
  253. int
  254. vips_copy_go(VipsImage *in, VipsImage **out) {
  255. return vips_copy(in, out, NULL);
  256. }
  257. int
  258. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  259. return vips_cast(in, out, format, NULL);
  260. }
  261. int
  262. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  263. return vips_rad2float(in, out, NULL);
  264. }
  265. int
  266. vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale) {
  267. if (!vips_image_hasalpha(in))
  268. return vips_resize(in, out, wscale, "vscale", hscale, NULL);
  269. VipsBandFormat format = vips_band_format(in);
  270. VipsImage *base = vips_image_new();
  271. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 4);
  272. int res =
  273. vips_premultiply(in, &t[0], NULL) ||
  274. vips_cast(t[0], &t[1], format, NULL) ||
  275. vips_resize(t[1], &t[2], wscale, "vscale", hscale, NULL) ||
  276. vips_unpremultiply(t[2], &t[3], NULL) ||
  277. vips_cast(t[3], out, format, NULL);
  278. clear_image(&base);
  279. return res;
  280. }
  281. int
  282. vips_icc_is_srgb_iec61966(VipsImage *in) {
  283. const void *data;
  284. size_t data_len;
  285. // 1998-12-01
  286. static char date[] = { 7, 206, 0, 2, 0, 9 };
  287. // 2.1
  288. static char version[] = { 2, 16, 0, 0 };
  289. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  290. return FALSE;
  291. /* Less than header size
  292. */
  293. if (data_len < 128)
  294. return FALSE;
  295. /* Predict it is sRGB IEC61966 2.1 by checking some header fields
  296. */
  297. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  298. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  299. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  300. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  301. (memcmp(data + 8, version, 4) == 0)); // Version
  302. }
  303. int
  304. vips_has_embedded_icc(VipsImage *in) {
  305. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  306. }
  307. int
  308. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  309. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  310. }
  311. int
  312. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  313. return vips_icc_export(in, out, "pcs", VIPS_PCS_LAB, NULL);
  314. }
  315. int
  316. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  317. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", VIPS_PCS_LAB, NULL);
  318. }
  319. int
  320. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  321. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  322. }
  323. int
  324. vips_icc_remove(VipsImage *in, VipsImage **out) {
  325. if (vips_copy(in, out, NULL)) return 1;
  326. vips_image_remove(*out, VIPS_META_ICC_NAME);
  327. vips_image_remove(*out, "exif-ifd0-WhitePoint");
  328. vips_image_remove(*out, "exif-ifd0-PrimaryChromaticities");
  329. vips_image_remove(*out, "exif-ifd2-ColorSpace");
  330. return 0;
  331. }
  332. int
  333. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  334. return vips_colourspace(in, out, cs, NULL);
  335. }
  336. int
  337. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  338. return vips_rot(in, out, angle, NULL);
  339. }
  340. int
  341. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  342. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  343. }
  344. int
  345. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  346. return vips_smartcrop(in, out, width, height, NULL);
  347. }
  348. int
  349. vips_apply_filters(VipsImage *in, VipsImage **out, double blur_sigma,
  350. double sharp_sigma, int pixelate_pixels) {
  351. VipsImage *base = vips_image_new();
  352. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 10);
  353. VipsInterpretation interpretation = in->Type;
  354. VipsBandFormat format = in->BandFmt;
  355. gboolean premultiplied = FALSE;
  356. if ((blur_sigma > 0 || sharp_sigma > 0) && vips_image_hasalpha(in)) {
  357. if (
  358. vips_premultiply(in, &t[0], NULL) ||
  359. vips_cast(t[0], &t[1], format, NULL)
  360. ) {
  361. clear_image(&base);
  362. return 1;
  363. }
  364. in = t[0];
  365. premultiplied = TRUE;
  366. }
  367. if (blur_sigma > 0.0) {
  368. if (vips_gaussblur(in, &t[2], blur_sigma, NULL)) {
  369. clear_image(&base);
  370. return 1;
  371. }
  372. in = t[2];
  373. }
  374. if (sharp_sigma > 0.0) {
  375. if (vips_sharpen(in, &t[3], "sigma", sharp_sigma, NULL)) {
  376. clear_image(&base);
  377. return 1;
  378. }
  379. in = t[3];
  380. }
  381. pixelate_pixels = VIPS_MIN(pixelate_pixels, VIPS_MAX(in->Xsize, in->Ysize));
  382. if (pixelate_pixels > 1) {
  383. int w, h, tw, th;
  384. w = in->Xsize;
  385. h = in->Ysize;
  386. tw = (int)(VIPS_CEIL((double)w / pixelate_pixels)) * pixelate_pixels;
  387. th = (int)(VIPS_CEIL((double)h / pixelate_pixels)) * pixelate_pixels;
  388. if (tw > w || th > h) {
  389. if (vips_embed(in, &t[4], 0, 0, tw, th, "extend", VIPS_EXTEND_MIRROR, NULL)) {
  390. clear_image(&base);
  391. return 1;
  392. }
  393. in = t[4];
  394. }
  395. if (
  396. vips_shrink(in, &t[5], pixelate_pixels, pixelate_pixels, NULL) ||
  397. vips_zoom(t[5], &t[6], pixelate_pixels, pixelate_pixels, NULL)
  398. ) {
  399. clear_image(&base);
  400. return 1;
  401. }
  402. in = t[6];
  403. if (tw > w || th > h) {
  404. if (vips_extract_area(in, &t[7], 0, 0, w, h, NULL)) {
  405. clear_image(&base);
  406. return 1;
  407. }
  408. in = t[7];
  409. }
  410. }
  411. if (premultiplied) {
  412. if (vips_unpremultiply(in, &t[8], NULL)) {
  413. clear_image(&base);
  414. return 1;
  415. }
  416. in = t[8];
  417. }
  418. int res =
  419. vips_colourspace(in, &t[9], interpretation, NULL) ||
  420. vips_cast(t[9], out, format, NULL);
  421. clear_image(&base);
  422. return res;
  423. }
  424. int
  425. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  426. if (!vips_image_hasalpha(in))
  427. return vips_copy(in, out, NULL);
  428. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  429. int res = vips_flatten(in, out, "background", bg, NULL);
  430. vips_area_unref((VipsArea *)bg);
  431. return res;
  432. }
  433. int
  434. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  435. return vips_extract_area(in, out, left, top, width, height, NULL);
  436. }
  437. int
  438. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  439. gboolean smart, double r, double g, double b,
  440. gboolean equal_hor, gboolean equal_ver) {
  441. VipsImage *base = vips_image_new();
  442. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  443. VipsImage *tmp = in;
  444. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  445. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  446. clear_image(&base);
  447. return 1;
  448. }
  449. tmp = t[0];
  450. }
  451. if (vips_image_hasalpha(tmp)) {
  452. if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
  453. clear_image(&base);
  454. return 1;
  455. }
  456. tmp = t[1];
  457. }
  458. double *bg = NULL;
  459. int bgn;
  460. VipsArrayDouble *bga;
  461. if (smart) {
  462. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  463. clear_image(&base);
  464. return 1;
  465. }
  466. bga = vips_array_double_new(bg, bgn);
  467. } else {
  468. bga = vips_array_double_newv(3, r, g, b);
  469. }
  470. int left, right, top, bot, width, height, diff;
  471. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  472. clear_image(&base);
  473. vips_area_unref((VipsArea *)bga);
  474. g_free(bg);
  475. if (res) {
  476. return 1;
  477. }
  478. if (equal_hor) {
  479. right = in->Xsize - left - width;
  480. diff = right - left;
  481. if (diff > 0) {
  482. width += diff;
  483. } else if (diff < 0) {
  484. left = right;
  485. width -= diff;
  486. }
  487. }
  488. if (equal_ver) {
  489. bot = in->Ysize - top - height;
  490. diff = bot - top;
  491. if (diff > 0) {
  492. height += diff;
  493. } else if (diff < 0) {
  494. top = bot;
  495. height -= diff;
  496. }
  497. }
  498. if (width == 0 || height == 0) {
  499. return vips_copy(in, out, NULL);
  500. }
  501. return vips_extract_area(in, out, left, top, width, height, NULL);
  502. }
  503. int
  504. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  505. VipsImage *tmp;
  506. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  507. return 1;
  508. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  509. clear_image(&tmp);
  510. return 1;
  511. }
  512. clear_image(&tmp);
  513. return 0;
  514. }
  515. int
  516. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  517. VipsImage *tmp = NULL;
  518. if (!vips_image_hasalpha(in)) {
  519. if (vips_bandjoin_const1(in, &tmp, 255, NULL))
  520. return 1;
  521. in = tmp;
  522. }
  523. int ret =
  524. vips_embed(in, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  525. if (tmp) clear_image(&tmp);
  526. return ret;
  527. }
  528. int
  529. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, int left, int top, double opacity) {
  530. VipsImage *base = vips_image_new();
  531. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 7);
  532. if (!vips_image_hasalpha(watermark)) {
  533. if (vips_bandjoin_const1(watermark, &t[0], 255, NULL))
  534. return 1;
  535. watermark = t[0];
  536. }
  537. if (opacity < 1) {
  538. if (
  539. vips_extract_band(watermark, &t[1], 0, "n", watermark->Bands - 1, NULL) ||
  540. vips_extract_band(watermark, &t[2], watermark->Bands - 1, "n", 1, NULL) ||
  541. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  542. vips_bandjoin2(t[1], t[3], &t[4], NULL)
  543. ) {
  544. clear_image(&base);
  545. return 1;
  546. }
  547. watermark = t[4];
  548. }
  549. int had_alpha = vips_image_hasalpha(in);
  550. if (
  551. vips_composite2(
  552. in, watermark, &t[5], VIPS_BLEND_MODE_OVER,
  553. "x", left, "y", top, "compositing_space", in->Type,
  554. NULL
  555. ) ||
  556. vips_cast(t[5], &t[6], vips_image_get_format(in), NULL)
  557. ) {
  558. clear_image(&base);
  559. return 1;
  560. }
  561. int res;
  562. if (!had_alpha && vips_image_hasalpha(t[6])) {
  563. res = vips_extract_band(t[6], out, 0, "n", t[6]->Bands - 1, NULL);
  564. } else {
  565. res = vips_copy(t[6], out, NULL);
  566. }
  567. clear_image(&base);
  568. return res;
  569. }
  570. int
  571. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  572. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  573. }
  574. int
  575. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright) {
  576. static double default_resolution = 72.0 / 25.4;
  577. if (vips_copy(
  578. in, out,
  579. "xres", default_resolution,
  580. "yres", default_resolution,
  581. NULL
  582. )) return 1;
  583. gchar **fields = vips_image_get_fields(in);
  584. for (int i = 0; fields[i] != NULL; i++) {
  585. gchar *name = fields[i];
  586. if (
  587. (strcmp(name, VIPS_META_ICC_NAME) == 0) ||
  588. (strcmp(name, VIPS_META_BITS_PER_SAMPLE) == 0) ||
  589. (strcmp(name, "width") == 0) ||
  590. (strcmp(name, "height") == 0) ||
  591. (strcmp(name, "bands") == 0) ||
  592. (strcmp(name, "format") == 0) ||
  593. (strcmp(name, "coding") == 0) ||
  594. (strcmp(name, "interpretation") == 0) ||
  595. (strcmp(name, "xoffset") == 0) ||
  596. (strcmp(name, "yoffset") == 0) ||
  597. (strcmp(name, "xres") == 0) ||
  598. (strcmp(name, "yres") == 0) ||
  599. (strcmp(name, "vips-loader") == 0) ||
  600. (strcmp(name, "background") == 0) ||
  601. (strcmp(name, "vips-sequential") == 0) ||
  602. (strcmp(name, "imgproxy-dpr-scale") == 0)
  603. ) continue;
  604. if (keep_exif_copyright) {
  605. if (
  606. (strcmp(name, VIPS_META_EXIF_NAME) == 0) ||
  607. (strcmp(name, "exif-ifd0-Copyright") == 0) ||
  608. (strcmp(name, "exif-ifd0-Artist") == 0)
  609. ) continue;
  610. }
  611. vips_image_remove(*out, name);
  612. }
  613. g_strfreev(fields);
  614. return 0;
  615. }
  616. int
  617. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  618. return vips_jpegsave_buffer(
  619. in, buf, len,
  620. "Q", quality,
  621. "optimize_coding", TRUE,
  622. "interlace", interlace,
  623. NULL
  624. );
  625. }
  626. int
  627. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  628. int bitdepth;
  629. if (quantize) {
  630. bitdepth = 1;
  631. if (colors > 16) bitdepth = 8;
  632. else if (colors > 4) bitdepth = 4;
  633. else if (colors > 2) bitdepth = 2;
  634. } else {
  635. bitdepth = vips_get_bits_per_sample(in);
  636. if (bitdepth && bitdepth <= 8) {
  637. if (bitdepth > 4) bitdepth = 8;
  638. else if (bitdepth > 2) bitdepth = 4;
  639. quantize = 1;
  640. colors = 1 << bitdepth;
  641. }
  642. }
  643. if (!quantize)
  644. return vips_pngsave_buffer(
  645. in, buf, len,
  646. "filter", VIPS_FOREIGN_PNG_FILTER_ALL,
  647. "interlace", interlace,
  648. NULL
  649. );
  650. return vips_pngsave_buffer(
  651. in, buf, len,
  652. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  653. "interlace", interlace,
  654. "palette", quantize,
  655. "bitdepth", bitdepth,
  656. NULL
  657. );
  658. }
  659. int
  660. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  661. return vips_webpsave_buffer(
  662. in, buf, len,
  663. "Q", quality,
  664. NULL
  665. );
  666. }
  667. int
  668. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  669. #if VIPS_SUPPORT_GIFSAVE
  670. int bitdepth = vips_get_bits_per_sample(in);
  671. if (bitdepth <= 0 || bitdepth > 8 ) bitdepth = 8;
  672. return vips_gifsave_buffer(in, buf, len, "bitdepth", bitdepth, NULL);
  673. #else
  674. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.12+ reuired)");
  675. return 1;
  676. #endif
  677. }
  678. int
  679. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  680. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  681. }
  682. int
  683. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  684. return vips_heifsave_buffer(
  685. in, buf, len,
  686. "Q", quality,
  687. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  688. #if VIPS_SUPPORT_AVIF_EFFORT
  689. "effort", 9-speed,
  690. #elif VIPS_SUPPORT_AVIF_SPEED
  691. "speed", speed,
  692. #endif
  693. NULL);
  694. }
  695. void
  696. vips_cleanup() {
  697. vips_error_clear();
  698. vips_thread_shutdown();
  699. }