-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (36 loc) · 1.13 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
const fsp = require('fs-promise');
const TGA = require('tga');
const PNG = require('pngjs').PNG;
function tga2png(file, savePath) {
if (!(file instanceof Buffer)) {
return fsp.readFile(String(file)).then(buf => {
return tga2png(buf, savePath);
}, err => {
console.error('Read file failed', err.message);
return Promise.reject(err);
});
} else {
var tga = new TGA(file);
var png = new PNG({
width: tga.header.width,
height: tga.header.height
});
png.data = tga.pixels;
return new Promise((resolve, reject) => {
var bufs = [];
png.pack().on('data', d => {
bufs.push(d);
}).on('end', () => {
var buffer = Buffer.concat(bufs);
if (savePath) {
resolve(fsp.writeFile(savePath, buffer).then(() => {
return buffer;
}));
} else {
resolve(buffer);
}
}).on('error', reject);
});
}
}
module.exports = tga2png;