mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 18:40:16 +02:00
Merge pull request #11 from pierreinglebert/master
Add support for Node.js v0.11+ via nan. Remove epeg test dependency as it fails to compile on v0.11+.
This commit is contained in:
commit
fe773733cd
@ -217,8 +217,6 @@ Test environment:
|
|||||||
* imagemagick x 5.53 ops/sec ±0.62% (31 runs sampled)
|
* imagemagick x 5.53 ops/sec ±0.62% (31 runs sampled)
|
||||||
* gm-file-file x 4.10 ops/sec ±0.41% (25 runs sampled)
|
* gm-file-file x 4.10 ops/sec ±0.41% (25 runs sampled)
|
||||||
* gm-file-buffer x 4.10 ops/sec ±0.36% (25 runs sampled)
|
* gm-file-buffer x 4.10 ops/sec ±0.36% (25 runs sampled)
|
||||||
* epeg-file-file x 23.82 ops/sec ±0.18% (60 runs sampled)
|
|
||||||
* epeg-file-buffer x 23.98 ops/sec ±0.16% (61 runs sampled)
|
|
||||||
|
|
||||||
* sharp-buffer-file x 20.76 ops/sec ±0.55% (54 runs sampled)
|
* sharp-buffer-file x 20.76 ops/sec ±0.55% (54 runs sampled)
|
||||||
* sharp-buffer-buffer x 20.90 ops/sec ±0.26% (54 runs sampled)
|
* sharp-buffer-buffer x 20.90 ops/sec ±0.26% (54 runs sampled)
|
||||||
|
@ -10,7 +10,8 @@
|
|||||||
'/usr/local/lib/glib-2.0/include',
|
'/usr/local/lib/glib-2.0/include',
|
||||||
'/usr/include/glib-2.0',
|
'/usr/include/glib-2.0',
|
||||||
'/usr/lib/glib-2.0/include',
|
'/usr/lib/glib-2.0/include',
|
||||||
'/usr/lib/x86_64-linux-gnu/glib-2.0/include'
|
'/usr/lib/x86_64-linux-gnu/glib-2.0/include',
|
||||||
|
'<!(node -e "require(\'nan\')")'
|
||||||
],
|
],
|
||||||
'cflags': ['-fexceptions', '-pedantic', '-Wall', '-O3'],
|
'cflags': ['-fexceptions', '-pedantic', '-Wall', '-O3'],
|
||||||
'cflags_cc': ['-fexceptions', '-pedantic', '-Wall', '-O3']
|
'cflags_cc': ['-fexceptions', '-pedantic', '-Wall', '-O3']
|
||||||
|
@ -29,12 +29,14 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"imagemagick": "*",
|
"imagemagick": "*",
|
||||||
"gm": "*",
|
"gm": "*",
|
||||||
"epeg": "*",
|
|
||||||
"async": "*",
|
"async": "*",
|
||||||
"benchmark": "*"
|
"benchmark": "*"
|
||||||
},
|
},
|
||||||
"license": "Apache 2.0",
|
"license": "Apache 2.0",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=0.8"
|
"node": ">=0.8"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"nan": "^0.8.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
58
src/sharp.cc
58
src/sharp.cc
@ -5,6 +5,8 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <vips/vips.h>
|
#include <vips/vips.h>
|
||||||
|
|
||||||
|
#include "nan.h"
|
||||||
|
|
||||||
using namespace v8;
|
using namespace v8;
|
||||||
using namespace node;
|
using namespace node;
|
||||||
|
|
||||||
@ -23,7 +25,6 @@ struct resize_baton {
|
|||||||
bool progessive;
|
bool progessive;
|
||||||
VipsAccess access_method;
|
VipsAccess access_method;
|
||||||
std::string err;
|
std::string err;
|
||||||
Persistent<Function> callback;
|
|
||||||
|
|
||||||
resize_baton(): buffer_in_len(0), buffer_out_len(0) {}
|
resize_baton(): buffer_in_len(0), buffer_out_len(0) {}
|
||||||
};
|
};
|
||||||
@ -67,9 +68,13 @@ void resize_error(resize_baton *baton, VipsImage *unref) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize_async(uv_work_t *work) {
|
class ResizeWorker : public NanAsyncWorker {
|
||||||
resize_baton* baton = static_cast<resize_baton*>(work->data);
|
public:
|
||||||
|
ResizeWorker(NanCallback *callback, resize_baton *baton)
|
||||||
|
: NanAsyncWorker(callback), baton(baton) {}
|
||||||
|
~ResizeWorker() {}
|
||||||
|
|
||||||
|
void Execute () {
|
||||||
// Input
|
// Input
|
||||||
ImageType inputImageType = JPEG;
|
ImageType inputImageType = JPEG;
|
||||||
VipsImage *in = vips_image_new();
|
VipsImage *in = vips_image_new();
|
||||||
@ -286,34 +291,28 @@ void resize_async(uv_work_t *work) {
|
|||||||
vips_thread_shutdown();
|
vips_thread_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
void resize_async_after(uv_work_t *work, int status) {
|
void HandleOKCallback () {
|
||||||
HandleScope scope;
|
NanScope();
|
||||||
|
|
||||||
resize_baton *baton = static_cast<resize_baton*>(work->data);
|
|
||||||
|
|
||||||
Handle<Value> argv[2] = { Null(), Null() };
|
Handle<Value> argv[2] = { Null(), Null() };
|
||||||
if (!baton->err.empty()) {
|
if (!baton->err.empty()) {
|
||||||
// Error
|
// Error
|
||||||
argv[0] = scope.Close(String::New(baton->err.data(), baton->err.size()));
|
argv[0] = String::New(baton->err.data(), baton->err.size());
|
||||||
} else if (baton->buffer_out_len > 0) {
|
} else if (baton->buffer_out_len > 0) {
|
||||||
// Buffer
|
// Buffer
|
||||||
Buffer *slowBuffer = Buffer::New(baton->buffer_out_len);
|
argv[1] = NanNewBufferHandle((char *)baton->buffer_out, baton->buffer_out_len);
|
||||||
memcpy(Buffer::Data(slowBuffer), baton->buffer_out, baton->buffer_out_len);
|
|
||||||
Local<Object> globalObj = Context::GetCurrent()->Global();
|
|
||||||
Local<Function> bufferConstructor = Local<Function>::Cast(globalObj->Get(String::New("Buffer")));
|
|
||||||
Handle<Value> constructorArgs[3] = { slowBuffer->handle_, v8::Integer::New(baton->buffer_out_len), v8::Integer::New(0) };
|
|
||||||
argv[1] = scope.Close(bufferConstructor->NewInstance(3, constructorArgs));
|
|
||||||
g_free(baton->buffer_out);
|
g_free(baton->buffer_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
|
|
||||||
baton->callback.Dispose();
|
|
||||||
delete baton;
|
delete baton;
|
||||||
delete work;
|
callback->Call(2, argv);
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle<Value> resize(const Arguments& args) {
|
private:
|
||||||
HandleScope scope;
|
resize_baton* baton;
|
||||||
|
};
|
||||||
|
|
||||||
|
NAN_METHOD(resize) {
|
||||||
|
NanScope();
|
||||||
|
|
||||||
resize_baton *baton = new resize_baton;
|
resize_baton *baton = new resize_baton;
|
||||||
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
||||||
@ -338,16 +337,15 @@ Handle<Value> resize(const Arguments& args) {
|
|||||||
baton->sharpen = args[6]->BooleanValue();
|
baton->sharpen = args[6]->BooleanValue();
|
||||||
baton->progessive = args[7]->BooleanValue();
|
baton->progessive = args[7]->BooleanValue();
|
||||||
baton->access_method = args[8]->BooleanValue() ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
|
baton->access_method = args[8]->BooleanValue() ? VIPS_ACCESS_SEQUENTIAL : VIPS_ACCESS_RANDOM;
|
||||||
baton->callback = Persistent<Function>::New(Local<Function>::Cast(args[9]));
|
|
||||||
|
|
||||||
uv_work_t *work = new uv_work_t;
|
NanCallback *callback = new NanCallback(args[9].As<v8::Function>());
|
||||||
work->data = baton;
|
|
||||||
uv_queue_work(uv_default_loop(), work, resize_async, (uv_after_work_cb)resize_async_after);
|
NanAsyncQueueWorker(new ResizeWorker(callback, baton));
|
||||||
return scope.Close(Undefined());
|
NanReturnUndefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
Handle<Value> cache(const Arguments& args) {
|
NAN_METHOD(cache) {
|
||||||
HandleScope scope;
|
NanScope();
|
||||||
|
|
||||||
// Set cache limit
|
// Set cache limit
|
||||||
if (args[0]->IsInt32()) {
|
if (args[0]->IsInt32()) {
|
||||||
@ -359,16 +357,16 @@ Handle<Value> cache(const Arguments& args) {
|
|||||||
cache->Set(String::NewSymbol("current"), Number::New(vips_tracked_get_mem() / 1048576));
|
cache->Set(String::NewSymbol("current"), Number::New(vips_tracked_get_mem() / 1048576));
|
||||||
cache->Set(String::NewSymbol("high"), Number::New(vips_tracked_get_mem_highwater() / 1048576));
|
cache->Set(String::NewSymbol("high"), Number::New(vips_tracked_get_mem_highwater() / 1048576));
|
||||||
cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576));
|
cache->Set(String::NewSymbol("limit"), Number::New(vips_cache_get_max_mem() / 1048576));
|
||||||
return scope.Close(cache);
|
NanReturnValue(cache);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void at_exit(void* arg) {
|
static void at_exit(void* arg) {
|
||||||
HandleScope scope;
|
NanScope();
|
||||||
vips_shutdown();
|
vips_shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void init(Handle<Object> target) {
|
extern "C" void init(Handle<Object> target) {
|
||||||
HandleScope scope;
|
NanScope();
|
||||||
vips_init("");
|
vips_init("");
|
||||||
AtExit(at_exit);
|
AtExit(at_exit);
|
||||||
NODE_SET_METHOD(target, "resize", resize);
|
NODE_SET_METHOD(target, "resize", resize);
|
||||||
|
@ -2,7 +2,6 @@ var sharp = require("../index");
|
|||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var imagemagick = require("imagemagick");
|
var imagemagick = require("imagemagick");
|
||||||
var gm = require("gm");
|
var gm = require("gm");
|
||||||
var epeg = require("epeg");
|
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var Benchmark = require("benchmark");
|
var Benchmark = require("benchmark");
|
||||||
@ -65,21 +64,6 @@ async.series({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).add("epeg-file-file", {
|
|
||||||
defer: true,
|
|
||||||
fn: function(deferred) {
|
|
||||||
var image = new epeg.Image({path: inputJpg});
|
|
||||||
image.downsize(width, height, 80).saveTo(outputJpg);
|
|
||||||
deferred.resolve();
|
|
||||||
}
|
|
||||||
}).add("epeg-file-buffer", {
|
|
||||||
defer: true,
|
|
||||||
fn: function(deferred) {
|
|
||||||
var image = new epeg.Image({path: inputJpg});
|
|
||||||
var buffer = image.downsize(width, height, 80).process();
|
|
||||||
assert.notStrictEqual(null, buffer);
|
|
||||||
deferred.resolve();
|
|
||||||
}
|
|
||||||
}).add("sharp-buffer-file", {
|
}).add("sharp-buffer-file", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
|
@ -2,7 +2,6 @@ var sharp = require("../index");
|
|||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
var imagemagick = require("imagemagick");
|
var imagemagick = require("imagemagick");
|
||||||
var gm = require("gm");
|
var gm = require("gm");
|
||||||
var epeg = require("epeg");
|
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
var assert = require("assert");
|
var assert = require("assert");
|
||||||
var Benchmark = require("benchmark");
|
var Benchmark = require("benchmark");
|
||||||
@ -46,14 +45,6 @@ new Benchmark.Suite("random").add("imagemagick", {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}).add("epeg", {
|
|
||||||
defer: true,
|
|
||||||
fn: function(deferred) {
|
|
||||||
var image = new epeg.Image({path: inputJpg});
|
|
||||||
var buffer = image.downsize(randomDimension(), randomDimension(), 80).process();
|
|
||||||
assert.notStrictEqual(null, buffer);
|
|
||||||
deferred.resolve();
|
|
||||||
}
|
|
||||||
}).add("sharp", {
|
}).add("sharp", {
|
||||||
defer: true,
|
defer: true,
|
||||||
fn: function(deferred) {
|
fn: function(deferred) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user