vips.c 12 KB

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