1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 | var readUInt16BE = require('../common').readUInt16BE;
|
6 |
|
7 |
|
8 | module.exports = function (data) {
|
9 | if (data.length < 2) return;
|
10 |
|
11 |
|
12 | if (data[0] !== 0xFF || data[1] !== 0xD8) return;
|
13 |
|
14 | var offset = 2;
|
15 |
|
16 | for (;;) {
|
17 | if (data.length - offset < 2) return;
|
18 |
|
19 | if (data[offset++] !== 0xFF) return;
|
20 |
|
21 | var code = data[offset++];
|
22 | var length;
|
23 |
|
24 |
|
25 | while (code === 0xFF) code = data[offset++];
|
26 |
|
27 |
|
28 |
|
29 | if ((0xD0 <= code && code <= 0xD9) || code === 0x01) {
|
30 | length = 0;
|
31 | } else if (0xC0 <= code && code <= 0xFE) {
|
32 |
|
33 | if (data.length - offset < 2) return;
|
34 |
|
35 | length = readUInt16BE(data, offset) - 2;
|
36 | offset += 2;
|
37 | } else {
|
38 |
|
39 | return;
|
40 | }
|
41 |
|
42 | if (code === 0xD9 || code === 0xDA ) {
|
43 |
|
44 | return;
|
45 | }
|
46 |
|
47 | if (length >= 5 &&
|
48 | (0xC0 <= code && code <= 0xCF) &&
|
49 | code !== 0xC4 && code !== 0xC8 && code !== 0xCC) {
|
50 |
|
51 | if (data.length - offset < length) return;
|
52 |
|
53 | return {
|
54 | width: readUInt16BE(data, offset + 3),
|
55 | height: readUInt16BE(data, offset + 1),
|
56 | type: 'jpg',
|
57 | mime: 'image/jpeg',
|
58 | wUnits: 'px',
|
59 | hUnits: 'px'
|
60 | };
|
61 | }
|
62 |
|
63 | offset += length;
|
64 | }
|
65 | };
|