refactor(resize)

This commit is contained in:
Tomas Aparicio 2015-04-06 17:06:44 +02:00
parent cdade0c27f
commit 40dd19fa6a
8 changed files with 207 additions and 141 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@ bin
/*.jpg
/*.png
/*.webp
/fixtures/*_out.*

View File

@ -15,13 +15,13 @@ bimg was heavily inspired in [sharp](https://github.com/lovell/sharp), a great n
- [libvips](https://github.com/jcupitt/libvips) v7.40.0+ (7.42.0+ recommended)
- C compatible compiler such as gcc 4.6+ or clang 3.0+
- Go 1.3+
## Installation
```bash
go get gopkg.in/h2non/bimg.v0
```
Requires Go 1.3+
### libvips
@ -37,13 +37,22 @@ The [install script](https://github.com/lovell/sharp/blob/master/preinstall.sh)
- Resize
- Enlarge
- Crop
- Zoom
- Rotate
- Flip/Flop
- Extract area
- Extract image metadata (size, format, profile, orientation...)
- Image conversion to multiple formats
## Performance
libvips is probably the faster open source solution for image processing.
Here you can see some performance test comparisons for multiple scenarios:
- [libvips speed and memory usage](http://www.vips.ecs.soton.ac.uk/index.php?title=Speed_and_Memory_Use)
- [sharp performance tests](https://github.com/lovell/sharp#the-task)
bimg performance tests coming soon!
## API
### Example

View File

@ -48,13 +48,13 @@ func Metadata(buf []byte) (ImageMetadata, error) {
}
metadata := ImageMetadata{
Size: size,
Channels: int(image.Bands),
Orientation: vipsExifOrientation(image),
Alpha: vipsHasAlpha(image),
Profile: vipsHasProfile(image),
Space: vipsSpace(image),
Channels: vipsImageBands(image),
Type: getImageTypeName(imageType),
Size: size,
}
return metadata, nil

View File

@ -10,6 +10,14 @@ const QUALITY = 80
type Gravity int
const (
CENTRE Gravity = iota
NORTH
EAST
SOUTH
WEST
)
type Interpolator int
const (

179
resize.go
View File

@ -11,14 +11,6 @@ import (
"math"
)
const (
CENTRE Gravity = iota
NORTH
EAST
SOUTH
WEST
)
func Resize(buf []byte, o Options) ([]byte, error) {
defer C.vips_thread_shutdown()
@ -46,37 +38,8 @@ func Resize(buf []byte, o Options) ([]byte, error) {
inWidth := int(image.Xsize)
inHeight := int(image.Ysize)
// 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)
factor := imageCalculations(o, inWidth, inHeight)
shrink := int(math.Max(math.Floor(factor), 1))
residual := float64(shrink) / factor
@ -91,12 +54,10 @@ func Resize(buf []byte, o Options) ([]byte, error) {
}
}
debug("factor: %v, shrink: %v, residual: %v", factor, shrink, residual)
// Try to use libjpeg shrink-on-load
if imageType == JPEG && shrink >= 2 {
// Recalculate integral shrink and double residual
tmpImage, factor, err := shrinkJpeg(buf, factor, shrink)
tmpImage, factor, err := shrinkJpegImage(buf, factor, shrink)
if err != nil {
return nil, err
}
@ -108,58 +69,35 @@ func Resize(buf []byte, o Options) ([]byte, error) {
}
}
// Calculate integral box shrink
windowSize := vipsWindowSize(o.Interpolator.String())
if factor >= 2 && windowSize > 3 {
// Shrink less, affine more with interpolators that use at least 4x4 pixel window, e.g. bicubic
shrink = int(math.Max(float64(math.Floor(factor*3.0/windowSize)), 1))
}
// Use vips_shrink with the integral reduction
if shrink > 1 {
debug("shrink %d", shrink)
// Use vips_shrink with the integral reduction
image, err = vipsShrink(image, shrink)
image, residual, err = shrinkImage(image, o, residual, 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)
debug("factor: %v, shrink: %v, residual: %v", factor, shrink, residual)
if affinedWidth != o.Width || affinedHeight != o.Height {
if o.Crop {
left, top := calculateCrop(affinedWidth, affinedHeight, 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)))
debug("crop image to %dx%d to %dx%d", left, top, o.Width, 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
}
}
// Extract image
image, err = extractImage(image, o)
if err != nil {
return nil, err
}
rotation, flip := calculateRotationAndFlip(image, o.Rotate)
@ -171,8 +109,7 @@ func Resize(buf []byte, o Options) ([]byte, error) {
}
if o.Rotate > 0 {
rotation := calculateRotation(o.Rotate)
image, err = vipsRotate(image, rotation)
image, err = vipsRotate(image, getAngle(o.Rotate))
if err != nil {
return nil, err
}
@ -195,11 +132,67 @@ func Resize(buf []byte, o Options) ([]byte, error) {
if err != nil {
return nil, err
}
C.vips_error_clear()
return buf, nil
}
func extractImage(image *C.struct__VipsImage, o Options) (*C.struct__VipsImage, error) {
var err error = nil
affinedWidth := int(image.Xsize)
affinedHeight := int(image.Ysize)
if affinedWidth != o.Width || affinedHeight != o.Height {
width := int(math.Min(float64(affinedWidth), float64(o.Width)))
height := int(math.Min(float64(affinedHeight), float64(o.Height)))
switch {
case o.Crop:
left, top := calculateCrop(affinedWidth, affinedHeight, o.Width, o.Height, o.Gravity)
image, err = vipsExtract(image, left, top, width, height)
break
case o.Embed:
left, top := (o.Width-affinedWidth)/2, (o.Height-affinedHeight)/2
image, err = vipsEmbed(image, left, top, o.Width, o.Height, o.Extend)
break
case o.Top > 0 && o.Left > 0:
image, err = vipsExtract(image, o.Left, o.Top, width, height)
break
}
}
return image, err
}
func imageCalculations(o Options, inWidth, inHeight int) float64 {
factor := 1.0
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:
o.Width = inWidth
o.Height = inHeight
}
return factor
}
func calculateCrop(inWidth, inHeight, outWidth, outHeight int, gravity Gravity) (int, int) {
left, top := 0, 0
@ -266,7 +259,27 @@ func calculateRotationAndFlip(image *C.struct__VipsImage, angle Angle) (Angle, b
return rotate, flip
}
func shrinkJpeg(buf []byte, factor float64, shrink int) (*C.struct__VipsImage, float64, error) {
func shrinkImage(image *C.struct__VipsImage, o Options, residual float64, shrink int) (*C.struct__VipsImage, float64, error) {
// Use vips_shrink with the integral reduction
image, err := vipsShrink(image, shrink)
if err != nil {
return nil, 0, err
}
// Recalculate residual float based on dimensions of required vs shrunk images
residualx := float64(o.Width) / float64(image.Xsize)
residualy := float64(o.Height) / float64(image.Ysize)
if o.Crop {
residual = math.Max(residualx, residualy)
} else {
residual = math.Min(residualx, residualy)
}
return image, residual, nil
}
func shrinkJpegImage(buf []byte, factor float64, shrink int) (*C.struct__VipsImage, float64, error) {
shrinkOnLoad := 1
switch {
@ -293,7 +306,7 @@ func shrinkJpeg(buf []byte, factor float64, shrink int) (*C.struct__VipsImage, f
return nil, factor, nil
}
func calculateRotation(angle Angle) Angle {
func getAngle(angle Angle) Angle {
divisor := angle % 90
if divisor != 0 {
angle = angle - divisor

View File

@ -28,14 +28,16 @@ func TestResize(t *testing.T) {
t.Fatal("Image is not jpeg")
}
err = ioutil.WriteFile("result.jpg", newImg, 0644)
err = ioutil.WriteFile("fixtures/test_out.jpg", newImg, 0644)
if err != nil {
t.Fatal("Cannot save the image")
}
}
func TestConvert(t *testing.T) {
options := Options{Width: 640, Height: 480, Crop: true, Type: PNG}
width, height := 640, 480
options := Options{Width: width, Height: height, Crop: true, Type: PNG}
img, err := os.Open("fixtures/test.jpg")
if err != nil {
t.Fatal(err)
@ -56,7 +58,47 @@ func TestConvert(t *testing.T) {
t.Fatal("Image is not png")
}
err = ioutil.WriteFile("result.png", newImg, 0644)
size, _ := Size(newImg)
if size.Height != height || size.Width != width {
t.Fatal("Invalid image size")
}
err = ioutil.WriteFile("fixtures/test_out.png", newImg, 0644)
if err != nil {
t.Fatal("Cannot save the image")
}
}
func TestResizePngWithTransparency(t *testing.T) {
width, height := 300, 240
options := Options{Width: width, Height: height, Crop: true}
img, err := os.Open("fixtures/transparent.png")
if err != nil {
t.Fatal(err)
}
defer img.Close()
buf, err := ioutil.ReadAll(img)
if err != nil {
t.Fatal(err)
}
newImg, err := Resize(buf, options)
if err != nil {
t.Errorf("Resize(imgData, %#v) error: %#v", options, err)
}
if DetermineImageType(newImg) != PNG {
t.Fatal("Image is not png")
}
size, _ := Size(newImg)
if size.Height != height || size.Width != width {
t.Fatal("Invalid image size")
}
err = ioutil.WriteFile("fixtures/transparent_out.png", newImg, 0644)
if err != nil {
t.Fatal("Cannot save the image")
}

16
vips.go
View File

@ -209,18 +209,14 @@ func vipsHasProfile(image *C.struct__VipsImage) bool {
return int(C.has_profile_embed(image)) > 0
}
func vipsWindowSize(name string) int {
return int(C.interpolator_window_size(C.CString(name)))
func vipsWindowSize(name string) float64 {
return float64(C.interpolator_window_size(C.CString(name)))
}
func vipsSpace(image *C.struct__VipsImage) string {
return C.GoString(C.vips_enum_nick_bridge(image))
}
func vipsImageBands(image *C.struct__VipsImage) int {
return int(C.vips_image_bands(image))
}
type vipsSaveOptions struct {
Quality int
Compression int
@ -232,6 +228,10 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
length := C.size_t(0)
err := C.int(0)
// cleanup
defer C.g_object_unref(C.gpointer(image))
defer C.g_free(C.gpointer(ptr))
switch {
case o.Type == PNG:
err = C.vips_pngsave_bridge(image, &ptr, &length, 1, C.int(o.Compression), C.int(o.Quality), 0)
@ -244,14 +244,12 @@ func vipsSave(image *C.struct__VipsImage, o vipsSaveOptions) ([]byte, error) {
break
}
C.g_object_unref(C.gpointer(image))
if err != 0 {
return nil, catchVipsError()
}
buf := C.GoBytes(ptr, C.int(length))
// cleanup
C.g_free(C.gpointer(ptr))
C.vips_error_clear()
return buf, nil
}

79
vips.h
View File

@ -59,15 +59,15 @@ vips_rotate(VipsImage *in, VipsImage **buf, int angle)
int
vips_exif_orientation(VipsImage *image) {
int orientation = 0;
const char **exif;
if (
vips_image_get_typeof(image, "exif-ifd0-Orientation") != 0 &&
!vips_image_get_string(image, "exif-ifd0-Orientation", exif)
) {
orientation = atoi(exif[0]);
}
return orientation;
int orientation = 0;
const char **exif;
if (
vips_image_get_typeof(image, "exif-ifd0-Orientation") != 0 &&
!vips_image_get_string(image, "exif-ifd0-Orientation", exif)
) {
orientation = atoi(exif[0]);
}
return orientation;
};
int
@ -78,18 +78,18 @@ has_profile_embed(VipsImage *image) {
int
has_alpha_channel(VipsImage *image) {
return (
(image->Bands == 2 && image->Type == VIPS_INTERPRETATION_B_W) ||
(image->Bands == 4 && image->Type != VIPS_INTERPRETATION_CMYK) ||
(image->Bands == 5 && image->Type == VIPS_INTERPRETATION_CMYK)
) ? 1 : 0;
(image->Bands == 2 && image->Type == VIPS_INTERPRETATION_B_W) ||
(image->Bands == 4 && image->Type != VIPS_INTERPRETATION_CMYK) ||
(image->Bands == 5 && image->Type == VIPS_INTERPRETATION_CMYK)
) ? 1 : 0;
};
int
interpolator_window_size(char const *name) {
VipsInterpolate *interpolator = vips_interpolate_new(name);
int window_size = vips_interpolate_get_window_size(interpolator);
g_object_unref(interpolator);
return window_size;
VipsInterpolate *interpolator = vips_interpolate_new(name);
int window_size = vips_interpolate_get_window_size(interpolator);
g_object_unref(interpolator);
return window_size;
};
const char *
@ -97,11 +97,6 @@ vips_enum_nick_bridge(VipsImage *image) {
return vips_enum_nick(VIPS_TYPE_INTERPRETATION, image->Type);
};
int
vips_image_bands(VipsImage *image) {
return image->Bands;
};
int
vips_embed_bridge(VipsImage *in, VipsImage **out, int left, int top, int width, int height, int extend)
{
@ -130,10 +125,10 @@ int
vips_pngsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int compression, int quality, int interlace)
{
#if (VIPS_MAJOR_VERSION >= 8 || (VIPS_MAJOR_VERSION >= 7 && VIPS_MINOR_VERSION >= 42))
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression,
"interlace", interlace, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
return vips_pngsave_buffer(in, buf, len, "strip", FALSE, "compression", compression,
"interlace", interlace, "filter", VIPS_FOREIGN_PNG_FILTER_NONE, NULL);
#else
return vips_pngsave_buffer(image, buf, len, "strip", FALSE, "compression", compression,
return vips_pngsave_buffer(image, buf, len, "strip", FALSE, "compression", compression,
"interlace", interlace, NULL);
#endif
};
@ -146,26 +141,26 @@ vips_webpsave_bridge(VipsImage *in, void **buf, size_t *len, int strip, int qual
int
vips_init_image(void *buf, size_t len, int imageType, VipsImage **out) {
int code = 1;
int code = 1;
if (imageType == JPEG) {
code = vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
} else if (imageType == PNG) {
code = vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
} else if (imageType == WEBP) {
code = vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
} else if (imageType == TIFF) {
code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
if (imageType == JPEG) {
code = vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
} else if (imageType == PNG) {
code = vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
} else if (imageType == WEBP) {
code = vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
} else if (imageType == TIFF) {
code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
#if (VIPS_MAJOR_VERSION >= 8)
} else if (imageType == MAGICK) {
code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
} else if (imageType == MAGICK) {
code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL);
#endif
}
if (out != NULL) {
// Listen for "postclose" signal to delete input buffer
//g_signal_connect(out, "postclose", G_CALLBACK(vips_malloc_cb), buf);
}
return code;
if (out != NULL) {
// Listen for "postclose" signal to delete input buffer
//g_signal_connect(out, "postclose", G_CALLBACK(vips_malloc_cb), buf);
}
return code;
};