vips.c 23 KB

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