From a29fa25dfcf28d8e82360a4b50826a93eaa617aa Mon Sep 17 00:00:00 2001 From: Tomas Aparicio Date: Tue, 5 May 2015 20:28:39 +0200 Subject: [PATCH] refactor(vips) --- resize.go | 2 ++ resize_test.go | 5 +++++ vips.go | 10 ++++++++-- vips.h | 14 +++++++------- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/resize.go b/resize.go index a45c988..36bb10a 100644 --- a/resize.go +++ b/resize.go @@ -72,6 +72,8 @@ func Resize(buf []byte, o Options) ([]byte, error) { residual = float64(shrink) / factor } + debug("Test %s, %s, %s", shrink, residual, factor) + // Zoom image if necessary image, err = zoomImage(image, o.Zoom) if err != nil { diff --git a/resize_test.go b/resize_test.go index a56c57b..6e8a150 100644 --- a/resize_test.go +++ b/resize_test.go @@ -153,6 +153,11 @@ func runBenchmarkResize(file string, o Options, b *testing.B) { } } +func BenchmarkRotateJpeg(b *testing.B) { + options := Options{Rotate: 180} + runBenchmarkResize("test.jpg", options, b) +} + func BenchmarkResizeLargeJpeg(b *testing.B) { options := Options{ Width: 800, diff --git a/vips.go b/vips.go index 06ce8c3..04bf8d6 100644 --- a/vips.go +++ b/vips.go @@ -15,6 +15,11 @@ import ( "unsafe" ) +const ( + maxCacheMem = 100 * 1024 * 1024 + maxCacheSize = 500 +) + var ( m sync.Mutex initialized bool @@ -67,8 +72,9 @@ func Initialize() { panic("unable to start vips!") } - C.vips_cache_set_max_mem(100 * 1024 * 1024) - C.vips_cache_set_max(500) + // Set libvips cache params + C.vips_cache_set_max_mem(maxCacheMem) + C.vips_cache_set_max(maxCacheSize) // Explicit concurrency limit to avoid thread-unsafe issues. // See: https://github.com/jcupitt/libvips/issues/261#issuecomment-92850414 diff --git a/vips.h b/vips.h index 8046e6e..37bcdde 100644 --- a/vips.h +++ b/vips.h @@ -56,7 +56,7 @@ vips_shrink_bridge(VipsImage *in, VipsImage **out, double xshrink, double yshrin }; int -vips_rotate(VipsImage *in, VipsImage **buf, int angle) +vips_rotate(VipsImage *in, VipsImage **out, int angle) { int rotate = VIPS_ANGLE_D0; @@ -68,7 +68,7 @@ vips_rotate(VipsImage *in, VipsImage **buf, int angle) rotate = VIPS_ANGLE_D270; } - return vips_rot(in, buf, rotate, NULL); + return vips_rot(in, out, rotate, NULL); }; int @@ -158,16 +158,16 @@ vips_init_image(void *buf, size_t len, int imageType, VipsImage **out) { int code = 1; if (imageType == JPEG) { - code = vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL); + code = vips_jpegload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); } else if (imageType == PNG) { - code = vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL); + code = vips_pngload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); } else if (imageType == WEBP) { - code = vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL); + code = vips_webpload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); } else if (imageType == TIFF) { - code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL); + code = vips_tiffload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); #if (VIPS_MAJOR_VERSION >= 8) } else if (imageType == MAGICK) { - code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_SEQUENTIAL, NULL); + code = vips_magickload_buffer(buf, len, out, "access", VIPS_ACCESS_RANDOM, NULL); #endif }