vips.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. int err;
  99. if (resize && scale != 1.0) {
  100. err = vips_resize(*img, &tmp, scale, NULL);
  101. swap_and_clear(img, tmp);
  102. if (err > 0) { return 1; }
  103. }
  104. if (angle != VIPS_ANGLE_D0 || flip) {
  105. tmp = vips_image_copy_memory(*img);
  106. swap_and_clear(img, tmp);
  107. if (tmp == NULL) { return 1; }
  108. if (angle != VIPS_ANGLE_D0) {
  109. err = vips_rot(*img, &tmp, angle, NULL);
  110. swap_and_clear(img, tmp);
  111. if (err > 0) { return err; }
  112. }
  113. if (flip) {
  114. err = vips_flip(*img, &tmp, VIPS_DIRECTION_HORIZONTAL, NULL);
  115. swap_and_clear(img, tmp);
  116. if (err > 0) { return err; }
  117. }
  118. }
  119. if (crop) {
  120. if (smart) {
  121. #if VIPS_SUPPORT_SMARTCROP
  122. err = vips_smartcrop(*img, &tmp, width, height, NULL);
  123. swap_and_clear(img, tmp);
  124. if (err > 0) { return 1; }
  125. #endif
  126. } else {
  127. vips_extract_area(*img, &tmp, left, top, width, height, NULL);
  128. swap_and_clear(img, tmp);
  129. if (err > 0) { return 1; }
  130. }
  131. }
  132. if (vips_image_guess_interpretation(*img) != VIPS_INTERPRETATION_sRGB) {
  133. err = vips_colourspace(*img, &tmp, VIPS_INTERPRETATION_sRGB, NULL);
  134. swap_and_clear(img, tmp);
  135. }
  136. return err;
  137. }
  138. int
  139. vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
  140. return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
  141. }
  142. int
  143. vips_pngsave_go(VipsImage *in, void **buf, size_t *len) {
  144. return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
  145. }
  146. int
  147. vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
  148. return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
  149. }
  150. void
  151. vips_cleanup() {
  152. vips_thread_shutdown();
  153. vips_error_clear();
  154. }