vips.c 14 KB

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