vips.c 14 KB

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