vips.c 15 KB

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