forked from cronvel/get-pixels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode-pixels.js
More file actions
143 lines (129 loc) · 3.21 KB
/
node-pixels.js
File metadata and controls
143 lines (129 loc) · 3.21 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
'use strict'
var ndarray = require('ndarray')
var PNG = require('pngjs').PNG
var jpeg = require('jpeg-js')
var pack = require('ndarray-pack')
var GifReader = require('omggif').GifReader
var Bitmap = require('node-bitmap')
function handlePNG(data, cb) {
var png = new PNG();
png.parse(data, function(err, img_data) {
if(err) {
cb(err)
return
}
cb(null, ndarray(new Uint8Array(img_data.data),
[img_data.width|0, img_data.height|0, 4],
[4, 4*img_data.width|0, 1],
0))
})
}
function handleJPEG(data, cb) {
var jpegData
try {
jpegData = jpeg.decode(data)
}
catch(e) {
cb(e)
return
}
if(!jpegData) {
cb(new Error("Error decoding jpeg"))
return
}
var nshape = [ jpegData.height, jpegData.width, 4 ]
var result = ndarray(jpegData.data, nshape)
cb(null, result.transpose(1,0))
}
function handleGIF(data, cb) {
var reader, nshape, ndata, result
try {
reader = new GifReader(data)
} catch(err) {
cb(err)
return
}
if(reader.numFrames() > 0) {
nshape = [reader.numFrames(), reader.height, reader.width, 4]
ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3])
result = ndarray(ndata, nshape)
try {
for(var i=0; i<reader.numFrames(); ++i) {
reader.decodeAndBlitFrameRGBA(i, ndata.subarray(
result.index(i, 0, 0, 0),
result.index(i+1, 0, 0, 0)))
}
} catch(err) {
cb(err)
return
}
cb(null, result.transpose(0,2,1))
} else {
nshape = [reader.height, reader.width, 4]
ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
result = ndarray(ndata, nshape)
try {
reader.decodeAndBlitFrameRGBA(0, ndata)
} catch(err) {
cb(err)
return
}
cb(null, result.transpose(1,0))
}
}
function handleBMP(data, cb) {
var bmp = new Bitmap(data)
try {
bmp.init()
} catch(e) {
cb(e)
return
}
var bmpData = bmp.getData()
var nshape = [ bmpData.getHeight(), bmpData.getWidth(), 4 ]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
var result = ndarray(ndata, nshape)
pack(bmpData, result)
cb(null, result.transpose(1,0))
}
function doParse(type, data, cb) {
switch(type) {
case 'png':
handlePNG(data, cb)
break
case 'jpg':
case 'jpeg':
handleJPEG(data, cb)
break
case 'gif':
handleGIF(data, cb)
break
case 'bmp':
handleBMP(data, cb)
break
default:
cb(new Error("Unsupported file type: " + type))
}
}
module.exports = function getPixels(buf) {
function compare(a, b) {
for(let i in a) {
if (a[i] !== b[i]) return false;
}
return true;
}
return new Promise( (resolve, reject) => {
var type;
if(compare([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a], buf))
type = 'png';
else if(compare([0xff, 0xe0], buf) && compare([0x4a, 0x46, 0x49, 0x46, 0x00], buf.slice(6)))
type = 'jpg';
else if(compare([0x47, 0x49, 0x46, 0x38]))
type = 'gif';
else if(compare([0x42, 0x4d], buf))
type = 'bmp';
else
return reject(new Error('Unknown file type'));
doParse(type, buf, (err, d) => err ? reject(err) : resolve(d));
})
}