forked from SidOfc/leather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiff.js
More file actions
57 lines (42 loc) · 1.56 KB
/
tiff.js
File metadata and controls
57 lines (42 loc) · 1.56 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
import {lazystream} from '../util.js';
export function attributes(input) {
const stream = lazystream(input);
const isBigEndian =
Buffer.compare(stream.take(2), Buffer.from([0x4d, 0x4d])) === 0;
const attrs = isBigEndian ? attributesBE(stream) : attributesLE(stream);
const result = {...attrs, size: stream.size(), mime: 'image/tiff'};
stream.close();
return result;
}
function attributesBE(stream) {
const result = {width: 0, height: 0};
const ifdIndex = stream.skip(2).takeUInt32BE();
stream.goto(ifdIndex).skip(2);
while (stream.more()) {
const code = stream.takeUInt16BE();
const type = stream.takeUInt16BE();
const h = stream.skip(4).takeUInt16BE();
const l = stream.takeUInt16BE();
const value = (l << 16) + h;
if (code === 0) break;
else if (code === 256) result.width = value;
else if (code === 257) result.height = value;
if (result.width > 0 && result.height > 0) break;
}
return result;
}
function attributesLE(stream) {
const result = {width: 0, height: 0};
const ifdIndex = stream.skip(2).takeUInt32LE();
stream.goto(ifdIndex).skip(2);
while (stream.more()) {
const code = stream.takeUInt16LE();
const type = stream.takeUInt16LE();
const value = stream.skip(4).takeUInt32LE();
if (code === 0) break;
else if (code === 256) result.width = value;
else if (code === 257) result.height = value;
if (result.width > 0 && result.height > 0) break;
}
return result;
}