-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathscript.js
166 lines (152 loc) · 4.79 KB
/
script.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
const { prompt } = require('inquirer');
const fs = require('fs');
const GifEncoder = require('gif-encoder');
const PNG = require('pngjs').PNG;
const hasPngExtension = str => str.substr(str.length-4) === ".png";
const removePngExtension = str => hasPngExtension(str) ? str.substr(0,str.length-4) : str;
const convertToGif = (props, name) => {
const { directory, storage, framewidth, framerate, transparentcolor, quality } = props;
const path = `${directory}/${name}.png`;
fs.exists(path,exists=>{
if(!exists){
console.log("Could not open file " + path);
return
}
fs.createReadStream(path)
.pipe(new PNG())
.on('parsed', function(){
const {data, width, height} = this;
// Create an object containing the rgb of transparentcolor
const transparentRGB = {
r:Number("0x"+transparentcolor.substr(0,2)),
g:Number("0x"+transparentcolor.substr(2,2)),
b:Number("0x"+transparentcolor.substr(4,2)),
};
// Create a 2D pixel array
let pixelmap = [];
for (let x = 0; x < width; x++) {
pixelmap.push([]);
for (let y = 0; y < height; y++) {
let idx = (width * y + x) << 2;
let r = data[idx];
let g = data[idx+1];
let b = data[idx+2];
let a = data[idx+3];
// Detect transparency and convert to our transparency color
if(a < 128){
({r,g,b} = transparentRGB);
a = 255;
}
pixelmap[x].push([r,g,b,a]);
}
}
// Set gif dimensions
const fh = height;
const fw = framewidth === "" ? fh : Number(framewidth);
const file = require('fs').createWriteStream(`${storage}/${name}.gif`);
const gif = new GifEncoder(fw, fh, {
highWaterMark: 5 * 1024 * 1024 // 5MB buffer
});
gif.setRepeat(0);
gif.setFrameRate(Number(framerate));
gif.setTransparent(Number("0x"+transparentcolor));
gif.setQuality(Number(quality));
// Start writing gif
gif.pipe(file);
gif.writeHeader();
for(let i = 0; i*fw < width; i++){
// Create a 1D array of pixels for each frame
let pixels = [];
for (let y = 0; y < fh; y++) {
for (let x = 0; x < fw; x++) {
pixels.push(pixelmap[(i*fw) + x][y][0]);
pixels.push(pixelmap[(i*fw) + x][y][1]);
pixels.push(pixelmap[(i*fw) + x][y][2]);
pixels.push(pixelmap[(i*fw) + x][y][3]);
}
}
gif.addFrame(pixels);
}
gif.finish();
console.log("Converted " + path);
});
});
}
const convertDir = props => {
const { directory } = props;
fs.readdir(directory, (err,data)=>{
if (err){
console.log(`Could not find directory ${directory}.`);
return;
};
data.forEach(item=>{
if(hasPngExtension(item)){
convertToGif(props, removePngExtension(item));
}
});
});
}
const questions = [
{
type : 'input',
name : 'directory',
default : "build/images",
filter: dir => dir ? dir : ".",
message : 'Enter folder directory. Press "enter" for auto:'
},
{
type : 'input',
name : 'name',
message : 'Enter name (no extension). Leave blank to convert all:'
},
{
type : 'input',
name : 'storage',
default : "build/gifs",
filter: dir => dir ? dir : ".",
message : 'Enter storage folder directory. Press "enter" for auto:'
},
{
type : 'input',
name : 'framerate',
default : 30,
message : 'Enter frames per second:'
},
{
type : 'input',
name : 'framewidth',
message : 'Enter frame width. Leave blank for auto:'
},
{
type : 'input',
name : 'transparentcolor',
default : "0000FF",
message : 'Enter transparent color in hex:'
},
{
type : 'input',
name : 'quality',
default : 10,
message : 'Enter quality (1 = best; 20 = worst):'
},
{
type : 'confirm',
name : 'confirm',
message : 'Proceed with conversion?'
}
];
const startPrompt = () => {
prompt(questions).then((answers) => {
if(!answers.confirm){
console.log("Conversion aborted.");
return;
}
if(answers.name){
convertToGif(answers, answers.name);
} else {
convertDir(answers);
}
})
.catch(console.log);
}
startPrompt();