forked from MikeKovarik/exifr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicc.mjs
203 lines (177 loc) · 5.53 KB
/
icc.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {AppSegmentParserBase} from '../parser.mjs'
import {segmentParsers} from '../plugins.mjs'
import {throwError, normalizeString} from '../util/helpers.mjs'
import {BufferView} from '../util/BufferView.mjs'
const PROFILE_HEADER_LENGTH = 84
const TAG_TYPE_DESC = 'desc'
const TAG_TYPE_MLUC = 'mluc'
const TAG_TYPE_TEXT = 'text'
const TAG_TYPE_SIG = 'sig '
// TODO: other types 'mft2', 'XYZ '
const EMPTY_VALUE = '\x00\x00\x00\x00'
export default class Icc extends AppSegmentParserBase {
static type = 'icc'
static multiSegment = true
static headerLength = 18
static canHandle(chunk, offset) {
return chunk.getUint8(offset + 1) === 0xE2
&& chunk.getUint32(offset + 4) === 0x4943435f // ICC_
// full header is: ICC_PROFILE
}
static findPosition(chunk, offset) {
let seg = super.findPosition(chunk, offset)
seg.chunkNumber = chunk.getUint8(offset + 16)
seg.chunkCount = chunk.getUint8(offset + 17)
seg.multiSegment = seg.chunkCount > 1
return seg
}
static handleMultiSegments(segments) {
return concatChunks(segments)
}
parse() {
this.raw = new Map
this.parseHeader()
this.parseTags()
this.translate()
return this.output
}
parseHeader() {
let {raw} = this
if (this.chunk.byteLength < PROFILE_HEADER_LENGTH)
throwError('ICC header is too short')
for (let [offset, parse] of Object.entries(headerParsers)) {
offset = parseInt(offset, 10)
let val = parse(this.chunk, offset)
if (val === EMPTY_VALUE) continue
raw.set(offset, val)
}
}
parseTags() {
let {raw} = this
let tagCount = this.chunk.getUint32(128)
let offset = 132
let chunkLength = this.chunk.byteLength
let code, valueOffset, valueLength, type, value
while (tagCount--) {
code = this.chunk.getString(offset, 4)
valueOffset = this.chunk.getUint32(offset + 4)
valueLength = this.chunk.getUint32(offset + 8)
type = this.chunk.getString(valueOffset, 4)
if (valueOffset + valueLength > chunkLength) {
console.warn('reached the end of the first ICC chunk. Enable options.tiff.multiSegment to read all ICC segments.')
return
}
value = this.parseTag(type, valueOffset, valueLength)
// Not all the type parsers are implemented.
if (value !== undefined && value !== EMPTY_VALUE)
raw.set(code, value)
offset += 12
}
}
parseTag(type, offset, length) {
switch (type) {
case TAG_TYPE_DESC: return this.parseDesc(offset)
case TAG_TYPE_MLUC: return this.parseMluc(offset)
case TAG_TYPE_TEXT: return this.parseText(offset, length)
case TAG_TYPE_SIG: return this.parseSig(offset)
// TODO: implement more types
}
if (offset + length > this.chunk.byteLength) {
// TODO: handle these when multi-segment ICC is being implemented
// look out for issue-metadata-extractor-65.jpg
} else {
return this.chunk.getUint8Array(offset, length)
}
}
parseDesc(offset) {
let length = this.chunk.getUint32(offset + 8) - 1 // last byte is null termination
return normalizeString(this.chunk.getString(offset + 12, length))
}
parseText(offset, length) {
return normalizeString(this.chunk.getString(offset + 8, length - 8))
}
// NOTE: some tags end with empty space. TODO: investigate. maybe add .trim()
parseSig(offset) {
return normalizeString(this.chunk.getString(offset + 8, 4))
}
// Multi Localized Unicode Type
parseMluc(tagOffset) {
let {chunk} = this
let entryCount = chunk.getUint32(tagOffset + 8)
let entrySize = chunk.getUint32(tagOffset + 12)
let entryOffset = tagOffset + 16
let values = []
for (let i = 0; i < entryCount; i++) {
let lang = chunk.getString(entryOffset + 0, 2)
let country = chunk.getString(entryOffset + 2, 2)
let length = chunk.getUint32(entryOffset + 4)
let offset = chunk.getUint32(entryOffset + 8) + tagOffset
let text = normalizeString(chunk.getUnicodeString(offset, length))
values.push({lang, country, text})
entryOffset += entrySize
}
if (entryCount === 1)
return values[0].text
else
return values
}
translateValue(val, tagEnum) {
if (typeof val === 'string')
return tagEnum[val] || tagEnum[val.toLowerCase()] || val
else
return tagEnum[val] || val
}
}
const headerParsers = {
4: parseString,
8: parseVersion,
12: parseString,
16: parseString,
20: parseString,
24: parseDate,
36: parseString,
40: parseString,
48: parseString,
52: parseString,
64: (view, offset) => view.getUint32(offset),
80: parseString
}
function parseString(view, offset) {
return normalizeString(view.getString(offset, 4))
}
function parseVersion(view, offset) {
return [
view.getUint8(offset),
view.getUint8(offset + 1) >> 4,
view.getUint8(offset + 1) % 16,
]
.map(num => num.toString(10))
.join('.')
}
function parseDate(view, offset) {
const year = view.getUint16(offset)
const month = view.getUint16(offset + 2) - 1
const day = view.getUint16(offset + 4)
const hours = view.getUint16(offset + 6)
const minutes = view.getUint16(offset + 8)
const seconds = view.getUint16(offset + 10)
return new Date(Date.UTC(year, month, day, hours, minutes, seconds))
}
function concatChunks(chunks) {
let buffers = chunks.map(s => s.chunk.toUint8())
let combined = concatBuffers(buffers)
return new BufferView(combined)
}
function concatBuffers(buffers) {
let ArrayType = buffers[0].constructor
let totalLength = 0
for (let buffer of buffers) totalLength += buffer.length
let result = new ArrayType(totalLength)
let offset = 0
for (let buffer of buffers) {
result.set(buffer, offset)
offset += buffer.length
}
return result
}
segmentParsers.set('icc', Icc)