+5
-0
| var sharp = require("./build/Release/sharp"); | ||
| module.exports.buffer = { | ||
| jpeg: "__jpeg", | ||
| png: "__png" | ||
| }; | ||
| module.exports.crop = function(input, output, width, height, callback) { | ||
@@ -4,0 +9,0 @@ sharp.resize(input, output, width, height, "c", callback); |
+3
-2
| { | ||
| "name": "sharp", | ||
| "version": "0.0.6", | ||
| "version": "0.0.7", | ||
| "author": "Lovell Fuller", | ||
@@ -23,3 +23,4 @@ "description": "High performance module to resize JPEG and PNG images using the libvips image processing library", | ||
| "libvips", | ||
| "fast" | ||
| "fast", | ||
| "buffer" | ||
| ], | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
+54
-14
@@ -36,8 +36,6 @@ # 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 | ||
@@ -53,8 +51,26 @@ 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) { | ||
| if (err) { | ||
| throw err; | ||
| } | ||
| // buffer contains JPEG image data | ||
| }); | ||
| ``` | ||
| 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.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.png", 200, 300, function(err) { | ||
| sharp.embedWhite("input.jpg", "output.jpg", 200, 300, function(err) { | ||
| if (err) { | ||
@@ -68,6 +84,15 @@ throw 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 | ||
@@ -83,2 +108,15 @@ 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 | ||
@@ -93,4 +131,4 @@ | ||
| * 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 | ||
@@ -104,3 +142,4 @@ * zlib1g 1.2.7 | ||
| * 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) | ||
@@ -111,7 +150,8 @@ #### PNG | ||
| * 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 | ||
@@ -118,0 +158,0 @@ Licensed under the Apache License, Version 2.0 (the "License"); |
+34
-8
@@ -6,4 +6,6 @@ #include <node.h> | ||
| #include <vips/vips.h> | ||
| #include <node_buffer.h> | ||
| using namespace v8; | ||
| using namespace node; | ||
@@ -31,2 +33,4 @@ // Free VipsImage children when object goes out of scope | ||
| std::string dst; | ||
| void* buffer_out; | ||
| size_t buffer_out_len; | ||
| int cols; | ||
@@ -38,2 +42,4 @@ int rows; | ||
| Persistent<Function> callback; | ||
| ResizeBaton() : buffer_out_len(0) {} | ||
| }; | ||
@@ -131,3 +137,18 @@ | ||
| 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)) { | ||
@@ -138,2 +159,3 @@ (baton->err).append(vips_error_buffer()); | ||
| } 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)) { | ||
@@ -153,10 +175,14 @@ (baton->err).append(vips_error_buffer()); | ||
| 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_); | ||
| } | ||
| baton->callback->Call(Context::GetCurrent()->Global(), 1, argv); | ||
| baton->callback->Call(Context::GetCurrent()->Global(), 2, argv); | ||
| baton->callback.Dispose(); | ||
@@ -177,3 +203,3 @@ delete baton; | ||
| if (canvas->Equals(String::NewSymbol("c"))) { | ||
| baton->crop = true; | ||
| baton->crop = true; | ||
| } else if (canvas->Equals(String::NewSymbol("w"))) { | ||
@@ -185,3 +211,3 @@ baton->crop = false; | ||
| baton->embed = 0; | ||
| } | ||
| } | ||
| baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[5])); | ||
@@ -201,2 +227,2 @@ | ||
| NODE_MODULE(sharp, init) | ||
| NODE_MODULE(sharp, init); |
+45
-17
@@ -11,5 +11,7 @@ var sharp = require("../index"); | ||
| 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; | ||
@@ -22,4 +24,4 @@ var width = 640; | ||
| (new Benchmark.Suite("jpeg")).add("imagemagick", { | ||
| "defer": true, | ||
| "fn": function(deferred) { | ||
| defer: true, | ||
| fn: function(deferred) { | ||
| imagemagick.resize({ | ||
@@ -40,4 +42,4 @@ srcPath: inputJpg, | ||
| }).add("gm", { | ||
| "defer": true, | ||
| "fn": function(deferred) { | ||
| defer: true, | ||
| fn: function(deferred) { | ||
| gm(inputJpg).crop(width, height).quality(80).write(outputJpg, function (err) { | ||
@@ -52,4 +54,4 @@ if (err) { | ||
| }).add("epeg", { | ||
| "defer": true, | ||
| "fn": function(deferred) { | ||
| defer: true, | ||
| fn: function(deferred) { | ||
| var image = new epeg.Image({path: inputJpg}); | ||
@@ -59,5 +61,5 @@ image.downsize(width, height, 80).saveTo(outputJpg); | ||
| } | ||
| }).add("sharp", { | ||
| "defer": true, | ||
| "fn": function(deferred) { | ||
| }).add("sharp-file", { | ||
| defer: true, | ||
| fn: function(deferred) { | ||
| sharp.crop(inputJpg, outputJpg, width, height, function(err) { | ||
@@ -71,2 +73,15 @@ if (err) { | ||
| } | ||
| }).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) { | ||
@@ -80,4 +95,4 @@ console.log("jpeg " + String(event.target)); | ||
| (new Benchmark.Suite("png")).add("imagemagick", { | ||
| "defer": true, | ||
| "fn": function(deferred) { | ||
| defer: true, | ||
| fn: function(deferred) { | ||
| imagemagick.resize({ | ||
@@ -97,4 +112,4 @@ srcPath: inputPng, | ||
| }).add("gm", { | ||
| "defer": true, | ||
| "fn": function(deferred) { | ||
| defer: true, | ||
| fn: function(deferred) { | ||
| gm(inputPng).crop(width, height).write(outputPng, function (err) { | ||
@@ -108,5 +123,5 @@ if (err) { | ||
| } | ||
| }).add("sharp", { | ||
| "defer": true, | ||
| "fn": function(deferred) { | ||
| }).add("sharp-file", { | ||
| defer: true, | ||
| fn: function(deferred) { | ||
| sharp.crop(inputPng, outputPng, width, height, function(err) { | ||
@@ -120,2 +135,15 @@ if (err) { | ||
| } | ||
| }).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) { | ||
@@ -130,4 +158,4 @@ console.log(" png " + String(event.target)); | ||
| 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); | ||
| }); | ||
| }); |
946367
0.3%160
25%160
33.33%