Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jajbshjahavahh
GitHub Repository: jajbshjahavahh/Gojo-Satoru
Path: blob/master/node_modules/bmp-js/lib/encoder.js
2591 views
1
/**
2
* @author shaozilee
3
*
4
* BMP format encoder,encode 24bit BMP
5
* Not support quality compression
6
*
7
*/
8
9
function BmpEncoder(imgData){
10
this.buffer = imgData.data;
11
this.width = imgData.width;
12
this.height = imgData.height;
13
this.extraBytes = this.width%4;
14
this.rgbSize = this.height*(3*this.width+this.extraBytes);
15
this.headerInfoSize = 40;
16
17
this.data = [];
18
/******************header***********************/
19
this.flag = "BM";
20
this.reserved = 0;
21
this.offset = 54;
22
this.fileSize = this.rgbSize+this.offset;
23
this.planes = 1;
24
this.bitPP = 24;
25
this.compress = 0;
26
this.hr = 0;
27
this.vr = 0;
28
this.colors = 0;
29
this.importantColors = 0;
30
}
31
32
BmpEncoder.prototype.encode = function() {
33
var tempBuffer = new Buffer(this.offset+this.rgbSize);
34
this.pos = 0;
35
tempBuffer.write(this.flag,this.pos,2);this.pos+=2;
36
tempBuffer.writeUInt32LE(this.fileSize,this.pos);this.pos+=4;
37
tempBuffer.writeUInt32LE(this.reserved,this.pos);this.pos+=4;
38
tempBuffer.writeUInt32LE(this.offset,this.pos);this.pos+=4;
39
40
tempBuffer.writeUInt32LE(this.headerInfoSize,this.pos);this.pos+=4;
41
tempBuffer.writeUInt32LE(this.width,this.pos);this.pos+=4;
42
tempBuffer.writeInt32LE(-this.height,this.pos);this.pos+=4;
43
tempBuffer.writeUInt16LE(this.planes,this.pos);this.pos+=2;
44
tempBuffer.writeUInt16LE(this.bitPP,this.pos);this.pos+=2;
45
tempBuffer.writeUInt32LE(this.compress,this.pos);this.pos+=4;
46
tempBuffer.writeUInt32LE(this.rgbSize,this.pos);this.pos+=4;
47
tempBuffer.writeUInt32LE(this.hr,this.pos);this.pos+=4;
48
tempBuffer.writeUInt32LE(this.vr,this.pos);this.pos+=4;
49
tempBuffer.writeUInt32LE(this.colors,this.pos);this.pos+=4;
50
tempBuffer.writeUInt32LE(this.importantColors,this.pos);this.pos+=4;
51
52
var i=0;
53
var rowBytes = 3*this.width+this.extraBytes;
54
55
for (var y = 0; y <this.height; y++){
56
for (var x = 0; x < this.width; x++){
57
var p = this.pos+y*rowBytes+x*3;
58
i++;//a
59
tempBuffer[p]= this.buffer[i++];//b
60
tempBuffer[p+1] = this.buffer[i++];//g
61
tempBuffer[p+2] = this.buffer[i++];//r
62
}
63
if(this.extraBytes>0){
64
var fillOffset = this.pos+y*rowBytes+this.width*3;
65
tempBuffer.fill(0,fillOffset,fillOffset+this.extraBytes);
66
}
67
}
68
69
return tempBuffer;
70
};
71
72
module.exports = function(imgData, quality) {
73
if (typeof quality === 'undefined') quality = 100;
74
var encoder = new BmpEncoder(imgData);
75
var data = encoder.encode();
76
return {
77
data: data,
78
width: imgData.width,
79
height: imgData.height
80
};
81
};
82
83