vips.c 13 KB

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