vips.c 16 KB

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