/
index.js
67 lines (54 loc) · 954 Bytes
/
index.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
module.exports = size;
/**
* Start of frame markers.
*/
var sof = {
0xc0: true,
0xc1: true,
0xc2: true,
0xc3: true,
0xc5: true,
0xc6: true,
0xc7: true,
0xc9: true,
0xca: true,
0xcb: true,
0xcd: true,
0xce: true,
0xcf: true
};
/**
* Uint16BE.
*/
function u16(buf, o) {
return buf[o] << 8 | buf[o + 1];
}
/**
* Return dimensions from jpeg `buf`.
*
* @param {Buffer} buf
* @return {Object} or undefined
* @api public
*/
function size(buf) {
var len = buf.length;
var o = 0;
// magick
var jpeg = 0xff == buf[0] && 0xd8 == buf[1];
if (!jpeg) return;
o += 2;
while (o < len) {
// find next marker
while (0xff != buf[o]) o++;
// skip marker
while (0xff == buf[o]) o++;
// non-SOF jump to the next marker
if (!sof[buf[o]]) {
o += u16(buf, ++o);
continue;
}
var w = u16(buf, o + 6);
var h = u16(buf, o + 4);
return { width: w, height: h };
}
}