forked from Automattic/node-canvas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageData.cc
109 lines (88 loc) · 3.07 KB
/
ImageData.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// ImageData.cc
//
// Copyright (c) 2010 LearnBoost <[email protected]>
//
#include "ImageData.h"
#include <iostream>
Persistent<FunctionTemplate> ImageData::constructor;
/*
* Initialize ImageData.
*/
void
ImageData::Initialize(Handle<Object> target) {
NanScope();
// Constructor
Local<FunctionTemplate> ctor = NanNew<FunctionTemplate>(ImageData::New);
NanAssignPersistent(constructor, ctor);
ctor->InstanceTemplate()->SetInternalFieldCount(1);
ctor->SetClassName(NanNew("ImageData"));
// Prototype
Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
proto->SetAccessor(NanNew("width"), GetWidth);
proto->SetAccessor(NanNew("height"), GetHeight);
target->Set(NanNew("ImageData"), ctor->GetFunction());
}
Local<Object> ImageData::NewInstance(uint8_t* data, int width, int height) {
NanEscapableScope();
std::cout << "Received d*, w, h=" << data << "," << width << "," << height << std::endl;
const int argc = 3;
int size = width * height * 4;
Local<ArrayBuffer> buffer = ArrayBuffer::New(Isolate::GetCurrent(), size);
buffer->SetIndexedPropertiesToExternalArrayData(data, ExternalArrayType::kExternalUint8ClampedArray, size);
Local<Uint8ClampedArray> clampedArray = Uint8ClampedArray::New(buffer, 0, size);
Local<Value> argv[argc] = { clampedArray, NanNew(width), NanNew(height) };
Local<FunctionTemplate> cons = NanNew(constructor);
Local<Object> instance = cons->GetFunction()->NewInstance(argc, argv);
return NanEscapeScope(instance);
}
/*
* Initialize a new ImageData object.
*/
NAN_METHOD(ImageData::New) {
NanScope();
Local<Uint8ClampedArray> clampedArray;
int width;
int height;
if (args[0]->IsUint32() && args[1]->IsUint32()) {
std::cout << "path a" << std::endl;
width = args[0]->Uint32Value();
std::cout << "width:" << width << std::endl;
height = args[1]->Uint32Value();
int size = width * height;
clampedArray = Uint8ClampedArray::New(ArrayBuffer::New(Isolate::GetCurrent(), size), 0, size);
} else if (args[0]->IsUint8ClampedArray() && args[1]->IsUint32()) {
std::cout << "path b" << std::endl;
clampedArray = args[0].As<Uint8ClampedArray>();
width = args[1]->Uint32Value();
if (args[2]->IsUint32()) {
height = args[2]->Uint32Value();
} else {
height = clampedArray->Length() / width;
}
} else {
NanThrowTypeError("Expected (Uint8ClampedArray, width[, height]) or (width, height)");
NanReturnUndefined();
}
void *dataPtr = clampedArray->GetIndexedPropertiesExternalArrayData();
ImageData *imageData = new ImageData(reinterpret_cast<uint8_t*>(dataPtr), width, height);
imageData->Wrap(args.This());
args.This()->Set(NanNew("data"), clampedArray);
NanReturnValue(args.This());
}
/*
* Get width.
*/
NAN_GETTER(ImageData::GetWidth) {
NanScope();
ImageData *imageData = ObjectWrap::Unwrap<ImageData>(args.This());
NanReturnValue(NanNew<Number>(imageData->width()));
}
/*
* Get height.
*/
NAN_GETTER(ImageData::GetHeight) {
NanScope();
ImageData *imageData = ObjectWrap::Unwrap<ImageData>(args.This());
NanReturnValue(NanNew<Number>(imageData->height()));
}