Comparing version 0.0.1 to 0.0.2
@@ -0,0 +0,0 @@ /** |
@@ -5,8 +5,9 @@ /** | ||
* Bmp format decoder,support 1bit 4bit 8bit 24bit bmp | ||
* | ||
* | ||
*/ | ||
function BmpDecoder(buffer) { | ||
function BmpDecoder(buffer,is_with_alpha) { | ||
this.pos = 0; | ||
this.buffer = buffer; | ||
this.is_with_alpha = !!is_with_alpha; | ||
this.flag = this.buffer.toString("utf-8", 0, this.pos += 2); | ||
@@ -48,4 +49,7 @@ if (this.flag != "BM") throw new Error("Invalid BMP File"); | ||
if (this.bitPP < 24) { | ||
var len = 1 << this.bitPP; | ||
if(this.bitPP === 16 && this.is_with_alpha){ | ||
this.bitPP = 15 | ||
}; | ||
if (this.bitPP < 15) { | ||
var len = this.colors === 0 ? 1 << this.bitPP : this.colors; | ||
this.palette = new Array(len); | ||
@@ -95,3 +99,3 @@ for (var i = 0; i < len; i++) { | ||
this.data[location+i*4 + 2] = rgb.red; | ||
this.data[location+i*4 + 3] = 0xFF; | ||
this.data[location+i*4 + 3] = 0xFF; | ||
}else{ | ||
@@ -124,3 +128,3 @@ break; | ||
this.data[location + 2] = rgb.red; | ||
this.data[location + 3] = 0xFF; | ||
this.data[location + 3] = 0xFF; | ||
@@ -133,3 +137,3 @@ if(x*2+1>=this.width)break; | ||
this.data[location+4 + 2] = rgb.red; | ||
this.data[location+4 + 3] = 0xFF; | ||
this.data[location+4 + 3] = 0xFF; | ||
} | ||
@@ -150,7 +154,14 @@ | ||
var location = y * this.width * 4 + x*4; | ||
var rgb = this.palette[b]; | ||
this.data[location] = rgb.blue; | ||
this.data[location + 1] = rgb.green; | ||
this.data[location + 2] = rgb.red; | ||
this.data[location + 3] = 0xFF; | ||
if(b < this.palette.length) { | ||
var rgb = this.palette[b]; | ||
this.data[location] = rgb.blue; | ||
this.data[location + 1] = rgb.green; | ||
this.data[location + 2] = rgb.red; | ||
this.data[location + 3] = 0xFF; | ||
} else { | ||
this.data[location] = 0xFF; | ||
this.data[location + 1] = 0xFF; | ||
this.data[location + 2] = 0xFF; | ||
this.data[location + 3] = 0xFF; | ||
} | ||
} | ||
@@ -163,2 +174,51 @@ if (mode != 0){ | ||
BmpDecoder.prototype.bit15 = function() { | ||
var dif_w =this.width % 3; | ||
var _11111 = parseInt("11111", 2),_1_5 = _11111; | ||
for (var y = this.height - 1; y >= 0; y--) { | ||
for (var x = 0; x < this.width; x++) { | ||
var B = this.buffer.readUInt16LE(this.pos); | ||
this.pos+=2; | ||
var blue = (B & _1_5) / _1_5 * 255 | 0; | ||
var green = (B >> 5 & _1_5 ) / _1_5 * 255 | 0; | ||
var red = (B >> 10 & _1_5) / _1_5 * 255 | 0; | ||
var alpha = (B>>15)?0xFF:0x00; | ||
var location = y * this.width * 4 + x * 4; | ||
this.data[location] = red; | ||
this.data[location + 1] = green; | ||
this.data[location + 2] = blue; | ||
this.data[location + 3] = alpha; | ||
} | ||
//skip extra bytes | ||
this.pos += dif_w; | ||
} | ||
}; | ||
BmpDecoder.prototype.bit16 = function() { | ||
var dif_w =this.width % 3; | ||
var _11111 = parseInt("11111", 2),_1_5 = _11111; | ||
var _111111 = parseInt("111111", 2),_1_6 = _111111; | ||
for (var y = this.height - 1; y >= 0; y--) { | ||
for (var x = 0; x < this.width; x++) { | ||
var B = this.buffer.readUInt16LE(this.pos); | ||
this.pos+=2; | ||
var alpha = 0xFF; | ||
var blue = (B & _1_5) / _1_5 * 255 | 0; | ||
var green = (B >> 5 & _1_6 ) / _1_6 * 255 | 0; | ||
var red = (B >> 11) / _1_5 * 255 | 0; | ||
var location = y * this.width * 4 + x * 4; | ||
this.data[location] = red; | ||
this.data[location + 1] = green; | ||
this.data[location + 2] = blue; | ||
this.data[location + 3] = alpha; | ||
} | ||
//skip extra bytes | ||
this.pos += dif_w; | ||
} | ||
}; | ||
BmpDecoder.prototype.bit24 = function() { | ||
@@ -183,2 +243,26 @@ //when height > 0 | ||
/** | ||
* add 32bit decode func | ||
* @author soubok | ||
*/ | ||
BmpDecoder.prototype.bit32 = function() { | ||
//when height > 0 | ||
for (var y = this.height - 1; y >= 0; y--) { | ||
for (var x = 0; x < this.width; x++) { | ||
var blue = this.buffer.readUInt8(this.pos++); | ||
var green = this.buffer.readUInt8(this.pos++); | ||
var red = this.buffer.readUInt8(this.pos++); | ||
var alpha = this.buffer.readUInt8(this.pos++); | ||
var location = y * this.width * 4 + x * 4; | ||
this.data[location] = red; | ||
this.data[location + 1] = green; | ||
this.data[location + 2] = blue; | ||
this.data[location + 3] = alpha; | ||
} | ||
//skip extra bytes | ||
this.pos += (this.width % 4); | ||
} | ||
}; | ||
BmpDecoder.prototype.getData = function() { | ||
@@ -188,5 +272,3 @@ return this.data; | ||
module.exports = decode = function(bmpData) { | ||
module.exports = function(bmpData) { | ||
var decoder = new BmpDecoder(bmpData); | ||
@@ -198,2 +280,2 @@ return { | ||
}; | ||
} | ||
}; |
@@ -6,3 +6,3 @@ /** | ||
* Not support quality compression | ||
* | ||
* | ||
*/ | ||
@@ -66,3 +66,3 @@ | ||
var fillOffset = this.pos+y*rowBytes+this.width*3; | ||
tempBuffer.fill(0,fillOffset,fillOffset+this.extraBytes); | ||
tempBuffer.fill(0,fillOffset,fillOffset+this.extraBytes); | ||
} | ||
@@ -74,6 +74,3 @@ } | ||
module.exports = encode = function(imgData, quality) { | ||
module.exports = function(imgData, quality) { | ||
if (typeof quality === 'undefined') quality = 100; | ||
@@ -87,2 +84,2 @@ var encoder = new BmpEncoder(imgData); | ||
}; | ||
} | ||
}; |
{ | ||
"name": "bmp-js", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A pure javascript BMP encoder and decoder", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,2 +34,2 @@ bmp-js | ||
--- | ||
U can use on free with [MIT License](https://github.com/shaozilee/bmp-js/blob/master/LICENSE.md) | ||
U can use on free with [MIT License](https://github.com/shaozilee/bmp-js/blob/master/LICENSE) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12913
328