forked from scijs/get-pixels
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node-pixels.js
193 lines (180 loc) · 4.28 KB
/
node-pixels.js
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
'use strict'
var ndarray = require('ndarray')
var path = require('path')
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')
var fs = require('fs')
var request = require('request')
var mime = require('mime-types')
var parseDataURI = require('parse-data-uri')
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
try {
reader = new GifReader(data)
} catch(err) {
cb(err)
return
}
if(reader.numFrames() > 0) {
var nshape = [reader.numFrames(), reader.height, reader.width, 4]
try {
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2] * nshape[3])
} catch(err) {
cb(err)
return
}
var 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 {
var nshape = [reader.height, reader.width, 4]
var ndata = new Uint8Array(nshape[0] * nshape[1] * nshape[2])
var 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(mimeType, data, cb) {
switch(mimeType) {
case 'image/png':
handlePNG(data, cb)
break
case 'image/jpg':
case 'image/jpeg':
handleJPEG(data, cb)
break
case 'image/gif':
handleGIF(data, cb)
break
case 'image/bmp':
handleBMP(data, cb)
break
default:
cb(new Error("Unsupported file type: " + mimeType))
}
}
module.exports = function getPixels(url, type, cb) {
if(!cb) {
cb = type
type = ''
}
if(Buffer.isBuffer(url)) {
if(!type) {
cb(new Error('Invalid file type'))
return
}
doParse(type, url, cb)
} else if(url.indexOf('data:') === 0) {
try {
var buffer = parseDataURI(url)
if(buffer) {
process.nextTick(function() {
doParse(type || buffer.mimeType, buffer.data, cb)
})
} else {
process.nextTick(function() {
cb(new Error('Error parsing data URI'))
})
}
} catch(err) {
process.nextTick(function() {
cb(err)
})
}
} else if(url.indexOf('http://') === 0 || url.indexOf('https://') === 0) {
request({url:url, encoding:null}, function(err, response, body) {
if(err) {
cb(err)
return
}
type = type;
if(!type){
if(response.getHeader !== undefined){
type = response.getHeader('content-type');
}else if(response.headers !== undefined){
type = response.headers['content-type'];
}
}
if(!type) {
cb(new Error('Invalid content-type'))
return
}
doParse(type, body, cb)
})
} else {
fs.readFile(url, function(err, data) {
if(err) {
cb(err)
return
}
type = type || mime.lookup(url)
if(!type) {
cb(new Error('Invalid file type'))
return
}
doParse(type, data, cb)
})
}
}