mirror of
https://github.com/lovell/sharp.git
synced 2026-02-04 13:46:19 +01:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7e8af63129 | ||
|
|
f7b8ce1287 | ||
|
|
dde9e94850 |
82
README.md
82
README.md
@@ -19,16 +19,11 @@ Performance is 4x-8x faster than ImageMagick and 2x-4x faster than GraphicsMagic
|
||||
|
||||
* Node.js v0.8+
|
||||
* node-gyp
|
||||
* libvips-dev 7.28+ (7.36+ for optimal JPEG Huffman coding)
|
||||
* [libvips](https://github.com/jcupitt/libvips) v7.37+
|
||||
|
||||
```
|
||||
sudo npm install -g node-gyp
|
||||
sudo apt-get install libvips-dev
|
||||
```
|
||||
For the sharpest results, please compile libvips from source.
|
||||
|
||||
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:
|
||||
|
||||
sudo ln -s /usr/lib/x86_64-linux-gnu/pkgconfig/vips-7.28.pc /usr/lib/pkgconfig/vips.pc
|
||||
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.
|
||||
|
||||
## Install
|
||||
|
||||
@@ -38,11 +33,9 @@ When installed as a package, please symlink `vips-7.28.pc` (or later, installed
|
||||
|
||||
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) {
|
||||
@@ -54,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;
|
||||
}
|
||||
@@ -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
|
||||
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
|
||||
|
||||
npm install --dev sharp
|
||||
npm test
|
||||
|
||||
## Performance
|
||||
@@ -92,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
|
||||
|
||||
@@ -102,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 31.60 ops/sec ±8.80% (80 runs sampled)
|
||||
* sharp-buffer x 34.04 ops/sec ±0.36% (82 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 39.47 ops/sec ±6.78% (68 runs sampled)
|
||||
* sharp-buffer x 42.87 ops/sec ±0.19% (71 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.
|
||||
|
||||
5
index.js
5
index.js
@@ -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);
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sharp",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.8",
|
||||
"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": "*",
|
||||
|
||||
54
src/sharp.cc
54
src/sharp.cc
@@ -3,8 +3,10 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <vips/vips.h>
|
||||
#include <node_buffer.h>
|
||||
|
||||
using namespace v8;
|
||||
using namespace node;
|
||||
|
||||
// Free VipsImage children when object goes out of scope
|
||||
// Thanks due to https://github.com/dosx/node-vips
|
||||
@@ -27,12 +29,16 @@ class ImageFreer {
|
||||
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) {
|
||||
@@ -125,19 +131,36 @@ void ResizeAsync(uv_work_t *work) {
|
||||
}
|
||||
img = t[3];
|
||||
|
||||
if (EndsWith(baton->dst, ".jpg") || EndsWith(baton->dst, ".jpeg")) {
|
||||
if (vips_jpegsave(img, baton->dst.c_str(), "Q", 80, "profile", "none", "optimize_coding", TRUE, NULL)) {
|
||||
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();
|
||||
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());
|
||||
vips_error_clear();
|
||||
}
|
||||
} 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());
|
||||
vips_error_clear();
|
||||
}
|
||||
} else {
|
||||
(baton->err).append("Unsupported output file type");
|
||||
}
|
||||
vips_thread_shutdown();
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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 +196,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 +212,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);
|
||||
|
||||
@@ -8,9 +8,11 @@ var Benchmark = require("benchmark");
|
||||
|
||||
var inputJpg = __dirname + "/2569067123_aca715a2ee_o.jpg"; // http://www.flickr.com/photos/grizdave/2569067123/
|
||||
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 outputPng = __dirname + "/output.png";
|
||||
var outputPngLength = 60380;
|
||||
|
||||
var width = 640;
|
||||
var height = 480;
|
||||
@@ -18,8 +20,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 +37,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 +48,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 +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) {
|
||||
console.log("jpeg " + String(event.target));
|
||||
}).on("complete", function() {
|
||||
@@ -71,8 +86,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 +102,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 +112,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 +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) {
|
||||
console.log(" png " + String(event.target));
|
||||
}).on("complete", function() {
|
||||
@@ -115,7 +143,8 @@ async.series({
|
||||
}).run();
|
||||
}
|
||||
}, function(err, results) {
|
||||
results.forEach(function(format, fastest) {
|
||||
assert(fastest === "sharp", "sharp was slower than " + fastest + " for " + format);
|
||||
assert(!err, err);
|
||||
Object.keys(results).forEach(function(format) {
|
||||
assert.strictEqual("sharp", results[format].toString().substr(0, 5), "sharp was slower than " + results[format] + " for " + format);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user