mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
0.11 compat using nan
This commit is contained in:
parent
6a2816e917
commit
e96fd8b9de
@ -10,7 +10,8 @@
|
||||
'/usr/local/lib/glib-2.0/include',
|
||||
'/usr/include/glib-2.0',
|
||||
'/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_cc': ['-fexceptions', '-pedantic', '-Wall', '-O3']
|
||||
|
@ -29,12 +29,14 @@
|
||||
"devDependencies": {
|
||||
"imagemagick": "*",
|
||||
"gm": "*",
|
||||
"epeg": "*",
|
||||
"async": "*",
|
||||
"benchmark": "*"
|
||||
},
|
||||
"license": "Apache 2.0",
|
||||
"engines": {
|
||||
"node": ">=0.8"
|
||||
},
|
||||
"dependencies": {
|
||||
"nan": "^0.8.0"
|
||||
}
|
||||
}
|
58
src/sharp.cc
58
src/sharp.cc
@ -5,6 +5,8 @@
|
||||
#include <string.h>
|
||||
#include <vips/vips.h>
|
||||
|
||||
#include "nan.h"
|
||||
|
||||
using namespace v8;
|
||||
using namespace node;
|
||||
|
||||
@ -23,7 +25,6 @@ struct resize_baton {
|
||||
bool progessive;
|
||||
VipsAccess access_method;
|
||||
std::string err;
|
||||
Persistent<Function> callback;
|
||||
|
||||
resize_baton(): buffer_in_len(0), buffer_out_len(0) {}
|
||||
};
|
||||
@ -67,9 +68,13 @@ void resize_error(resize_baton *baton, VipsImage *unref) {
|
||||
return;
|
||||
}
|
||||
|
||||
void resize_async(uv_work_t *work) {
|
||||
resize_baton* baton = static_cast<resize_baton*>(work->data);
|
||||
class ResizeWorker : public NanAsyncWorker {
|
||||
public:
|
||||
ResizeWorker(NanCallback *callback, resize_baton *baton)
|
||||
: NanAsyncWorker(callback), baton(baton) {}
|
||||
~ResizeWorker() {}
|
||||
|
||||
void Execute () {
|
||||
// Input
|
||||
ImageType inputImageType = JPEG;
|
||||
VipsImage *in = vips_image_new();
|
||||
@ -286,34 +291,28 @@ void resize_async(uv_work_t *work) {
|
||||
vips_thread_shutdown();
|
||||
}
|
||||
|
||||
void resize_async_after(uv_work_t *work, int status) {
|
||||
HandleScope scope;
|
||||
|
||||
resize_baton *baton = static_cast<resize_baton*>(work->data);
|
||||
void HandleOKCallback () {
|
||||
NanScope();
|
||||
|
||||
Handle<Value> argv[2] = { Null(), Null() };
|
||||
if (!baton->err.empty()) {
|
||||
// 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) {
|
||||
// Buffer
|
||||
Buffer *slowBuffer = Buffer::New(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));
|
||||
argv[1] = NanNewBufferHandle((char *)baton->buffer_out, baton->buffer_out_len);
|
||||
g_free(baton->buffer_out);
|
||||
}
|
||||
|
||||
baton->callback->Call(Context::GetCurrent()->Global(), 2, argv);
|
||||
baton->callback.Dispose();
|
||||
delete baton;
|
||||
delete work;
|
||||
callback->Call(2, argv);
|
||||
}
|
||||
|
||||
Handle<Value> resize(const Arguments& args) {
|
||||
HandleScope scope;
|
||||
private:
|
||||
resize_baton* baton;
|
||||
};
|
||||
|
||||
NAN_METHOD(resize) {
|
||||
NanScope();
|
||||
|
||||
resize_baton *baton = new resize_baton;
|
||||
baton->file_in = *String::Utf8Value(args[0]->ToString());
|
||||
@ -338,16 +337,15 @@ Handle<Value> resize(const Arguments& args) {
|
||||
baton->sharpen = args[6]->BooleanValue();
|
||||
baton->progessive = args[7]->BooleanValue();
|
||||
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;
|
||||
work->data = baton;
|
||||
uv_queue_work(uv_default_loop(), work, resize_async, (uv_after_work_cb)resize_async_after);
|
||||
return scope.Close(Undefined());
|
||||
NanCallback *callback = new NanCallback(args[9].As<v8::Function>());
|
||||
|
||||
NanAsyncQueueWorker(new ResizeWorker(callback, baton));
|
||||
NanReturnUndefined();
|
||||
}
|
||||
|
||||
Handle<Value> cache(const Arguments& args) {
|
||||
HandleScope scope;
|
||||
NAN_METHOD(cache) {
|
||||
NanScope();
|
||||
|
||||
// Set cache limit
|
||||
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("high"), Number::New(vips_tracked_get_mem_highwater() / 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) {
|
||||
HandleScope scope;
|
||||
NanScope();
|
||||
vips_shutdown();
|
||||
}
|
||||
|
||||
extern "C" void init(Handle<Object> target) {
|
||||
HandleScope scope;
|
||||
NanScope();
|
||||
vips_init("");
|
||||
AtExit(at_exit);
|
||||
NODE_SET_METHOD(target, "resize", resize);
|
||||
|
@ -2,7 +2,7 @@ var sharp = require("../index");
|
||||
var fs = require("fs");
|
||||
var imagemagick = require("imagemagick");
|
||||
var gm = require("gm");
|
||||
var epeg = require("epeg");
|
||||
//var epeg = require("epeg");
|
||||
var async = require("async");
|
||||
var assert = require("assert");
|
||||
var Benchmark = require("benchmark");
|
||||
@ -65,7 +65,8 @@ async.series({
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("epeg-file-file", {
|
||||
})
|
||||
/*.add("epeg-file-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
var image = new epeg.Image({path: inputJpg});
|
||||
@ -80,7 +81,8 @@ async.series({
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
}).add("sharp-buffer-file", {
|
||||
})*/
|
||||
.add("sharp-buffer-file", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpgBuffer).resize(width, height).write(outputJpg, function(err) {
|
||||
|
@ -2,7 +2,7 @@ var sharp = require("../index");
|
||||
var fs = require("fs");
|
||||
var imagemagick = require("imagemagick");
|
||||
var gm = require("gm");
|
||||
var epeg = require("epeg");
|
||||
//var epeg = require("epeg");
|
||||
var async = require("async");
|
||||
var assert = require("assert");
|
||||
var Benchmark = require("benchmark");
|
||||
@ -46,7 +46,7 @@ new Benchmark.Suite("random").add("imagemagick", {
|
||||
}
|
||||
});
|
||||
}
|
||||
}).add("epeg", {
|
||||
})/*.add("epeg", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
var image = new epeg.Image({path: inputJpg});
|
||||
@ -54,7 +54,7 @@ new Benchmark.Suite("random").add("imagemagick", {
|
||||
assert.notStrictEqual(null, buffer);
|
||||
deferred.resolve();
|
||||
}
|
||||
}).add("sharp", {
|
||||
})*/.add("sharp", {
|
||||
defer: true,
|
||||
fn: function(deferred) {
|
||||
sharp(inputJpg).resize(randomDimension(), randomDimension()).toBuffer(function(err, buffer) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user