/
thmclrx.cc
173 lines (141 loc) · 5.21 KB
/
thmclrx.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
*
* ▄▄▄▄▀ ▄ █ █▀▄▀█ ▄█▄ █ █▄▄▄▄ ▄ ,ae,
* ▀▀▀ █ █ █ █ █ █ █▀ ▀▄ █ █ ▄▀ ▀▄ █ ,88888e
* █ ██▀▀█ █ ▄ █ █ ▀ █ █▀▀▌ █ ▀ ,a888b.9888888i
* █ █ █ █ █ █▄ ▄▀ ███▄ █ █ ▄ █ 888888888888888
* ▀ █ █ ▀███▀ ▀ █ █ ▀▄ 88888888888888Y
* ▀ ▀ ▀ ▀ '8888888888888'
* "S888888888"
* "7888888Y
* ThmclrX - A theme color extractor module for Node.js. "e88j
* "Y
* Copyright (c) 2022 XadillaX <i@2333.moe>
*
* MIT LIcense <https://github.com/XadillaX/thmclrx/blob/master/LICENSE>
*/
#include <byakuren/byakuren.h>
#include <nan.h>
#include "common.h"
namespace __thmclrx__ {
using v8::Array;
using v8::Context;
using v8::Local;
using v8::Uint32;
NAN_METHOD(GetByOctree) {
if (info.Length() < 1) {
return Nan::ThrowError("Wrong number of arguments.");
}
if (!info[0]->IsArray()) {
return Nan::ThrowTypeError("The argument should be an array.");
}
Local<Array> rgb_array = info[0].As<Array>();
PicturePixels pixels;
if (!PicturePixels::CreateFromV8(rgb_array, &pixels)) {
return Nan::ThrowTypeError("Failed to recognize pixels.");
}
size_t max_color = 256;
if (info.Length() > 1 && info[1]->IsUint32()) {
max_color = Nan::To<Uint32>(info[1]).ToLocalChecked()->Value();
}
bkr_octree_node* root =
bkr_build_octree(pixels.colors, pixels.count, max_color);
bkr_color_stats* stats = reinterpret_cast<bkr_color_stats*>(
malloc(sizeof(bkr_color_stats) * max_color));
size_t color_count = bkr_octree_calculate_color_stats(root, stats);
bkr_release_octree(root);
PicturePixels::SafeDestroyInner(&pixels);
info.GetReturnValue().Set(StatsToV8(stats, color_count));
free(stats);
}
NAN_METHOD(GetByMinDiff) {
if (info.Length() < 1) {
return Nan::ThrowError("Wrong number of arguments.");
}
if (!info[0]->IsArray()) {
return Nan::ThrowTypeError("The argument should be an array.");
}
Local<Array> rgb_array = info[0].As<Array>();
PicturePixels pixels;
if (!PicturePixels::CreateFromV8(rgb_array, &pixels)) {
return Nan::ThrowTypeError("Failed to recognize pixels.");
}
Palette palette(0, nullptr);
if (info.Length() != 1) {
Palette::CreateFromV8(info[1], &palette);
} else {
palette = *Palette::GetDefaultPalette();
}
bkr_color_stats* stats = reinterpret_cast<bkr_color_stats*>(
malloc(sizeof(bkr_color_stats) * palette.count));
bkr_mindiff_parameter param;
param.gray_offset = 5;
param.palette = &palette;
size_t color_count = bkr_mindiff_calculate_color_stats(
pixels.colors, pixels.count, stats, ¶m);
PicturePixels::SafeDestroyInner(&pixels);
Palette::SafeDestroyInner(&palette);
info.GetReturnValue().Set(StatsToV8(stats, color_count));
free(stats);
}
NAN_METHOD(GetByMixed) {
if (info.Length() < 1) {
return Nan::ThrowError("Wrong number of arguments.");
}
if (!info[0]->IsArray()) {
return Nan::ThrowTypeError("The argument should be an array.");
}
Local<Array> rgb_array = info[0].As<Array>();
PicturePixels pixels;
if (!PicturePixels::CreateFromV8(rgb_array, &pixels)) {
return Nan::ThrowTypeError("Failed to recognize pixels.");
}
size_t max_color = 256;
if (info.Length() > 1 && info[1]->IsUint32()) {
max_color = Nan::To<Uint32>(info[1]).ToLocalChecked()->Value();
}
Palette palette(0, nullptr);
if (info.Length() >= 3) {
Palette::CreateFromV8(info[2], &palette);
} else {
palette = *Palette::GetDefaultPalette();
}
bkr_color_stats* stats = reinterpret_cast<bkr_color_stats*>(
malloc(sizeof(bkr_color_stats) * palette.count));
bkr_mindiff_parameter param;
param.gray_offset = 5;
param.palette = &palette;
size_t color_count = bkr_mix_calculate_color_stats(
pixels.colors, pixels.count, max_color, ¶m, stats);
PicturePixels::SafeDestroyInner(&pixels);
Palette::SafeDestroyInner(&palette);
info.GetReturnValue().Set(StatsToV8(stats, color_count));
free(stats);
}
NAN_MODULE_INIT(Init) {
bkr_init();
Local<Context> context = target->GetIsolate()->GetCurrentContext();
// Set the function of
// "Getting by octree"
Nan::Set(target,
Nan::New("getByOctree").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(GetByOctree)
->GetFunction(context)
.ToLocalChecked());
// Set the function of
// "Getting by mindiff"
Nan::Set(target,
Nan::New("getByMinDiff").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(GetByMinDiff)
->GetFunction(context)
.ToLocalChecked());
// Set the function of
// "Getting by mixed"
Nan::Set(target,
Nan::New("getByMixed").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(GetByMixed)
->GetFunction(context)
.ToLocalChecked());
}
} // namespace __thmclrx__
NODE_MODULE(thmclrx, __thmclrx__::Init);