-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
78 lines (63 loc) · 1.75 KB
/
index.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
'use strict'
const fs = require('fs')
const path = require('path')
const pify = require('pify')
const pump = require('pump-promise')
const getPixels = pify(require('get-pixels'))
const savePixels = require('save-pixels')
const supportedFormats = new Set([
'jpg',
'png',
'gif'
])
module.exports = async (opts) => {
const {
input,
output,
coalesce = true
} = opts
const format = output
? path.extname(output).substr(1)
: undefined
if (format && !supportedFormats.has(format)) {
throw new Error(`invalid output format "${format}"`)
}
const results = await getPixels(input)
const { shape } = results
if (shape.length === 4) {
// animated gif with multiple frames
const [
frames,
width,
height,
channels
] = shape
const numPixelsInFrame = width * height
for (let i = 0; i < frames; ++i) {
if (i > 0 && coalesce) {
const currIndex = results.index(i, 0, 0, 0)
const prevIndex = results.index(i - 1, 0, 0, 0)
for (let j = 0; j < numPixelsInFrame; ++j) {
const curr = currIndex + j * channels
if (results.data[curr + channels - 1] === 0) {
const prev = prevIndex + j * channels
for (let k = 0; k < channels; ++k) {
results.data[curr + k] = results.data[prev + k]
}
}
}
}
if (output) {
await saveFrame(results.pick(i), format, output.replace('%d', i))
}
}
} else if (output) {
// non-animated gif with a single frame
await saveFrame(results, format, output.replace('%d', 0))
}
return results
}
function saveFrame (data, format, filename) {
const stream = savePixels(data, format)
return pump(stream, fs.createWriteStream(filename))
}