Repository navigation
Expand file tree
/
Copy pathwbmp.js
More file actions
104 lines (89 loc) · 2.79 KB
/
Copy pathwbmp.js
File metadata and controls
104 lines (89 loc) · 2.79 KB
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
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
// see http://www.wapforum.org/what/technical/SPEC-WAESpec-19990524.pdf
/*exported WBMP */
'use strict';
var WBMP = (function(document) {
function decode(arrayBuffer, callback) {
// Pointer into the byte stream.
var bytes = new Uint8Array(arrayBuffer);
var ptr = 0;
// Read unsigned octet from the byte stream.
function readOctet() {
return bytes[ptr++] & 0xff;
}
// Read unsigned multi-byte integer (6.3.1) from the byte stream.
function readMultiByteInteger() {
var result = 0;
while (true) {
if (result & 0xfe000000) {
throw 'error parsing integer';
}
var b = bytes[ptr++];
result = (result << 7) | (b & 0x7f);
if (!(b & 0x80)) {
return result;
}
}
}
// write a pixel
function write(data, w, bit) {
var color = bit ? 255 : 0;
data[w] = color;
data[w + 1] = color;
data[w + 2] = color;
data[w + 3] = 255;
}
try {
// We only support image type 0: B/W, no compression
if (readMultiByteInteger() !== 0) {
return false;
}
// We don't expect any extended headers here.
if (readOctet() !== 0) {
return false;
}
var width = readMultiByteInteger();
var height = readMultiByteInteger();
// Reject incorrect image dimensions.
if (width === 0 || width > 65535 || height === 0 || height > 65535) {
return false;
}
// Create a canvas to draw the pixels into.
var canvas = document.createElement('canvas');
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
var ctx = canvas.getContext('2d', { willReadFrequently: true });
var imageData = ctx.createImageData(width, height);
var data = imageData.data;
// Decode the image.
for (var y = 0; y < height; ++y) {
for (var x = 0; x < width; x += 8) {
var bits = bytes[ptr++];
var w = (y * width + x) * 4;
write(data, w, bits & 0x80);
write(data, w + 4
47AC
, bits & 0x40);
write(data, w + 8, bits & 0x20);
write(data, w + 12, bits & 0x10);
write(data, w + 16, bits & 0x08);
write(data, w + 20, bits & 0x04);
write(data, w + 24, bits & 0x02);
write(data, w + 28, bits & 0x01);
}
}
if (ptr > bytes.length) {
return null;
}
// Update the canvas pixels.
ctx.putImageData(imageData, 0, 0);
// Convert to an image.
canvas.toBlob(callback);
} catch (e) {
// Error occured.
return null;
}
}
return {
decode: decode
};
})(document);