vips.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <stdlib.h>
  2. #include <vips/vips.h>
  3. #include <vips/vips7compat.h>
  4. #define VIPS_SUPPORT_SMARTCROP \
  5. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  6. #define VIPS_SUPPORT_HASALPHA \
  7. (VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
  8. #define VIPS_SUPPORT_GIF \
  9. VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3)
  10. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  11. enum types {
  12. UNKNOWN = 0,
  13. JPEG,
  14. PNG,
  15. WEBP,
  16. GIF
  17. };
  18. int
  19. vips_initialize() {
  20. return vips_init("imgproxy");
  21. }
  22. void
  23. clear_image(VipsImage **in) {
  24. if (G_IS_OBJECT(*in)) g_clear_object(in);
  25. }
  26. void
  27. g_free_go(void **buf) {
  28. g_free(*buf);
  29. }
  30. void
  31. swap_and_clear(VipsImage **in, VipsImage *out) {
  32. clear_image(in);
  33. *in = out;
  34. }
  35. int
  36. vips_type_find_load_go(int imgtype) {
  37. if (imgtype == JPEG) {
  38. return vips_type_find("VipsOperation", "jpegload");
  39. }
  40. if (imgtype == PNG) {
  41. return vips_type_find("VipsOperation", "pngload");
  42. }
  43. if (imgtype == WEBP) {
  44. return vips_type_find("VipsOperation", "webpload");
  45. }
  46. if (imgtype == GIF) {
  47. return vips_type_find("VipsOperation", "gifload");
  48. }
  49. return 0;
  50. }
  51. int
  52. vips_type_find_save_go(int imgtype) {
  53. if (imgtype == JPEG) {
  54. return vips_type_find("VipsOperation", "jpegsave_buffer");
  55. }
  56. if (imgtype == PNG) {
  57. return vips_type_find("VipsOperation", "pngsave_buffer");
  58. }
  59. if (imgtype == WEBP) {
  60. return vips_type_find("VipsOperation", "webpsave_buffer");
  61. }
  62. return 0;
  63. }
  64. int
  65. vips_load_buffer(void *buf, size_t len, int imgtype, int shrink, VipsImage **out) {
  66. switch (imgtype) {
  67. case JPEG:
  68. if (shrink > 1) {
  69. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  70. }
  71. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  72. case PNG:
  73. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  74. case WEBP:
  75. if (shrink > 1) {
  76. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, "shrink", shrink, NULL);
  77. }
  78. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  79. #if VIPS_SUPPORT_GIF
  80. case GIF:
  81. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  82. #endif
  83. }
  84. return 1;
  85. }
  86. int
  87. vips_get_exif_orientation(VipsImage *image) {
  88. const char *orientation;
  89. if (
  90. vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
  91. !vips_image_get_string(image, EXIF_ORIENTATION, &orientation)
  92. ) return atoi(&orientation[0]);
  93. return 1;
  94. }
  95. int
  96. vips_support_smartcrop() {
  97. #if VIPS_SUPPORT_SMARTCROP
  98. return 1;
  99. #else
  100. return 0;
  101. #endif
  102. }
  103. VipsBandFormat
  104. vips_band_format(VipsImage *in) {
  105. return in->BandFmt;
  106. }
  107. gboolean
  108. vips_image_hasalpha_go(VipsImage * in) {
  109. #if VIPS_SUPPORT_HASALPHA
  110. return vips_image_hasalpha(in);
  111. #else
  112. return( in->Bands == 2 ||
  113. (in->Bands == 4 && in->Type != VIPS_INTERPRETATION_CMYK) ||
  114. in->Bands > 4 );
  115. #endif
  116. }
  117. int
  118. vips_premultiply_go(VipsImage *in, VipsImage **out) {
  119. return vips_premultiply(in, out, NULL);
  120. }
  121. int
  122. vips_unpremultiply_go(VipsImage *in, VipsImage **out) {
  123. return vips_unpremultiply(in, out, NULL);
  124. }
  125. int
  126. vips_cast_go(VipsImage *in, VipsImage **out, VipsBandFormat format) {
  127. return vips_cast(in, out, format, NULL);
  128. }
  129. int
  130. vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
  131. return vips_resize(in, out, scale, NULL);
  132. }
  133. int
  134. vips_need_icc_import(VipsImage *in) {
  135. return in->Type == VIPS_INTERPRETATION_CMYK;
  136. }
  137. int
  138. vips_icc_import_go(VipsImage *in, VipsImage **out, char *profile) {
  139. return vips_icc_import(in, out, "input_profile", profile, "embedded", TRUE, "pcs", VIPS_PCS_XYZ, NULL);
  140. }
  141. int
  142. vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation cs) {
  143. return vips_colourspace(in, out, cs, NULL);
  144. }
  145. int
  146. vips_rot_go(VipsImage *in, VipsImage **out, VipsAngle angle) {
  147. return vips_rot(in, out, angle, NULL);
  148. }
  149. int
  150. vips_flip_horizontal_go(VipsImage *in, VipsImage **out) {
  151. return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL);
  152. }
  153. int
  154. vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
  155. #if VIPS_SUPPORT_SMARTCROP
  156. return vips_smartcrop(in, out, width, height, NULL);
  157. #else
  158. return 1;
  159. #endif
  160. }
  161. int
  162. vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
  163. return vips_extract_area(in, out, left, top, width, height, NULL);
  164. }
  165. int
  166. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  167. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  168. }
  169. int
  170. vips_pngsave_go(VipsImage *in, void **buf, size_t *len) {
  171. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
  172. }
  173. int
  174. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  175. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  176. }
  177. void
  178. vips_cleanup() {
  179. vips_thread_shutdown();
  180. vips_error_clear();
  181. }