Compare commits

..

3 Commits

5 changed files with 157 additions and 54 deletions

View File

@@ -19,16 +19,11 @@ Performance is 4x-8x faster than ImageMagick and 2x-4x faster than GraphicsMagic
* Node.js v0.8+ * Node.js v0.8+
* node-gyp * node-gyp
* libvips-dev 7.28+ (7.36+ for optimal JPEG Huffman coding) * [libvips](https://github.com/jcupitt/libvips) v7.37+
``` For the sharpest results, please compile libvips from source.
sudo npm install -g node-gyp
sudo apt-get install libvips-dev
```
When installed as a package, please symlink `vips-7.28.pc` (or later, installed with libvips-dev) as `/usr/lib/pkgconfig/vips.pc`. To do this in Ubuntu 13.04 (64-bit), use: If you prefer to run a stable, package-managed environment such as Ubuntu 12.04 LTS, [v0.0.3](https://github.com/lovell/sharp/tree/v0.0.3) will work with the libvips-dev package.
sudo ln -s /usr/lib/x86_64-linux-gnu/pkgconfig/vips-7.28.pc /usr/lib/pkgconfig/vips.pc
## Install ## Install
@@ -38,11 +33,9 @@ When installed as a package, please symlink `vips-7.28.pc` (or later, installed
var sharp = require("sharp"); 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. Scale and crop to `width` x `height` calling `callback` when complete.
Example:
```javascript ```javascript
sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) { sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
@@ -54,12 +47,30 @@ sharp.crop("input.jpg", "output.jpg", 300, 200, function(err) {
}); });
``` ```
### embedWhite(inputPath, outputPath, width, height, callback) ```javascript
sharp.crop("input.jpg", sharp.buffer.jpeg, 300, 200, function(err, buffer) {
Scale and embed `inputPath` to `width` x `height` using a white canvas and write to `outputPath` calling `callback` when complete. if (err) {
throw err;
}
// buffer contains JPEG image data
});
```
```javascript ```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) { if (err) {
throw err; throw err;
} }
@@ -68,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 ```javascript
sharp.embedBlack("input.png", "output.png", 200, 300, function(err) { sharp.embedBlack("input.png", "output.png", 200, 300, function(err) {
@@ -82,9 +102,21 @@ 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 ## Testing
npm install --dev sharp
npm test npm test
## Performance ## Performance
@@ -92,8 +124,8 @@ sharp.embedBlack("input.png", "output.png", 200, 300, function(err) {
Test environment: Test environment:
* AMD Athlon 4 core 3.3GHz 512KB L2 CPU 1333 DDR3 * AMD Athlon 4 core 3.3GHz 512KB L2 CPU 1333 DDR3
* libvips 7.36 * libvips 7.37
* libjpeg-turbo8 1.2.1 * libjpeg-turbo8 1.3.0
* libpng 1.6.6 * libpng 1.6.6
* zlib1g 1.2.7 * zlib1g 1.2.7
@@ -102,17 +134,19 @@ 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 x 31.60 ops/sec ±8.80% (80 runs sampled) * sharp-file x 31.60 ops/sec ±8.80% (80 runs sampled)
* sharp-buffer x 34.04 ops/sec ±0.36% (82 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 x 39.47 ops/sec ±6.78% (68 runs sampled) * sharp-file x 39.47 ops/sec ±6.78% (68 runs sampled)
* sharp-buffer x 42.87 ops/sec ±0.19% (71 runs sampled)
## Licence ## Licence
Copyright 2013 Lovell Fuller Copyright 2013, 2014 Lovell Fuller
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with 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"); var sharp = require("./build/Release/sharp");
module.exports.buffer = {
jpeg: "__jpeg",
png: "__png"
};
module.exports.crop = function(input, output, width, height, callback) { module.exports.crop = function(input, output, width, height, callback) {
sharp.resize(input, output, width, height, "c", callback); sharp.resize(input, output, width, height, "c", callback);
}; };

View File

@@ -1,6 +1,6 @@
{ {
"name": "sharp", "name": "sharp",
"version": "0.0.5", "version": "0.0.8",
"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": {
@@ -20,7 +20,8 @@
"crop", "crop",
"embed", "embed",
"libvips", "libvips",
"fast" "fast",
"buffer"
], ],
"devDependencies": { "devDependencies": {
"imagemagick": "*", "imagemagick": "*",

View File

@@ -3,8 +3,10 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <vips/vips.h> #include <vips/vips.h>
#include <node_buffer.h>
using namespace v8; using namespace v8;
using namespace node;
// Free VipsImage children when object goes out of scope // Free VipsImage children when object goes out of scope
// Thanks due to https://github.com/dosx/node-vips // Thanks due to https://github.com/dosx/node-vips
@@ -27,12 +29,16 @@ class ImageFreer {
struct ResizeBaton { struct ResizeBaton {
std::string src; std::string src;
std::string dst; std::string dst;
void* buffer_out;
size_t buffer_out_len;
int cols; int cols;
int rows; int rows;
bool crop; bool crop;
int embed; int embed;
std::string err; std::string err;
Persistent<Function> callback; Persistent<Function> callback;
ResizeBaton() : buffer_out_len(0) {}
}; };
bool EndsWith(std::string const &str, std::string const &end) { bool EndsWith(std::string const &str, std::string const &end) {
@@ -125,19 +131,36 @@ void ResizeAsync(uv_work_t *work) {
} }
img = t[3]; img = t[3];
if (EndsWith(baton->dst, ".jpg") || EndsWith(baton->dst, ".jpeg")) { if (baton->dst == "__jpeg") {
if (vips_jpegsave(img, baton->dst.c_str(), "Q", 80, "profile", "none", "optimize_coding", TRUE, NULL)) { // 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();
return;
}
} 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();
return;
}
} 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()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
} }
} else if (EndsWith(baton->dst, ".png")) { } else if (EndsWith(baton->dst, ".png")) {
if (vips_pngsave(img, baton->dst.c_str(), "compression", 6, "interlace", FALSE, NULL)) { // 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()); (baton->err).append(vips_error_buffer());
vips_error_clear(); vips_error_clear();
} }
} else { } else {
(baton->err).append("Unsupported output file type"); (baton->err).append("Unsupported output file type");
} }
vips_thread_shutdown();
} }
void ResizeAsyncAfter(uv_work_t *work, int status) { void ResizeAsyncAfter(uv_work_t *work, int status) {
@@ -145,14 +168,19 @@ void ResizeAsyncAfter(uv_work_t *work, int status) {
ResizeBaton *baton = static_cast<ResizeBaton*>(work->data); 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()) { if (!baton->err.empty()) {
// Error
argv[0] = String::New(baton->err.data(), baton->err.size()); argv[0] = String::New(baton->err.data(), baton->err.size());
} else { } else if (baton->buffer_out_len > 0) {
argv[0] = Local<Value>::New(Null()); // 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(); baton->callback.Dispose();
delete baton; delete baton;
delete work; delete work;
@@ -168,14 +196,14 @@ Handle<Value> Resize(const Arguments& args) {
baton->rows = args[3]->Int32Value(); baton->rows = args[3]->Int32Value();
Local<String> canvas = args[4]->ToString(); Local<String> canvas = args[4]->ToString();
if (canvas->Equals(String::NewSymbol("c"))) { if (canvas->Equals(String::NewSymbol("c"))) {
baton->crop = true; baton->crop = true;
} else if (canvas->Equals(String::NewSymbol("w"))) { } else if (canvas->Equals(String::NewSymbol("w"))) {
baton->crop = false; baton->crop = false;
baton->embed = 4; baton->embed = 4;
} else if (canvas->Equals(String::NewSymbol("b"))) { } else if (canvas->Equals(String::NewSymbol("b"))) {
baton->crop = false; baton->crop = false;
baton->embed = 0; baton->embed = 0;
} }
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[5])); baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[5]));
uv_work_t *work = new uv_work_t; uv_work_t *work = new uv_work_t;
@@ -184,10 +212,16 @@ Handle<Value> Resize(const Arguments& args) {
return Undefined(); return Undefined();
} }
static void at_exit(void* arg) {
HandleScope scope;
vips_shutdown();
}
extern "C" void init(Handle<Object> target) { extern "C" void init(Handle<Object> target) {
HandleScope scope; HandleScope scope;
vips_init(""); vips_init("");
AtExit(at_exit);
NODE_SET_METHOD(target, "resize", Resize); NODE_SET_METHOD(target, "resize", Resize);
}; };
NODE_MODULE(sharp, init) NODE_MODULE(sharp, init);

View File

@@ -8,9 +8,11 @@ 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;
@@ -18,8 +20,8 @@ var height = 480;
async.series({ async.series({
jpeg: function(callback) { jpeg: function(callback) {
(new Benchmark.Suite("jpeg")).add("imagemagick", { (new Benchmark.Suite("jpeg")).add("imagemagick", {
"defer": true, defer: true,
"fn": function(deferred) { fn: function(deferred) {
imagemagick.resize({ imagemagick.resize({
srcPath: inputJpg, srcPath: inputJpg,
dstPath: outputJpg, dstPath: outputJpg,
@@ -35,8 +37,8 @@ async.series({
}); });
} }
}).add("gm", { }).add("gm", {
"defer": true, defer: true,
"fn": function(deferred) { fn: function(deferred) {
gm(inputJpg).crop(width, height).quality(80).write(outputJpg, function (err) { gm(inputJpg).crop(width, height).quality(80).write(outputJpg, function (err) {
if (err) { if (err) {
throw err; throw err;
@@ -46,15 +48,15 @@ async.series({
}); });
} }
}).add("epeg", { }).add("epeg", {
"defer": true, defer: true,
"fn": function(deferred) { fn: function(deferred) {
var image = new epeg.Image({path: inputJpg}); var image = new epeg.Image({path: inputJpg});
image.downsize(width, height, 80).saveTo(outputJpg); image.downsize(width, height, 80).saveTo(outputJpg);
deferred.resolve(); deferred.resolve();
} }
}).add("sharp", { }).add("sharp-file", {
"defer": true, defer: true,
"fn": function(deferred) { fn: function(deferred) {
sharp.crop(inputJpg, outputJpg, width, height, function(err) { sharp.crop(inputJpg, outputJpg, width, height, function(err) {
if (err) { if (err) {
throw err; throw err;
@@ -63,6 +65,19 @@ 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);
assert.strictEqual(outputJpgLength, buffer.length);
deferred.resolve();
}
});
}
}).on("cycle", function(event) { }).on("cycle", function(event) {
console.log("jpeg " + String(event.target)); console.log("jpeg " + String(event.target));
}).on("complete", function() { }).on("complete", function() {
@@ -71,8 +86,8 @@ async.series({
}, },
png: function(callback) { png: function(callback) {
(new Benchmark.Suite("png")).add("imagemagick", { (new Benchmark.Suite("png")).add("imagemagick", {
"defer": true, defer: true,
"fn": function(deferred) { fn: function(deferred) {
imagemagick.resize({ imagemagick.resize({
srcPath: inputPng, srcPath: inputPng,
dstPath: outputPng, dstPath: outputPng,
@@ -87,8 +102,8 @@ async.series({
}); });
} }
}).add("gm", { }).add("gm", {
"defer": true, defer: true,
"fn": function(deferred) { fn: function(deferred) {
gm(inputPng).crop(width, height).write(outputPng, function (err) { gm(inputPng).crop(width, height).write(outputPng, function (err) {
if (err) { if (err) {
throw err; throw err;
@@ -97,9 +112,9 @@ async.series({
} }
}); });
} }
}).add("sharp", { }).add("sharp-file", {
"defer": true, defer: true,
"fn": function(deferred) { fn: function(deferred) {
sharp.crop(inputPng, outputPng, width, height, function(err) { sharp.crop(inputPng, outputPng, width, height, function(err) {
if (err) { if (err) {
throw err; throw err;
@@ -108,6 +123,19 @@ 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);
assert.strictEqual(outputPngLength, buffer.length);
deferred.resolve();
}
});
}
}).on("cycle", function(event) { }).on("cycle", function(event) {
console.log(" png " + String(event.target)); console.log(" png " + String(event.target));
}).on("complete", function() { }).on("complete", function() {
@@ -115,7 +143,8 @@ async.series({
}).run(); }).run();
} }
}, function(err, results) { }, function(err, results) {
results.forEach(function(format, fastest) { assert(!err, err);
assert(fastest === "sharp", "sharp was slower than " + fastest + " for " + format); Object.keys(results).forEach(function(format) {
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
}); });
}); });