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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type imageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
UNKNOWN imageType = iota
|
|
|
|
JPEG
|
|
|
|
PNG
|
|
|
|
WEBP
|
|
|
|
GIF
|
|
|
|
)
|
|
|
|
|
|
|
|
var imageTypes = map[string]imageType{
|
|
|
|
"jpeg": JPEG,
|
|
|
|
"jpg": JPEG,
|
|
|
|
"png": PNG,
|
|
|
|
"webp": WEBP,
|
|
|
|
"gif": GIF,
|
|
|
|
}
|
|
|
|
|
|
|
|
type gravityType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
CENTER gravityType = iota
|
|
|
|
NORTH
|
|
|
|
EAST
|
|
|
|
SOUTH
|
|
|
|
WEST
|
|
|
|
SMART
|
|
|
|
)
|
|
|
|
|
|
|
|
var gravityTypes = map[string]gravityType{
|
|
|
|
"ce": CENTER,
|
|
|
|
"no": NORTH,
|
|
|
|
"ea": EAST,
|
|
|
|
"so": SOUTH,
|
|
|
|
"we": WEST,
|
|
|
|
"sm": SMART,
|
|
|
|
}
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
type resizeType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
FIT resizeType = iota
|
|
|
|
FILL
|
|
|
|
CROP
|
2017-06-20 15:58:55 +02:00
|
|
|
)
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
var resizeTypes = map[string]resizeType{
|
|
|
|
"fit": FIT,
|
|
|
|
"fill": FILL,
|
|
|
|
"crop": CROP,
|
|
|
|
}
|
|
|
|
|
2017-06-20 15:58:55 +02:00
|
|
|
type processingOptions struct {
|
2017-09-27 10:42:49 +02:00
|
|
|
resize resizeType
|
2017-06-20 15:58:55 +02:00
|
|
|
width int
|
|
|
|
height int
|
2017-09-27 10:42:49 +02:00
|
|
|
gravity gravityType
|
2017-06-20 15:58:55 +02:00
|
|
|
enlarge bool
|
2017-09-27 10:42:49 +02:00
|
|
|
format imageType
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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!")
|
|
|
|
}
|
|
|
|
|
|
|
|
C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100Mb
|
|
|
|
C.vips_cache_set_max(500)
|
2017-09-27 21:17:17 +02:00
|
|
|
|
2017-09-28 10:00:51 +02:00
|
|
|
if len(os.Getenv("IMGPROXY_DEBUG_VIPS")) > 0 {
|
|
|
|
C.vips_cache_set_dump(C.gboolean(1))
|
|
|
|
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
|
|
|
|
2017-10-06 22:54:24 +02:00
|
|
|
if int(C.vips_type_find_load_go(C.JPEG)) != 0 {
|
|
|
|
vipsTypeSupportLoad[JPEG] = true
|
|
|
|
}
|
|
|
|
if int(C.vips_type_find_load_go(C.PNG)) != 0 {
|
|
|
|
vipsTypeSupportLoad[PNG] = true
|
|
|
|
}
|
|
|
|
if int(C.vips_type_find_load_go(C.WEBP)) != 0 {
|
|
|
|
vipsTypeSupportLoad[WEBP] = true
|
|
|
|
}
|
|
|
|
if int(C.vips_type_find_load_go(C.GIF)) != 0 {
|
|
|
|
vipsTypeSupportLoad[GIF] = true
|
2017-09-28 19:18:01 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:54:24 +02:00
|
|
|
if int(C.vips_type_find_save_go(C.JPEG)) != 0 {
|
|
|
|
vipsTypeSupportSave[JPEG] = true
|
|
|
|
}
|
|
|
|
if int(C.vips_type_find_save_go(C.PNG)) != 0 {
|
|
|
|
vipsTypeSupportSave[PNG] = true
|
|
|
|
}
|
|
|
|
if int(C.vips_type_find_save_go(C.WEBP)) != 0 {
|
|
|
|
vipsTypeSupportSave[WEBP] = true
|
2017-09-27 10:42:49 +02:00
|
|
|
}
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 22:54:24 +02:00
|
|
|
func randomAccessRequired(po processingOptions) int {
|
|
|
|
if po.gravity == SMART {
|
|
|
|
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
|
|
|
|
|
|
|
func calcScale(width, height int, po processingOptions) float64 {
|
|
|
|
if (po.width == width && po.height == height) || (po.resize != FILL && po.resize != FIT) {
|
|
|
|
return 1
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
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
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.resize == FIT {
|
|
|
|
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-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
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.gravity == NORTH {
|
|
|
|
top = 0
|
|
|
|
}
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.gravity == EAST {
|
|
|
|
left = width - po.width
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.gravity == SOUTH {
|
|
|
|
top = height - po.height
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.gravity == WEST {
|
|
|
|
left = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
func processImage(data []byte, imgtype imageType, po processingOptions, t *timer) ([]byte, error) {
|
2017-09-27 19:22:13 +02:00
|
|
|
defer keepAlive(data)
|
2017-09-27 10:42:49 +02:00
|
|
|
|
2017-09-28 19:18:01 +02:00
|
|
|
if po.gravity == SMART && !vipsSupportSmartcrop {
|
|
|
|
return nil, errors.New("Smart crop is not supported by used version of libvips")
|
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
err := C.int(0)
|
|
|
|
|
2017-09-27 21:17:17 +02:00
|
|
|
var img *C.struct__VipsImage
|
2017-09-28 15:43:21 +02:00
|
|
|
defer C.clear_image(&img)
|
2017-09-27 10:42:49 +02:00
|
|
|
|
2017-09-27 21:17:17 +02:00
|
|
|
defer C.vips_cleanup()
|
2017-09-27 10:42:49 +02:00
|
|
|
|
|
|
|
// Load the image
|
|
|
|
switch imgtype {
|
|
|
|
case JPEG:
|
2017-09-28 19:18:01 +02:00
|
|
|
err = C.vips_jpegload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
2017-09-27 10:42:49 +02:00
|
|
|
case PNG:
|
2017-09-28 19:18:01 +02:00
|
|
|
err = C.vips_pngload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
2017-09-27 10:42:49 +02:00
|
|
|
case GIF:
|
2017-09-28 19:18:01 +02:00
|
|
|
err = C.vips_gifload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
2017-09-27 10:42:49 +02:00
|
|
|
case WEBP:
|
2017-09-28 19:18:01 +02:00
|
|
|
err = C.vips_webpload_buffer_go(unsafe.Pointer(&data[0]), C.size_t(len(data)), &img, C.int(randomAccessRequired(po)))
|
2017-09-27 10:42:49 +02:00
|
|
|
}
|
|
|
|
if err != 0 {
|
|
|
|
return nil, vipsError()
|
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
t.Check()
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
imgWidth := int(img.Xsize)
|
|
|
|
imgHeight := int(img.Ysize)
|
|
|
|
|
|
|
|
// Ensure we won't crop out of bounds
|
|
|
|
if !po.enlarge || po.resize == CROP {
|
|
|
|
if imgWidth < po.width {
|
|
|
|
po.width = imgWidth
|
|
|
|
}
|
2017-06-20 15:58:55 +02:00
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if imgHeight < po.height {
|
|
|
|
po.height = imgHeight
|
|
|
|
}
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.width != imgWidth || po.height != imgHeight {
|
2017-09-27 21:17:17 +02:00
|
|
|
var (
|
|
|
|
pResize, pCrop int
|
|
|
|
pScale float64
|
|
|
|
pSmart int
|
|
|
|
pLeft, pTop, pWidth, pHeight int
|
|
|
|
)
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.resize == FILL || po.resize == FIT {
|
2017-09-27 21:17:17 +02:00
|
|
|
pResize = 1
|
|
|
|
pScale = calcScale(imgWidth, imgHeight, po)
|
|
|
|
} else {
|
|
|
|
pScale = 1.0
|
2017-09-27 10:42:49 +02:00
|
|
|
}
|
2017-09-27 21:17:17 +02:00
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
if po.resize == FILL || po.resize == CROP {
|
2017-09-27 21:17:17 +02:00
|
|
|
pCrop = 1
|
|
|
|
pWidth, pHeight = po.width, po.height
|
|
|
|
|
2017-09-28 19:18:01 +02:00
|
|
|
if po.gravity == SMART {
|
2017-09-27 21:17:17 +02:00
|
|
|
pSmart = 1
|
2017-09-27 10:42:49 +02:00
|
|
|
} else {
|
2017-09-27 21:17:17 +02:00
|
|
|
pLeft, pTop = calcCrop(round(float64(imgWidth)*pScale), round(float64(imgHeight)*pScale), po)
|
2017-09-27 10:42:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-27 21:17:17 +02:00
|
|
|
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))
|
|
|
|
if err != 0 {
|
|
|
|
return nil, vipsError()
|
|
|
|
}
|
2017-09-27 10:42:49 +02:00
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
t.Check()
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
// Finally, save
|
|
|
|
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
|
|
|
|
|
|
|
imgsize := C.size_t(0)
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
switch po.format {
|
|
|
|
case JPEG:
|
|
|
|
err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0)
|
|
|
|
case PNG:
|
|
|
|
err = C.vips_pngsave_go(img, &ptr, &imgsize)
|
|
|
|
case WEBP:
|
|
|
|
err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality))
|
|
|
|
}
|
|
|
|
if err != 0 {
|
|
|
|
return nil, vipsError()
|
|
|
|
}
|
|
|
|
|
2017-10-04 21:44:58 +02:00
|
|
|
t.Check()
|
|
|
|
|
2017-09-27 10:42:49 +02:00
|
|
|
buf := C.GoBytes(ptr, C.int(imgsize))
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vipsError() error {
|
|
|
|
return errors.New(C.GoString(C.vips_error_buffer()))
|
2017-06-20 15:58:55 +02:00
|
|
|
}
|