-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathImageData.cc
132 lines (108 loc) · 3.79 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
#include "ImageData.h"
#include "InstanceData.h"
/*
* Initialize ImageData.
*/
void
ImageData::Initialize(Napi::Env& env, Napi::Object& exports) {
Napi::HandleScope scope(env);
InstanceData *data = env.GetInstanceData<InstanceData>();
Napi::Function ctor = DefineClass(env, "ImageData", {
InstanceAccessor<&ImageData::GetWidth>("width", napi_default_jsproperty),
InstanceAccessor<&ImageData::GetHeight>("height", napi_default_jsproperty)
});
exports.Set("ImageData", ctor);
data->ImageDataCtor = Napi::Persistent(ctor);
}
/*
* Initialize a new ImageData object.
*/
ImageData::ImageData(const Napi::CallbackInfo& info) : Napi::ObjectWrap<ImageData>(info), env(info.Env()) {
Napi::TypedArray dataArray;
uint32_t width;
uint32_t height;
int length;
if (info[0].IsNumber() && info[1].IsNumber()) {
width = info[0].As<Napi::Number>().Uint32Value();
if (width == 0) {
Napi::RangeError::New(env, "The source width is zero.").ThrowAsJavaScriptException();
return;
}
height = info[1].As<Napi::Number>().Uint32Value();
if (height == 0) {
Napi::RangeError::New(env, "The source height is zero.").ThrowAsJavaScriptException();
return;
}
length = width * height * 4; // ImageData(w, h) constructor assumes 4 BPP; documented.
dataArray = Napi::Uint8Array::New(env, length, napi_uint8_clamped_array);
} else if (
info[0].IsTypedArray() &&
info[0].As<Napi::TypedArray>().TypedArrayType() == napi_uint8_clamped_array &&
info[1].IsNumber()
) {
dataArray = info[0].As<Napi::Uint8Array>();
length = dataArray.ElementLength();
if (length == 0) {
Napi::RangeError::New(env, "The input data has a zero byte length.").ThrowAsJavaScriptException();
return;
}
// Don't assert that the ImageData length is a multiple of four because some
// data formats are not 4 BPP.
width = info[1].As<Napi::Number>().Uint32Value();
if (width == 0) {
Napi::RangeError::New(env, "The source width is zero.").ThrowAsJavaScriptException();
return;
}
// Don't assert that the byte length is a multiple of 4 * width, ditto.
if (info[2].IsNumber()) { // Explicit height given
height = info[2].As<Napi::Number>().Uint32Value();
} else { // Calculate height assuming 4 BPP
int size = length / 4;
height = size / width;
}
} else if (
info[0].IsTypedArray() &&
info[0].As<Napi::TypedArray>().TypedArrayType() == napi_uint16_array &&
info[1].IsNumber()
) { // Intended for RGB16_565 format
dataArray = info[0].As<Napi::TypedArray>();
length = dataArray.ElementLength();
if (length == 0) {
Napi::RangeError::New(env, "The input data has a zero byte length.").ThrowAsJavaScriptException();
return;
}
width = info[1].As<Napi::Number>().Uint32Value();
if (width == 0) {
Napi::RangeError::New(env, "The source width is zero.").ThrowAsJavaScriptException();
return;
}
if (info[2].IsNumber()) { // Explicit height given
height = info[2].As<Napi::Number>().Uint32Value();
} else { // Calculate height assuming 2 BPP
int size = length / 2;
height = size / width;
}
} else {
Napi::TypeError::New(env, "Expected (Uint8ClampedArray, width[, height]), (Uint16Array, width[, height]) or (width, height)").ThrowAsJavaScriptException();
return;
}
_width = width;
_height = height;
_data = dataArray.As<Napi::Uint8Array>().Data();
info.This().As<Napi::Object>().Set("data", dataArray);
}
/*
* Get width.
*/
Napi::Value
ImageData::GetWidth(const Napi::CallbackInfo& info) {
return Napi::Number::New(env, width());
}
/*
* Get height.
*/
Napi::Value
ImageData::GetHeight(const Napi::CallbackInfo& info) {
return Napi::Number::New(env, height());
}