vips.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <stdlib.h>
  2. #include <vips/vips.h>
  3. #include <vips/vips7compat.h>
  4. #include "image_types.h"
  5. #define VIPS_SUPPORT_SMARTCROP \
  6. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  7. #define VIPS_SUPPORT_HASALPHA \
  8. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  9. #define VIPS_SUPPORT_GIF \
  10. VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3)
  11. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  12. int
  13. vips_initialize() {
  14. return vips_init("imgproxy");
  15. }
  16. void
  17. clear_image(VipsImage **in) {
  18. if (G_IS_OBJECT(*in)) g_clear_object(in);
  19. }
  20. void
  21. g_free_go(void **buf) {
  22. g_free(*buf);
  23. }
  24. void
  25. swap_and_clear(VipsImage **in, VipsImage *out) {
  26. clear_image(in);
  27. *in = out;
  28. }
  29. int
  30. vips_type_find_load_go(int imgtype) {
  31. if (imgtype == JPEG) {
  32. return vips_type_find("VipsOperation", "jpegload");
  33. }
  34. if (imgtype == PNG) {
  35. return vips_type_find("VipsOperation", "pngload");
  36. }
  37. if (imgtype == WEBP) {
  38. return vips_type_find("VipsOperation", "webpload");
  39. }
  40. if (imgtype == GIF) {
  41. return vips_type_find("VipsOperation", "gifload");
  42. }
  43. return 0;
  44. }
  45. int
  46. vips_type_find_save_go(int imgtype) {
  47. if (imgtype == JPEG) {
  48. return vips_type_find("VipsOperation", "jpegsave_buffer");
  49. }
  50. if (imgtype == PNG) {
  51. return vips_type_find("VipsOperation", "pngsave_buffer");
  52. }
  53. if (imgtype == WEBP) {
  54. return vips_type_find("VipsOperation", "webpsave_buffer");
  55. }
  56. return 0;
  57. }
  58. int
  59. vips_load_buffer(void *buf, size_t len, int imgtype, int shrink, VipsImage **out) {
  60. switch (imgtype) {
  61. case JPEG:
  62. if (shrink > 1) {
  63. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  64. }
  65. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  66. case PNG:
  67. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  68. case WEBP:
  69. if (shrink > 1) {
  70. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  71. }
  72. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  73. #if VIPS_SUPPORT_GIF
  74. case GIF:
  75. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  76. #endif
  77. }
  78. return 1;
  79. }
  80. int
  81. vips_get_exif_orientation(VipsImage *image) {
  82. const char *orientation;
  83. if (
  84. vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
  85. !vips_image_get_string(image, EXIF_ORIENTATION, &orientation)
  86. ) return atoi(&orientation[0]);
  87. return 1;
  88. }
  89. int
  90. vips_support_smartcrop() {
  91. #if VIPS_SUPPORT_SMARTCROP
  92. return 1;
  93. #else
  94. return 0;
  95. #endif
  96. }
  97. VipsBandFormat
  98. vips_band_format(VipsImage *in) {
  99. return in->BandFmt;
  100. }
  101. gboolean
  102. vips_image_hasalpha_go(VipsImage * in) {
  103. #if VIPS_SUPPORT_HASALPHA
  104. return vips_image_hasalpha(in);
  105. #else
  106. return( in->Bands == 2 ||
  107. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  108. in->Bands > 4 );
  109. #endif
  110. }
  111. int
  112. vips_premultiply_go(VipsImage *in, VipsImage **out) {
  113. return vips_premultiply(in, out, NULL);
  114. }
  115. int
  116. vips_unpremultiply_go(VipsImage *in, VipsImage **out) {
  117. return vips_unpremultiply(in, out, NULL);
  118. }
  119. int
  120. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  121. return vips_cast(in, out, format, NULL);
  122. }
  123. int
  124. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  125. return vips_resize(in, out, scale, NULL);
  126. }
  127. int
  128. vips_need_icc_import(VipsImage *in) {
  129. return in->Type == VIPS_INTERPRETATION_CMYK;
  130. }
  131. int
  132. vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
  133. return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  134. }
  135. int
  136. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  137. return vips_colourspace(in, out, cs, NULL);
  138. }
  139. int
  140. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  141. return vips_rot(in, out, angle, NULL);
  142. }
  143. int
  144. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  145. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  146. }
  147. int
  148. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  149. #if VIPS_SUPPORT_SMARTCROP
  150. return vips_smartcrop(in, out, width, height, NULL);
  151. #else
  152. return 1;
  153. #endif
  154. }
  155. int
  156. vips_gaussblur_go(VipsImage *in, VipsImage **out, double sigma) {
  157. return vips_gaussblur(in, out, sigma, NULL);
  158. }
  159. int
  160. vips_sharpen_go(VipsImage *in, VipsImage **out, double sigma) {
  161. return vips_sharpen(in, out, "sigma", sigma, NULL);
  162. }
  163. int
  164. vips_flatten_go(VipsImage *in, VipsImage **out, double r, double g, double b) {
  165. VipsArrayDouble *bg = vips_array_double_newv(3, r, g, b);
  166. int res = vips_flatten(in, out, "background", bg, NULL);
  167. vips_area_unref((VipsArea *)bg);
  168. return res;
  169. }
  170. int
  171. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  172. return vips_extract_area(in, out, left, top, width, height, NULL);
  173. }
  174. int
  175. vips_replicate_go(VipsImage *in, VipsImage **out, int across, int down) {
  176. return vips_replicate(in, out, across, down, NULL);
  177. }
  178. int
  179. vips_embed_go(VipsImage *in, VipsImage **out, int x, int y, int width, int height) {
  180. return vips_embed(in, out, x, y, width, height, NULL);
  181. }
  182. int
  183. vips_extract_band_go(VipsImage *in, VipsImage **out, int band, int band_num) {
  184. return vips_extract_band(in, out, band, "n", band_num, NULL);
  185. }
  186. int
  187. vips_bandjoin_go (VipsImage *in1, VipsImage *in2, VipsImage **out) {
  188. return vips_bandjoin2(in1, in2, out, NULL);
  189. }
  190. int
  191. vips_bandjoin_const_go (VipsImage *in, VipsImage **out, double c) {
  192. return vips_bandjoin_const1(in, out, c, NULL);
  193. }
  194. int
  195. vips_linear_go (VipsImage *in, VipsImage **out, double a, double b) {
  196. return vips_linear1(in, out, a, b, NULL);
  197. }
  198. int
  199. vips_ifthenelse_go(VipsImage *cond, VipsImage *in1, VipsImage *in2, VipsImage **out) {
  200. return vips_ifthenelse(cond, in1, in2, out, "blend", TRUE, NULL);
  201. }
  202. int
  203. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  204. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  205. }
  206. int
  207. vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace) {
  208. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL);
  209. }
  210. int
  211. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  212. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  213. }
  214. void
  215. vips_cleanup() {
  216. vips_thread_shutdown();
  217. vips_error_clear();
  218. }