vips.c 13 KB

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