1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-11-29 23:07:40 +02:00
Files
imgproxy/vips.h

232 lines
5.4 KiB
C
Raw Normal View History

2017-09-27 14:42:49 +06:00
#include <stdlib.h>
#include <vips/vips.h>
#include <vips/vips7compat.h>
#define VIPS_SUPPORT_SMARTCROP \
(VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 5))
2017-09-28 02:12:38 +06:00
#define VIPS_SUPPORT_GIF \
VIPS_MAJOR_VERSION > 8 || (VIPS_MAJOR_VERSION == 8 && VIPS_MINOR_VERSION >= 3)
#define EXIF_ORIENTATION "exif-ifd0-Orientation"
2017-09-27 14:42:49 +06:00
enum types {
2017-10-05 05:59:23 +06:00
JPEG = 0,
2017-09-27 14:42:49 +06:00
PNG,
2017-10-05 05:59:23 +06:00
WEBP,
GIF
2017-09-27 14:42:49 +06:00
};
int
2017-10-05 05:59:23 +06:00
vips_initialize() {
2017-09-27 14:42:49 +06:00
return vips_init("imgproxy");
}
2017-09-28 19:43:21 +06:00
void
clear_image(VipsImage **in) {
2017-10-05 22:50:41 +06:00
if (G_IS_OBJECT(*in)) g_clear_object(in);
2017-09-28 19:43:21 +06:00
}
2017-10-05 05:56:42 +06:00
void
g_free_go(void **buf) {
g_free(*buf);
}
2017-09-28 23:18:01 +06:00
VipsAccess
access_mode(int random) {
if (random > 0) return VIPS_ACCESS_RANDOM;
return VIPS_ACCESS_SEQUENTIAL;
}
2017-09-28 01:17:17 +06:00
void
swap_and_clear(VipsImage **in, VipsImage *out) {
2017-09-28 19:43:21 +06:00
clear_image(in);
2017-09-28 01:17:17 +06:00
*in = out;
}
2017-09-27 14:42:49 +06:00
int
vips_type_find_load_go(int imgtype) {
if (imgtype == JPEG) {
return vips_type_find("VipsOperation", "jpegload");
}
if (imgtype == PNG) {
return vips_type_find("VipsOperation", "pngload");
}
if (imgtype == WEBP) {
return vips_type_find("VipsOperation", "webpload");
}
2017-10-05 05:59:23 +06:00
if (imgtype == GIF) {
return vips_type_find("VipsOperation", "gifload");
}
return 0;
2017-09-27 14:42:49 +06:00
}
int
vips_type_find_save_go(int imgtype) {
if (imgtype == JPEG) {
return vips_type_find("VipsOperation", "jpegsave_buffer");
}
if (imgtype == PNG) {
return vips_type_find("VipsOperation", "pngsave_buffer");
}
2017-10-05 05:59:23 +06:00
if (imgtype == WEBP) {
return vips_type_find("VipsOperation", "webpsave_buffer");
}
return 0;
2017-09-27 14:42:49 +06:00
}
int
2017-10-05 05:59:23 +06:00
vips_jpegload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
2017-09-28 23:18:01 +06:00
return vips_jpegload_buffer(buf, len, out, "access", access_mode(random), NULL);
2017-09-28 01:17:17 +06:00
}
2017-09-27 14:42:49 +06:00
int
2017-10-05 05:59:23 +06:00
vips_pngload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
2017-09-28 23:18:01 +06:00
return vips_pngload_buffer(buf, len, out, "access", access_mode(random), NULL);
2017-09-28 01:17:17 +06:00
}
2017-09-27 14:42:49 +06:00
int
2017-10-05 05:59:23 +06:00
vips_gifload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
2017-09-28 02:12:38 +06:00
#if VIPS_SUPPORT_GIF
2017-09-28 23:18:01 +06:00
return vips_gifload_buffer(buf, len, out, "access", access_mode(random), NULL);
2017-09-28 02:12:38 +06:00
#else
return 0;
#endif
2017-09-28 01:17:17 +06:00
}
2017-09-27 14:42:49 +06:00
int
2017-10-05 05:59:23 +06:00
vips_webpload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
2017-09-28 23:18:01 +06:00
return vips_webpload_buffer(buf, len, out, "access", access_mode(random), NULL);
2017-09-28 01:17:17 +06:00
}
2017-09-27 14:42:49 +06:00
int
vips_get_exif_orientation(VipsImage *image) {
const char *orientation;
if (
vips_image_get_typeof(image, EXIF_ORIENTATION) != 0 &&
!vips_image_get_string(image, EXIF_ORIENTATION, &orientation)
) return atoi(&orientation[0]);
return 1;
}
int
vips_exif_rotate(VipsImage **img, int orientation) {
int err;
int angle = VIPS_ANGLE_D0;
gboolean flip = FALSE;
VipsImage *tmp;
if (orientation == 3 || orientation == 4) angle = VIPS_ANGLE_D180;
if (orientation == 5 || orientation == 6) angle = VIPS_ANGLE_D90;
if (orientation == 7 || orientation == 8) angle = VIPS_ANGLE_D270;
if (orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7) {
flip = TRUE;
}
err = vips_rot(*img, &tmp, angle, NULL);
swap_and_clear(img, tmp);
if (err > 0) { return err; }
if (flip) {
err = vips_flip(*img, &tmp, VIPS_DIRECTION_HORIZONTAL, NULL);
swap_and_clear(img, tmp);
if (err > 0) { return err; }
}
return 0;
}
2017-09-27 14:42:49 +06:00
int
2017-10-05 05:59:23 +06:00
vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
2017-09-27 14:42:49 +06:00
return vips_resize(in, out, scale, NULL);
2017-09-28 01:17:17 +06:00
}
2017-09-27 14:42:49 +06:00
int
vips_support_smartcrop() {
#if VIPS_SUPPORT_SMARTCROP
2017-10-05 05:59:23 +06:00
return 1;
2017-09-27 14:42:49 +06:00
#else
2017-10-05 05:59:23 +06:00
return 0;
2017-09-27 14:42:49 +06:00
#endif
}
int
vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
#if VIPS_SUPPORT_SMARTCROP
2017-10-05 05:59:23 +06:00
return vips_smartcrop(in, out, width, height, NULL);
2017-09-27 14:42:49 +06:00
#else
2017-10-05 05:59:23 +06:00
return 0;
2017-09-27 14:42:49 +06:00
#endif
}
int
2017-10-05 05:59:23 +06:00
vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation space) {
2017-09-27 14:42:49 +06:00
return vips_colourspace(in, out, space, NULL);
2017-09-28 01:17:17 +06:00
}
2017-09-27 14:42:49 +06:00
int
2017-10-05 05:59:23 +06:00
vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
2017-09-27 14:42:49 +06:00
return vips_extract_area(in, out, left, top, width, height, NULL);
}
2017-09-28 01:17:17 +06:00
int
2017-10-05 05:59:23 +06:00
vips_process_image(VipsImage **img, int resize, double scale, int crop, int smart, int left, int top, int width, int height) {
2017-09-28 01:17:17 +06:00
VipsImage *tmp;
int err;
int exif_orientation = vips_get_exif_orientation(*img);
if (exif_orientation > 1) {
vips_exif_rotate(img, exif_orientation);
if (err > 0) { return 1; }
}
2017-09-28 01:17:17 +06:00
if (resize > 0) {
err = vips_resize_go(*img, &tmp, scale);
swap_and_clear(img, tmp);
if (err > 0) { return 1; }
2017-09-28 01:17:17 +06:00
}
if (crop > 0) {
if (smart > 0) {
err = vips_smartcrop_go(*img, &tmp, width, height);
swap_and_clear(img, tmp);
if (err > 0) { return 1; }
2017-09-28 01:17:17 +06:00
} else {
vips_extract_area_go(*img, &tmp, left, top, width, height);
swap_and_clear(img, tmp);
if (err > 0) { return 1; }
2017-09-28 01:17:17 +06:00
}
}
2017-09-28 13:59:59 +06:00
if (vips_image_guess_interpretation(*img) != VIPS_INTERPRETATION_sRGB) {
err = vips_colourspace_go(*img, &tmp, VIPS_INTERPRETATION_sRGB);
swap_and_clear(img, tmp);
}
2017-09-28 01:17:17 +06:00
return err;
}
2017-09-27 14:42:49 +06:00
int
2017-10-05 05:59:23 +06:00
vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, int interlace) {
2017-09-27 14:42:49 +06:00
return vips_jpegsave_buffer(in, buf, len, "strip", strip, "Q", quality, "optimize_coding", TRUE, "interlace", interlace, NULL);
}
int
2017-10-05 05:59:23 +06:00
vips_pngsave_go(VipsImage *in, void **buf, size_t *len) {
2017-09-27 23:22:36 +06:00
return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
2017-09-27 14:42:49 +06:00
}
int
2017-10-05 05:59:23 +06:00
vips_webpsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality) {
return vips_webpsave_buffer(in, buf, len, "strip", strip, "Q", quality, NULL);
2017-09-27 14:42:49 +06:00
}
2017-09-28 01:17:17 +06:00
void
2017-10-05 05:59:23 +06:00
vips_cleanup() {
2017-09-28 01:17:17 +06:00
vips_thread_shutdown();
vips_error_clear();
}