mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Add support for writing image data output to a buffer, boosting ops/sec by ~7%. Partially implements #3.
This commit is contained in:
parent
dde9e94850
commit
f7b8ce1287
70
README.md
70
README.md
@ -33,11 +33,9 @@ If you prefer to run a stable, package-managed environment such as Ubuntu 12.04
|
|||||||
|
|
||||||
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) {
|
||||||
@ -49,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;
|
||||||
}
|
}
|
||||||
@ -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
|
```javascript
|
||||||
sharp.embedBlack("input.png", "output.png", 200, 300, function(err) {
|
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
|
## Testing
|
||||||
|
|
||||||
npm test
|
npm test
|
||||||
@ -86,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
|
||||||
|
|
||||||
@ -96,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.
|
||||||
|
5
index.js
5
index.js
@ -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);
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "sharp",
|
"name": "sharp",
|
||||||
"version": "0.0.6",
|
"version": "0.0.7",
|
||||||
"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": "*",
|
||||||
|
38
src/sharp.cc
38
src/sharp.cc
@ -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,12 +131,28 @@ 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") {
|
||||||
|
// 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)) {
|
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")) {
|
||||||
|
// Write PNG to file
|
||||||
if (vips_foreign_save(img, baton->dst.c_str(), "strip", TRUE, "compression", 6, "interlace", FALSE, NULL)) {
|
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();
|
||||||
@ -145,14 +167,18 @@ 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_);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -190,4 +216,4 @@ extern "C" void init(Handle<Object> target) {
|
|||||||
NODE_SET_METHOD(target, "resize", Resize);
|
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 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 = 60379;
|
||||||
|
|
||||||
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() {
|
||||||
@ -117,6 +145,6 @@ async.series({
|
|||||||
}, function(err, results) {
|
}, function(err, results) {
|
||||||
assert(!err, err);
|
assert(!err, err);
|
||||||
Object.keys(results).forEach(function(format) {
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user