vips.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_GIF \
  7. VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3)
  8. #define EXIF_ORIENTATION "exif-ifd0-Orientation"
  9. enum types {
  10. UNKNOWN = 0,
  11. JPEG,
  12. PNG,
  13. WEBP,
  14. GIF
  15. };
  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. if (imgtype == JPEG) {
  36. return vips_type_find("VipsOperation", "jpegload");
  37. }
  38. if (imgtype == PNG) {
  39. return vips_type_find("VipsOperation", "pngload");
  40. }
  41. if (imgtype == WEBP) {
  42. return vips_type_find("VipsOperation", "webpload");
  43. }
  44. if (imgtype == GIF) {
  45. return vips_type_find("VipsOperation", "gifload");
  46. }
  47. return 0;
  48. }
  49. int
  50. vips_type_find_save_go(int imgtype) {
  51. if (imgtype == JPEG) {
  52. return vips_type_find("VipsOperation", "jpegsave_buffer");
  53. }
  54. if (imgtype == PNG) {
  55. return vips_type_find("VipsOperation", "pngsave_buffer");
  56. }
  57. if (imgtype == WEBP) {
  58. return vips_type_find("VipsOperation", "webpsave_buffer");
  59. }
  60. return 0;
  61. }
  62. int
  63. vips_load_buffer(void *buf, size_t len, int imgtype, VipsImage **out) {
  64. switch (imgtype) {
  65. case JPEG:
  66. return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  67. case PNG:
  68. return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  69. case WEBP:
  70. return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  71. #if VIPS_SUPPORT_GIF
  72. case GIF:
  73. return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
  74. #endif
  75. }
  76. return 1;
  77. }
  78. int
  79. vips_get_exif_orientation(VipsImage *image) {
  80. const char *orientation;
  81. if (
  82. vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
  83. !vips_image_get_string(image, EXIF_ORIENTATION, &orientation)
  84. ) return atoi(&orientation[0]);
  85. return 1;
  86. }
  87. int
  88. vips_support_smartcrop() {
  89. #if VIPS_SUPPORT_SMARTCROP
  90. return 1;
  91. #else
  92. return 0;
  93. #endif
  94. }
  95. int
  96. vips_process_image(VipsImage **img, gboolean resize, double scale, gboolean crop, gboolean smart, int left, int top, int width, int height, VipsAngle angle, gboolean flip) {
  97. VipsImage *tmp;
  98. if (resize && scale != 1.0) {
  99. if (vips_resize(*img, &tmp, scale, NULL)) return 1;
  100. swap_and_clear(img, tmp);
  101. }
  102. if (vips_image_guess_interpretation(*img) != VIPS_INTERPRETATION_sRGB) {
  103. if (vips_colourspace(*img, &tmp, VIPS_INTERPRETATION_sRGB, NULL)) return 1;
  104. swap_and_clear(img, tmp);
  105. }
  106. if (angle != VIPS_ANGLE_D0 || flip) {
  107. if (!(tmp = vips_image_copy_memory(*img))) return 1;
  108. swap_and_clear(img, tmp);
  109. if (angle != VIPS_ANGLE_D0) {
  110. if (vips_rot(*img, &tmp, angle, NULL)) return 1;
  111. swap_and_clear(img, tmp);
  112. }
  113. if (flip) {
  114. if (vips_flip(*img, &tmp, VIPS_DIRECTION_HORIZONTAL, NULL)) return 1;
  115. swap_and_clear(img, tmp);
  116. }
  117. }
  118. if (crop) {
  119. if (smart) {
  120. #if VIPS_SUPPORT_SMARTCROP
  121. if (vips_smartcrop(*img, &tmp, width, height, NULL)) return 1;
  122. swap_and_clear(img, tmp);
  123. #endif
  124. } else {
  125. if (vips_extract_area(*img, &tmp, left, top, width, height, NULL)) return 1;
  126. swap_and_clear(img, tmp);
  127. }
  128. }
  129. return 0;
  130. }
  131. int
  132. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  133. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  134. }
  135. int
  136. vips_pngsave_go(VipsImage *in, void **buf, size_t *len) {
  137. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
  138. }
  139. int
  140. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  141. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  142. }
  143. void
  144. vips_cleanup() {
  145. vips_thread_shutdown();
  146. vips_error_clear();
  147. }