vips.c 15 KB

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