Browse Source

Support node <0.12 using kExternalPixelArray.

v1.x
Zach Bjornson 10 years ago
parent
commit
7d9286638f
  1. 2
      package.json
  2. 12
      src/CanvasRenderingContext2d.cc
  3. 23
      src/ImageData.cc

2
package.json

@ -34,7 +34,7 @@
"should": "*"
},
"engines": {
"node": ">= 0.12.0"
"node": ">=0.8.0 <3"
},
"main": "./lib/canvas.js",
"license": "MIT"

12
src/CanvasRenderingContext2d.cc

@ -738,9 +738,21 @@ NAN_METHOD(Context2d::GetImageData) {
uint8_t *dst = (uint8_t *)calloc(1, size);
NanAdjustExternalMemory(size);
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
Local<Object> global = Context::GetCurrent()->Global();
Handle<Value> bufargv[] = { NanNew(size) };
Local<Object> buffer = global->Get(NanNew("ArrayBuffer")).As<Function>()->NewInstance(1, bufargv);
Handle<Value> caargv[] = { buffer, NanNew(0), NanNew(size) };
Local<Object> clampedArray = global->Get(NanNew("Uint8ClampedArray")).As<Function>()->NewInstance(3, caargv);
clampedArray->SetIndexedPropertiesToExternalArrayData(dst, kExternalPixelArray, size);
#else
Local<ArrayBuffer> buffer = ArrayBuffer::New(Isolate::GetCurrent(), size);
Local<Uint8ClampedArray> clampedArray = Uint8ClampedArray::New(buffer, 0, size);
clampedArray->SetIndexedPropertiesToExternalArrayData(dst, kExternalUint8ClampedArray, size);
#endif
// Normalize data (argb -> rgba)
for (int y = 0; y < sh; ++y) {

23
src/ImageData.cc

@ -37,22 +37,45 @@ ImageData::Initialize(Handle<Object> target) {
NAN_METHOD(ImageData::New) {
NanScope();
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
Local<v8::Object> clampedArray;
Local<Object> global = Context::GetCurrent()->Global();
#else
Local<Uint8ClampedArray> clampedArray;
#endif
int width;
int height;
if (args[0]->IsUint32() && args[1]->IsUint32()) {
width = args[0]->Uint32Value();
height = args[1]->Uint32Value();
int size = width * height;
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
Handle<Value> caargv[] = { NanNew(size) };
Local<Object> clampedArray = global->Get(NanNew("Uint8ClampedArray")).As<Function>()->NewInstance(1, caargv);
#else
clampedArray = Uint8ClampedArray::New(ArrayBuffer::New(Isolate::GetCurrent(), size), 0, size);
#endif
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
} else if (args[0]->ToObject()->GetIndexedPropertiesExternalArrayDataType() == kExternalPixelArray && args[1]->IsUint32()) {
clampedArray = args[0]->ToObject();
#else
} else if (args[0]->IsUint8ClampedArray() && args[1]->IsUint32()) {
clampedArray = args[0].As<Uint8ClampedArray>();
#endif
width = args[1]->Uint32Value();
if (args[2]->IsUint32()) {
height = args[2]->Uint32Value();
} else {
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
height = clampedArray->GetIndexedPropertiesExternalArrayDataLength() / width;
#else
height = clampedArray->Length() / width;
#endif
}
} else {
NanThrowTypeError("Expected (Uint8ClampedArray, width[, height]) or (width, height)");

Loading…
Cancel
Save