-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (87 loc) · 3.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict';
const yargs = require('yargs');
const argv = yargs
.usage('Usage: node $0 [options] <file>...')
.demandCommand(1, 'You must specify at least one .bmp file')
.option('d', {description: 'output directory for generated PNG images'})
.alias('d', 'outdir')
.default('d', 'samples/out/')
.option('l', {description: 'a text file where to log failed images'})
.alias('l', 'log-failed')
.option('v', {description: 'dump some additional debugging info about the BMP structure', type: "boolean"})
.alias('v', 'verbose')
.default('v', false)
.argv;
const fs = require('fs');
const path = require('path');
const KaitaiStream = require('kaitai-struct/KaitaiStream');
const Compression = require('./src/enum').Compression;
const utils = require('./src/utils');
const PNG = require('pngjs').PNG;
if (argv._.length > 0) {
const failedImages = [];
const handleError = (fileName, e) => {
console.log('ERROR: ', e.message);
console.log(e.stack);
failedImages.push(
`${fileName}: ${e.message}`
);
};
argv._.forEach((fileName, idx) => {
fs.readFile(fileName, (err, data) => {
if (err) {
handleError(fileName, err);
return;
}
console.log(`${fileName}`);
const Bmp = require('./src/Bmp');
let bmp;
try {
bmp = new Bmp(new KaitaiStream(data));
} catch (e) {
handleError(fileName, e);
return;
}
if (argv.verbose) {
const dibHdr = utils.getOwnPropsFromStruct(bmp.dibInfo.header, true);
console.dir(dibHdr, {depth: null});
}
let comprType;
try {
comprType = bmp.bitmap.getCompression();
} catch (e) {
handleError(fileName, e);
return;
}
if (argv.verbose) {
console.log('compression:', typeof comprType === 'number' ? Compression[comprType] : 'UNKNOWN');
if (comprType !== Compression.JPEG && comprType !== Compression.PNG && bmp.dibInfo.header.usesFixedPalette) {
console.log(`Number of colors in palette: ${bmp.dibInfo.colorTable.colors.length}`);
const byteToHex = b => b.toString(16).padStart(2, '0');
console.log(bmp.dibInfo.colorTable.colors.map(c => '#' + [c.red, c.green, c.blue].map(byteToHex).join('')));
}
}
if (argv.verbose) {
utils.dumpBitmap(bmp);
}
const png = new PNG({width: bmp.dibInfo.header.imageWidth, height: bmp.dibInfo.header.imageHeight});
png.data = Buffer.from(bmp.bitmap.data);
const outputFileName = path.resolve(argv.d, path.basename(fileName, '.bmp') + '.png');
png.pack().pipe(fs.createWriteStream(outputFileName));
console.log(`output PNG: ${outputFileName}`);
});
});
let alreadyExecuted = false;
process.on('beforeExit', () => {
if (alreadyExecuted) return;
alreadyExecuted = true;
console.log('-'.repeat(60));
console.log('Failed images: ');
console.log(failedImages.join("\n"));
if (failedImages.length > 0 && argv.l) {
fs.writeFile(argv.l, failedImages.join("\n") + "\n", {flag: 'a'}, () => {
console.log(`successfully written to file ${argv.l}`);
});
}
});
}