Path: blob/master/node_modules/bmp-js/lib/encoder.js
2591 views
/**1* @author shaozilee2*3* BMP format encoder,encode 24bit BMP4* Not support quality compression5*6*/78function BmpEncoder(imgData){9this.buffer = imgData.data;10this.width = imgData.width;11this.height = imgData.height;12this.extraBytes = this.width%4;13this.rgbSize = this.height*(3*this.width+this.extraBytes);14this.headerInfoSize = 40;1516this.data = [];17/******************header***********************/18this.flag = "BM";19this.reserved = 0;20this.offset = 54;21this.fileSize = this.rgbSize+this.offset;22this.planes = 1;23this.bitPP = 24;24this.compress = 0;25this.hr = 0;26this.vr = 0;27this.colors = 0;28this.importantColors = 0;29}3031BmpEncoder.prototype.encode = function() {32var tempBuffer = new Buffer(this.offset+this.rgbSize);33this.pos = 0;34tempBuffer.write(this.flag,this.pos,2);this.pos+=2;35tempBuffer.writeUInt32LE(this.fileSize,this.pos);this.pos+=4;36tempBuffer.writeUInt32LE(this.reserved,this.pos);this.pos+=4;37tempBuffer.writeUInt32LE(this.offset,this.pos);this.pos+=4;3839tempBuffer.writeUInt32LE(this.headerInfoSize,this.pos);this.pos+=4;40tempBuffer.writeUInt32LE(this.width,this.pos);this.pos+=4;41tempBuffer.writeInt32LE(-this.height,this.pos);this.pos+=4;42tempBuffer.writeUInt16LE(this.planes,this.pos);this.pos+=2;43tempBuffer.writeUInt16LE(this.bitPP,this.pos);this.pos+=2;44tempBuffer.writeUInt32LE(this.compress,this.pos);this.pos+=4;45tempBuffer.writeUInt32LE(this.rgbSize,this.pos);this.pos+=4;46tempBuffer.writeUInt32LE(this.hr,this.pos);this.pos+=4;47tempBuffer.writeUInt32LE(this.vr,this.pos);this.pos+=4;48tempBuffer.writeUInt32LE(this.colors,this.pos);this.pos+=4;49tempBuffer.writeUInt32LE(this.importantColors,this.pos);this.pos+=4;5051var i=0;52var rowBytes = 3*this.width+this.extraBytes;5354for (var y = 0; y <this.height; y++){55for (var x = 0; x < this.width; x++){56var p = this.pos+y*rowBytes+x*3;57i++;//a58tempBuffer[p]= this.buffer[i++];//b59tempBuffer[p+1] = this.buffer[i++];//g60tempBuffer[p+2] = this.buffer[i++];//r61}62if(this.extraBytes>0){63var fillOffset = this.pos+y*rowBytes+this.width*3;64tempBuffer.fill(0,fillOffset,fillOffset+this.extraBytes);65}66}6768return tempBuffer;69};7071module.exports = function(imgData, quality) {72if (typeof quality === 'undefined') quality = 100;73var encoder = new BmpEncoder(imgData);74var data = encoder.encode();75return {76data: data,77width: imgData.width,78height: imgData.height79};80};818283