vips.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 in->Type == VIPS_INTERPRETATION_CMYK &&
  199. in->Coding == VIPS_CODING_NONE &&
  200. (in->BandFmt == VIPS_FORMAT_UCHAR ||
  201. in->BandFmt == VIPS_FORMAT_USHORT);
  202. }
  203. int
  204. vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
  205. return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  206. }
  207. int
  208. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  209. return vips_colourspace(in, out, cs, NULL);
  210. }
  211. int
  212. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  213. return vips_rot(in, out, angle, NULL);
  214. }
  215. int
  216. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  217. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  218. }
  219. int
  220. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  221. #if VIPS_SUPPORT_SMARTCROP
  222. return vips_smartcrop(in, out, width, height, NULL);
  223. #else
  224. vips_error("vips_smartcrop_go", "Smart crop is not supported");
  225. return 1;
  226. #endif
  227. }
  228. int
  229. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  230. return vips_gaussblur(in, out, sigma, NULL);
  231. }
  232. int
  233. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  234. return vips_sharpen(in, out, "sigma", sigma, NULL);
  235. }
  236. int
  237. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  238. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  239. int res = vips_flatten(in, out, "background", bg, NULL);
  240. vips_area_unref((VipsArea *)bg);
  241. return res;
  242. }
  243. int
  244. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  245. return vips_extract_area(in, out, left, top, width, height, NULL);
  246. }
  247. int
  248. vips_replicate_go(VipsImage *in, VipsImage **out, int width, int height) {
  249. VipsImage *tmp;
  250. if (vips_replicate(in, &tmp, 1 + width / in->Xsize, 1 + height / in->Ysize, NULL))
  251. return 1;
  252. if (vips_extract_area(tmp, out, 0, 0, width, height, NULL)) {
  253. clear_image(&tmp);
  254. return 1;
  255. }
  256. clear_image(&tmp);
  257. return 0;
  258. }
  259. int
  260. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  261. return vips_embed(in, out, x, y, width, height, NULL);
  262. }
  263. int
  264. vips_ensure_alpha(VipsImage *in, VipsImage **out) {
  265. if (vips_image_hasalpha_go(in)) {
  266. return vips_copy(in, out, NULL);
  267. }
  268. return vips_bandjoin_const1(in, out, 255, NULL);
  269. }
  270. int
  271. vips_apply_opacity(VipsImage *in, VipsImage **out, double opacity){
  272. if (vips_image_hasalpha_go(in)) {
  273. if (opacity < 1) {
  274. VipsImage *img, *img_alpha, *tmp;
  275. if (vips_extract_band(in, &img, 0, "n", in->Bands - 1, NULL))
  276. return 1;
  277. if (vips_extract_band(in, &img_alpha, in->Bands - 1, "n", 1, NULL)) {
  278. clear_image(&img);
  279. return 1;
  280. }
  281. if (vips_linear1(img_alpha, &tmp, opacity, 0, NULL)) {
  282. clear_image(&img);
  283. clear_image(&img_alpha);
  284. return 1;
  285. }
  286. swap_and_clear(&img_alpha, tmp);
  287. if (vips_bandjoin2(img, img_alpha, out, NULL)) {
  288. clear_image(&img);
  289. clear_image(&img_alpha);
  290. return 1;
  291. }
  292. clear_image(&img);
  293. clear_image(&img_alpha);
  294. } else {
  295. if (vips_copy(in, out, NULL)) {
  296. return 1;
  297. }
  298. }
  299. } else {
  300. if (vips_bandjoin_const1(in, out, opacity * 255, NULL)) {
  301. return 1;
  302. }
  303. }
  304. return 0;
  305. }
  306. int
  307. vips_apply_watermark(VipsImage *in, VipsImage *watermark, VipsImage **out, double opacity) {
  308. VipsImage *wm, *wm_alpha, *tmp;
  309. if (vips_extract_band(watermark, &wm, 0, "n", watermark->Bands - 1, NULL))
  310. return 1;
  311. if (vips_extract_band(watermark, &wm_alpha, watermark->Bands - 1, "n", 1, NULL)) {
  312. clear_image(&wm);
  313. return 1;
  314. }
  315. VipsInterpretation img_interpolation = vips_image_guess_interpretation(in);
  316. if (img_interpolation != vips_image_guess_interpretation(wm)) {
  317. if (vips_colourspace(wm, &tmp, img_interpolation, NULL)) {
  318. clear_image(&wm);
  319. clear_image(&wm_alpha);
  320. return 1;
  321. }
  322. swap_and_clear(&wm, tmp);
  323. }
  324. if (opacity < 1) {
  325. if (vips_linear1(wm_alpha, &tmp, opacity, 0, NULL)) {
  326. clear_image(&wm);
  327. clear_image(&wm_alpha);
  328. return 1;
  329. }
  330. swap_and_clear(&wm_alpha, tmp);
  331. }
  332. VipsBandFormat img_format;
  333. VipsImage *img, *img_alpha;
  334. img_format = vips_image_get_format(in);
  335. gboolean has_alpha = vips_image_hasalpha_go(in);
  336. if (has_alpha) {
  337. if (vips_extract_band(in, &img, 0, "n", in->Bands - 1, NULL)) {
  338. clear_image(&wm);
  339. clear_image(&wm_alpha);
  340. return 1;
  341. }
  342. if (vips_extract_band(in, &img_alpha, in->Bands - 1, "n", 1, NULL)) {
  343. clear_image(&wm);
  344. clear_image(&wm_alpha);
  345. clear_image(&img);
  346. return 1;
  347. }
  348. } else {
  349. if (vips_copy(in, &img, NULL)) {
  350. clear_image(&wm);
  351. clear_image(&wm_alpha);
  352. return 1;
  353. }
  354. }
  355. if (vips_ifthenelse(wm_alpha, wm, img, &tmp, "blend", TRUE, NULL)) {
  356. clear_image(&wm);
  357. clear_image(&wm_alpha);
  358. clear_image(&img);
  359. clear_image(&img_alpha);
  360. return 1;
  361. }
  362. swap_and_clear(&img, tmp);
  363. clear_image(&wm);
  364. clear_image(&wm_alpha);
  365. if (has_alpha) {
  366. if (vips_bandjoin2(img, img_alpha, &tmp, NULL)) {
  367. clear_image(&img);
  368. clear_image(&img_alpha);
  369. return 1;
  370. }
  371. swap_and_clear(&img, tmp);
  372. clear_image(&img_alpha);
  373. }
  374. if (img_format != vips_image_get_format(img)) {
  375. if (vips_cast(img, &tmp, img_format, NULL)) {
  376. clear_image(&img);
  377. return 1;
  378. }
  379. swap_and_clear(&img, tmp);
  380. }
  381. *out = img;
  382. return 0;
  383. }
  384. int
  385. vips_arrayjoin_go(VipsImage **in, VipsImage **out, int n) {
  386. return vips_arrayjoin(in, out, n, "across", 1, NULL);
  387. }
  388. int
  389. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int quality, int interlace) {
  390. return vips_jpegsave_buffer(in, buf, len, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  391. }
  392. int
  393. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace, int embed_profile) {
  394. if (embed_profile)
  395. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  396. return vips_pngsave_buffer(in, buf, len, "profile", "none", "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  397. }
  398. int
  399. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int quality) {
  400. return vips_webpsave_buffer(in, buf, len, "Q", quality, NULL);
  401. }
  402. int
  403. vips_gifsave_go(VipsImage *in, void **buf, size_t *len) {
  404. #if VIPS_SUPPORT_MAGICK
  405. return vips_magicksave_buffer(in, buf, len, "format", "gif", NULL);
  406. #else
  407. vips_error("vips_gifsave_go", "Saving GIF is not supported");
  408. return 1;
  409. #endif
  410. }
  411. int
  412. vips_icosave_go(VipsImage *in, void **buf, size_t *len) {
  413. #if VIPS_SUPPORT_MAGICK
  414. return vips_magicksave_buffer(in, buf, len, "format", "ico", NULL);
  415. #else
  416. vips_error("vips_icosave_go", "Saving ICO is not supported");
  417. return 1;
  418. #endif
  419. }
  420. void
  421. vips_cleanup() {
  422. vips_error_clear();
  423. vips_thread_shutdown();
  424. }