diff --git a/README.md b/README.md index 99c444e..bd4a29e 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,34 @@ # bimg [![Build Status](https://travis-ci.org/h2non/bimg.png)](https://travis-ci.org/h2non/bimg) [![GitHub release](https://img.shields.io/github/tag/h2non/bimg.svg)]() [![GoDoc](https://godoc.org/github.com/h2non/bimg?status.png)](https://godoc.org/github.com/h2non/bimg) -Go library for blazing fast image processing based on [libvips](https://github.com/jcupitt/libvips) using C bindings +Go library for blazing fast image processing based on [libvips](https://github.com/jcupitt/libvips) using C bindings. + +**bimg** was focused on performance, resizing an image with libvips is typically 4x faster than using the quickest ImageMagick and GraphicsMagick settings. + +**bimg** was heavily inspired in [sharp](https://github.com/lovell/sharp), a great node.js package for image processing build by [Lovell Fuller](https://github.com/lovell). `Work in progress` +## Prerequisites + +- [libvips](https://github.com/jcupitt/libvips) v7.40.0+ (7.42.0+ recommended) +- C++11 compatible compiler such as gcc 4.6+ or clang 3.0+ + ## Installation ```bash go get gopkg.in/h2non/bimg.v0 ``` - Requires Go 1.3+ +### libvips + +Run the following script as `sudo` (supports OSX, Debian/Ubuntu, Redhat, Fedora, Amazon Linux): +```bash +curl -s https://raw.githubusercontent.com/lovell/sharp/master/preinstall.sh | sudo bash - +``` + +The [install script](https://github.com/lovell/sharp/blob/master/preinstall.sh) requires `curl` and `pkg-config`. + ## API ```go diff --git a/resize.go b/resize.go index 72d946f..cbea3dd 100644 --- a/resize.go +++ b/resize.go @@ -44,20 +44,142 @@ func Resize(buf []byte, o Options) ([]byte, error) { o.Type = imageType } + if IsTypeSupported(o.Type) == false { + return nil, errors.New("Unsupported image output type") + } + // get WxH inWidth := int(image.Xsize) inHeight := int(image.Ysize) - if o.Crop { - left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity) - o.Width = int(math.Min(float64(inWidth), float64(o.Width))) - o.Height = int(math.Min(float64(inHeight), float64(o.Height))) - image, err = vipsExtract(image, left, top, o.Width, o.Height) + // prepare for factor + factor := 0.0 + + // image calculations + switch { + // Fixed width and height + case o.Width > 0 && o.Height > 0: + xf := float64(inWidth) / float64(o.Width) + yf := float64(inHeight) / float64(o.Height) + if o.Crop { + factor = math.Min(xf, yf) + } else { + factor = math.Max(xf, yf) + } + // Fixed width, auto height + case o.Width > 0: + factor = float64(inWidth) / float64(o.Width) + o.Height = int(math.Floor(float64(inHeight) / factor)) + // Fixed height, auto width + case o.Height > 0: + factor = float64(inHeight) / float64(o.Height) + o.Width = int(math.Floor(float64(inWidth) / factor)) + // Identity transform + default: + factor = 1 + o.Width = inWidth + o.Height = inHeight + } + + debug("transform from %dx%d to %dx%d", inWidth, inHeight, o.Width, o.Height) + + shrink := int(math.Max(math.Floor(factor), 1)) + residual := float64(shrink) / factor + + // Do not enlarge the output if the input width *or* height are already less than the required dimensions + if o.Enlarge == false { + if inWidth < o.Width && inHeight < o.Height { + factor = 1 + shrink = 1 + residual = 0 + o.Width = inWidth + o.Height = inHeight + } + } + + debug("factor: %v, shrink: %v, residual: %v", factor, shrink, residual) + + // Try to use libjpeg shrink-on-load + shrinkOnLoad := 1 + if imageType == JPEG && shrink >= 2 { + switch { + case shrink >= 8: + factor = factor / 8 + shrinkOnLoad = 8 + case shrink >= 4: + factor = factor / 4 + shrinkOnLoad = 4 + case shrink >= 2: + factor = factor / 2 + shrinkOnLoad = 2 + } + + if shrinkOnLoad > 1 { + // Recalculate integral shrink and double residual + factor = math.Max(factor, 1.0) + shrink = int(math.Floor(factor)) + residual = float64(shrink) / factor + // Reload input using shrink-on-load + image, err = vipsShrinkJpeg(buf, shrinkOnLoad) + if err != nil { + return nil, err + } + } + } + + if shrink > 1 { + debug("shrink %d", shrink) + // Use vips_shrink with the integral reduction + image, err = vipsShrink(image, shrink) + if err != nil { + return nil, err + } + + // Recalculate residual float based on dimensions of required vs shrunk images + shrunkWidth := int(image.Xsize) + shrunkHeight := int(image.Ysize) + + residualx := float64(o.Width) / float64(shrunkWidth) + residualy := float64(o.Height) / float64(shrunkHeight) + if o.Crop { + residual = math.Max(residualx, residualy) + } else { + residual = math.Min(residualx, residualy) + } + } + + // Use vips_affine with the remaining float part + if residual != 0 { + debug("residual %.2f", residual) + image, err = vipsAffine(image, residual, o.Interpolator) if err != nil { return nil, err } } + // Crop/embed + affinedWidth := int(image.Xsize) + affinedHeight := int(image.Ysize) + + if affinedWidth != o.Width || affinedHeight != o.Height { + if o.Crop { + left, top := calculateCrop(inWidth, inHeight, o.Width, o.Height, o.Gravity) + o.Width = int(math.Min(float64(inWidth), float64(o.Width))) + o.Height = int(math.Min(float64(inHeight), float64(o.Height))) + image, err = vipsExtract(image, left, top, o.Width, o.Height) + if err != nil { + return nil, err + } + } else if o.Embed { + left := (o.Width - affinedWidth) / 2 + top := (o.Height - affinedHeight) / 2 + image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend) + if err != nil { + return nil, err + } + } + } + rotation, flip := calculateRotationAndFlip(image, o.Rotate) if flip { o.Flip = HORIZONTAL @@ -81,10 +203,6 @@ func Resize(buf []byte, o Options) ([]byte, error) { } } - if IsTypeSupported(o.Type) == false { - return nil, errors.New("Unsupported image output type") - } - saveOptions := vipsSaveOptions{ Quality: o.Quality, Type: o.Type, diff --git a/vips.go b/vips.go index 2216e9c..e5f4dca 100644 --- a/vips.go +++ b/vips.go @@ -19,13 +19,17 @@ func init() { runtime.LockOSThread() defer runtime.UnlockOSThread() + if C.VIPS_MAJOR_VERSION <= 7 && C.VIPS_MINOR_VERSION < 40 { + panic("unsupported old vips version") + } + err := C.vips_init(C.CString("bimg")) if err != 0 { C.vips_shutdown() panic("unable to start vips!") } - C.vips_concurrency_set(1) // default + C.vips_concurrency_set(1) // single-thread C.vips_cache_set_max_mem(100 * 1024 * 1024) // 100 MB C.vips_cache_set_max(500) // 500 operations } @@ -50,7 +54,7 @@ func vipsRotate(image *C.struct__VipsImage, angle Angle) (*C.struct__VipsImage, func vipsFlip(image *C.struct__VipsImage, direction Direction) (*C.struct__VipsImage, error) { var out *C.struct__VipsImage - err := C.vips_flip_seq(image, &out) + err := C.vips_flip_custom(image, &out, C.int(direction)) C.g_object_unref(C.gpointer(image)) if err != 0 { return nil, catchVipsError() @@ -81,10 +85,10 @@ func vipsRead(buf []byte) (*C.struct__VipsImage, ImageType, error) { return image, imageType, nil } -func vipsExtract(image *C.struct__VipsImage, left int, top int, width int, height int) (*C.struct__VipsImage, error) { +func vipsExtract(image *C.struct__VipsImage, left, top, width, height int) (*C.struct__VipsImage, error) { var buf *C.struct__VipsImage - err := C.vips_extract_area_0(image, &buf, C.int(left), C.int(top), C.int(width), C.int(height)) + err := C.vips_extract_area_custom(image, &buf, C.int(left), C.int(top), C.int(width), C.int(height)) C.g_object_unref(C.gpointer(image)) if err != 0 { return nil, catchVipsError() @@ -93,6 +97,59 @@ func vipsExtract(image *C.struct__VipsImage, left int, top int, width int, heigh return buf, nil } +func vipsShrinkJpeg(buf []byte, shrink int) (*C.struct__VipsImage, error) { + var image *C.struct__VipsImage + + err := C.vips_jpegload_buffer_shrink(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), &image, C.int(shrink)) + C.g_object_unref(C.gpointer(image)) + if err != 0 { + return nil, catchVipsError() + } + + return image, nil +} + +func vipsShrink(input *C.struct__VipsImage, shrink int) (*C.struct__VipsImage, error) { + var image *C.struct__VipsImage + + err := C.vips_shrink_0(input, &image, C.double(float64(shrink)), C.double(float64(shrink))) + C.g_object_unref(C.gpointer(image)) + if err != 0 { + return nil, catchVipsError() + } + + return image, nil +} + +func vipsEmbed(input *C.struct__VipsImage, left, top, width, height, extend int) (*C.struct__VipsImage, error) { + var image *C.struct__VipsImage + + err := C.vips_embed_custom(input, &image, C.int(left), C.int(top), C.int(width), C.int(height), C.int(extend)) + C.g_object_unref(C.gpointer(image)) + if err != 0 { + return nil, catchVipsError() + } + + return image, nil +} + +func vipsAffine(input *C.struct__VipsImage, residual float64, i Interpolator) (*C.struct__VipsImage, error) { + var image *C.struct__VipsImage + + istring := C.CString(i.String()) + interpolator := C.vips_interpolate_new(istring) + + // Perform affine transformation + err := C.vips_affine_interpolator(input, &image, C.double(residual), 0, 0, C.double(residual), interpolator) + C.g_object_unref(C.gpointer(image)) + C.free(unsafe.Pointer(istring)) + if err != 0 { + return nil, catchVipsError() + } + + return image, nil +} + func vipsImageType(buf []byte) ImageType { imageType := UNKNOWN diff --git a/vips.h b/vips.h index d8c199f..94b34ae 100644 --- a/vips.h +++ b/vips.h @@ -23,13 +23,6 @@ vips_affine_interpolator(VipsImage *in, VipsImage **out, double a, double b, dou return vips_affine(in, out, a, b, c, d, "interpolate", interpolator, NULL); }; -VipsImage* -vips_image_buffer_seq(void *buf, size_t len) -{ - // todo: handle postclose callback - return vips_image_new_from_buffer(buf, len, "access", VIPS_ACCESS_SEQUENTIAL, NULL); -}; - int vips_jpegload_buffer_shrink(void *buf, size_t len, VipsImage **out, int shrink) { @@ -37,9 +30,9 @@ vips_jpegload_buffer_shrink(void *buf, size_t len, VipsImage **out, int shrink) }; int -vips_flip_seq(VipsImage *in, VipsImage **out) +vips_flip_custom(VipsImage *in, VipsImage **out, int direction) { - return vips_flip(in, out, VIPS_DIRECTION_HORIZONTAL, NULL); + return vips_flip(in, out, direction, NULL); }; int @@ -48,12 +41,6 @@ vips_shrink_0(VipsImage *in, VipsImage **out, double xshrink, double yshrink) return vips_shrink(in, out, xshrink, yshrink, NULL); }; -int -vips_copy_0(VipsImage *in, VipsImage **out) -{ - return vips_copy(in, out, NULL); -}; - int vips_rotate(VipsImage *in, VipsImage **buf, int angle) { @@ -84,19 +71,19 @@ vips_exif_orientation(VipsImage *image) { }; int -vips_embed_extend(VipsImage *in, VipsImage **out, int left, int top, int width, int height, int extend) +vips_embed_custom(VipsImage *in, VipsImage **out, int left, int top, int width, int height, int extend) { return vips_embed(in, out, left, top, width, height, "extend", extend, NULL); }; int -vips_colourspace_0(VipsImage *in, VipsImage **out, VipsInterpretation space) +vips_colourspace_custom(VipsImage *in, VipsImage **out, VipsInterpretation space) { return vips_colourspace(in, out, space, NULL); }; int -vips_extract_area_0(VipsImage *in, VipsImage **out, int left, int top, int width, int height) +vips_extract_area_custom(VipsImage *in, VipsImage **out, int left, int top, int width, int height) { return vips_extract_area(in, out, left, top, width, height, NULL); };