vips.c 13 KB

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