-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathico.node.js
69 lines (62 loc) · 1.6 KB
/
ico.node.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
var fs = require('fs');
var jParser = require('../../src/jparser.js');
fs.readFile('favicon.ico', function (err, buffer) {
var parser = new jParser(buffer, {
uint4: function () {
// By default, we can only parse 8 bits at a time.
// When uint4 is called, it will parse 8 bits,
// return the first 4 and cache the 4 others for
// the next call.
if (this.hasUint4Buffer) {
this.hasUint4Buffer = false;
return this.uint4Buffer;
} else {
this.hasUint4Buffer = true;
var uint8 = this.parse('uint8');
this.uint4Buffer = uint8 >>> 4;
return uint8 & 0x0f;
}
},
rgba: {
r: 'uint8',
g: 'uint8',
b: 'uint8',
a: 'uint8'
},
header: {
reserved: 'uint8',
type: 'uint8',
imageCount: 'uint8',
padding: ['array', 'uint8', 3]
},
image: {
width: 'uint8',
height: 'uint8',
paletteCount: 'uint8',
reserved: 'uint8',
colorPlanes: 'uint16',
bitsPerPixel: 'uint16',
size: 'uint32',
offset: 'uint32',
content: function () {
var that = this;
return that.seek(that.current.offset, function () {
return that.parse({
palette: ['array', 'rgba', that.current.paletteCount],
pixels: [
'array',
['array', 'uint' + that.current.bitsPerPixel, that.current.width],
that.current.height
]
});
});
}
},
file: {
header: 'header',
images: ['array', 'image', function () { return this.current.header.imageCount; }]
}
});
var ico = parser.parse('file');
console.log(require('util').inspect(ico, false, 10));
});