vips.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. int
  12. vips_initialize() {
  13. return vips_init("imgproxy");
  14. }
  15. void
  16. clear_image(VipsImage **in) {
  17. if (G_IS_OBJECT(*in)) g_clear_object(in);
  18. }
  19. void
  20. g_free_go(void **buf) {
  21. g_free(*buf);
  22. }
  23. void
  24. swap_and_clear(VipsImage **in, VipsImage *out) {
  25. clear_image(in);
  26. *in = out;
  27. }
  28. int
  29. vips_jpegload_go(void *buf, size_t len, int shrink, VipsImage **out) {
  30. if (shrink > 1)
  31. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  32. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  33. }
  34. int
  35. vips_pngload_go(void *buf, size_t len, VipsImage **out) {
  36. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  37. }
  38. int
  39. vips_webpload_go(void *buf, size_t len, double scale, int pages, VipsImage **out) {
  40. return vips_webpload_buffer(
  41. buf, len, out,
  42. "access", VIPS_ACCESS_SEQUENTIAL,
  43. "scale", scale,
  44. "n", pages,
  45. NULL
  46. );
  47. }
  48. int
  49. vips_gifload_go(void *buf, size_t len, int pages, VipsImage **out) {
  50. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "n", pages, NULL);
  51. }
  52. int
  53. vips_svgload_go(void *buf, size_t len, double scale, VipsImage **out) {
  54. // libvips limits the minimal scale to 0.001, so we have to scale down dpi
  55. // for lower scale values
  56. double dpi = 72.0;
  57. if (scale < 0.001) {
  58. dpi *= VIPS_MAX(scale / 0.001, 0.001);
  59. scale = 0.001;
  60. }
  61. return vips_svgload_buffer(
  62. buf, len, out,
  63. "access", VIPS_ACCESS_SEQUENTIAL,
  64. "scale", scale,
  65. "dpi", dpi,
  66. NULL
  67. );
  68. }
  69. int
  70. vips_heifload_go(void *buf, size_t len, VipsImage **out, int thumbnail) {
  71. return vips_heifload_buffer(
  72. buf, len, out,
  73. "access", VIPS_ACCESS_SEQUENTIAL,
  74. "thumbnail", thumbnail,
  75. NULL
  76. );
  77. }
  78. int
  79. vips_tiffload_go(void *buf, size_t len, VipsImage **out) {
  80. return vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  81. }
  82. int
  83. vips_black_go(VipsImage **out, int width, int height, int bands) {
  84. VipsImage *tmp;
  85. int res = vips_black(&tmp, width, height, "bands", bands, NULL) ||
  86. vips_copy(tmp, out, "interpretation", VIPS_INTERPRETATION_sRGB, NULL);
  87. clear_image(&tmp);
  88. return res;
  89. }
  90. int
  91. vips_get_orientation(VipsImage *image) {
  92. int orientation;
  93. if (
  94. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  95. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0
  96. ) return orientation;
  97. return 1;
  98. }
  99. int
  100. vips_get_palette_bit_depth(VipsImage *image) {
  101. int palette_bit_depth;
  102. if (
  103. vips_image_get_typeof(image, "palette-bit-depth") == G_TYPE_INT &&
  104. vips_image_get_int(image, "palette-bit-depth", &palette_bit_depth) == 0
  105. ) return palette_bit_depth;
  106. return 0;
  107. }
  108. VipsBandFormat
  109. vips_band_format(VipsImage *in) {
  110. return in->BandFmt;
  111. }
  112. gboolean
  113. vips_is_animated(VipsImage * in) {
  114. int n_pages;
  115. return( vips_image_get_typeof(in, "delay") != G_TYPE_INVALID &&
  116. vips_image_get_typeof(in, "loop") != G_TYPE_INVALID &&
  117. vips_image_get_typeof(in, "page-height") == G_TYPE_INT &&
  118. vips_image_get_typeof(in, "n-pages") == G_TYPE_INT &&
  119. vips_image_get_int(in, "n-pages", &n_pages) == 0 &&
  120. n_pages > 1 );
  121. }
  122. int
  123. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
  124. return vips_image_get_array_int(image, name, out, n);
  125. }
  126. void
  127. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
  128. vips_image_set_array_int(image, name, array, n);
  129. }
  130. int
  131. vips_addalpha_go(VipsImage *in, VipsImage **out) {
  132. return vips_addalpha(in, out, NULL);
  133. }
  134. int
  135. vips_premultiply_go(VipsImage *in, VipsImage **out) {
  136. if (!vips_image_hasalpha(in))
  137. return vips_copy(in, out, NULL);
  138. return vips_premultiply(in, out, NULL);
  139. }
  140. int
  141. vips_unpremultiply_go(VipsImage *in, VipsImage **out) {
  142. if (!vips_image_hasalpha(in))
  143. return vips_copy(in, out, NULL);
  144. return vips_unpremultiply(in, out, NULL);
  145. }
  146. int
  147. vips_copy_go(VipsImage *in, VipsImage **out) {
  148. return vips_copy(in, out, NULL);
  149. }
  150. int
  151. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  152. return vips_cast(in, out, format, NULL);
  153. }
  154. int
  155. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  156. return vips_rad2float(in, out, NULL);
  157. }
  158. int
  159. vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale) {
  160. if (!vips_image_hasalpha(in))
  161. return vips_resize(in, out, wscale, "vscale", hscale, NULL);
  162. VipsBandFormat format = vips_band_format(in);
  163. VipsImage *base = vips_image_new();
  164. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 3);
  165. int res =
  166. vips_premultiply(in, &t[0], NULL) ||
  167. vips_resize(t[0], &t[1], wscale, "vscale", hscale, NULL) ||
  168. vips_unpremultiply(t[1], &t[2], NULL) ||
  169. vips_cast(t[2], out, format, NULL);
  170. clear_image(&base);
  171. return 0;
  172. }
  173. int
  174. vips_pixelate(VipsImage *in, VipsImage **out, int pixels) {
  175. VipsImage *base = vips_image_new();
  176. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 3);
  177. int w, h, tw, th;
  178. w = in->Xsize;
  179. h = in->Ysize;
  180. tw = (int)((double)(w + pixels - 1) / pixels) * pixels;
  181. th = (int)((double)(h + pixels - 1) / pixels) * pixels;
  182. if (tw > w || th > h) {
  183. if (vips_embed(in, &t[0], 0, 0, tw, th, "extend", VIPS_EXTEND_COPY, NULL)) {
  184. clear_image(&base);
  185. return 1;
  186. }
  187. } else {
  188. if (vips_copy(in, &t[0], NULL)) {
  189. clear_image(&base);
  190. return 1;
  191. }
  192. }
  193. if (
  194. vips_shrink(t[0], &t[1], pixels, pixels, NULL) ||
  195. vips_zoom(t[1], &t[2], pixels, pixels, NULL)
  196. ) {
  197. clear_image(&base);
  198. return 1;
  199. }
  200. if (tw > w || th > h) {
  201. if (vips_extract_area(t[2], out, 0, 0, w, h, NULL)) {
  202. clear_image(&base);
  203. return 1;
  204. }
  205. } else {
  206. if (vips_copy(t[2], out, NULL)) {
  207. clear_image(&base);
  208. return 1;
  209. }
  210. }
  211. clear_image(&base);
  212. return 0;
  213. }
  214. int
  215. vips_icc_is_srgb_iec61966(VipsImage *in) {
  216. const void *data;
  217. size_t data_len;
  218. // 1998-12-01
  219. static char date[] = { 7, 206, 0, 2, 0, 9 };
  220. // 2.1
  221. static char version[] = { 2, 16, 0, 0 };
  222. // The image had no profile and built-in CMYK was imported.
  223. // Vips gives us an invalid data pointer when the built-in profile was imported,
  224. // so we check this mark before receiving an actual profile.
  225. // if (vips_image_get_typeof(in, "icc-cmyk-no-profile"))
  226. // return FALSE;
  227. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  228. return FALSE;
  229. // Less than header size
  230. if (data_len < 128)
  231. return FALSE;
  232. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  233. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  234. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  235. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  236. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  237. (memcmp(data + 8, version, 4) == 0)); // Version
  238. }
  239. int
  240. vips_has_embedded_icc(VipsImage *in) {
  241. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  242. }
  243. int
  244. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  245. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  246. }
  247. int
  248. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  249. return vips_icc_export(in, out, "pcs", VIPS_PCS_LAB, NULL);
  250. }
  251. int
  252. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  253. return vips_icc_export(in, out, "output_profile", "sRGB", "pcs", VIPS_PCS_LAB, NULL);
  254. }
  255. int
  256. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  257. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_LAB, NULL);
  258. }
  259. int
  260. vips_icc_remove(VipsImage *in, VipsImage **out) {
  261. if (vips_copy(in, out, NULL)) return 1;
  262. vips_image_remove(*out, VIPS_META_ICC_NAME);
  263. return 0;
  264. }
  265. int
  266. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  267. return vips_colourspace(in, out, cs, NULL);
  268. }
  269. int
  270. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  271. return vips_rot(in, out, angle, NULL);
  272. }
  273. int
  274. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  275. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  276. }
  277. int
  278. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  279. return vips_smartcrop(in, out, width, height, NULL);
  280. }
  281. int
  282. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  283. return vips_gaussblur(in, out, sigma, NULL);
  284. }
  285. int
  286. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  287. return vips_sharpen(in, out, "sigma", sigma, NULL);
  288. }
  289. int
  290. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  291. if (!vips_image_hasalpha(in))
  292. return vips_copy(in, out, NULL);
  293. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  294. int res = vips_flatten(in, out, "background", bg, NULL);
  295. vips_area_unref((VipsArea *)bg);
  296. return res;
  297. }
  298. int
  299. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  300. return vips_extract_area(in, out, left, top, width, height, NULL);
  301. }
  302. int
  303. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  304. gboolean smart, double r, double g, double b,
  305. gboolean equal_hor, gboolean equal_ver) {
  306. VipsImage *base = vips_image_new();
  307. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 2);
  308. VipsImage *tmp = in;
  309. if (vips_image_guess_interpretation(in) != VIPS_INTERPRETATION_sRGB) {
  310. if (vips_colourspace(in, &t[0], VIPS_INTERPRETATION_sRGB, NULL)) {
  311. clear_image(&base);
  312. return 1;
  313. }
  314. tmp = t[0];
  315. }
  316. if (vips_image_hasalpha(tmp)) {
  317. if (vips_flatten_go(tmp, &t[1], 255.0, 0, 255.0)) {
  318. clear_image(&base);
  319. return 1;
  320. }
  321. tmp = t[1];
  322. }
  323. double *bg = NULL;
  324. int bgn;
  325. VipsArrayDouble *bga;
  326. if (smart) {
  327. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  328. clear_image(&base);
  329. return 1;
  330. }
  331. bga = vips_array_double_new(bg, bgn);
  332. } else {
  333. bga = vips_array_double_newv(3, r, g, b);
  334. }
  335. int left, right, top, bot, width, height, diff;
  336. int res = vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL);
  337. clear_image(&base);
  338. vips_area_unref((VipsArea *)bga);
  339. g_free(bg);
  340. if (res) {
  341. return 1;
  342. }
  343. if (equal_hor) {
  344. right = in->Xsize - left - width;
  345. diff = right - left;
  346. if (diff > 0) {
  347. width += diff;
  348. } else if (diff < 0) {
  349. left = right;
  350. width -= diff;
  351. }
  352. }
  353. if (equal_ver) {
  354. bot = in->Ysize - top - height;
  355. diff = bot - top;
  356. if (diff > 0) {
  357. height += diff;
  358. } else if (diff < 0) {
  359. top = bot;
  360. height -= diff;
  361. }
  362. }
  363. if (width == 0 || height == 0) {
  364. return vips_copy(in, out, NULL);
  365. }
  366. return vips_extract_area(in, out, left, top, width, height, NULL);
  367. }
  368. int
  369. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  370. VipsImage *tmp;
  371. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  372. return 1;
  373. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  374. clear_image(&tmp);
  375. return 1;
  376. }
  377. clear_image(&tmp);
  378. return 0;
  379. }
  380. int
  381. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  382. VipsImage *tmp;
  383. int ret =
  384. vips_ensure_alpha(in, &tmp) ||
  385. vips_embed(tmp, out, x, y, width, height, "extend", VIPS_EXTEND_BLACK, NULL);
  386. clear_image(&tmp);
  387. return ret;
  388. }
  389. int
  390. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  391. if (vips_image_hasalpha(in))
  392. return vips_copy(in, out, NULL);
  393. return vips_bandjoin_const1(in, out, 255, NULL);
  394. }
  395. int
  396. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  397. VipsImage *base = vips_image_new();
  398. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 6);
  399. if (vips_ensure_alpha(watermark, &t[0])) {
  400. clear_image(&base);
  401. return 1;
  402. }
  403. if (opacity < 1) {
  404. if (
  405. vips_extract_band(t[0], &t[1], 0, "n", t[0]->Bands - 1, NULL) ||
  406. vips_extract_band(t[0], &t[2], t[0]->Bands - 1, "n", 1, NULL) ||
  407. vips_linear1(t[2], &t[3], opacity, 0, NULL) ||
  408. vips_bandjoin2(t[1], t[3], &t[4], NULL)
  409. ) {
  410. clear_image(&base);
  411. return 1;
  412. }
  413. } else {
  414. if (vips_copy(t[0], &t[4], NULL)) {
  415. clear_image(&base);
  416. return 1;
  417. }
  418. }
  419. int res =
  420. vips_composite2(in, t[4], &t[5], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  421. vips_cast(t[5], out, vips_image_get_format(in), NULL);
  422. clear_image(&base);
  423. return res;
  424. }
  425. int
  426. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  427. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  428. }
  429. int
  430. vips_strip(VipsImage *in, VipsImage **out, int keep_exif_copyright) {
  431. static double default_resolution = 72.0 / 25.4;
  432. if (vips_copy(
  433. in, out,
  434. "xres", default_resolution,
  435. "yres", default_resolution,
  436. NULL
  437. )) return 1;
  438. gchar **fields = vips_image_get_fields(in);
  439. for (int i = 0; fields[i] != NULL; i++) {
  440. gchar *name = fields[i];
  441. if (strcmp(name, VIPS_META_ICC_NAME) == 0) continue;
  442. if (strcmp(name, "palette-bit-depth") == 0) continue;
  443. if (keep_exif_copyright) {
  444. if (strcmp(name, VIPS_META_EXIF_NAME) == 0) continue;
  445. if (strcmp(name, "exif-ifd0-Copyright") == 0) continue;
  446. if (strcmp(name, "exif-ifd0-Artist") == 0) continue;
  447. }
  448. vips_image_remove(*out, name);
  449. }
  450. g_strfreev(fields);
  451. return 0;
  452. }
  453. int
  454. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  455. return vips_jpegsave_buffer(
  456. in, buf, len,
  457. "Q", quality,
  458. "optimize_coding", TRUE,
  459. "interlace", interlace,
  460. NULL
  461. );
  462. }
  463. int
  464. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  465. int bitdepth;
  466. if (quantize) {
  467. bitdepth = 1;
  468. if (colors > 16) bitdepth = 8;
  469. else if (colors > 4) bitdepth = 4;
  470. else if (colors > 2) bitdepth = 2;
  471. } else {
  472. bitdepth = vips_get_palette_bit_depth(in);
  473. if (bitdepth) {
  474. if (bitdepth > 4) bitdepth = 8;
  475. else if (bitdepth > 2) bitdepth = 4;
  476. quantize = 1;
  477. colors = 1 << bitdepth;
  478. }
  479. }
  480. if (!quantize)
  481. return vips_pngsave_buffer(
  482. in, buf, len,
  483. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  484. "interlace", interlace,
  485. NULL
  486. );
  487. return vips_pngsave_buffer(
  488. in, buf, len,
  489. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  490. "interlace", interlace,
  491. "palette", quantize,
  492. "bitdepth", bitdepth,
  493. NULL
  494. );
  495. }
  496. int
  497. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  498. return vips_webpsave_buffer(
  499. in, buf, len,
  500. "Q", quality,
  501. NULL
  502. );
  503. }
  504. int
  505. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  506. #if VIPS_SUPPORT_GIFSAVE
  507. return vips_gifsave_buffer(in, buf, len, NULL);
  508. #else
  509. vips_error("vips_gifsave_go", "Saving GIF is not supported (libvips 8.12+ reuired)");
  510. return 1;
  511. #endif
  512. }
  513. int
  514. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  515. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  516. }
  517. int
  518. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality, int speed) {
  519. return vips_heifsave_buffer(
  520. in, buf, len,
  521. "Q", quality,
  522. "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1,
  523. #if VIPS_SUPPORT_AVIF_EFFORT
  524. "effort", 9-speed,
  525. #elif VIPS_SUPPORT_AVIF_SPEED
  526. "speed", speed,
  527. #endif
  528. NULL);
  529. }
  530. void
  531. vips_cleanup() {
  532. vips_error_clear();
  533. vips_thread_shutdown();
  534. }