Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"pretest": "standard examples/*.js test/server.js test/public/*.js benchmarks/run.js lib/context2d.js util/has_lib.js browser.js index.js && node-gyp build",
"test": "mocha test/*.test.js",
"pretest-server": "node-gyp build",
"test-server": "node test/server.js",
"install": "node-pre-gyp install --fallback-to-build"
"test-server": "node test/server.js"
},
"binary": {
"module_name": "canvas",
Expand Down
20 changes: 20 additions & 0 deletions src/Image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Image::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
SetProtoAccessor(proto, Nan::New("naturalWidth").ToLocalChecked(), GetNaturalWidth, NULL, ctor);
SetProtoAccessor(proto, Nan::New("naturalHeight").ToLocalChecked(), GetNaturalHeight, NULL, ctor);
SetProtoAccessor(proto, Nan::New("dataMode").ToLocalChecked(), GetDataMode, SetDataMode, ctor);
SetProtoAccessor(proto, Nan::New("rawData").ToLocalChecked(), GetRawData, NULL, ctor);

ctor->Set(Nan::New("MODE_IMAGE").ToLocalChecked(), Nan::New<Number>(DATA_IMAGE));
ctor->Set(Nan::New("MODE_MIME").ToLocalChecked(), Nan::New<Number>(DATA_MIME));
Expand Down Expand Up @@ -185,6 +186,25 @@ NAN_METHOD(Image::GetSource){
info.GetReturnValue().Set(Nan::New<String>(img->filename ? img->filename : "").ToLocalChecked());
}

/*
* Get raw data.
*/
NAN_GETTER(Image::GetRawData) {
Image *img = Nan::ObjectWrap::Unwrap<Image>(info.This());
if (img->_surface) {
// Return raw ARGB data -- just a memcpy()
cairo_surface_t *surface = img->_surface;
cairo_surface_flush(surface);
const unsigned char *data = cairo_image_surface_get_data(surface);
unsigned int nBytes = cairo_image_surface_get_stride(surface) * img->height;
Local<Object> buf = Nan::CopyBuffer(reinterpret_cast<const char*>(data), nBytes).ToLocalChecked();
info.GetReturnValue().Set(buf);
} else {
info.GetReturnValue().Set(Nan::Undefined());
}
return;
}

/*
* Clean up assets and variables.
*/
Expand Down
1 change: 1 addition & 0 deletions src/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Image: public Nan::ObjectWrap {
static NAN_SETTER(SetHeight);
static NAN_METHOD(GetSource);
static NAN_METHOD(SetSource);
static NAN_GETTER(GetRawData);
inline uint8_t *data(){ return cairo_image_surface_get_data(_surface); }
inline int stride(){ return cairo_image_surface_get_stride(_surface); }
static int isPNG(uint8_t *data);
Expand Down
8 changes: 8 additions & 0 deletions test/image.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ describe('Image', function () {
})
})

it('returns raw image data', function() {
const img = new Image();
img.src = png_clock;
const buf = img.rawData;
assert.ok(buf instanceof Buffer);
assert.strictEqual(buf.length, 409600);
})

it('calls Image#onload multiple times', function () {
return loadImage(png_clock).then((img) => {
let onloadCalled = 0
Expand Down