-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (44 loc) · 1.74 KB
/
index.js
File metadata and controls
57 lines (44 loc) · 1.74 KB
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
const fs = require('fs');
const util = require('util');
const Jimp = require('jimp');
// https://stackoverflow.com/questions/7998324/dot-cpbitmap-images-imgaename-cpbitmap/48158807#48158807
//in terminal: node index.js C:\Users\Kyle\Desktop\foundit\OriginalLockBackground.cpbitmap C:\Users\Kyle\Desktop\foundit\IGwallpaper
const main = async () => {
if (process.argv.length != 4) {
console.log('Need two args: input filename and result filename');
console.log(`Example: ${process.argv[0]} ${process.argv[1]} HomeBackground.cpbitmap HomeBackground.png`);
return
}
const inpFileName = process.argv[2]
const outFileName = process.argv[3]
const readFile = util.promisify(fs.readFile)
const cpbmp = await readFile(inpFileName)
const width = cpbmp.readInt32LE(cpbmp.length - 4 * 5)
const height = cpbmp.readInt32LE(cpbmp.length - 4 * 4)
console.log(`Image height: ${height}, width: ${width}`)
const image = await new Jimp(width, height, 0x000000FF)
const calcOffsetInCpbmp = (x, y, width) => {
const lineSize = Math.ceil(width / 8) * 8
return x * 4 + y * lineSize * 4
}
const calcOffsetInImage = (x, y, width) => {
return x * 4 + y * width * 4
}
const swapRBColors = (c) => {
const r = c & 0xFF
const b = (c & 0xFF0000) >> 16
c &= 0xFF00FF00
c |= r << 16
c |= b
return c
}
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const color = cpbmp.readInt32LE(calcOffsetInCpbmp(x, y, width))
image.bitmap.data.writeInt32LE(swapRBColors(color), calcOffsetInImage(x, y, width))
}
}
await image.write(outFileName)
console.log('Done')
}
main()