vips.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. int
  139. vips_get_palette_bit_depth(VipsImage *image) {
  140. int palette_bit_depth;
  141. if (
  142. vips_image_get_typeof(image, "palette-bit-depth") == G_TYPE_INT &&
  143. vips_image_get_int(image, "palette-bit-depth", &palette_bit_depth) == 0
  144. ) return palette_bit_depth;
  145. return 0;
  146. }
  147. VipsBandFormat
  148. vips_band_format(VipsImage *in) {
  149. return in->BandFmt;
  150. }
  151. gboolean
  152. vips_is_animated(VipsImage * in) {
  153. return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
  154. vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
  155. vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
  156. }
  157. int
  158. vips_image_get_array_int_go(VipsImage *image, const char *name, int **out, int *n) {
  159. #if VIPS_SUPPORT_ARRAY_HEADERS
  160. return vips_image_get_array_int(image, name, out, n);
  161. #else
  162. vips_error("vips_image_get_array_int_go", "Array headers are not supported (libvips 8.9+ reuired)");
  163. return 1;
  164. #endif
  165. }
  166. void
  167. vips_image_set_array_int_go(VipsImage *image, const char *name, const int *array, int n) {
  168. #if VIPS_SUPPORT_ARRAY_HEADERS
  169. vips_image_set_array_int(image, name, array, n);
  170. #endif
  171. }
  172. gboolean
  173. vips_image_hasalpha_go(VipsImage * in) {
  174. return vips_image_hasalpha(in);
  175. }
  176. int
  177. vips_addalpha_go(VipsImage *in, VipsImage **out) {
  178. return vips_addalpha(in, out, NULL);
  179. }
  180. int
  181. vips_copy_go(VipsImage *in, VipsImage **out) {
  182. return vips_copy(in, out, NULL);
  183. }
  184. int
  185. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  186. return vips_cast(in, out, format, NULL);
  187. }
  188. int
  189. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  190. return vips_rad2float(in, out, NULL);
  191. }
  192. int
  193. vips_resize_go(VipsImage *in, VipsImage **out, double wscale, double hscale) {
  194. return vips_resize(in, out, wscale, "vscale", hscale, NULL);
  195. }
  196. int
  197. vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double wscale, double hscale) {
  198. VipsBandFormat format;
  199. VipsImage *tmp1, *tmp2;
  200. format = vips_band_format(in);
  201. if (vips_premultiply(in, &tmp1, NULL))
  202. return 1;
  203. if (vips_resize(tmp1, &tmp2, wscale, "vscale", hscale, NULL)) {
  204. clear_image(&tmp1);
  205. return 1;
  206. }
  207. swap_and_clear(&tmp1, tmp2);
  208. if (vips_unpremultiply(tmp1, &tmp2, NULL)) {
  209. clear_image(&tmp1);
  210. return 1;
  211. }
  212. swap_and_clear(&tmp1, tmp2);
  213. if (vips_cast(tmp1, out, format, NULL)) {
  214. clear_image(&tmp1);
  215. return 1;
  216. }
  217. clear_image(&tmp1);
  218. return 0;
  219. }
  220. int
  221. vips_icc_is_srgb_iec61966(VipsImage *in) {
  222. const void *data;
  223. size_t data_len;
  224. // 1998-12-01
  225. static char date[] = { 7, 206, 0, 2, 0, 9 };
  226. // 2.1
  227. static char version[] = { 2, 16, 0, 0 };
  228. if (vips_image_get_blob(in, VIPS_META_ICC_NAME, &data, &data_len))
  229. return FALSE;
  230. // Less than header size
  231. if (data_len < 128)
  232. return FALSE;
  233. // Predict it is sRGB IEC61966 2.1 by checking some header fields
  234. return ((memcmp(data + 48, "IEC ", 4) == 0) && // Device manufacturer
  235. (memcmp(data + 52, "sRGB", 4) == 0) && // Device model
  236. (memcmp(data + 80, "HP ", 4) == 0) && // Profile creator
  237. (memcmp(data + 24, date, 6) == 0) && // Date of creation
  238. (memcmp(data + 8, version, 4) == 0)); // Version
  239. }
  240. int
  241. vips_has_embedded_icc(VipsImage *in) {
  242. return vips_image_get_typeof(in, VIPS_META_ICC_NAME) != 0;
  243. }
  244. int
  245. vips_icc_import_go(VipsImage *in, VipsImage **out) {
  246. return vips_icc_import(in, out, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  247. }
  248. int
  249. vips_icc_export_go(VipsImage *in, VipsImage **out) {
  250. return vips_icc_export(in, out, NULL);
  251. }
  252. int
  253. vips_icc_export_srgb(VipsImage *in, VipsImage **out) {
  254. return vips_icc_export(in, out, "output_profile", "sRGB", NULL);
  255. }
  256. int
  257. vips_icc_transform_go(VipsImage *in, VipsImage **out) {
  258. return vips_icc_transform(in, out, "sRGB", "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  259. }
  260. int
  261. vips_icc_remove(VipsImage *in, VipsImage **out) {
  262. if (vips_copy(in, out, NULL)) return 1;
  263. vips_image_remove(*out, VIPS_META_ICC_NAME);
  264. return 0;
  265. }
  266. int
  267. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  268. return vips_colourspace(in, out, cs, NULL);
  269. }
  270. int
  271. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  272. return vips_rot(in, out, angle, NULL);
  273. }
  274. int
  275. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  276. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  277. }
  278. int
  279. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  280. return vips_smartcrop(in, out, width, height, NULL);
  281. }
  282. int
  283. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  284. return vips_gaussblur(in, out, sigma, NULL);
  285. }
  286. int
  287. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  288. return vips_sharpen(in, out, "sigma", sigma, NULL);
  289. }
  290. int
  291. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  292. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  293. int res = vips_flatten(in, out, "background", bg, NULL);
  294. vips_area_unref((VipsArea *)bg);
  295. return res;
  296. }
  297. int
  298. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  299. return vips_extract_area(in, out, left, top, width, height, NULL);
  300. }
  301. int
  302. vips_trim(VipsImage *in, VipsImage **out, double threshold,
  303. gboolean smart, double r, double g, double b,
  304. gboolean equal_hor, gboolean equal_ver) {
  305. VipsImage *tmp;
  306. if (vips_image_hasalpha(in)) {
  307. if (vips_flatten(in, &tmp, NULL))
  308. return 1;
  309. } else {
  310. if (vips_copy(in, &tmp, NULL))
  311. return 1;
  312. }
  313. double *bg;
  314. int bgn;
  315. VipsArrayDouble *bga;
  316. if (smart) {
  317. if (vips_getpoint(tmp, &bg, &bgn, 0, 0, NULL)) {
  318. clear_image(&tmp);
  319. return 1;
  320. }
  321. bga = vips_array_double_new(bg, bgn);
  322. } else {
  323. bga = vips_array_double_newv(3, r, g, b);
  324. bg = 0;
  325. }
  326. int left, right, top, bot, width, height, diff;
  327. if (vips_find_trim(tmp, &left, &top, &width, &height, "background", bga, "threshold", threshold, NULL)) {
  328. clear_image(&tmp);
  329. vips_area_unref((VipsArea *)bga);
  330. g_free(bg);
  331. return 1;
  332. }
  333. if (equal_hor) {
  334. right = in->Xsize - left - width;
  335. diff = right - left;
  336. if (diff > 0) {
  337. width += diff;
  338. } else if (diff < 0) {
  339. left = right;
  340. width -= diff;
  341. }
  342. }
  343. if (equal_ver) {
  344. bot = in->Ysize - top - height;
  345. diff = bot - top;
  346. if (diff > 0) {
  347. height += diff;
  348. } else if (diff < 0) {
  349. top = bot;
  350. height -= diff;
  351. }
  352. }
  353. clear_image(&tmp);
  354. vips_area_unref((VipsArea *)bga);
  355. g_free(bg);
  356. if (width == 0 || height == 0) {
  357. return vips_copy(in, out, NULL);
  358. }
  359. return vips_extract_area(in, out, left, top, width, height, NULL);
  360. }
  361. int
  362. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  363. VipsImage *tmp;
  364. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  365. return 1;
  366. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  367. clear_image(&tmp);
  368. return 1;
  369. }
  370. clear_image(&tmp);
  371. return 0;
  372. }
  373. int
  374. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height, double *bg, int bgn) {
  375. VipsArrayDouble *bga = vips_array_double_new(bg, bgn);
  376. int ret = vips_embed(
  377. in, out, x, y, width, height,
  378. "extend", VIPS_EXTEND_BACKGROUND,
  379. "background", bga,
  380. NULL
  381. );
  382. vips_area_unref((VipsArea *)bga);
  383. return ret;
  384. }
  385. int
  386. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  387. if (vips_image_hasalpha_go(in)) {
  388. return vips_copy(in, out, NULL);
  389. }
  390. return vips_bandjoin_const1(in, out, 255, NULL);
  391. }
  392. int
  393. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  394. VipsImage *base = vips_image_new();
  395. VipsImage **t = (VipsImage **) vips_object_local_array(VIPS_OBJECT(base), 5);
  396. if (opacity < 1) {
  397. if (
  398. vips_extract_band(watermark, &t[0], 0, "n", watermark->Bands - 1, NULL) ||
  399. vips_extract_band(watermark, &t[1], watermark->Bands - 1, "n", 1, NULL) ||
  400. vips_linear1(t[1], &t[2], opacity, 0, NULL) ||
  401. vips_bandjoin2(t[0], t[2], &t[3], NULL)
  402. ) {
  403. clear_image(&base);
  404. return 1;
  405. }
  406. } else {
  407. if (vips_copy(watermark, &t[3], NULL)) {
  408. clear_image(&base);
  409. return 1;
  410. }
  411. }
  412. int res =
  413. vips_composite2(in, t[3], &t[4], VIPS_BLEND_MODE_OVER, "compositing_space", in->Type, NULL) ||
  414. vips_cast(t[4], out, vips_image_get_format(in), NULL);
  415. clear_image(&base);
  416. return res;
  417. }
  418. int
  419. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  420. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  421. }
  422. int
  423. vips_strip(VipsImage *in, VipsImage **out) {
  424. if (vips_copy(in, out, NULL)) return 1;
  425. gchar **fields = vips_image_get_fields(in);
  426. for (int i = 0; fields[i] != NULL; i++) {
  427. gchar *name = fields[i];
  428. if (strcmp(name, VIPS_META_ICC_NAME) == 0) continue;
  429. if (strcmp(name, "palette-bit-depth") == 0) continue;
  430. vips_image_remove(*out, name);
  431. }
  432. g_strfreev(fields);
  433. return 0;
  434. }
  435. int
  436. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  437. return vips_jpegsave_buffer(
  438. in, buf, len,
  439. "Q", quality,
  440. "optimize_coding", TRUE,
  441. "interlace", interlace,
  442. NULL
  443. );
  444. }
  445. int
  446. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int quantize, int colors) {
  447. int bitdepth;
  448. if (quantize) {
  449. bitdepth = 1;
  450. if (colors > 16) bitdepth = 8;
  451. else if (colors > 4) bitdepth = 4;
  452. else if (colors > 2) bitdepth = 2;
  453. } else {
  454. bitdepth = vips_get_palette_bit_depth(in);
  455. if (bitdepth) {
  456. quantize = 1;
  457. colors = 1 << bitdepth;
  458. }
  459. }
  460. if (!quantize)
  461. return vips_pngsave_buffer(
  462. in, buf, len,
  463. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  464. "interlace", interlace,
  465. NULL
  466. );
  467. return vips_pngsave_buffer(
  468. in, buf, len,
  469. "filter", VIPS_FOREIGN_PNG_FILTER_NONE,
  470. "interlace", interlace,
  471. #if VIPS_SUPPORT_PNG_BITDEPTH
  472. "palette", quantize,
  473. "bitdepth", bitdepth,
  474. #else // VIPS_SUPPORT_PNG_BITDEPTH
  475. "palette", quantize,
  476. "colours", colors,
  477. #endif // VIPS_SUPPORT_PNG_BITDEPTH
  478. NULL
  479. );
  480. }
  481. int
  482. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  483. return vips_webpsave_buffer(
  484. in, buf, len,
  485. "Q", quality,
  486. NULL
  487. );
  488. }
  489. int
  490. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  491. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  492. }
  493. int
  494. vips_tiffsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  495. return vips_tiffsave_buffer(in, buf, len, "Q", quality, NULL);
  496. }
  497. int
  498. vips_avifsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  499. #if VIPS_SUPPORT_AVIF
  500. return vips_heifsave_buffer(in, buf, len, "Q", quality, "compression", VIPS_FOREIGN_HEIF_COMPRESSION_AV1, NULL);
  501. #else
  502. vips_error("vips_avifsave_go", "Saving AVIF is not supported (libvips 8.9+ reuired)");
  503. return 1;
  504. #endif
  505. }
  506. int
  507. vips_bmpsave_go(VipsImage *in, void **buf, size_t *len) {
  508. return vips_magicksave_buffer(in, buf, len, "format", "bmp", NULL);
  509. }
  510. void
  511. vips_cleanup() {
  512. vips_error_clear();
  513. vips_thread_shutdown();
  514. }