mirror of
https://github.com/imgproxy/imgproxy.git
synced 2025-01-03 10:43:58 +02:00
Rotate after resize; Use most vips functions directly
This commit is contained in:
parent
78b9fd50a2
commit
50e68556d0
57
process.go
57
process.go
@ -19,11 +19,11 @@ import (
|
|||||||
type imageType int
|
type imageType int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UNKNOWN imageType = iota
|
UNKNOWN = C.UNKNOWN
|
||||||
JPEG
|
JPEG = C.JPEG
|
||||||
PNG
|
PNG = C.PNG
|
||||||
WEBP
|
WEBP = C.WEBP
|
||||||
GIF
|
GIF = C.GIF
|
||||||
)
|
)
|
||||||
|
|
||||||
var imageTypes = map[string]imageType{
|
var imageTypes = map[string]imageType{
|
||||||
@ -138,6 +138,33 @@ func round(f float64) int {
|
|||||||
return int(f + .5)
|
return int(f + .5)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractMeta(img *C.VipsImage) (int, int, int, int) {
|
||||||
|
width := int(img.Xsize)
|
||||||
|
height := int(img.Ysize)
|
||||||
|
|
||||||
|
angle := C.VIPS_ANGLE_D0
|
||||||
|
flip := C.FALSE
|
||||||
|
|
||||||
|
orientation := C.vips_get_exif_orientation(img)
|
||||||
|
if orientation >= 5 && orientation <= 8 {
|
||||||
|
width, height = height, width
|
||||||
|
}
|
||||||
|
if orientation == 3 || orientation == 4 {
|
||||||
|
angle = C.VIPS_ANGLE_D180
|
||||||
|
}
|
||||||
|
if orientation == 5 || orientation == 6 {
|
||||||
|
angle = C.VIPS_ANGLE_D90
|
||||||
|
}
|
||||||
|
if orientation == 7 || orientation == 8 {
|
||||||
|
angle = C.VIPS_ANGLE_D270
|
||||||
|
}
|
||||||
|
if orientation == 2 || orientation == 4 || orientation == 5 || orientation == 7 {
|
||||||
|
flip = C.TRUE
|
||||||
|
}
|
||||||
|
|
||||||
|
return width, height, angle, flip
|
||||||
|
}
|
||||||
|
|
||||||
func calcScale(width, height int, po processingOptions) float64 {
|
func calcScale(width, height int, po processingOptions) float64 {
|
||||||
if (po.width == width && po.height == height) || (po.resize != FILL && po.resize != FIT) {
|
if (po.width == width && po.height == height) || (po.resize != FILL && po.resize != FIT) {
|
||||||
return 1
|
return 1
|
||||||
@ -193,24 +220,14 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
|
|||||||
defer C.vips_cleanup()
|
defer C.vips_cleanup()
|
||||||
|
|
||||||
// Load the image
|
// Load the image
|
||||||
switch imgtype {
|
err = C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), &img)
|
||||||
case JPEG:
|
|
||||||
err = C.vips_jpegload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
|
||||||
case PNG:
|
|
||||||
err = C.vips_pngload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
|
||||||
case GIF:
|
|
||||||
err = C.vips_gifload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
|
||||||
case WEBP:
|
|
||||||
err = C.vips_webpload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
|
||||||
}
|
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return nil, vipsError()
|
return nil, vipsError()
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Check()
|
t.Check()
|
||||||
|
|
||||||
imgWidth := int(img.Xsize)
|
imgWidth, imgHeight, angle, flip := extractMeta(img)
|
||||||
imgHeight := int(img.Ysize)
|
|
||||||
|
|
||||||
// Ensure we won't crop out of bounds
|
// Ensure we won't crop out of bounds
|
||||||
if !po.enlarge || po.resize == CROP {
|
if !po.enlarge || po.resize == CROP {
|
||||||
@ -224,10 +241,10 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
|
|||||||
}
|
}
|
||||||
|
|
||||||
if po.width != imgWidth || po.height != imgHeight {
|
if po.width != imgWidth || po.height != imgHeight {
|
||||||
|
pResize, pCrop, pSmart := C.FALSE, C.FALSE, C.FALSE
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pResize, pCrop int
|
|
||||||
pScale float64
|
pScale float64
|
||||||
pSmart int
|
|
||||||
pLeft, pTop, pWidth, pHeight int
|
pLeft, pTop, pWidth, pHeight int
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -249,7 +266,7 @@ func processImage(data []byte, imgtype imageType, po processingOptions, t *timer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = C.vips_process_image(&img, C.int(pResize), C.double(pScale), C.int(pCrop), C.int(pSmart), C.int(pLeft), C.int(pTop), C.int(pWidth), C.int(pHeight))
|
err = C.vips_process_image(&img, C.gboolean(pResize), C.double(pScale), C.gboolean(pCrop), C.gboolean(pSmart), C.int(pLeft), C.int(pTop), C.int(pWidth), C.int(pHeight), C.VipsAngle(angle), C.gboolean(flip))
|
||||||
if err != 0 {
|
if err != 0 {
|
||||||
return nil, vipsError()
|
return nil, vipsError()
|
||||||
}
|
}
|
||||||
|
138
vips.h
138
vips.h
@ -11,7 +11,8 @@
|
|||||||
#define EXIF_ORIENTATION "exif-ifd0-Orientation"
|
#define EXIF_ORIENTATION "exif-ifd0-Orientation"
|
||||||
|
|
||||||
enum types {
|
enum types {
|
||||||
JPEG = 0,
|
UNKNOWN = 0,
|
||||||
|
JPEG,
|
||||||
PNG,
|
PNG,
|
||||||
WEBP,
|
WEBP,
|
||||||
GIF
|
GIF
|
||||||
@ -32,12 +33,6 @@ g_free_go(void **buf) {
|
|||||||
g_free(*buf);
|
g_free(*buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
VipsAccess
|
|
||||||
access_mode(int random) {
|
|
||||||
if (random > 0) return VIPS_ACCESS_RANDOM;
|
|
||||||
return VIPS_ACCESS_SEQUENTIAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
swap_and_clear(VipsImage **in, VipsImage *out) {
|
swap_and_clear(VipsImage **in, VipsImage *out) {
|
||||||
clear_image(in);
|
clear_image(in);
|
||||||
@ -76,27 +71,20 @@ vips_type_find_save_go(int imgtype) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_jpegload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
|
vips_load_buffer(void *buf, size_t len, int imgtype, VipsImage **out) {
|
||||||
return vips_jpegload_buffer(buf, len, out, "access", access_mode(random), NULL);
|
switch (imgtype) {
|
||||||
}
|
case JPEG:
|
||||||
|
return vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
int
|
case PNG:
|
||||||
vips_pngload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
|
return vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
return vips_pngload_buffer(buf, len, out, "access", access_mode(random), NULL);
|
case WEBP:
|
||||||
}
|
return vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
|
#if VIPS_SUPPORT_GIF
|
||||||
int
|
case GIF:
|
||||||
vips_gifload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
|
return vips_gifload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
|
||||||
#if VIPS_SUPPORT_GIF
|
#endif
|
||||||
return vips_gifload_buffer(buf, len, out, "access", access_mode(random), NULL);
|
}
|
||||||
#else
|
return 1;
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
vips_webpload_buffer_go(void *buf, size_t len, VipsImage **out, int random) {
|
|
||||||
return vips_webpload_buffer(buf, len, out, "access", access_mode(random), NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -111,39 +99,6 @@ vips_get_exif_orientation(VipsImage *image) {
|
|||||||
return 1;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
vips_resize_go(VipsImage *in, VipsImage **out, double scale) {
|
|
||||||
return vips_resize(in, out, scale, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_support_smartcrop() {
|
vips_support_smartcrop() {
|
||||||
#if VIPS_SUPPORT_SMARTCROP
|
#if VIPS_SUPPORT_SMARTCROP
|
||||||
@ -154,55 +109,50 @@ vips_support_smartcrop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
vips_smartcrop_go(VipsImage *in, VipsImage **out, int width, int height) {
|
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) {
|
||||||
#if VIPS_SUPPORT_SMARTCROP
|
|
||||||
return vips_smartcrop(in, out, width, height, NULL);
|
|
||||||
#else
|
|
||||||
return 0;
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
vips_colourspace_go(VipsImage *in, VipsImage **out, VipsInterpretation space) {
|
|
||||||
return vips_colourspace(in, out, space, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
vips_extract_area_go(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
|
|
||||||
return vips_extract_area(in, out, left, top, width, height, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
|
||||||
vips_process_image(VipsImage **img, int resize, double scale, int crop, int smart, int left, int top, int width, int height) {
|
|
||||||
VipsImage *tmp;
|
VipsImage *tmp;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
int exif_orientation = vips_get_exif_orientation(*img);
|
if (resize && scale != 1.0) {
|
||||||
if (exif_orientation > 1) {
|
err = vips_resize(*img, &tmp, scale, NULL);
|
||||||
vips_exif_rotate(img, exif_orientation);
|
|
||||||
if (err > 0) { return 1; }
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resize > 0) {
|
|
||||||
err = vips_resize_go(*img, &tmp, scale);
|
|
||||||
swap_and_clear(img, tmp);
|
swap_and_clear(img, tmp);
|
||||||
if (err > 0) { return 1; }
|
if (err > 0) { return 1; }
|
||||||
}
|
}
|
||||||
|
|
||||||
if (crop > 0) {
|
if (angle != VIPS_ANGLE_D0 || flip) {
|
||||||
if (smart > 0) {
|
tmp = vips_image_copy_memory(*img);
|
||||||
err = vips_smartcrop_go(*img, &tmp, width, height);
|
swap_and_clear(img, tmp);
|
||||||
|
if (tmp == NULL) { return 1; }
|
||||||
|
|
||||||
|
if (angle != VIPS_ANGLE_D0) {
|
||||||
|
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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (crop) {
|
||||||
|
if (smart) {
|
||||||
|
#if VIPS_SUPPORT_SMARTCROP
|
||||||
|
err = vips_smartcrop(*img, &tmp, width, height, NULL);
|
||||||
swap_and_clear(img, tmp);
|
swap_and_clear(img, tmp);
|
||||||
if (err > 0) { return 1; }
|
if (err > 0) { return 1; }
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
vips_extract_area_go(*img, &tmp, left, top, width, height);
|
vips_extract_area(*img, &tmp, left, top, width, height, NULL);
|
||||||
swap_and_clear(img, tmp);
|
swap_and_clear(img, tmp);
|
||||||
if (err > 0) { return 1; }
|
if (err > 0) { return 1; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vips_image_guess_interpretation(*img) != VIPS_INTERPRETATION_sRGB) {
|
if (vips_image_guess_interpretation(*img) != VIPS_INTERPRETATION_sRGB) {
|
||||||
err = vips_colourspace_go(*img, &tmp, VIPS_INTERPRETATION_sRGB);
|
err = vips_colourspace(*img, &tmp, VIPS_INTERPRETATION_sRGB, NULL);
|
||||||
swap_and_clear(img, tmp);
|
swap_and_clear(img, tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user