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": "*" "should": "*"
}, },
"engines": { "engines": {
"node": ">= 0.12.0" "node": ">=0.8.0 <3"
}, },
"main": "./lib/canvas.js", "main": "./lib/canvas.js",
"license": "MIT" "license": "MIT"

12
src/CanvasRenderingContext2d.cc

@ -738,9 +738,21 @@ NAN_METHOD(Context2d::GetImageData) {
uint8_t *dst = (uint8_t *)calloc(1, size); uint8_t *dst = (uint8_t *)calloc(1, size);
NanAdjustExternalMemory(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<ArrayBuffer> buffer = ArrayBuffer::New(Isolate::GetCurrent(), size);
Local<Uint8ClampedArray> clampedArray = Uint8ClampedArray::New(buffer, 0, size); Local<Uint8ClampedArray> clampedArray = Uint8ClampedArray::New(buffer, 0, size);
clampedArray->SetIndexedPropertiesToExternalArrayData(dst, kExternalUint8ClampedArray, size); clampedArray->SetIndexedPropertiesToExternalArrayData(dst, kExternalUint8ClampedArray, size);
#endif
// Normalize data (argb -> rgba) // Normalize data (argb -> rgba)
for (int y = 0; y < sh; ++y) { for (int y = 0; y < sh; ++y) {

23
src/ImageData.cc

@ -37,22 +37,45 @@ ImageData::Initialize(Handle<Object> target) {
NAN_METHOD(ImageData::New) { NAN_METHOD(ImageData::New) {
NanScope(); NanScope();
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
Local<v8::Object> clampedArray;
Local<Object> global = Context::GetCurrent()->Global();
#else
Local<Uint8ClampedArray> clampedArray; Local<Uint8ClampedArray> clampedArray;
#endif
int width; int width;
int height; int height;
if (args[0]->IsUint32() && args[1]->IsUint32()) { if (args[0]->IsUint32() && args[1]->IsUint32()) {
width = args[0]->Uint32Value(); width = args[0]->Uint32Value();
height = args[1]->Uint32Value(); height = args[1]->Uint32Value();
int size = width * height; 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); 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()) { } else if (args[0]->IsUint8ClampedArray() && args[1]->IsUint32()) {
clampedArray = args[0].As<Uint8ClampedArray>(); clampedArray = args[0].As<Uint8ClampedArray>();
#endif
width = args[1]->Uint32Value(); width = args[1]->Uint32Value();
if (args[2]->IsUint32()) { if (args[2]->IsUint32()) {
height = args[2]->Uint32Value(); height = args[2]->Uint32Value();
} else { } else {
#if NODE_MAJOR_VERSION == 0 && NODE_MINOR_VERSION <= 10
height = clampedArray->GetIndexedPropertiesExternalArrayDataLength() / width;
#else
height = clampedArray->Length() / width; height = clampedArray->Length() / width;
#endif
} }
} else { } else {
NanThrowTypeError("Expected (Uint8ClampedArray, width[, height]) or (width, height)"); NanThrowTypeError("Expected (Uint8ClampedArray, width[, height]) or (width, height)");

Loading…
Cancel
Save