vips.c 13 KB

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