vips.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #ifdef VIPS_META_ORIENTATION
  99. int
  100. vips_get_exif_orientation(VipsImage *image) {
  101. int orientation;
  102. if (
  103. vips_image_get_typeof(image, VIPS_META_ORIENTATION) == G_TYPE_INT &&
  104. vips_image_get_int(image, VIPS_META_ORIENTATION, &orientation) == 0
  105. ) return orientation;
  106. return 1;
  107. }
  108. #else
  109. int
  110. vips_get_exif_orientation(VipsImage *image) {
  111. const char *orientation;
  112. if (
  113. vips_image_get_typeof(image, EXIF_ORIENTATION) == VIPS_TYPE_REF_STRING &&
  114. vips_image_get_string(image, EXIF_ORIENTATION, &orientation) == 0
  115. ) return atoi(orientation);
  116. return 1;
  117. }
  118. #endif // VIPS_META_ORIENTATION
  119. void
  120. vips_strip_meta(VipsImage *image) {
  121. #ifdef VIPS_META_ORIENTATION
  122. vips_image_remove(image, VIPS_META_ORIENTATION);
  123. #endif
  124. vips_image_remove(image, VIPS_META_EXIF_NAME);
  125. vips_image_remove(image, VIPS_META_PHOTOSHOP_NAME);
  126. vips_image_remove(image, EXIF_ORIENTATION);
  127. }
  128. int
  129. vips_support_smartcrop() {
  130. #if VIPS_SUPPORT_SMARTCROP
  131. return 1;
  132. #else
  133. return 0;
  134. #endif
  135. }
  136. VipsBandFormat
  137. vips_band_format(VipsImage *in) {
  138. return in->BandFmt;
  139. }
  140. gboolean
  141. vips_is_animated_gif(VipsImage * in) {
  142. return( vips_image_get_typeof(in, "page-height") != G_TYPE_INVALID &&
  143. vips_image_get_typeof(in, "gif-delay") != G_TYPE_INVALID &&
  144. vips_image_get_typeof(in, "gif-loop") != G_TYPE_INVALID );
  145. }
  146. gboolean
  147. vips_image_hasalpha_go(VipsImage * in) {
  148. #if VIPS_SUPPORT_HASALPHA
  149. return vips_image_hasalpha(in);
  150. #else
  151. return( in->Bands == 2 ||
  152. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  153. in->Bands > 4 );
  154. #endif
  155. }
  156. int
  157. vips_copy_go(VipsImage *in, VipsImage **out) {
  158. return vips_copy(in, out, NULL);
  159. }
  160. int
  161. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  162. return vips_cast(in, out, format, NULL);
  163. }
  164. int
  165. vips_rad2float_go(VipsImage *in, VipsImage **out) {
  166. return vips_rad2float(in, out, NULL);
  167. }
  168. int
  169. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  170. return vips_resize(in, out, scale, NULL);
  171. }
  172. int
  173. vips_resize_with_premultiply(VipsImage *in, VipsImage **out, double scale) {
  174. VipsBandFormat format;
  175. VipsImage *tmp1, *tmp2;
  176. format = vips_band_format(in);
  177. if (vips_premultiply(in, &tmp1, NULL))
  178. return 1;
  179. if (vips_resize(tmp1, &tmp2, scale, NULL)) {
  180. clear_image(&tmp1);
  181. return 1;
  182. }
  183. swap_and_clear(&tmp1, tmp2);
  184. if (vips_unpremultiply(tmp1, &tmp2, NULL)) {
  185. clear_image(&tmp1);
  186. return 1;
  187. }
  188. swap_and_clear(&tmp1, tmp2);
  189. if (vips_cast(tmp1, out, format, NULL)) {
  190. clear_image(&tmp1);
  191. return 1;
  192. }
  193. clear_image(&tmp1);
  194. return 0;
  195. }
  196. int
  197. vips_need_icc_import(VipsImage *in) {
  198. return (vips_image_get_typeof(in, VIPS_META_ICC_NAME) ||
  199. in->Type == VIPS_INTERPRETATION_CMYK) &&
  200. in->Coding == VIPS_CODING_NONE &&
  201. (in->BandFmt == VIPS_FORMAT_UCHAR ||
  202. in->BandFmt == VIPS_FORMAT_USHORT);
  203. }
  204. int
  205. vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
  206. return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  207. }
  208. int
  209. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  210. return vips_colourspace(in, out, cs, NULL);
  211. }
  212. int
  213. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  214. return vips_rot(in, out, angle, NULL);
  215. }
  216. int
  217. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  218. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  219. }
  220. int
  221. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  222. #if VIPS_SUPPORT_SMARTCROP
  223. return vips_smartcrop(in, out, width, height, NULL);
  224. #else
  225. vips_error("vips_smartcrop_go", "Smart crop is not supported");
  226. return 1;
  227. #endif
  228. }
  229. int
  230. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  231. return vips_gaussblur(in, out, sigma, NULL);
  232. }
  233. int
  234. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  235. return vips_sharpen(in, out, "sigma", sigma, NULL);
  236. }
  237. int
  238. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  239. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  240. int res = vips_flatten(in, out, "background", bg, NULL);
  241. vips_area_unref((VipsArea *)bg);
  242. return res;
  243. }
  244. int
  245. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  246. return vips_extract_area(in, out, left, top, width, height, NULL);
  247. }
  248. int
  249. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  250. VipsImage *tmp;
  251. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  252. return 1;
  253. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  254. clear_image(&tmp);
  255. return 1;
  256. }
  257. clear_image(&tmp);
  258. return 0;
  259. }
  260. int
  261. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  262. return vips_embed(in, out, x, y, width, height, NULL);
  263. }
  264. int
  265. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  266. if (vips_image_hasalpha_go(in)) {
  267. return vips_copy(in, out, NULL);
  268. }
  269. return vips_bandjoin_const1(in, out, 255, NULL);
  270. }
  271. int
  272. vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity){
  273. if (vips_image_hasalpha_go(in)) {
  274. if (opacity < 1) {
  275. VipsImage *img, *img_alpha, *tmp;
  276. if (vips_extract_band(in, &img, 0, "n", in->Bands - 1, NULL))
  277. return 1;
  278. if (vips_extract_band(in, &img_alpha, in->Bands - 1, "n", 1, NULL)) {
  279. clear_image(&img);
  280. return 1;
  281. }
  282. if (vips_linear1(img_alpha, &tmp, opacity, 0, NULL)) {
  283. clear_image(&img);
  284. clear_image(&img_alpha);
  285. return 1;
  286. }
  287. swap_and_clear(&img_alpha, tmp);
  288. if (vips_bandjoin2(img, img_alpha, out, NULL)) {
  289. clear_image(&img);
  290. clear_image(&img_alpha);
  291. return 1;
  292. }
  293. clear_image(&img);
  294. clear_image(&img_alpha);
  295. } else {
  296. if (vips_copy(in, out, NULL)) {
  297. return 1;
  298. }
  299. }
  300. } else {
  301. if (vips_bandjoin_const1(in, out, opacity * 255, NULL)) {
  302. return 1;
  303. }
  304. }
  305. return 0;
  306. }
  307. int
  308. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  309. VipsImage *wm, *wm_alpha, *tmp;
  310. if (vips_extract_band(watermark, &wm, 0, "n", watermark->Bands - 1, NULL))
  311. return 1;
  312. if (vips_extract_band(watermark, &wm_alpha, watermark->Bands - 1, "n", 1, NULL)) {
  313. clear_image(&wm);
  314. return 1;
  315. }
  316. VipsInterpretation img_interpolation = vips_image_guess_interpretation(in);
  317. if (img_interpolation != vips_image_guess_interpretation(wm)) {
  318. if (vips_colourspace(wm, &tmp, img_interpolation, NULL)) {
  319. clear_image(&wm);
  320. clear_image(&wm_alpha);
  321. return 1;
  322. }
  323. swap_and_clear(&wm, tmp);
  324. }
  325. if (opacity < 1) {
  326. if (vips_linear1(wm_alpha, &tmp, opacity, 0, NULL)) {
  327. clear_image(&wm);
  328. clear_image(&wm_alpha);
  329. return 1;
  330. }
  331. swap_and_clear(&wm_alpha, tmp);
  332. }
  333. VipsBandFormat img_format;
  334. VipsImage *img, *img_alpha;
  335. img_format = vips_image_get_format(in);
  336. gboolean has_alpha = vips_image_hasalpha_go(in);
  337. if (has_alpha) {
  338. if (vips_extract_band(in, &img, 0, "n", in->Bands - 1, NULL)) {
  339. clear_image(&wm);
  340. clear_image(&wm_alpha);
  341. return 1;
  342. }
  343. if (vips_extract_band(in, &img_alpha, in->Bands - 1, "n", 1, NULL)) {
  344. clear_image(&wm);
  345. clear_image(&wm_alpha);
  346. clear_image(&img);
  347. return 1;
  348. }
  349. } else {
  350. if (vips_copy(in, &img, NULL)) {
  351. clear_image(&wm);
  352. clear_image(&wm_alpha);
  353. return 1;
  354. }
  355. }
  356. if (vips_ifthenelse(wm_alpha, wm, img, &tmp, "blend", TRUE, NULL)) {
  357. clear_image(&wm);
  358. clear_image(&wm_alpha);
  359. clear_image(&img);
  360. clear_image(&img_alpha);
  361. return 1;
  362. }
  363. swap_and_clear(&img, tmp);
  364. clear_image(&wm);
  365. clear_image(&wm_alpha);
  366. if (has_alpha) {
  367. if (vips_bandjoin2(img, img_alpha, &tmp, NULL)) {
  368. clear_image(&img);
  369. clear_image(&img_alpha);
  370. return 1;
  371. }
  372. swap_and_clear(&img, tmp);
  373. clear_image(&img_alpha);
  374. }
  375. if (img_format != vips_image_get_format(img)) {
  376. if (vips_cast(img, &tmp, img_format, NULL)) {
  377. clear_image(&img);
  378. return 1;
  379. }
  380. swap_and_clear(&img, tmp);
  381. }
  382. *out = img;
  383. return 0;
  384. }
  385. int
  386. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  387. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  388. }
  389. int
  390. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  391. return vips_jpegsave_buffer(in, buf, len, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  392. }
  393. int
  394. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int embed_profile) {
  395. if (embed_profile)
  396. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  397. return vips_pngsave_buffer(in, buf, len, "profile", "none", "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  398. }
  399. int
  400. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  401. return vips_webpsave_buffer(in, buf, len, "Q", quality, NULL);
  402. }
  403. int
  404. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  405. #if VIPS_SUPPORT_MAGICK
  406. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  407. #else
  408. vips_error("vips_gifsave_go", "Saving GIF is not supported");
  409. return 1;
  410. #endif
  411. }
  412. int
  413. vips_icosave_go(VipsImage *in, void **buf, size_t *len) {
  414. #if VIPS_SUPPORT_MAGICK
  415. return vips_magicksave_buffer(in, buf, len, "format", "ico", NULL);
  416. #else
  417. vips_error("vips_icosave_go", "Saving ICO is not supported");
  418. return 1;
  419. #endif
  420. }
  421. void
  422. vips_cleanup() {
  423. vips_error_clear();
  424. vips_thread_shutdown();
  425. }