-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmp.js
95 lines (84 loc) · 2.73 KB
/
bmp.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
const fs = require('fs');
function BmpDecoder(buffer, output_filename) {
this.pos = 0;
this.buffer = buffer;
this.output_filename = output_filename;
this.bottom_up = true;
this.flag = this.buffer.toString("utf-8", 0, this.pos += 2);
if (this.flag != "BM") throw new Error("Invalid BMP File");
console.log('output filename', this.output_filename)
this.parseHeader();
this.getData();
this.writeOutput();
}
BmpDecoder.prototype.bitRevAndMask = function (num) {
const bits = num.toString(2);
const padded = bits.padStart(8, 0)
const reversed = padded.split('').reverse().join('');;
const reversedNumber = parseInt(reversed, 2);
return reversedNumber ^ 0xFF;
}
BmpDecoder.prototype.parseHeader = function() {
this.fileSize = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.reserved = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.offset = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.headerSize = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.width = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.height = this.buffer.readInt32LE(this.pos);
this.pos += 4;
this.planes = this.buffer.readUInt16LE(this.pos);
this.pos += 2;
this.bitPP = this.buffer.readUInt16LE(this.pos);
this.pos += 2;
this.compress = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.rawSize = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.hr = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.vr = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.colors = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
this.importantColors = this.buffer.readUInt32LE(this.pos);
this.pos += 4;
if(this.height < 0) {
this.height *= -1;
this.bottom_up = false;
}
}
BmpDecoder.prototype.getData = function() {
const bitn = "bit" + this.bitPP;
this.data = Buffer.from([]);
this[bitn]();
};
BmpDecoder.prototype.bit1 = function() {
this.pos = this.offset;
let row = 480;
while(row > 0) {
row--;
const columnBuffer = Buffer.alloc(100);
for (let col = 0; col < 100 /* 800 / 8*/; col++) {
const imageByte = this.buffer.readUInt8(this.pos++);
// Not needed as long as the columns are read correctly
//const imageByteReversed = this.bitRevAndMask(imageByte);
columnBuffer.writeUintBE(imageByte, col, 1);
}
this.data = Buffer.concat([columnBuffer, this.data])
}
console.log('row', row)
console.log('pos', this.pos)
};
BmpDecoder.prototype.writeOutput = function () {
fs.writeFileSync(this.output_filename, this.data)
console.log('wrote ', this.output_filename);
}
module.exports = function(bmpData, output_filename) {
const decoder = new BmpDecoder(bmpData, output_filename);
return decoder;
};