vips.c 12 KB

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