forked from scijs/save-pixels
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save-pixels.js
142 lines (132 loc) · 3.89 KB
/
save-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
'use strict'
var ContentStream = require('contentstream')
var GifEncoder = require('flav-gif-encoder')
var jpegJs = require('jpeg-js')
var PNG = require('pngjs-nozlib').PNG
var ndarray = require('ndarray')
var ops = require('ndarray-ops')
var through = require('through')
function handleData (array, data, frame) {
var i, j, ptr = 0, c
if (array.shape.length === 4) {
return handleData(array.pick(frame), data, 0)
} else if (array.shape.length === 3) {
if (array.shape[2] === 3) {
ops.assign(
ndarray(data,
[array.shape[0], array.shape[1], 3],
[4, 4 * array.shape[0], 1]),
array)
ops.assigns(
ndarray(data,
[array.shape[0] * array.shape[1]],
[4],
3),
255)
} else if (array.shape[2] === 4) {
ops.assign(
ndarray(data,
[array.shape[0], array.shape[1], 4],
[4, array.shape[0] * 4, 1]),
array)
} else if (array.shape[2] === 1) {
ops.assign(
ndarray(data,
[array.shape[0], array.shape[1], 3],
[4, 4 * array.shape[0], 1]),
ndarray(array.data,
[array.shape[0], array.shape[1], 3],
[array.stride[0], array.stride[1], 0],
array.offset))
ops.assigns(
ndarray(data,
[array.shape[0] * array.shape[1]],
[4],
3),
255)
} else {
return new Error('Incompatible array shape')
}
} else if (array.shape.length === 2) {
ops.assign(
ndarray(data,
[array.shape[0], array.shape[1], 3],
[4, 4 * array.shape[0], 1]),
ndarray(array.data,
[array.shape[0], array.shape[1], 3],
[array.stride[0], array.stride[1], 0],
array.offset))
ops.assigns(
ndarray(data,
[array.shape[0] * array.shape[1]],
[4],
3),
255)
} else {
return new Error('Incompatible array shape')
}
return data
}
function haderror (err) {
var result = through()
result.emit('error', err)
return result
}
module.exports = function savePixels (array, type, options) {
options = options || {}
switch (type.toUpperCase()) {
case 'JPG':
case '.JPG':
case 'JPEG':
case '.JPEG':
case 'JPE':
case '.JPE':
var width = array.shape[0]
var height = array.shape[1]
var data = new Buffer(width * height * 4)
data = handleData(array, data)
var rawImageData = {
data: data,
width: width,
height: height
}
var jpegImageData = jpegJs.encode(rawImageData, options.quality)
return new ContentStream(jpegImageData.data)
case 'GIF':
case '.GIF':
var frames = array.shape.length === 4 ? array.shape[0] : 1
var width = array.shape.length === 4 ? array.shape[1] : array.shape[0]
var height = array.shape.length === 4 ? array.shape[2] : array.shape[1]
var data = new Buffer(width * height * 4)
var gif = new GifEncoder(width, height)
gif.writeHeader()
for (var i = 0; i < frames; i++) {
data = handleData(array, data, i)
gif.addFrame(data)
}
gif.finish()
return gif
case 'PNG':
case '.PNG':
var png = new PNG({
width: array.shape[0],
height: array.shape[1]
})
var data = handleData(array, png.data)
if (typeof data === 'Error') return haderror(data)
png.data = data
return png.pack()
case 'CANVAS':
var Canvas = require('canvas')
var canvas = Canvas.createCanvas(array.shape[0], array.shape[1])
var context = canvas.getContext('2d')
var imageData = context.getImageData(0, 0, canvas.width, canvas.height)
var data = imageData.data
data = handleData(array, data)
if (typeof data === 'Error') return haderror(data)
context.putImageData(imageData, 0, 0)
return canvas
default:
return haderror(new Error('Unsupported file type: ' + type))
}
}