Use shrink-on-load for JPEG images, partially implementing #4. Switch to new vip_ methods from legacy im_ methods. Large performance gains all round.

This commit is contained in:
Lovell Fuller 2014-01-16 22:51:44 +00:00
parent 7e8af63129
commit be8f35d830
4 changed files with 69 additions and 52 deletions

View File

@ -13,7 +13,7 @@ It is somewhat opinionated in that it only deals with JPEG and PNG images, alway
Under the hood you'll find the blazingly fast [libvips](https://github.com/jcupitt/libvips) image processing library, originally created in 1989 at Birkbeck College and currently maintained by the University of Southampton. Under the hood you'll find the blazingly fast [libvips](https://github.com/jcupitt/libvips) image processing library, originally created in 1989 at Birkbeck College and currently maintained by the University of Southampton.
Performance is 4x-8x faster than ImageMagick and 2x-4x faster than GraphicsMagick, based mainly on the number of CPU cores available. Performance is 12x-15x faster than ImageMagick and 4x-6x faster than GraphicsMagick, based mainly on the number of CPU cores available.
## Prerequisites ## Prerequisites
@ -134,15 +134,15 @@ Test environment:
* imagemagick x 5.53 ops/sec ±0.55% (31 runs sampled) * imagemagick x 5.53 ops/sec ±0.55% (31 runs sampled)
* gm x 10.86 ops/sec ±0.43% (56 runs sampled) * gm x 10.86 ops/sec ±0.43% (56 runs sampled)
* epeg x 28.07 ops/sec ±0.07% (70 runs sampled) * epeg x 28.07 ops/sec ±0.07% (70 runs sampled)
* sharp-file x 31.60 ops/sec ±8.80% (80 runs sampled) * sharp-file x 72.01 ops/sec ±7.19% (74 runs sampled)
* sharp-buffer x 34.04 ops/sec ±0.36% (82 runs sampled) * sharp-buffer x 75.73 ops/sec ±0.44% (75 runs sampled)
#### PNG #### PNG
* imagemagick x 4.65 ops/sec ±0.37% (27 runs sampled) * imagemagick x 4.65 ops/sec ±0.37% (27 runs sampled)
* gm x 21.65 ops/sec ±0.18% (56 runs sampled) * gm x 21.65 ops/sec ±0.18% (56 runs sampled)
* sharp-file x 39.47 ops/sec ±6.78% (68 runs sampled) * sharp-file x 43.80 ops/sec ±6.81% (75 runs sampled)
* sharp-buffer x 42.87 ops/sec ±0.19% (71 runs sampled) * sharp-buffer x 45.67 ops/sec ±0.41% (75 runs sampled)
## Licence ## Licence

View File

@ -1,6 +1,6 @@
{ {
"name": "sharp", "name": "sharp",
"version": "0.0.8", "version": "0.0.9",
"author": "Lovell Fuller", "author": "Lovell Fuller",
"description": "High performance module to resize JPEG and PNG images using the libvips image processing library", "description": "High performance module to resize JPEG and PNG images using the libvips image processing library",
"scripts": { "scripts": {

View File

@ -1,31 +1,12 @@
#include <node.h> #include <node.h>
#include <node_buffer.h>
#include <math.h> #include <math.h>
#include <string> #include <string>
#include <vector>
#include <vips/vips.h> #include <vips/vips.h>
#include <node_buffer.h>
using namespace v8; using namespace v8;
using namespace node; using namespace node;
// Free VipsImage children when object goes out of scope
// Thanks due to https://github.com/dosx/node-vips
class ImageFreer {
public:
ImageFreer() {}
~ImageFreer() {
for (uint16_t i = 0; i < v_.size(); i++) {
if (v_[i] != NULL) {
g_object_unref(v_[i]);
}
}
v_.clear();
}
void add(VipsImage* i) { v_.push_back(i); }
private:
std::vector<VipsImage*> v_;
};
struct ResizeBaton { struct ResizeBaton {
std::string src; std::string src;
std::string dst; std::string dst;
@ -45,13 +26,21 @@ bool EndsWith(std::string const &str, std::string const &end) {
return str.length() >= end.length() && 0 == str.compare(str.length() - end.length(), end.length(), end); return str.length() >= end.length() && 0 == str.compare(str.length() - end.length(), end.length(), end);
} }
bool IsJpeg(std::string const &str) {
return EndsWith(str, ".jpg") || EndsWith(str, ".jpeg");
}
bool IsPng(std::string const &str) {
return EndsWith(str, ".png");
}
void ResizeAsync(uv_work_t *work) { void ResizeAsync(uv_work_t *work) {
ResizeBaton* baton = static_cast<ResizeBaton*>(work->data); ResizeBaton* baton = static_cast<ResizeBaton*>(work->data);
VipsImage *in = vips_image_new_mode((baton->src).c_str(), "p"); VipsImage *in = vips_image_new();
if (EndsWith(baton->src, ".jpg") || EndsWith(baton->src, ".jpeg")) { if (IsJpeg(baton->src)) {
vips_jpegload((baton->src).c_str(), &in, NULL); vips_jpegload((baton->src).c_str(), &in, NULL);
} else if (EndsWith(baton->src, ".png")) { } else if (IsPng(baton->src)) {
vips_pngload((baton->src).c_str(), &in, NULL); vips_pngload((baton->src).c_str(), &in, NULL);
} else { } else {
(baton->err).append("Unsupported input file type"); (baton->err).append("Unsupported input file type");
@ -62,36 +51,66 @@ void ResizeAsync(uv_work_t *work) {
vips_error_clear(); vips_error_clear();
return; return;
} }
ImageFreer freer;
freer.add(in);
VipsImage* img = in; double xfactor = static_cast<double>(in->Xsize) / std::max(baton->cols, 1);
VipsImage* t[4]; double yfactor = static_cast<double>(in->Ysize) / std::max(baton->rows, 1);
if (im_open_local_array(img, t, 4, "temp", "p")) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
return;
}
double xfactor = static_cast<double>(img->Xsize) / std::max(baton->cols, 1);
double yfactor = static_cast<double>(img->Ysize) / std::max(baton->rows, 1);
double factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor); double factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
factor = std::max(factor, 1.0); factor = std::max(factor, 1.0);
int shrink = floor(factor); int shrink = floor(factor);
double residual = shrink / factor; double residual = shrink / factor;
// Use im_shrink with the integral reduction // Try to use libjpeg shrink-on-load
if (im_shrink(img, t[0], shrink, shrink)) { int shrink_on_load = 1;
if (IsJpeg(baton->src)) {
if (shrink >= 8) {
residual = residual * shrink / 8;
shrink_on_load = 8;
shrink = 1;
} else if (shrink >= 4) {
residual = residual * shrink / 4;
shrink_on_load = 4;
shrink = 1;
} else if (shrink >= 2) {
residual = residual * shrink / 2;
shrink_on_load = 2;
shrink = 1;
}
if (shrink_on_load > 1) {
if (vips_jpegload((baton->src).c_str(), &in, "shrink", shrink_on_load, NULL)) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
g_object_unref(in);
return;
}
}
}
VipsImage* img = in;
VipsImage* t[4];
if (im_open_local_array(img, t, 4, "temp", "p")) {
(baton->err).append(vips_error_buffer()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
g_object_unref(in);
return; return;
} }
// Use im_affinei with the remaining float part using bilinear interpolation if (shrink > 1) {
if (im_affinei_all(t[0], t[1], vips_interpolate_bilinear_static(), residual, 0, 0, residual, 0, 0)) { // Use vips_shrink with the integral reduction
if (vips_shrink(img, &t[0], shrink, shrink, NULL)) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
g_object_unref(in);
return;
}
} else {
t[0] = img;
}
// Use vips_affine with the remaining float part using bilinear interpolation
if (vips_affine(t[0], &t[1], residual, 0, 0, residual, "interpolate", vips_interpolate_bilinear_static(), NULL)) {
(baton->err).append(vips_error_buffer()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
g_object_unref(in);
return; return;
} }
img = t[1]; img = t[1];
@ -104,6 +123,7 @@ void ResizeAsync(uv_work_t *work) {
if (im_extract_area(img, t[2], left, top, width, height)) { if (im_extract_area(img, t[2], left, top, width, height)) {
(baton->err).append(vips_error_buffer()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
g_object_unref(in);
return; return;
} }
img = t[2]; img = t[2];
@ -113,6 +133,7 @@ void ResizeAsync(uv_work_t *work) {
if (im_embed(img, t[2], baton->embed, left, top, baton->cols, baton->rows)) { if (im_embed(img, t[2], baton->embed, left, top, baton->cols, baton->rows)) {
(baton->err).append(vips_error_buffer()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
g_object_unref(in);
return; return;
} }
img = t[2]; img = t[2];
@ -127,6 +148,7 @@ void ResizeAsync(uv_work_t *work) {
if (im_conv(img, t[3], sharpen)) { if (im_conv(img, t[3], sharpen)) {
(baton->err).append(vips_error_buffer()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
g_object_unref(in);
return; return;
} }
img = t[3]; img = t[3];
@ -136,14 +158,12 @@ void ResizeAsync(uv_work_t *work) {
if (vips_jpegsave_buffer(img, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, "optimize_coding", TRUE, NULL)) { if (vips_jpegsave_buffer(img, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "Q", 80, "optimize_coding", TRUE, NULL)) {
(baton->err).append(vips_error_buffer()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
return;
} }
} else if (baton->dst == "__png") { } else if (baton->dst == "__png") {
// Write PNG to buffer // Write PNG to buffer
if (vips_pngsave_buffer(img, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "compression", 6, "interlace", FALSE, NULL)) { if (vips_pngsave_buffer(img, &baton->buffer_out, &baton->buffer_out_len, "strip", TRUE, "compression", 6, "interlace", FALSE, NULL)) {
(baton->err).append(vips_error_buffer()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
return;
} }
} else if (EndsWith(baton->dst, ".jpg") || EndsWith(baton->dst, ".jpeg")) { } else if (EndsWith(baton->dst, ".jpg") || EndsWith(baton->dst, ".jpeg")) {
// Write JPEG to file // Write JPEG to file
@ -160,6 +180,7 @@ void ResizeAsync(uv_work_t *work) {
} else { } else {
(baton->err).append("Unsupported output file type"); (baton->err).append("Unsupported output file type");
} }
g_object_unref(in);
vips_thread_shutdown(); vips_thread_shutdown();
} }

View File

@ -8,11 +8,9 @@ var Benchmark = require("benchmark");
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/ var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
var outputJpg = __dirname + "/output.jpg"; var outputJpg = __dirname + "/output.jpg";
var outputJpgLength = 47035;
var inputPng = __dirname + "/50020484-00001.png"; // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png var inputPng = __dirname + "/50020484-00001.png"; // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
var outputPng = __dirname + "/output.png"; var outputPng = __dirname + "/output.png";
var outputPngLength = 60380;
var width = 640; var width = 640;
var height = 480; var height = 480;
@ -73,7 +71,6 @@ async.series({
throw err; throw err;
} else { } else {
assert.notStrictEqual(null, buffer); assert.notStrictEqual(null, buffer);
assert.strictEqual(outputJpgLength, buffer.length);
deferred.resolve(); deferred.resolve();
} }
}); });
@ -131,7 +128,6 @@ async.series({
throw err; throw err;
} else { } else {
assert.notStrictEqual(null, buffer); assert.notStrictEqual(null, buffer);
assert.strictEqual(outputPngLength, buffer.length);
deferred.resolve(); deferred.resolve();
} }
}); });