-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.mjs
88 lines (73 loc) · 1.88 KB
/
convert.mjs
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
import * as R from 'ramda';
import fs from 'fs';
import bmp from 'bmp-js';
import { globby } from 'globby';
import path from 'path';
const readbmp = R.pipe(
x => fs.readFileSync(x),
bmp.decode
);
const decToHex = n => n.toString(16).padStart(2, 0);
const half = R.pipe(
R.divide(R.__, 2),
Math.round
);
const extractHexArray = (width) => R.pipe(
buffer => new Uint8Array(buffer), // convert to array
R.splitEvery(4), // convert to JS array of buffers (4 bytes per pixel)
R.map(R.reverse), // reverse little endian
R.map(R.take(3)), // take only R, G, B bytes
R.map(R.map(R.pipe( // map over each individual value
half, // morph only uses 0-128 of the byte for some reason
decToHex
))),
R.map(R.join('')),
R.map(R.concat('0x')),
R.splitEvery(width)
);
const renameFile = R.replace('.bmp', '.ini');
const prettyName = R.pipe(
path.basename,
renameFile,
R.replace(/\//g, ' - ')
);
const createMorphTxt = (name, width, height) => R.pipe(
R.map(R.join(', ')),
R.join('\n'),
R.concat(`[main]
orig_preset_name = ${name}
display_name = ${name}
description = ${name}
tags = Comma separated list of tags, e.g. N64,320x240
; orig_feature_mask = 0x00000200
[slotmask]
intensity = 1
conversion = none
data = <<EOF
vMFX
# mode: 1
# offset: 0,0
# w,h
${width},${height}
# lut
`),
R.concat(R.__, `
EOF`)
);
const main = R.pipe(
R.map(filename => R.pipe(
readbmp,
bmp => R.pipe(
extractHexArray(bmp.width),
createMorphTxt(prettyName(filename), bmp.width, bmp.height),
)(bmp.data),
text => fs.writeFileSync(renameFile(filename), text)
)(filename))
);
const files = await globby(['./**/*.bmp', "!node_modules"]);
console.log('Converting Files:');
main(files);
console.log('Success!');
files.forEach(file => {
console.log(`${file} --> ${renameFile(file)}`)
});