1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2024-11-29 08:22:11 +02:00
imgproxy/process.go

540 lines
11 KiB
Go
Raw Normal View History

2017-06-20 15:58:55 +02:00
package main
2017-09-27 10:42:49 +02:00
/*
#cgo pkg-config: vips
2017-09-27 19:22:51 +02:00
#cgo LDFLAGS: -s -w
2017-09-27 10:42:49 +02:00
#include "vips.h"
*/
import "C"
2017-06-20 15:58:55 +02:00
import (
2017-09-27 10:42:49 +02:00
"errors"
"log"
2017-06-20 15:58:55 +02:00
"math"
2017-09-28 10:00:51 +02:00
"os"
2017-09-27 10:42:49 +02:00
"runtime"
"unsafe"
)
2017-09-27 21:17:17 +02:00
var vipsSupportSmartcrop bool
2017-10-06 22:54:24 +02:00
var vipsTypeSupportLoad = make(map[imageType]bool)
var vipsTypeSupportSave = make(map[imageType]bool)
2017-09-27 21:17:17 +02:00
type cConfig struct {
Quality C.int
JpegProgressive C.int
PngInterlaced C.int
}
var cConf cConfig
2017-09-27 10:42:49 +02:00
func initVips() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if err := C.vips_initialize(); err != 0 {
C.vips_shutdown()
log.Fatalln("unable to start vips!")
}
2018-10-01 17:00:33 +02:00
// Disable libvips cache. Since processing pipeline is fine tuned, we won't get much profit from it.
// Enabled cache can cause SIGSEGV on Musl-based systems like Alpine.
C.vips_cache_set_max_mem(0)
C.vips_cache_set_max(0)
2017-09-27 21:17:17 +02:00
2017-10-06 23:20:42 +02:00
if len(os.Getenv("IMGPROXY_VIPS_LEAK_CHECK")) > 0 {
C.vips_leak_set(C.gboolean(1))
}
if len(os.Getenv("IMGPROXY_VIPS_CACHE_TRACE")) > 0 {
2017-09-28 10:00:51 +02:00
C.vips_cache_set_trace(C.gboolean(1))
}
2017-09-27 21:17:17 +02:00
vipsSupportSmartcrop = C.vips_support_smartcrop() == 1
2017-06-20 15:58:55 +02:00
if int(C.vips_type_find_load_go(imageTypeJPEG)) != 0 {
vipsTypeSupportLoad[imageTypeJPEG] = true
2017-10-06 22:54:24 +02:00
}
if int(C.vips_type_find_load_go(imageTypePNG)) != 0 {
vipsTypeSupportLoad[imageTypePNG] = true
2017-10-06 22:54:24 +02:00
}
if int(C.vips_type_find_load_go(imageTypeWEBP)) != 0 {
vipsTypeSupportLoad[imageTypeWEBP] = true
2017-10-06 22:54:24 +02:00
}
if int(C.vips_type_find_load_go(imageTypeGIF)) != 0 {
vipsTypeSupportLoad[imageTypeGIF] = true
2017-09-28 19:18:01 +02:00
}
if int(C.vips_type_find_save_go(imageTypeJPEG)) != 0 {
vipsTypeSupportSave[imageTypeJPEG] = true
2017-10-06 22:54:24 +02:00
}
if int(C.vips_type_find_save_go(imageTypePNG)) != 0 {
vipsTypeSupportSave[imageTypePNG] = true
2017-10-06 22:54:24 +02:00
}
if int(C.vips_type_find_save_go(imageTypeWEBP)) != 0 {
vipsTypeSupportSave[imageTypeWEBP] = true
2017-09-27 10:42:49 +02:00
}
cConf.Quality = C.int(conf.Quality)
if conf.JpegProgressive {
cConf.JpegProgressive = C.int(1)
}
if conf.PngInterlaced {
cConf.PngInterlaced = C.int(1)
}
2017-06-20 15:58:55 +02:00
}
2017-10-17 14:23:48 +02:00
func shutdownVips() {
C.vips_shutdown()
}
2017-10-06 22:54:24 +02:00
func randomAccessRequired(po processingOptions) int {
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravitySmart {
2017-10-06 22:54:24 +02:00
return 1
2017-09-27 10:42:49 +02:00
}
2017-10-06 22:54:24 +02:00
return 0
2017-06-20 15:58:55 +02:00
}
func round(f float64) int {
return int(f + .5)
}
2017-09-27 10:42:49 +02:00
2017-10-18 00:23:17 +02:00
func extractMeta(img *C.VipsImage) (int, int, int, bool) {
width := int(img.Xsize)
height := int(img.Ysize)
angle := C.VIPS_ANGLE_D0
2017-10-18 00:23:17 +02:00
flip := 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 {
2017-10-18 00:23:17 +02:00
flip = true
}
return width, height, angle, flip
}
func needToScale(width, height int, po processingOptions) bool {
return ((po.Width != 0 && po.Width != width) || (po.Height != 0 && po.Height != height)) &&
(po.Resize == resizeFill || po.Resize == resizeFit)
}
2017-06-20 15:58:55 +02:00
func needToCrop(width, height int, po processingOptions) bool {
return (po.Width != width || po.Height != height) &&
(po.Resize == resizeFill || po.Resize == resizeCrop)
}
func calcScale(width, height int, po processingOptions) float64 {
fsw, fsh, fow, foh := float64(width), float64(height), float64(po.Width), float64(po.Height)
2017-06-20 15:58:55 +02:00
wr := fow / fsw
hr := foh / fsh
if po.Width == 0 {
return hr
}
if po.Height == 0 {
return wr
}
if po.Resize == resizeFit {
2017-09-27 10:42:49 +02:00
return math.Min(wr, hr)
2017-06-20 15:58:55 +02:00
}
2017-09-27 10:42:49 +02:00
return math.Max(wr, hr)
2017-06-20 15:58:55 +02:00
}
2017-10-17 23:12:58 +02:00
func calcShink(scale float64, imgtype imageType) int {
shrink := int(1.0 / scale)
if imgtype != imageTypeJPEG {
2017-10-17 23:12:58 +02:00
return shrink
}
2018-09-07 18:05:28 +02:00
if imgtype != imageTypeWEBP {
switch {
case shrink >= 16:
return 8
case shrink >= 8:
return 4
case shrink >= 4:
return 2
}
2017-10-17 23:12:58 +02:00
}
return 1
}
2017-09-27 10:42:49 +02:00
func calcCrop(width, height int, po processingOptions) (left, top int) {
left = (width - po.Width + 1) / 2
top = (height - po.Height + 1) / 2
2017-06-20 15:58:55 +02:00
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravityNorth {
2017-09-27 10:42:49 +02:00
top = 0
}
2017-06-20 15:58:55 +02:00
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravityEast {
left = width - po.Width
2017-06-20 15:58:55 +02:00
}
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravitySouth {
top = height - po.Height
2017-06-20 15:58:55 +02:00
}
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravityWest {
2017-09-27 10:42:49 +02:00
left = 0
}
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravityFocusPoint {
pointX := int(math.Round(float64(width) * po.Gravity.X))
pointY := int(math.Round(float64(height) * po.Gravity.Y))
left = maxInt(0, minInt(pointX-po.Width/2, width-po.Width))
top = maxInt(0, minInt(pointY-po.Height/2, height-po.Height))
}
2017-09-27 10:42:49 +02:00
return
}
2017-10-04 21:44:58 +02:00
func processImage(data []byte, imgtype imageType, po processingOptions, t *timer) ([]byte, error) {
2017-10-18 00:23:17 +02:00
defer C.vips_cleanup()
2018-09-10 08:17:00 +02:00
defer runtime.KeepAlive(data)
2017-09-27 10:42:49 +02:00
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravitySmart && !vipsSupportSmartcrop {
2017-09-28 19:18:01 +02:00
return nil, errors.New("Smart crop is not supported by used version of libvips")
}
2017-10-18 00:23:17 +02:00
img, err := vipsLoadImage(data, imgtype, 1)
if err != nil {
return nil, err
2017-09-27 10:42:49 +02:00
}
2017-10-18 00:23:17 +02:00
defer C.clear_image(&img)
2017-09-27 10:42:49 +02:00
2017-10-04 21:44:58 +02:00
t.Check()
imgWidth, imgHeight, angle, flip := extractMeta(img)
2017-09-27 10:42:49 +02:00
// Ensure we won't crop out of bounds
if !po.Enlarge || po.Resize == resizeCrop {
if imgWidth < po.Width {
po.Width = imgWidth
2017-09-27 10:42:49 +02:00
}
2017-06-20 15:58:55 +02:00
if imgHeight < po.Height {
po.Height = imgHeight
2017-09-27 10:42:49 +02:00
}
2017-06-20 15:58:55 +02:00
}
if needToScale(imgWidth, imgHeight, po) {
scale := calcScale(imgWidth, imgHeight, po)
2017-10-18 00:23:17 +02:00
// Do some shrink-on-load
if scale < 1.0 {
2018-09-07 18:05:28 +02:00
if shrink := calcShink(scale, imgtype); shrink != 1 {
scale = scale * float64(shrink)
2017-10-18 00:23:17 +02:00
if tmp, e := vipsLoadImage(data, imgtype, shrink); e == nil {
C.swap_and_clear(&img, tmp)
} else {
return nil, e
2017-10-18 00:23:17 +02:00
}
}
}
premultiplied := false
var bandFormat C.VipsBandFormat
2017-10-19 17:51:44 +02:00
if vipsImageHasAlpha(img) {
if bandFormat, err = vipsPremultiply(&img); err != nil {
2017-10-18 00:23:17 +02:00
return nil, err
}
premultiplied = true
}
2017-10-19 17:51:44 +02:00
if err = vipsResize(&img, scale); err != nil {
return nil, err
}
// Update actual image size after resize
imgWidth, imgHeight, _, _ = extractMeta(img)
if premultiplied {
if err = vipsUnpremultiply(&img, bandFormat); err != nil {
return nil, err
2017-10-19 17:51:44 +02:00
}
2017-10-18 00:23:17 +02:00
}
}
if err = vipsImportColourProfile(&img); err != nil {
return nil, err
}
if err = vipsFixColourspace(&img); err != nil {
return nil, err
}
2017-09-27 21:17:17 +02:00
t.Check()
if angle != C.VIPS_ANGLE_D0 || flip {
if err = vipsImageCopyMemory(&img); err != nil {
return nil, err
}
if angle != C.VIPS_ANGLE_D0 {
if err = vipsRotate(&img, angle); err != nil {
return nil, err
}
2017-09-27 10:42:49 +02:00
}
2017-09-27 21:17:17 +02:00
if flip {
if err = vipsFlip(&img); err != nil {
return nil, err
}
}
}
t.Check()
2017-09-27 21:17:17 +02:00
if po.Width == 0 {
po.Width = imgWidth
}
if po.Height == 0 {
po.Height = imgHeight
}
if needToCrop(imgWidth, imgHeight, po) {
2018-09-11 13:23:28 +02:00
if po.Gravity.Type == gravitySmart {
2017-10-18 00:23:17 +02:00
if err = vipsImageCopyMemory(&img); err != nil {
return nil, err
2017-09-27 10:42:49 +02:00
}
if err = vipsSmartCrop(&img, po.Width, po.Height); err != nil {
return nil, err
2017-10-18 00:23:17 +02:00
}
} else {
left, top := calcCrop(imgWidth, imgHeight, po)
if err = vipsCrop(&img, left, top, po.Width, po.Height); err != nil {
return nil, err
2017-10-17 23:12:58 +02:00
}
}
2017-10-18 00:23:17 +02:00
t.Check()
2017-09-27 10:42:49 +02:00
}
2018-09-07 16:46:16 +02:00
if po.Blur > 0 {
if err = vipsBlur(&img, po.Blur); err != nil {
return nil, err
}
}
if po.Sharpen > 0 {
if err = vipsSharpen(&img, po.Sharpen); err != nil {
return nil, err
}
}
t.Check()
return vipsSaveImage(img, po.Format)
2017-10-18 00:23:17 +02:00
}
func vipsLoadImage(data []byte, imgtype imageType, shrink int) (*C.struct__VipsImage, error) {
var img *C.struct__VipsImage
if C.vips_load_buffer(unsafe.Pointer(&data[0]), C.size_t(len(data)), C.int(imgtype), C.int(shrink), &img) != 0 {
return nil, vipsError()
}
return img, nil
}
func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error) {
2017-09-27 10:42:49 +02:00
var ptr unsafe.Pointer
2017-10-05 01:56:42 +02:00
defer C.g_free_go(&ptr)
2017-09-28 15:43:21 +02:00
2017-10-18 00:23:17 +02:00
err := C.int(0)
2017-09-28 15:43:21 +02:00
imgsize := C.size_t(0)
2017-10-18 00:23:17 +02:00
switch imgtype {
case imageTypeJPEG:
err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, cConf.Quality, cConf.JpegProgressive)
case imageTypePNG:
err = C.vips_pngsave_go(img, &ptr, &imgsize, cConf.PngInterlaced)
case imageTypeWEBP:
err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, cConf.Quality)
2017-09-27 10:42:49 +02:00
}
if err != 0 {
return nil, vipsError()
}
2017-10-18 00:23:17 +02:00
return C.GoBytes(ptr, C.int(imgsize)), nil
}
2017-10-19 17:51:44 +02:00
func vipsImageHasAlpha(img *C.struct__VipsImage) bool {
2017-10-19 18:38:09 +02:00
return C.vips_image_hasalpha_go(img) > 0
2017-10-19 17:51:44 +02:00
}
func vipsPremultiply(img **C.struct__VipsImage) (C.VipsBandFormat, error) {
var tmp *C.struct__VipsImage
format := C.vips_band_format(*img)
if C.vips_premultiply_go(*img, &tmp) != 0 {
return 0, vipsError()
}
C.swap_and_clear(img, tmp)
return format, nil
}
func vipsUnpremultiply(img **C.struct__VipsImage, format C.VipsBandFormat) error {
var tmp *C.struct__VipsImage
if C.vips_unpremultiply_go(*img, &tmp) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
if C.vips_cast_go(*img, &tmp, format) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
2017-10-18 00:23:17 +02:00
func vipsResize(img **C.struct__VipsImage, scale float64) error {
var tmp *C.struct__VipsImage
if C.vips_resize_go(*img, &tmp, C.double(scale)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsRotate(img **C.struct__VipsImage, angle int) error {
var tmp *C.struct__VipsImage
if C.vips_rot_go(*img, &tmp, C.VipsAngle(angle)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsFlip(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if C.vips_flip_horizontal_go(*img, &tmp) != 0 {
return vipsError()
}
2017-10-04 21:44:58 +02:00
2017-10-18 00:23:17 +02:00
C.swap_and_clear(img, tmp)
return nil
}
func vipsCrop(img **C.struct__VipsImage, left, top, width, height int) error {
var tmp *C.struct__VipsImage
if C.vips_extract_area_go(*img, &tmp, C.int(left), C.int(top), C.int(width), C.int(height)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsSmartCrop(img **C.struct__VipsImage, width, height int) error {
var tmp *C.struct__VipsImage
if C.vips_smartcrop_go(*img, &tmp, C.int(width), C.int(height)) != 0 {
2018-09-07 16:46:16 +02:00
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsBlur(img **C.struct__VipsImage, sigma float32) error {
var tmp *C.struct__VipsImage
if C.vips_gaussblur_go(*img, &tmp, C.double(sigma)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
}
func vipsSharpen(img **C.struct__VipsImage, sigma float32) error {
var tmp *C.struct__VipsImage
if C.vips_sharpen_go(*img, &tmp, C.double(sigma)) != 0 {
2017-10-18 00:23:17 +02:00
return vipsError()
}
2017-09-27 10:42:49 +02:00
2017-10-18 00:23:17 +02:00
C.swap_and_clear(img, tmp)
return nil
}
func vipsImportColourProfile(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if C.vips_need_icc_import(*img) > 0 {
profile, err := cmykProfilePath()
if err != nil {
return err
}
if C.vips_icc_import_go(*img, &tmp, C.CString(profile)) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
}
return nil
}
2017-10-18 00:23:17 +02:00
func vipsFixColourspace(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if C.vips_image_guess_interpretation(*img) != C.VIPS_INTERPRETATION_sRGB {
if C.vips_colourspace_go(*img, &tmp, C.VIPS_INTERPRETATION_sRGB) != 0 {
return vipsError()
}
C.swap_and_clear(img, tmp)
}
return nil
}
func vipsImageCopyMemory(img **C.struct__VipsImage) error {
var tmp *C.struct__VipsImage
if tmp = C.vips_image_copy_memory(*img); tmp == nil {
return vipsError()
}
C.swap_and_clear(img, tmp)
return nil
2017-09-27 10:42:49 +02:00
}
func vipsError() error {
return errors.New(C.GoString(C.vips_error_buffer()))
2017-06-20 15:58:55 +02:00
}