-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmp.js
366 lines (327 loc) · 10 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Adapted from https://github.com/shaozilee/bmp-js/blob/master/lib/encoder.js by shaozilee
* With Buffer shim and small modifications by Crystalline Emerald
*/
// Decoder
var console = {log: function() {
var out = "";
for (var i=0; i<arguments.length; i++) {
out += arguments[i];
out += " ";
}
out += "\n";
print(out);
}};
function writeUInt32LE(uint, pos) {
this[pos] = uint&0xFF;
this[pos+1] = (uint&0xFF00)>>8;
this[pos+2] = (uint&0xFF0000)>>16;
this[pos+3] = (uint&0xFF000000)>>24;
};
function writeUInt16LE (uint, pos) {
this[pos] = uint&0xFF;
this[pos+1] = (uint&0xFF00)>>8;
};
function fill(byte, start, end) {
for (var i=start; i<end; i++) {
this[i] = byte;
}
}
function readUInt8(pos) {
return this[pos];
};
function readUInt16LE(pos) {
var uint = this[pos];
uint |= this[pos+1]<<8;
return uint;
};
function readUInt32LE(pos) {
var uint = this[pos];
uint |= this[pos+1]<<8;
uint |= this[pos+2]<<16;
uint |= this[pos+3]<<32;
return uint;
};
var BufferProto = {
writeUInt32LE: writeUInt32LE,
writeUInt16LE: writeUInt16LE,
fill: fill,
readUInt8: readUInt8,
readUInt16LE: readUInt16LE,
readUInt32LE: readUInt32LE
}
BufferProto.__proto__ = Uint8Array;
function addBufferTraits(b) {
b.__proto__ = BufferProto;
}
function Buffer(arg) {
if (typeof arg === "number") {
//print("New Buffer("+arg+")");
ret = new Uint8Array(arg);
addBufferTraits(ret);
} else if (arg.__proto__ == Uint8Array.prototype) {
//print("New Buffer("+arg.length+")");
ret = new Uint8Array(arg);
addBufferTraits(ret);
}
return ret;
}
function BmpEncoder(w, h, data){
this.buffer = data;
this.width = w;
this.height = h;
this.extraBytes = this.width%4;
this.rgbSize = this.height*(3*this.width+this.extraBytes);
this.headerInfoSize = 40;
this.data = [];
/******************header***********************/
this.flag0 = 66;
this.flag1 = 77;
this.reserved = 0;
this.offset = 54;
this.fileSize = this.rgbSize+this.offset;
this.planes = 1;
this.bitPP = 24;
this.compress = 0;
this.hr = 0;
this.vr = 0;
this.colors = 0;
this.importantColors = 0;
}
BmpEncoder.prototype.encode = function() {
var tempBuffer = new Buffer(this.offset+this.rgbSize);
this.pos = 0;
tempBuffer[this.pos] = this.flag0; this.pos+=1;
tempBuffer[this.pos] = this.flag1; this.pos+=1;
tempBuffer.writeUInt32LE(this.fileSize,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.reserved,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.offset,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.headerInfoSize,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.width,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.height,this.pos);this.pos+=4;
tempBuffer.writeUInt16LE(this.planes,this.pos);this.pos+=2;
tempBuffer.writeUInt16LE(this.bitPP,this.pos);this.pos+=2;
tempBuffer.writeUInt32LE(this.compress,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.rgbSize,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.hr,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.vr,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.colors,this.pos);this.pos+=4;
tempBuffer.writeUInt32LE(this.importantColors,this.pos);this.pos+=4;
var i=0;
var rowBytes = 3*this.width+this.extraBytes;
for (var y = this.height - 1; y >= 0; y--){
for (var x = 0; x < this.width; x++){
var p = this.pos+y*rowBytes+x*3;
tempBuffer[p+2]= this.buffer[i++];//r
tempBuffer[p+1] = this.buffer[i++];//g
tempBuffer[p] = this.buffer[i++];//b
i++;
}
if(this.extraBytes>0){
var fillOffset = this.pos+y*rowBytes+this.width*3;
tempBuffer.fill(0,fillOffset,fillOffset+this.extraBytes);
}
}
return tempBuffer;
};
function encodeBmp(w, h, imgDataData) {
var encoder = new BmpEncoder(w, h, imgDataData);
return encoder.encode();
}
// Decoder
function BmpDecoder(buffer) {
this.pos = 0;
this.buffer = buffer;
//this.flag = this.buffer.toString("utf-8", 0, this.pos += 2);
this.flag0 = this.buffer[0];
this.flag1 = this.buffer[1];
this.pos += 2;
if (this.flag0 != 66 || this.flag1 != 77) throw new Error("Invalid BMP File");
this.parseHeader();
this.parseBGR();
}
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.readUInt32LE(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.bitPP < 24) {
var len = this.colors === 0 ? 1 << this.bitPP : this.colors;
this.palette = new Array(len);
for (var i = 0; i < len; i++) {
var blue = this.buffer.readUInt8(this.pos++);
var green = this.buffer.readUInt8(this.pos++);
var red = this.buffer.readUInt8(this.pos++);
var quad = this.buffer.readUInt8(this.pos++);
this.palette[i] = {
red: red,
green: green,
blue: blue,
quad: quad
};
}
}
}
BmpDecoder.prototype.parseBGR = function() {
this.pos = this.offset;
try {
var bitn = "bit" + this.bitPP;
var len = this.width * this.height * 4;
this.data = new Buffer(len);
this[bitn]();
} catch (e) {
console.log("bit decode error:" + e);
}
};
BmpDecoder.prototype.bit1 = function() {
var xlen = Math.ceil(this.width / 8);
var mode = xlen%4;
for (var y = this.height - 1; y >= 0; y--) {
for (var x = 0; x < xlen; x++) {
var b = this.buffer.readUInt8(this.pos++);
var location = y * this.width * 4 + x*8*4;
for (var i = 0; i < 8; i++) {
if(x*8+i<this.width){
var rgb = this.palette[((b>>(7-i))&0x1)];
this.data[location+i*4] = rgb.blue;
this.data[location+i*4 + 1] = rgb.green;
this.data[location+i*4 + 2] = rgb.red;
this.data[location+i*4 + 3] = 0xFF;
}else{
break;
}
}
}
if (mode != 0){
this.pos+=(4 - mode);
}
}
};
BmpDecoder.prototype.bit4 = function() {
var xlen = Math.ceil(this.width/2);
var mode = xlen%4;
for (var y = this.height - 1; y >= 0; y--) {
for (var x = 0; x < xlen; x++) {
var b = this.buffer.readUInt8(this.pos++);
var location = y * this.width * 4 + x*2*4;
var before = b>>4;
var after = b&0x0F;
var rgb = this.palette[before];
this.data[location] = rgb.blue;
this.data[location + 1] = rgb.green;
this.data[location + 2] = rgb.red;
this.data[location + 3] = 0xFF;
if(x*2+1>=this.width)break;
rgb = this.palette[after];
this.data[location+4] = rgb.blue;
this.data[location+4 + 1] = rgb.green;
this.data[location+4 + 2] = rgb.red;
this.data[location+4 + 3] = 0xFF;
}
if (mode != 0){
this.pos+=(4 - mode);
}
}
};
BmpDecoder.prototype.bit8 = function() {
var mode = this.width%4;
for (var y = this.height - 1; y >= 0; y--) {
for (var x = 0; x < this.width; x++) {
var b = this.buffer.readUInt8(this.pos++);
var location = y * this.width * 4 + x*4;
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;
}
}
if (mode != 0){
this.pos+=(4 - mode);
}
}
};
BmpDecoder.prototype.bit24 = 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 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] = 0xFF;
}
//skip extra bytes
this.pos += (this.width % 4);
}
};
/**
* 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() {
return this.data;
};
function decodeBMP(bmpData) {
var arg = bmpData;
if (bmpData.__proto__ == Uint8Array.prototype) {
arg = new Buffer(bmpData)
}
var decoder = new BmpDecoder(arg);
return {
data: decoder.getData(),
width: decoder.width,
height: decoder.height
};
}