From c3d39b2b424ee726c04aa4db7e8cdbfc57bbbaee Mon Sep 17 00:00:00 2001 From: DarthSim Date: Mon, 16 Jul 2018 21:14:24 +0600 Subject: [PATCH] Progressive JPEG & interlaced PNG support --- README.md | 2 ++ config.go | 4 ++++ process.go | 24 +++++++++++++++++++++--- vips.h | 4 ++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c2e37e34..daa97836 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,8 @@ You can also specify a secret to enable authorization with the HTTP `Authorizati * `IMGPROXY_QUALITY` — quality of the resulting image, percentage. Default: `80`; * `IMGPROXY_GZIP_COMPRESSION` — GZip compression level. Default: `5`; +* `IMGPROXY_JPEG_PROGRESSIVE` — when true, enables progressive compression of JPEG. Default: false; +* `IMGPROXY_PNG_INTERLACED` — when true, enables interlaced compression of PNG. Default: false; #### Miscellaneous diff --git a/config.go b/config.go index 1be29560..65a55126 100644 --- a/config.go +++ b/config.go @@ -87,6 +87,8 @@ type config struct { MaxSrcDimension int MaxSrcResolution int + JpegProgressive bool + PngInterlaced bool Quality int GZipCompression int @@ -140,6 +142,8 @@ func init() { intEnvConfig(&conf.MaxSrcDimension, "IMGPROXY_MAX_SRC_DIMENSION") megaIntEnvConfig(&conf.MaxSrcResolution, "IMGPROXY_MAX_SRC_RESOLUTION") + boolEnvConfig(&conf.JpegProgressive, "IMGPROXY_JPEG_PROGRESSIVE") + boolEnvConfig(&conf.PngInterlaced, "IMGPROXY_PNG_INTERLACED") intEnvConfig(&conf.Quality, "IMGPROXY_QUALITY") intEnvConfig(&conf.GZipCompression, "IMGPROXY_GZIP_COMPRESSION") diff --git a/process.go b/process.go index 1ee9a0f4..0e94d5aa 100644 --- a/process.go +++ b/process.go @@ -81,6 +81,14 @@ var vipsSupportSmartcrop bool var vipsTypeSupportLoad = make(map[imageType]bool) var vipsTypeSupportSave = make(map[imageType]bool) +type cConfig struct { + Quality C.int + JpegProgressive C.int + PngInterlaced C.int +} + +var cConf cConfig + func initVips() { runtime.LockOSThread() defer runtime.UnlockOSThread() @@ -125,6 +133,16 @@ func initVips() { if int(C.vips_type_find_save_go(C.WEBP)) != 0 { vipsTypeSupportSave[WEBP] = true } + + cConf.Quality = C.int(conf.Quality) + + if conf.JpegProgressive { + cConf.JpegProgressive = C.int(1) + } + + if conf.PngInterlaced { + cConf.PngInterlaced = C.int(1) + } } func shutdownVips() { @@ -366,11 +384,11 @@ func vipsSaveImage(img *C.struct__VipsImage, imgtype imageType) ([]byte, error) switch imgtype { case JPEG: - err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality), 0) + err = C.vips_jpegsave_go(img, &ptr, &imgsize, 1, cConf.Quality, cConf.JpegProgressive) case PNG: - err = C.vips_pngsave_go(img, &ptr, &imgsize) + err = C.vips_pngsave_go(img, &ptr, &imgsize, cConf.PngInterlaced) case WEBP: - err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, C.int(conf.Quality)) + err = C.vips_webpsave_go(img, &ptr, &imgsize, 1, cConf.Quality) } if err != 0 { return nil, vipsError() diff --git a/vips.h b/vips.h index 9b7d8600..a5996b83 100644 --- a/vips.h +++ b/vips.h @@ -198,8 +198,8 @@ vips_jpegsave_go(VipsImage *in, void **buf, size_t *len, int strip, int quality, } int -vips_pngsave_go(VipsImage *in, void **buf, size_t *len) { - return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL); +vips_pngsave_go(VipsImage *in, void **buf, size_t *len, int interlace) { + return vips_pngsave_buffer(in, buf, len, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, "interlace", interlace, NULL); } int