Compare commits

...

3 Commits

5 changed files with 207 additions and 82 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.
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
@@ -33,11 +33,9 @@ If you prefer to run a stable, package-managed environment such as Ubuntu 12.04
var sharp = require("sharp");
### crop(inputPath, outputPath, width, height, callback)
### crop(input, output, width, height, callback)
Scale and crop `inputPath` to `width` x `height` and write to `outputPath` calling `callback` when complete.
Example:
Scale and crop to `width` x `height` calling `callback` when complete.
```javascript
sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
@@ -49,12 +47,30 @@ sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
});
```
### embedWhite(inputPath, outputPath, width, height, callback)
Scale and embed `inputPath` to `width` x `height` using a white canvas and write to `outputPath` calling `callback` when complete.
```javascript
sharp.crop("input.jpg", sharp.buffer.jpeg, 300, 200, function(err, buffer) {
if (err) {
throw err;
}
// buffer contains JPEG image data
});
```
```javascript
sharp.embedWhite("input.jpg", "output.png", 200, 300, function(err) {
sharp.crop("input.jpg", sharp.buffer.png, 300, 200, function(err, buffer) {
if (err) {
throw err;
}
// buffer contains PNG image data (converted from JPEG)
});
```
### embedWhite(input, output, width, height, callback)
Scale and embed to `width` x `height` using a white canvas calling `callback` when complete.
```javascript
sharp.embedWhite("input.jpg", "output.jpg", 200, 300, function(err) {
if (err) {
throw err;
}
@@ -63,9 +79,18 @@ sharp.embedWhite("input.jpg", "output.png", 200, 300, function(err) {
});
```
### embedBlack(inputPath, outputPath, width, height, callback)
```javascript
sharp.embedWhite("input.jpg", sharp.buffer.jpeg, 200, 300, function(err, buffer) {
if (err) {
throw err;
}
// buffer contains JPEG image data
});
```
Scale and embed `inputPath` to `width` x `height` using a black canvas and write to `outputPath` calling `callback` when complete.
### embedBlack(input, output, width, height, callback)
Scale and embed to `width` x `height` using a black canvas calling `callback` when complete.
```javascript
sharp.embedBlack("input.png", "output.png", 200, 300, function(err) {
@@ -77,6 +102,19 @@ sharp.embedBlack("input.png", "output.png", 200, 300, function(err) {
});
```
### Parameters common to all methods
#### input
String containing the filename to read from.
#### output
One of:
* String containing the filename to write to.
* `sharp.buffer.jpeg` to pass a Buffer containing JPEG image data to `callback`.
* `sharp.buffer.png` to pass a Buffer containing PNG image data to `callback`.
## Testing
npm test
@@ -86,8 +124,8 @@ sharp.embedBlack("input.png", "output.png", 200, 300, function(err) {
Test environment:
* AMD Athlon 4 core 3.3GHz 512KB L2 CPU 1333 DDR3
* libvips 7.36
* libjpeg-turbo8 1.2.1
* libvips 7.37
* libjpeg-turbo8 1.3.0
* libpng 1.6.6
* zlib1g 1.2.7
@@ -96,17 +134,19 @@ Test environment:
* imagemagick x 5.53 ops/sec ±0.55% (31 runs sampled)
* gm x 10.86 ops/sec ±0.43% (56 runs sampled)
* epeg x 28.07 ops/sec ±0.07% (70 runs sampled)
* sharp 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 75.73 ops/sec ±0.44% (75 runs sampled)
#### PNG
* imagemagick x 4.65 ops/sec ±0.37% (27 runs sampled)
* gm x 21.65 ops/sec ±0.18% (56 runs sampled)
* sharp 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 45.67 ops/sec ±0.41% (75 runs sampled)
## Licence
Copyright 2013 Lovell Fuller
Copyright 2013, 2014 Lovell Fuller
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,10 @@
var sharp = require("./build/Release/sharp");
module.exports.buffer = {
jpeg: "__jpeg",
png: "__png"
};
module.exports.crop = function(input, output, width, height, callback) {
sharp.resize(input, output, width, height, "c", callback);
};

View File

@@ -1,6 +1,6 @@
{
"name": "sharp",
"version": "0.0.6",
"version": "0.0.9",
"author": "Lovell Fuller",
"description": "High performance module to resize JPEG and PNG images using the libvips image processing library",
"scripts": {
@@ -20,7 +20,8 @@
"crop",
"embed",
"libvips",
"fast"
"fast",
"buffer"
],
"devDependencies": {
"imagemagick": "*",

View File

@@ -1,51 +1,46 @@
#include <node.h>
#include <node_buffer.h>
#include <math.h>
#include <string>
#include <vector>
#include <vips/vips.h>
using namespace v8;
// 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_;
};
using namespace node;
struct ResizeBaton {
std::string src;
std::string dst;
void* buffer_out;
size_t buffer_out_len;
int cols;
int rows;
bool crop;
int embed;
std::string err;
Persistent<Function> callback;
ResizeBaton() : buffer_out_len(0) {}
};
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);
}
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) {
ResizeBaton* baton = static_cast<ResizeBaton*>(work->data);
VipsImage *in = vips_image_new_mode((baton->src).c_str(), "p");
if (EndsWith(baton->src, ".jpg") || EndsWith(baton->src, ".jpeg")) {
VipsImage *in = vips_image_new();
if (IsJpeg(baton->src)) {
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);
} else {
(baton->err).append("Unsupported input file type");
@@ -56,36 +51,66 @@ void ResizeAsync(uv_work_t *work) {
vips_error_clear();
return;
}
ImageFreer freer;
freer.add(in);
VipsImage* img = in;
VipsImage* t[4];
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 xfactor = static_cast<double>(in->Xsize) / std::max(baton->cols, 1);
double yfactor = static_cast<double>(in->Ysize) / std::max(baton->rows, 1);
double factor = baton->crop ? std::min(xfactor, yfactor) : std::max(xfactor, yfactor);
factor = std::max(factor, 1.0);
int shrink = floor(factor);
double residual = shrink / factor;
// Use im_shrink with the integral reduction
if (im_shrink(img, t[0], shrink, shrink)) {
// Try to use libjpeg shrink-on-load
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());
vips_error_clear();
g_object_unref(in);
return;
}
// Use im_affinei with the remaining float part using bilinear interpolation
if (im_affinei_all(t[0], t[1], vips_interpolate_bilinear_static(), residual, 0, 0, residual, 0, 0)) {
if (shrink > 1) {
// 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());
vips_error_clear();
g_object_unref(in);
return;
}
img = t[1];
@@ -98,6 +123,7 @@ void ResizeAsync(uv_work_t *work) {
if (im_extract_area(img, t[2], left, top, width, height)) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
g_object_unref(in);
return;
}
img = t[2];
@@ -107,6 +133,7 @@ void ResizeAsync(uv_work_t *work) {
if (im_embed(img, t[2], baton->embed, left, top, baton->cols, baton->rows)) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
g_object_unref(in);
return;
}
img = t[2];
@@ -121,16 +148,31 @@ void ResizeAsync(uv_work_t *work) {
if (im_conv(img, t[3], sharpen)) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
g_object_unref(in);
return;
}
img = t[3];
if (EndsWith(baton->dst, ".jpg") || EndsWith(baton->dst, ".jpeg")) {
if (baton->dst == "__jpeg") {
// Write JPEG to buffer
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());
vips_error_clear();
}
} else if (baton->dst == "__png") {
// Write PNG to buffer
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());
vips_error_clear();
}
} else if (EndsWith(baton->dst, ".jpg") || EndsWith(baton->dst, ".jpeg")) {
// Write JPEG to file
if (vips_foreign_save(img, baton->dst.c_str(), "strip", TRUE, "Q", 80, "optimize_coding", TRUE, NULL)) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
}
} else if (EndsWith(baton->dst, ".png")) {
// Write PNG to file
if (vips_foreign_save(img, baton->dst.c_str(), "strip", TRUE, "compression", 6, "interlace", FALSE, NULL)) {
(baton->err).append(vips_error_buffer());
vips_error_clear();
@@ -138,6 +180,8 @@ void ResizeAsync(uv_work_t *work) {
} else {
(baton->err).append("Unsupported output file type");
}
g_object_unref(in);
vips_thread_shutdown();
}
void ResizeAsyncAfter(uv_work_t *work, int status) {
@@ -145,14 +189,19 @@ void ResizeAsyncAfter(uv_work_t *work, int status) {
ResizeBaton *baton = static_cast<ResizeBaton*>(work->data);
Local<Value> argv[1];
Local<Value> null = Local<Value>::New(Null());
Local<Value> argv[2] = {null, null};
if (!baton->err.empty()) {
// Error
argv[0] = String::New(baton->err.data(), baton->err.size());
} else {
argv[0] = Local<Value>::New(Null());
} else if (baton->buffer_out_len > 0) {
// Buffer
Buffer *buffer = Buffer::New((const char*)(baton->buffer_out), baton->buffer_out_len);
argv[1] = Local<Object>::New(buffer->handle_);
vips_free(baton->buffer_out);
}
baton->callback->Call(Context::GetCurrent()->Global(), 1, argv);
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
baton->callback.Dispose();
delete baton;
delete work;
@@ -168,14 +217,14 @@ Handle<Value> Resize(const Arguments& args) {
baton->rows = args[3]->Int32Value();
Local<String> canvas = args[4]->ToString();
if (canvas->Equals(String::NewSymbol("c"))) {
baton->crop = true;
baton->crop = true;
} else if (canvas->Equals(String::NewSymbol("w"))) {
baton->crop = false;
baton->embed = 4;
} else if (canvas->Equals(String::NewSymbol("b"))) {
baton->crop = false;
baton->embed = 0;
}
}
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[5]));
uv_work_t *work = new uv_work_t;
@@ -184,10 +233,16 @@ Handle<Value> Resize(const Arguments& args) {
return Undefined();
}
static void at_exit(void* arg) {
HandleScope scope;
vips_shutdown();
}
extern "C" void init(Handle<Object> target) {
HandleScope scope;
vips_init("");
AtExit(at_exit);
NODE_SET_METHOD(target, "resize", Resize);
};
NODE_MODULE(sharp, init)
NODE_MODULE(sharp, init);

View File

@@ -18,8 +18,8 @@ var height = 480;
async.series({
jpeg: function(callback) {
(new Benchmark.Suite("jpeg")).add("imagemagick", {
"defer": true,
"fn": function(deferred) {
defer: true,
fn: function(deferred) {
imagemagick.resize({
srcPath: inputJpg,
dstPath: outputJpg,
@@ -35,8 +35,8 @@ async.series({
});
}
}).add("gm", {
"defer": true,
"fn": function(deferred) {
defer: true,
fn: function(deferred) {
gm(inputJpg).crop(width, height).quality(80).write(outputJpg, function (err) {
if (err) {
throw err;
@@ -46,15 +46,15 @@ async.series({
});
}
}).add("epeg", {
"defer": true,
"fn": function(deferred) {
defer: true,
fn: function(deferred) {
var image = new epeg.Image({path: inputJpg});
image.downsize(width, height, 80).saveTo(outputJpg);
deferred.resolve();
}
}).add("sharp", {
"defer": true,
"fn": function(deferred) {
}).add("sharp-file", {
defer: true,
fn: function(deferred) {
sharp.crop(inputJpg, outputJpg, width, height, function(err) {
if (err) {
throw err;
@@ -63,6 +63,18 @@ async.series({
}
});
}
}).add("sharp-buffer", {
defer: true,
fn: function(deferred) {
sharp.crop(inputJpg, sharp.buffer.jpeg, width, height, function(err, buffer) {
if (err) {
throw err;
} else {
assert.notStrictEqual(null, buffer);
deferred.resolve();
}
});
}
}).on("cycle", function(event) {
console.log("jpeg " + String(event.target));
}).on("complete", function() {
@@ -71,8 +83,8 @@ async.series({
},
png: function(callback) {
(new Benchmark.Suite("png")).add("imagemagick", {
"defer": true,
"fn": function(deferred) {
defer: true,
fn: function(deferred) {
imagemagick.resize({
srcPath: inputPng,
dstPath: outputPng,
@@ -87,8 +99,8 @@ async.series({
});
}
}).add("gm", {
"defer": true,
"fn": function(deferred) {
defer: true,
fn: function(deferred) {
gm(inputPng).crop(width, height).write(outputPng, function (err) {
if (err) {
throw err;
@@ -97,9 +109,9 @@ async.series({
}
});
}
}).add("sharp", {
"defer": true,
"fn": function(deferred) {
}).add("sharp-file", {
defer: true,
fn: function(deferred) {
sharp.crop(inputPng, outputPng, width, height, function(err) {
if (err) {
throw err;
@@ -108,6 +120,18 @@ async.series({
}
});
}
}).add("sharp-buffer", {
defer: true,
fn: function(deferred) {
sharp.crop(inputPng, sharp.buffer.png, width, height, function(err, buffer) {
if (err) {
throw err;
} else {
assert.notStrictEqual(null, buffer);
deferred.resolve();
}
});
}
}).on("cycle", function(event) {
console.log(" png " + String(event.target));
}).on("complete", function() {
@@ -117,6 +141,6 @@ async.series({
}, function(err, results) {
assert(!err, err);
Object.keys(results).forEach(function(format) {
assert(results[format] == "sharp", "sharp was slower than " + results[format] + " for " + format);
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
});
});