Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
243 lines (229 loc) · 7.62 KB
/
Copy pathindex.js
File metadata and controls
243 lines (229 loc) · 7.62 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var pako = require("pako");
var fs = require("fs");
var https = require("https");
module.exports.decode = decode;
module.exports.mean = mean;
/**
* @typedef {Object} imageData
* @property {number} width
* @property {number} height
* @property {number} depth
* @property {number} color
* @property {[number, number, number, number][]} data
*
*/
/**
* Downloads or reads the png file and parses the average color
* of the image
* @param {String} url HTTPS url or file path to a png
* @returns {Promise<[number, number, number, number]>}
* If successful, returns an `array` of 4 `numbers` in RGBA format
*
* ```js
* var imageDecoder = require("@nicklasbns/imagedecoder");
*
* let meanColor = await imageDecoder.mean(url).catch((e) => {
* console.log("Error:", e);
* }) || [0, 0, 0, 0]; //if the image is invalid we instead use a deafult color
*
* console.log(meanColor) //[168, 87, 115, 255]
* ```
*/
async function mean(url) {
let image = await decode(url)
// console.log(image);
var totals = [0, 0, 0, 0];
image.data.forEach(pixel => {
for (let i in pixel) {
totals[i] += pixel[i];
}
});
for (var i = 0; i < 4; i++) {
totals[i] = Math.round(totals[i]/image.data.length);
}
return totals
}
/**
* Downloads or reads the png file and if successful, returns {@link imageData}
* @param {String} url HTTPS url or file path to a png
* @returns {Promise<imageData>} Object of type `imageData`
* @example
* ```js
* var imageDecoder = require("@nicklasbns/imagedecoder");
*
* let image = await imageDecoder.decode(url).catch((e) => {
* console.log("Error:", e);
* });
*
* console.log(image)
* ```
*
* example output:
* ```js
* {
* width: 200,
* height: 200,
* depth: 8,
* color: 6,
* data: [[128, 128, 128, 255], ...]
* }
* ```
*
*/
async function decode(url) {
return new Promise(async (respond, reject) => {
var png = await download(url).catch((e) => {
reject(e);
});
if (!png) return
var imageData = {};
var base = [], pixels = [];
if (png.subarray(0, 8).join() != "137,80,78,71,13,10,26,10") {
reject(new Error("This file is not a vaild png"));
return
}
var i = 8;
while (i < png["byteLength"]) {
var length = read32(png, i);
switch (String.fromCharCode(...png.subarray(i+4, i+8))) {
case "IHDR":
imageData = {
width: read32(png, i+8),
height: read32(png, i+12),
depth: png[i+16],
color: png[i+17], // 1: palette, 2: color, 4: alpha
}
// console.log(imageData);
if (png[i+20] == 1) {
reject(new Error("This image is fucking interlaced you stoopid"));
return
}
if (imageData.color == 6) {
imageData.channels = 4;
} else if (imageData.color == 2) {
imageData.channels = 3;
} else if (imageData.color == 3) {
reject(new Error("This image uses a palette, i have not added support for this yet:tm: (dm me nicklasbns#8693)"));
} else {
reject(new Error("Unsupported color-type: " + imageData.color + ". (dm me nicklasbns#8693 if you want support for this color type)"));
return
}
break
case "PLTE":
break
case "IDAT":
base.push(...png.subarray(i+8, i+8+length));
break
case "IEND":
var baseData = pako.inflate(base);
pixels = decodeIDAT(baseData, imageData.width*imageData.depth*imageData.channels/8, imageData.height, imageData.channels, reject);
break
default:
// console.log(String.fromCharCode(...png.subarray(i+4, i+8)));
}
i += length+12;
}
var pixels2 = []
for (let i = 0; i < pixels.length/imageData.channels; i++) {
pixels2[i] = []
for (let j = 0; j < imageData.channels; j++) {
pixels2[i][j] = pixels[i*imageData.channels+j];
}
}
imageData.data = pixels2
respond(imageData);
});
}
function decodeIDAT(data, length, height, channels, reject) {
var bytes = []
for (let j = 0; j < height; j++) {
let scanline = new Uint8Array(data.subarray(j*(length+1)+1, (j+1)*(length+1)));
let start = j*(length);
switch (data[j*(length+1)]) {
case 0:
for (let k = 0; k < length; k++) {
bytes.push(scanline[k]%256);
}
break
case 1:
for (let k = 0; k < length; k++) {
let a = k>channels-1 ? bytes[start+k-channels] : 0;
bytes.push((a+scanline[k])%256);
}
break
case 2:
for (let k = 0; k < length; k++) {
let b = j ? bytes[start+k-length] : 0;
bytes.push((b+scanline[k])%256);
}
break
case 3:
for (let k = 0; k < length; k++) {
let a = k>channels-1 ? bytes[start+k-channels] : 0;
let b = j ? bytes[start+k-length] : 0;
let avg = Math.floor((a+b)/2);
bytes.push((avg+scanline[k])%256);
}
break
case 4:
for (let k = 0; k < length; k++) {
let abs = Math.abs;
let a = k>channels-1 ? bytes[start+k-channels] : 0;
let b = j ? bytes[start+k-length] : 0;
let c = k>channels-1&&j ? bytes[start+k-length-channels] : 0;
let p = a + b - c
if (abs(p-a) > abs(p-b)) {
if (abs(p-b) > abs(p-c)) {
bytes.push((c+scanline[k])%256);
} else {
bytes.push((b+scanline[k])%256);
}
} else {
if (abs(p-a) > abs(p-c)) {
bytes.push((c+scanline[k])%256);
} else {
bytes.push((a+scanline[k])%256);
}
}
}
break
default:
reject(new Error(`Unsupported filter(${data[j*(length+1)]}) on ${j}th scanline.
This should never happen since all fliters should be supported. dm me nicklasbns#8693`));
return []
}
}
return bytes
}
async function download(url) {
return new Promise((res, reject) => {
try {
if (url.split(":")[0] == "https") {
https.get(url, {} , async data => {
let array = new Uint8Array();
data.on("data", data => {
array = new Uint8Array([...array, ...data]);
});
data.on("end", () => {res(array)});
}).on("error", e => {
reject(e);
});
} else if (url.split(":")[0] == "http") {
reject(new Error("Does not support http"));
} else {
fs.readFile(url, {}, (err, data) => {
if (err) reject(err);
res(data);
})
}
} catch(e) {
reject(e);
}
});
}
function read32(arr, int) {
return (arr[int++] << 24) |
(arr[int++] << 16) |
(arr[int++] << 08) |
arr[int++]
}