1 |
|
2 |
|
3 | #include "ImageData.h"
|
4 |
|
5 | #include "Util.h"
|
6 |
|
7 | using namespace v8;
|
8 |
|
9 | Nan::Persistent<FunctionTemplate> ImageData::constructor;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | void
|
16 | ImageData::Initialize(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target) {
|
17 | Nan::HandleScope scope;
|
18 |
|
19 |
|
20 | Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(ImageData::New);
|
21 | constructor.Reset(ctor);
|
22 | ctor->InstanceTemplate()->SetInternalFieldCount(1);
|
23 | ctor->SetClassName(Nan::New("ImageData").ToLocalChecked());
|
24 |
|
25 |
|
26 | Local<ObjectTemplate> proto = ctor->PrototypeTemplate();
|
27 | SetProtoAccessor(proto, Nan::New("width").ToLocalChecked(), GetWidth, NULL, ctor);
|
28 | SetProtoAccessor(proto, Nan::New("height").ToLocalChecked(), GetHeight, NULL, ctor);
|
29 | Local<Context> ctx = Nan::GetCurrentContext();
|
30 | Nan::Set(target, Nan::New("ImageData").ToLocalChecked(), ctor->GetFunction(ctx).ToLocalChecked());
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | NAN_METHOD(ImageData::New) {
|
38 | if (!info.IsConstructCall()) {
|
39 | return Nan::ThrowTypeError("Class constructors cannot be invoked without 'new'");
|
40 | }
|
41 |
|
42 | Local<TypedArray> dataArray;
|
43 | uint32_t width;
|
44 | uint32_t height;
|
45 | int length;
|
46 |
|
47 | if (info[0]->IsUint32() && info[1]->IsUint32()) {
|
48 | width = Nan::To<uint32_t>(info[0]).FromMaybe(0);
|
49 | if (width == 0) {
|
50 | Nan::ThrowRangeError("The source width is zero.");
|
51 | return;
|
52 | }
|
53 | height = Nan::To<uint32_t>(info[1]).FromMaybe(0);
|
54 | if (height == 0) {
|
55 | Nan::ThrowRangeError("The source height is zero.");
|
56 | return;
|
57 | }
|
58 | length = width * height * 4;
|
59 |
|
60 | dataArray = Uint8ClampedArray::New(ArrayBuffer::New(Isolate::GetCurrent(), length), 0, length);
|
61 |
|
62 | } else if (info[0]->IsUint8ClampedArray() && info[1]->IsUint32()) {
|
63 | dataArray = info[0].As<Uint8ClampedArray>();
|
64 |
|
65 | length = dataArray->Length();
|
66 | if (length == 0) {
|
67 | Nan::ThrowRangeError("The input data has a zero byte length.");
|
68 | return;
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | width = Nan::To<uint32_t>(info[1]).FromMaybe(0);
|
75 | if (width == 0) {
|
76 | Nan::ThrowRangeError("The source width is zero.");
|
77 | return;
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 | if (info[2]->IsUint32()) {
|
83 | height = Nan::To<uint32_t>(info[2]).FromMaybe(0);
|
84 | } else {
|
85 | int size = length / 4;
|
86 | height = size / width;
|
87 | }
|
88 |
|
89 | } else if (info[0]->IsUint16Array() && info[1]->IsUint32()) {
|
90 | dataArray = info[0].As<Uint16Array>();
|
91 |
|
92 | length = dataArray->Length();
|
93 | if (length == 0) {
|
94 | Nan::ThrowRangeError("The input data has a zero byte length.");
|
95 | return;
|
96 | }
|
97 |
|
98 | width = Nan::To<uint32_t>(info[1]).FromMaybe(0);
|
99 | if (width == 0) {
|
100 | Nan::ThrowRangeError("The source width is zero.");
|
101 | return;
|
102 | }
|
103 |
|
104 | if (info[2]->IsUint32()) {
|
105 | height = Nan::To<uint32_t>(info[2]).FromMaybe(0);
|
106 | } else {
|
107 | int size = length / 2;
|
108 | height = size / width;
|
109 | }
|
110 |
|
111 | } else {
|
112 | Nan::ThrowTypeError("Expected (Uint8ClampedArray, width[, height]), (Uint16Array, width[, height]) or (width, height)");
|
113 | return;
|
114 | }
|
115 |
|
116 | Nan::TypedArrayContents<uint8_t> dataPtr(dataArray);
|
117 |
|
118 | ImageData *imageData = new ImageData(reinterpret_cast<uint8_t*>(*dataPtr), width, height);
|
119 | imageData->Wrap(info.This());
|
120 | Nan::Set(info.This(), Nan::New("data").ToLocalChecked(), dataArray).Check();
|
121 | info.GetReturnValue().Set(info.This());
|
122 | }
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | NAN_GETTER(ImageData::GetWidth) {
|
129 | ImageData *imageData = Nan::ObjectWrap::Unwrap<ImageData>(info.This());
|
130 | info.GetReturnValue().Set(Nan::New<Number>(imageData->width()));
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | NAN_GETTER(ImageData::GetHeight) {
|
138 | ImageData *imageData = Nan::ObjectWrap::Unwrap<ImageData>(info.This());
|
139 | info.GetReturnValue().Set(Nan::New<Number>(imageData->height()));
|
140 | }
|