forked from MikeKovarik/exifr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiff.mjs
55 lines (46 loc) · 1.91 KB
/
tiff.mjs
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
import {FileParserBase} from '../parser.mjs'
import {TAG_XMP, TAG_IPTC, TAG_ICC} from '../tags.mjs'
import {fileParsers} from '../plugins.mjs'
import {estimateMetadataSize} from '../util/helpers.mjs'
import {TIFF_LITTLE_ENDIAN, TIFF_BIG_ENDIAN} from '../util/helpers.mjs'
export class TiffFileParser extends FileParserBase {
static type = 'tiff'
static canHandle(file, firstTwoBytes) {
return firstTwoBytes === TIFF_LITTLE_ENDIAN
|| firstTwoBytes === TIFF_BIG_ENDIAN
}
extendOptions(options) {
// note: skipping is done on global level in Options class
let {ifd0, xmp, iptc, icc} = options
if (xmp.enabled) ifd0.deps.add(TAG_XMP)
if (iptc.enabled) ifd0.deps.add(TAG_IPTC)
if (icc.enabled) ifd0.deps.add(TAG_ICC)
ifd0.finalizeFilters()
}
async parse() {
let {tiff, xmp, iptc, icc} = this.options
if (tiff.enabled || xmp.enabled || iptc.enabled || icc.enabled) {
// TODO: refactor this in the future
// Tiff files start with TIFF structure (instead of JPEGs FF D8) but offsets can point to any place in the file.
// even wihin single block. Crude option is to just read as big chunk as possible.
// TODO: in the future, block reading will be recursive or looped until all pointers are resolved.
// SIDE NOTE: .tif files stor XMP as ApplicationNotes tag in TIFF structure as well.
let length = Math.max(estimateMetadataSize(this.options), this.options.chunkSize)
await this.file.ensureChunk(0, length)
this.createParser('tiff', this.file)
this.parsers.tiff.parseHeader()
await this.parsers.tiff.parseIfd0Block()
this.adaptTiffPropAsSegment('xmp')
this.adaptTiffPropAsSegment('iptc')
this.adaptTiffPropAsSegment('icc')
}
}
adaptTiffPropAsSegment(type) {
if (this.parsers.tiff[type]) {
// TIFF stores all other segments as tags in IFD0 object. Get the tag.
let raw = this.parsers.tiff[type]
this.injectSegment(type, raw)
}
}
}
fileParsers.set('tiff', TiffFileParser)