vips.c 12 KB

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