vips.c 15 KB

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