exifreader 4.53.0 → 4.54.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/mpf-tags.js CHANGED
@@ -6,7 +6,7 @@ import ByteOrder from './byte-order.js';
6
6
  import Types from './types.js';
7
7
  import TagNamesMpfIfd from './tag-names-mpf-ifd.js';
8
8
  import {IFD_TYPE_MPF} from './ifd-types.js';
9
- import {deferInit, getBase64Image} from './utils.js';
9
+ import {deferInit} from './utils.js';
10
10
  import {readIfd, get0thIfdOffset} from './tags-helpers.js';
11
11
 
12
12
  export default {
@@ -31,7 +31,8 @@ function read(
31
31
  includeUnknown,
32
32
  computed = false,
33
33
  tagFilter = undefined,
34
- valueBudget = undefined
34
+ valueBudget = undefined,
35
+ getBase64Image = undefined
35
36
  ) {
36
37
  try {
37
38
  const byteOrder = ByteOrder.getByteOrder(dataView, dataOffset);
@@ -52,7 +53,7 @@ function read(
52
53
  valueBudget,
53
54
  getTagNames()
54
55
  );
55
- return addMpfImages(dataView, dataOffset, tags, byteOrder);
56
+ return addMpfImages(dataView, dataOffset, tags, byteOrder, getBase64Image);
56
57
  } catch (error) {
57
58
  return {};
58
59
  }
@@ -65,7 +66,7 @@ function getTagNames() {
65
66
  return tagNames;
66
67
  }
67
68
 
68
- function addMpfImages(dataView, dataOffset, tags, byteOrder) {
69
+ function addMpfImages(dataView, dataOffset, tags, byteOrder, getBase64Image) {
69
70
  if (!tags['MPEntry']) {
70
71
  return tags;
71
72
  }
@@ -115,9 +116,11 @@ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
115
116
  const end = Math.min(start + Math.max(imageSize, 0), bufferLength, start + remainingImageBytes);
116
117
  images[i].image = dataView.buffer.slice(byteOffset + start, byteOffset + end);
117
118
  remainingImageBytes -= end - start;
118
- deferInit(images[i], 'base64', function () {
119
- return getBase64Image(this.image);
120
- });
119
+ if (getBase64Image) {
120
+ deferInit(images[i], 'base64', function () {
121
+ return getBase64Image(this.image);
122
+ });
123
+ }
121
124
  }
122
125
 
123
126
  tags['Images'] = images;
@@ -4,355 +4,125 @@
4
4
 
5
5
  // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
6
 
7
- import {getStringValueFromArray, getStringFromDataView, decompress, setProperty, getTagKey, objectAssign, COMPRESSION_METHOD_NONE, getHexDigitValue} from './utils.js';
7
+ import {getStringFromDataView, decompress, getCompressionMethod, setProperty, getTagKey, COMPRESSION_METHOD_NONE, getHexDigitValue} from './utils.js';
8
8
  import TagDecoder from './tag-decoder.js';
9
- import {PNG_CHUNK_LENGTH_OFFSET, PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_DATA_OFFSET, PNG_CHUNK_TYPE_SIZE, TYPE_TEXT, TYPE_ITXT, TYPE_ZTXT} from './image-header-png.js';
10
- import {NOOP_TAG_FILTER} from './tag-filter.js';
11
- import {addDecompressedValueAllowance, addDecompressedIptcAllowance, takeEmbeddedExifThumbnail} from './tags-helpers.js';
9
+ import {TYPE_TEXT, TYPE_ITXT, TYPE_ZTXT, EXIF_PROFILE_NAME, IPTC_PROFILE_NAME} from './image-header-png.js';
10
+ import {filterTagsForParse} from './tag-filter.js';
12
11
 
13
12
  export default {
14
13
  read
15
14
  };
16
15
 
17
- const STATE_KEYWORD = 'STATE_KEYWORD';
18
- const STATE_COMPRESSION = 'STATE_COMPRESSION';
19
- const STATE_LANG = 'STATE_LANG';
20
- const STATE_TRANSLATED_KEYWORD = 'STATE_TRANSLATED_KEYWORD';
21
- const STATE_TEXT = 'STATE_TEXT';
22
- const COMPRESSION_SECTION_ITXT_EXTRA_BYTE = 1;
23
- const COMPRESSION_FLAG_COMPRESSED = 1;
24
16
  const EXIF_OFFSET = 6;
25
17
  const MAX_COMPRESSED_TEXT_CHUNKS = 255;
26
- const MAX_DECOMPRESSIONS_IN_FLIGHT = 4;
27
- // PNG spec: a keyword is 1-79 bytes.
28
- const MAX_KEYWORD_LENGTH = 79;
29
- const IPTC_PROFILE_NAME = 'raw profile type iptc';
30
- // Our own bound: BCP 47 sets no maximum, but real tags are far shorter.
31
- const MAX_LANGUAGE_TAG_LENGTH = 79;
32
18
 
33
19
  /**
34
- * @param {number[]} pngTextChunks Chunk start offsets, where each chunk's length field is.
35
- * @param {function(DataView, object, number): object} [getThumbnail] Called with a
36
- * decoded Exif raw profile, its thumbnail IFD tags and the TIFF header offset;
37
- * returns the thumbnail tags, with `image` set when the thumbnail was read.
38
- * No thumbnail is returned when it is not passed.
39
- * @param {function(DataView, object, number, number, object, {decompressed: boolean, hasIptcBlock: boolean}): object} [readCarriedGroups]
40
- * Called with a decoded Exif raw profile, the Exif tags read from it, the TIFF
41
- * header offset, the byte order, the value budget and where the profile came
42
- * from: `decompressed` when the chunk was compressed, `hasIptcBlock` when the
43
- * file has a `Raw profile type iptc` chunk. Returns the parsed groups the Exif
44
- * carries, keyed by group name. No groups are read when it is not passed.
45
- * @param {{exif?: function(DataView, number, boolean, boolean, object, object): {tags: object, byteOrder: number}, iptc?: function(DataView, number, boolean, object, object): object}} [rawProfileReaders]
46
- * Read the decoded `Raw profile type exif` and `Raw profile type iptc`
47
- * profiles: `exif(dataView, tiffHeaderOffset, includeUnknown, computed,
48
- * tagFilter, valueBudget)` as Tags.read and `iptc(dataView, offset,
49
- * includeUnknown, tagFilter, valueBudget)` as IptcTags.read. A profile
50
- * whose reader is missing is neither decoded nor read.
51
- * @returns {{readTags: object, embeddedExifTags: object|undefined, embeddedIptcTags: object|undefined, embeddedExifThumbnail: object|undefined, exifCarriedGroups: object|undefined, readTagsPromise: Promise<object[]>|undefined}}
52
- * `readTags`, the two embedded groups, `exifCarriedGroups` and
53
- * `embeddedExifThumbnail` come from uncompressed chunks and are ready at
54
- * once; the embedded groups and `exifCarriedGroups` are undefined when no
55
- * uncompressed raw profile was parsed, and `embeddedExifThumbnail` is the
56
- * first thumbnail with an image. `readTagsPromise` resolves to one result per
57
- * compressed chunk, in chunk order, each with its own `exifCarriedGroups`, and
58
- * its own `embeddedExifThumbnail` when it has one and no uncompressed chunk
59
- * had one, and is undefined when none is read.
20
+ * Reads one PNG text-chunk block: `{kind, compression?, text: {chunkType, name,
21
+ * valueOffset, valueLength}}`. A block whose `text.name` is undefined (a
22
+ * keyword or language tag over 79 bytes) is skipped, and so is a compressed
23
+ * chunk past the first MAX_COMPRESSED_TEXT_CHUNKS of a load.
24
+ *
25
+ * @param {object} block The finder's block.
26
+ * @param {object} context The load context: `dataView`, `tagFilter` and the
27
+ * pooled `decompress`.
28
+ * @returns {{tags: object, blocks?: Array<object>}|Promise<{tags: object, blocks?: Array<object>}>|undefined}
29
+ * The text tag, parse-filtered, or for a raw profile no tags and an emitted
30
+ * exif or iptc block whose `getDataView()` decodes the profile on demand
31
+ * (and throws on a bad one). A compressed chunk gives a promise; a
32
+ * decompression failure gives a placeholder text tag.
60
33
  */
61
- function read(
62
- dataView,
63
- pngTextChunks,
64
- async,
65
- includeUnknown,
66
- computed = false,
67
- tagFilter = NOOP_TAG_FILTER,
68
- decompressConfig,
69
- valueBudget,
70
- getThumbnail,
71
- readCarriedGroups,
72
- rawProfileReaders = {}
73
- ) {
74
- const tags = {};
75
- let embeddedExifTags;
76
- let embeddedIptcTags;
77
- let embeddedExifThumbnail;
78
- let exifCarriedGroups;
79
- const uncompressedRawProfiles = [];
80
- const compressedChunks = [];
81
-
82
- for (let i = 0; i < pngTextChunks.length; i++) {
83
- const chunkOffset = pngTextChunks[i];
84
- const length = dataView.getUint32(chunkOffset + PNG_CHUNK_LENGTH_OFFSET);
85
- const type = getStringFromDataView(dataView, chunkOffset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
86
- const textChunk = parseTextChunk(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET, length, type);
87
- if (!textChunk) {
88
- continue;
89
- }
90
- if (textChunk.compressionMethod === COMPRESSION_METHOD_NONE) {
91
- const tag = getUncompressedTag(textChunk);
92
- if (isRawProfileTag(tag)) {
93
- uncompressedRawProfiles.push(tag);
94
- } else if (tag.name && tagFilter.shouldParseGroup('png')) {
95
- setProperty(tags, getTagKey(tag.name), {
96
- value: tag.value,
97
- description: tag.description
98
- });
99
- }
100
- } else if (async && compressedChunks.length < MAX_COMPRESSED_TEXT_CHUNKS) {
101
- compressedChunks.push(textChunk);
102
- }
34
+ function read(block, context) {
35
+ if (block.text.name === undefined) {
36
+ return undefined;
103
37
  }
104
-
105
- const hasIptcProfile = uncompressedRawProfiles.some((tag) => isIptcGroupTag(tag.name, tag.value))
106
- || compressedChunks.some(isCompressedIptcProfile);
107
-
108
- for (let i = 0; i < uncompressedRawProfiles.length; i++) {
109
- const rawProfileTags = getTagsFromTextTag(
110
- uncompressedRawProfiles[i],
111
- includeUnknown,
112
- computed,
113
- tagFilter,
114
- valueBudget,
115
- embeddedExifThumbnail ? undefined : getThumbnail,
116
- readCarriedGroups,
117
- false,
118
- hasIptcProfile,
119
- rawProfileReaders
120
- );
121
- if (rawProfileTags.embeddedExifTags) {
122
- embeddedExifTags = objectAssign(embeddedExifTags || {}, rawProfileTags.embeddedExifTags);
123
- }
124
- if (rawProfileTags.exifCarriedGroups) {
125
- exifCarriedGroups = mergeCarriedGroups(exifCarriedGroups || {}, rawProfileTags.exifCarriedGroups);
126
- }
127
- if (rawProfileTags.embeddedExifThumbnail) {
128
- embeddedExifThumbnail = rawProfileTags.embeddedExifThumbnail;
129
- }
130
- if (rawProfileTags.embeddedIptcTags) {
131
- embeddedIptcTags = objectAssign(embeddedIptcTags || {}, rawProfileTags.embeddedIptcTags);
132
- }
38
+ if (block.compression === undefined) {
39
+ return classify(getUncompressedTag(context.dataView, block.text), context, false);
133
40
  }
134
-
135
- const compressedThumbnailReader = embeddedExifThumbnail ? undefined : getThumbnail;
136
- const decompressionTasks = compressedChunks.map((textChunk) => () => decompressTextChunk(textChunk, decompressConfig)
137
- .then((tag) => getTagsFromTextTag(
138
- tag,
139
- includeUnknown,
140
- computed,
141
- tagFilter,
142
- valueBudget,
143
- compressedThumbnailReader,
144
- readCarriedGroups,
145
- true,
146
- hasIptcProfile,
147
- rawProfileReaders
148
- )));
149
-
150
- return {
151
- readTags: tags,
152
- embeddedExifTags,
153
- embeddedIptcTags,
154
- embeddedExifThumbnail,
155
- exifCarriedGroups,
156
- readTagsPromise: decompressionTasks.length > 0 ? runTasksInOrder(decompressionTasks, MAX_DECOMPRESSIONS_IN_FLIGHT) : undefined
157
- };
158
- }
159
-
160
- // A compressed profile counts before its value can be checked.
161
- function isCompressedIptcProfile({type, langChars, keywordChars}) {
162
- return getName(type, langChars, keywordChars).toLowerCase() === IPTC_PROFILE_NAME;
41
+ if (context.compressedPngTextChunks >= MAX_COMPRESSED_TEXT_CHUNKS) {
42
+ return undefined;
43
+ }
44
+ context.compressedPngTextChunks++;
45
+ return decompressTextChunk(context, block).then((tag) => classify(tag, context, true));
163
46
  }
164
47
 
165
- function mergeCarriedGroups(target, source) {
166
- const groupKeys = Object.keys(source);
167
- for (let i = 0; i < groupKeys.length; i++) {
168
- target[groupKeys[i]] = objectAssign(target[groupKeys[i]] || {}, source[groupKeys[i]]);
48
+ // The payload check keeps a chunk whose keyword says profile but whose payload
49
+ // does not as a plain text tag.
50
+ function classify({name, value, description}, context, decompressed) {
51
+ if (isExifGroupTag(name, value)) {
52
+ return {tags: {}, blocks: [{kind: 'exif', tiffHeaderOffset: EXIF_OFFSET, origin: 'rawProfile', decompressed, getDataView: () => decodeRawData(value)}]};
53
+ }
54
+ if (isIptcGroupTag(name, value)) {
55
+ return {tags: {}, blocks: [{kind: 'iptc', dataOffset: 0, origin: 'rawProfile', decompressed, getDataView: () => decodeRawData(value)}]};
169
56
  }
170
- return target;
57
+ if (name && context.tagFilter.shouldParseGroup('png')) {
58
+ const tags = {};
59
+ setProperty(tags, getTagKey(name), {value, description});
60
+ return {tags: filterTagsForParse('png', tags, context.tagFilter)};
61
+ }
62
+ return undefined;
171
63
  }
172
64
 
173
- function parseTextChunk(dataView, offset, length, type) {
174
- const keywordChars = [];
175
- const langChars = [];
176
- let valueChars;
177
- let parsingState = STATE_KEYWORD;
178
- let compressionMethod = COMPRESSION_METHOD_NONE;
179
- const byteOffset = dataView.byteOffset;
180
- const chunkEnd = Math.min(offset + length, dataView.byteLength);
181
-
182
- for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
183
- if (parsingState === STATE_COMPRESSION) {
184
- compressionMethod = getCompressionMethod({type, dataView, offset: offset + i, chunkEnd});
185
- if (type === TYPE_ITXT) {
186
- i += COMPRESSION_SECTION_ITXT_EXTRA_BYTE;
187
- }
188
- parsingState = moveToNextState(type, parsingState);
65
+ /**
66
+ * Whether the finder blocks hold a `Raw profile type iptc` text chunk that
67
+ * `read` will read: by payload for an uncompressed chunk, by keyword alone for
68
+ * a compressed one among the first MAX_COMPRESSED_TEXT_CHUNKS named compressed
69
+ * chunks, the ones `read` decompresses.
70
+ *
71
+ * @param {Array<object>} finderBlocks In dispatch order.
72
+ * @param {DataView} dataView The file.
73
+ * @returns {boolean}
74
+ */
75
+ export function hasRawIptcProfile(finderBlocks, dataView) {
76
+ let compressedChunks = 0;
77
+ for (let i = 0; i < finderBlocks.length; i++) {
78
+ const block = finderBlocks[i];
79
+ if (block.text === undefined || block.text.name === undefined) {
189
80
  continue;
190
- } else if (parsingState === STATE_TEXT) {
191
- const slice = dataView.buffer.slice(byteOffset + offset + i, byteOffset + offset + length);
192
- valueChars = new DataView(slice);
193
- break;
194
81
  }
195
- const byte = dataView.getUint8(offset + i);
196
- if (byte === 0) {
197
- parsingState = moveToNextState(type, parsingState);
198
- } else if (parsingState === STATE_KEYWORD) {
199
- if (keywordChars.length >= MAX_KEYWORD_LENGTH) {
200
- return undefined;
201
- }
202
- keywordChars.push(byte);
203
- } else if (parsingState === STATE_LANG) {
204
- if (langChars.length >= MAX_LANGUAGE_TAG_LENGTH) {
205
- return undefined;
82
+ if (block.compression !== undefined) {
83
+ compressedChunks++;
84
+ if (block.kind === 'iptc' && compressedChunks <= MAX_COMPRESSED_TEXT_CHUNKS) {
85
+ return true;
206
86
  }
207
- langChars.push(byte);
87
+ } else if (block.kind === 'iptc' && isUncompressedIptcProfile(block, dataView)) {
88
+ return true;
208
89
  }
209
90
  }
210
-
211
- return {type, keywordChars, langChars, compressionMethod, valueChars};
91
+ return false;
212
92
  }
213
93
 
214
- function getUncompressedTag({type, keywordChars, langChars, valueChars}) {
215
- const decodedValueChars = decompress(valueChars, COMPRESSION_METHOD_NONE, getEncodingFromType(type), 'string');
216
- return constructTag(decodedValueChars, type, langChars, keywordChars);
94
+ function isUncompressedIptcProfile(block, dataView) {
95
+ const tag = getUncompressedTag(dataView, block.text);
96
+ return isIptcGroupTag(tag.name, tag.value);
217
97
  }
218
98
 
219
- function isRawProfileTag({name, value}) {
220
- return isExifGroupTag(name, value) || isIptcGroupTag(name, value);
99
+ function getUncompressedTag(dataView, text) {
100
+ const decodedValueChars = decompress(getValueView(dataView, text), COMPRESSION_METHOD_NONE, getEncodingFromType(text.chunkType), 'string');
101
+ return constructTag(decodedValueChars, text.chunkType, text.name);
221
102
  }
222
103
 
223
- function getTagsFromTextTag({name, value, description}, includeUnknown, computed, tagFilter, valueBudget, getThumbnail, readCarriedGroups, isDecompressed, hasIptcProfile, rawProfileReaders) {
224
- try {
225
- if (isExifGroupTag(name, value)) {
226
- if (!rawProfileReaders.exif || !tagFilter.shouldParseGroup('exif')) {
227
- return {};
228
- }
229
- const exifDataView = decodeRawData(value);
230
- if (isDecompressed) {
231
- addDecompressedValueAllowance(valueBudget, exifDataView.byteLength);
232
- }
233
- const {tags: embeddedExifTags, byteOrder} = rawProfileReaders.exif(
234
- exifDataView,
235
- EXIF_OFFSET,
236
- includeUnknown,
237
- computed,
238
- tagFilter,
239
- valueBudget
240
- );
241
- const result = {embeddedExifTags};
242
- if (readCarriedGroups) {
243
- result.exifCarriedGroups = readCarriedGroups(
244
- exifDataView,
245
- embeddedExifTags,
246
- EXIF_OFFSET,
247
- byteOrder,
248
- valueBudget,
249
- {decompressed: isDecompressed, hasIptcBlock: hasIptcProfile}
250
- );
251
- }
252
- if (embeddedExifTags['MakerNote']) {
253
- delete embeddedExifTags['MakerNote'].__offset;
254
- }
255
- const thumbnail = takeEmbeddedExifThumbnail(embeddedExifTags, exifDataView, EXIF_OFFSET, getThumbnail);
256
- if (thumbnail) {
257
- result.embeddedExifThumbnail = thumbnail;
258
- }
259
- return result;
260
- } else if (isIptcGroupTag(name, value)) {
261
- if (!rawProfileReaders.iptc || !tagFilter.shouldParseGroup('iptc')) {
262
- return {};
263
- }
264
- const iptcDataView = decodeRawData(value);
265
- if (isDecompressed) {
266
- addDecompressedIptcAllowance(valueBudget, iptcDataView.byteLength);
267
- }
268
- return {
269
- embeddedIptcTags: rawProfileReaders.iptc(
270
- iptcDataView,
271
- 0,
272
- includeUnknown,
273
- tagFilter,
274
- valueBudget
275
- )
276
- };
277
- } else if (name) {
278
- if (!tagFilter.shouldParseGroup('png')) {
279
- return {};
280
- }
281
- const readTags = {};
282
- setProperty(readTags, getTagKey(name), {
283
- value,
284
- description
285
- });
286
- return {readTags};
287
- }
288
- } catch (error) {
289
- // Ignore the broken tag.
290
- }
291
- return {};
292
- }
293
-
294
- function decompressTextChunk({type, keywordChars, langChars, compressionMethod, valueChars}, decompressConfig) {
295
- if (valueChars === undefined) {
296
- return Promise.resolve(constructPlaceholderTag(type, langChars, keywordChars));
297
- }
298
- return decompress(valueChars, compressionMethod, getEncodingFromType(type), 'string', decompressConfig)
299
- .then((decompressedValueChars) => constructTag(decompressedValueChars, type, langChars, keywordChars))
300
- .catch(() => constructPlaceholderTag(type, langChars, keywordChars));
301
- }
302
-
303
- function constructPlaceholderTag(type, langChars, keywordChars) {
304
- return constructTag('<text using unknown compression>', type, langChars, keywordChars);
305
- }
306
-
307
- function runTasksInOrder(tasks, limit) {
308
- const results = [];
309
- const workers = [];
310
- let nextIndex = 0;
311
-
312
- for (let i = 0; i < Math.min(limit, tasks.length); i++) {
313
- workers.push(runNext());
314
- }
315
- return Promise.all(workers).then(() => results);
316
-
317
- function runNext() {
318
- if (nextIndex >= tasks.length) {
319
- return Promise.resolve();
320
- }
321
- const index = nextIndex++;
322
- // The wrapper turns a synchronous throw from a task into a rejection
323
- // that Promise.all observes, instead of an exception out of read().
324
- return new Promise((resolve) => resolve(tasks[index]())).then((result) => {
325
- results[index] = result;
326
- return runNext();
327
- });
104
+ // The same slice the parser made while it copied the value: the chunk's declared
105
+ // end, clamped to the buffer by slice itself.
106
+ function getValueView(dataView, {valueOffset, valueLength}) {
107
+ if (valueOffset === undefined) {
108
+ return undefined;
328
109
  }
110
+ const byteOffset = dataView.byteOffset;
111
+ return new DataView(dataView.buffer.slice(byteOffset + valueOffset, byteOffset + valueOffset + valueLength));
329
112
  }
330
113
 
331
- function getCompressionMethod({type, dataView, offset, chunkEnd}) {
332
- if (type === TYPE_ITXT) {
333
- if (offset + 1 < chunkEnd && dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
334
- return dataView.getUint8(offset + 1);
335
- }
336
- } else if (type === TYPE_ZTXT) {
337
- return dataView.getUint8(offset);
114
+ function decompressTextChunk(context, {compression, text}) {
115
+ const valueView = getValueView(context.dataView, text);
116
+ if (valueView === undefined) {
117
+ return Promise.resolve(constructPlaceholderTag(text));
338
118
  }
339
- return COMPRESSION_METHOD_NONE;
119
+ return context.decompress(valueView, getCompressionMethod(compression), getEncodingFromType(text.chunkType), 'string')
120
+ .then((decompressedValueChars) => constructTag(decompressedValueChars, text.chunkType, text.name))
121
+ .catch(() => constructPlaceholderTag(text));
340
122
  }
341
123
 
342
- function moveToNextState(type, parsingState) {
343
- if (parsingState === STATE_KEYWORD && [TYPE_ITXT, TYPE_ZTXT].includes(type)) {
344
- return STATE_COMPRESSION;
345
- }
346
- if (parsingState === STATE_COMPRESSION) {
347
- if (type === TYPE_ITXT) {
348
- return STATE_LANG;
349
- }
350
- return STATE_TEXT;
351
- }
352
- if (parsingState === STATE_LANG) {
353
- return STATE_TRANSLATED_KEYWORD;
354
- }
355
- return STATE_TEXT;
124
+ function constructPlaceholderTag({chunkType, name}) {
125
+ return constructTag('<text using unknown compression>', chunkType, name);
356
126
  }
357
127
 
358
128
  function getEncodingFromType(type) {
@@ -362,25 +132,16 @@ function getEncodingFromType(type) {
362
132
  return 'utf-8';
363
133
  }
364
134
 
365
- function constructTag(valueChars, type, langChars, keywordChars) {
135
+ function constructTag(valueChars, chunkType, name) {
366
136
  const value = getValue(valueChars);
367
137
 
368
138
  return {
369
- name: getName(type, langChars, keywordChars),
139
+ name,
370
140
  value,
371
- description: type === TYPE_ITXT ? getDescription(valueChars) : value
141
+ description: chunkType === TYPE_ITXT ? getDescription(valueChars) : value
372
142
  };
373
143
  }
374
144
 
375
- function getName(type, langChars, keywordChars) {
376
- const name = getStringValueFromArray(keywordChars);
377
- if (type === TYPE_TEXT || langChars.length === 0) {
378
- return name;
379
- }
380
- const lang = getStringValueFromArray(langChars);
381
- return `${name} (${lang})`;
382
- }
383
-
384
145
  function getValue(valueChars) {
385
146
  if (valueChars instanceof DataView) {
386
147
  return getStringFromDataView(valueChars, 0, valueChars.byteLength);
@@ -393,7 +154,7 @@ function getDescription(valueChars) {
393
154
  }
394
155
 
395
156
  function isExifGroupTag(name, value) {
396
- return name.toLowerCase() === 'raw profile type exif' && value.substring(1, 5) === 'exif';
157
+ return name.toLowerCase() === EXIF_PROFILE_NAME && value.substring(1, 5) === 'exif';
397
158
  }
398
159
 
399
160
  function isIptcGroupTag(name, value) {
package/src/tag-filter.js CHANGED
@@ -2,6 +2,7 @@
2
2
  * License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
+ import {setProperty} from './utils.js';
5
6
  import {
6
7
  FILTER_GROUPS,
7
8
  ID_CAPABLE_GROUPS,
@@ -409,3 +410,54 @@ function addRequiredTags(requiredTags, tags) {
409
410
  requiredTags[tags[i]] = true;
410
411
  }
411
412
  }
413
+
414
+ export function filterTagsForParse(groupKey, readTags, tagFilter) {
415
+ if (!tagFilter.isActive) {
416
+ return readTags;
417
+ }
418
+
419
+ return filterTags(groupKey, readTags, tagFilter.shouldParseTag);
420
+ }
421
+
422
+ export function filterTagsForReturn(groupKey, readTags, tagFilter) {
423
+ if (!tagFilter.isActive) {
424
+ return readTags;
425
+ }
426
+
427
+ return filterTags(groupKey, readTags, tagFilter.shouldReturnTag);
428
+ }
429
+
430
+ export function filterTags(groupKey, readTags, matchesTag) {
431
+ if (!readTags) {
432
+ return readTags;
433
+ }
434
+
435
+ const filteredTags = {};
436
+
437
+ for (const tagName in readTags) {
438
+ const tagValue = readTags[tagName];
439
+ const tagId = getTagId(tagValue);
440
+
441
+ if (matchesTag(groupKey, tagName, tagId)) {
442
+ setProperty(filteredTags, tagName, tagValue);
443
+ }
444
+ }
445
+
446
+ return filteredTags;
447
+
448
+ function getTagId(tagValue) {
449
+ if (!tagValue) {
450
+ return undefined;
451
+ }
452
+
453
+ if (Array.isArray(tagValue)) {
454
+ if (tagValue.length === 0) {
455
+ return undefined;
456
+ }
457
+
458
+ return tagValue[0].id;
459
+ }
460
+
461
+ return tagValue.id;
462
+ }
463
+ }
@@ -4,7 +4,7 @@
4
4
 
5
5
  import TagNamesCommon from './tag-names-common.js';
6
6
  import {joinMapped} from './utils.js';
7
- import {getTableDescription, isStringValue, getStringsText} from './tag-names-utils.js';
7
+ import {getTableDescription, isArrayWithoutCharacterCodes, getStringsText} from './tag-names-utils.js';
8
8
 
9
9
  export default {
10
10
  0x000b: 'ProcessingSoftware',
@@ -385,7 +385,7 @@ function decodeXPValue(value) {
385
385
  // A one-element value arrives as a bare number, which Uint8Array would take as a length.
386
386
  return '';
387
387
  }
388
- if (isStringValue(value)) {
388
+ if (isArrayWithoutCharacterCodes(value)) {
389
389
  // An ASCII-typed value holds strings, not UCS-2 bytes.
390
390
  return getStringsText(value);
391
391
  }
@@ -7,7 +7,7 @@ import {getStringValueFromArray as getStringValue, getByteString} from './utils.
7
7
  export {getStringValue};
8
8
 
9
9
  export function getEncodedString(value) {
10
- if (isStringValue(value)) {
10
+ if (isArrayWithoutCharacterCodes(value)) {
11
11
  return getStringsText(value) || 'Undefined';
12
12
  }
13
13
  if (value.length >= 8) {
@@ -41,7 +41,7 @@ export function getEncodedString(value) {
41
41
  */
42
42
  export function getVersionString(value) {
43
43
  if (Array.isArray(value)) {
44
- if (isStringValue(value)) {
44
+ if (isArrayWithoutCharacterCodes(value)) {
45
45
  return getStringsText(value);
46
46
  }
47
47
  return getByteString(value);
@@ -50,14 +50,12 @@ export function getVersionString(value) {
50
50
  }
51
51
 
52
52
  /**
53
- * Tells whether a value is an array without character codes. Character codes
54
- * are numbers. An ASCII-typed value holds strings, with a hole where an empty
55
- * piece was, index 0 included; a rational value holds number pairs. Neither
56
- * has character codes to convert, and an empty array has none either.
53
+ * An ASCII-typed value holds strings, with a hole where an empty piece was,
54
+ * index 0 included, and a rational value holds number pairs.
57
55
  * @param {*} value
58
56
  * @returns {boolean} True for an array whose element 0 is not a number.
59
57
  */
60
- export function isStringValue(value) {
58
+ export function isArrayWithoutCharacterCodes(value) {
61
59
  return Array.isArray(value) && typeof value[0] !== 'number';
62
60
  }
63
61