diff --git a/README.md b/README.md index ba61e2fd..d59887f8 100755 --- a/README.md +++ b/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"); -### 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 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. diff --git a/index.js b/index.js index 1d95cf89..97bbcb28 100755 --- a/index.js +++ b/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); }; diff --git a/package.json b/package.json index b9955590..3b2d46d7 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sharp", - "version": "0.0.6", + "version": "0.0.7", "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": "*", diff --git a/src/sharp.cc b/src/sharp.cc index a78d2015..693ea97b 100755 --- a/src/sharp.cc +++ b/src/sharp.cc @@ -3,8 +3,10 @@ #include #include #include +#include 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 callback; + + ResizeBaton() : buffer_out_len(0) {} }; bool EndsWith(std::string const &str, std::string const &end) { @@ -125,12 +131,28 @@ void ResizeAsync(uv_work_t *work) { } 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)) { (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(); @@ -145,14 +167,18 @@ void ResizeAsyncAfter(uv_work_t *work, int status) { ResizeBaton *baton = static_cast(work->data); - Local argv[1]; + Local null = Local::New(Null()); + Local argv[2] = {null, null}; if (!baton->err.empty()) { + // Error argv[0] = String::New(baton->err.data(), baton->err.size()); - } else { - argv[0] = Local::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::New(buffer->handle_); } - 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 +194,14 @@ Handle Resize(const Arguments& args) { baton->rows = args[3]->Int32Value(); Local 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::New(Local::Cast(args[5])); uv_work_t *work = new uv_work_t; @@ -190,4 +216,4 @@ extern "C" void init(Handle target) { NODE_SET_METHOD(target, "resize", Resize); }; -NODE_MODULE(sharp, init) +NODE_MODULE(sharp, init); diff --git a/tests/perf.js b/tests/perf.js index 95e96b86..f3c9e078 100755 --- a/tests/perf.js +++ b/tests/perf.js @@ -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 = 60379; 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() { @@ -117,6 +145,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); }); });