exifreader 4.48.0 → 4.49.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.
@@ -4,6 +4,11 @@
4
4
 
5
5
  import Constants from './constants.js';
6
6
 
7
+ // The order the main Exif block reads them in. It is the key order of expanded
8
+ // output, and in flat output later groups win a key clash, so Exif wins over
9
+ // all of them but an ApplicationNotes XMP packet.
10
+ export const EXIF_CARRIED_GROUP_ORDER = ['iptc', 'xmp', 'photoshop', 'icc', 'makerNotes'];
11
+
7
12
  export function buildTagsFromMergeSteps({
8
13
  mergeSteps,
9
14
  deferredResults,
@@ -117,22 +122,25 @@ export function applyMergeStep({
117
122
  }
118
123
 
119
124
  if (Constants.USE_JXL && step.type === 'mergeBrobExifDeferred') {
120
- const resolvedReadTags = deferredResults[step.deferredKey];
121
- if (!resolvedReadTags || Object.keys(resolvedReadTags).length === 0) {
125
+ const resolved = deferredResults[step.deferredKey];
126
+ if (!resolved) {
122
127
  return tags;
123
128
  }
124
- const parsedExifTags =
125
- deps.filterTagsForParse('exif', resolvedReadTags, tagFilter);
126
- parsedGroups.exif = !parsedGroups.exif ? parsedExifTags : deps.objectAssign({}, parsedGroups.exif, parsedExifTags);
127
-
128
- if (!tagFilter.shouldReturnGroup('exif')) {
129
+ addEmbeddedExifThumbnail(embeddedExifThumbnails, resolved.thumbnail);
130
+ if (Object.keys(resolved.exifTags).length === 0) {
129
131
  return tags;
130
132
  }
131
133
 
132
- const returnedTags =
133
- deps.filterTagsForReturn('exif', parsedExifTags, tagFilter);
134
-
135
- return mergeAssignGroup(tags, 'exif', returnedTags, expanded, deps);
134
+ return addExifAndCarriedGroupsToTagsAndGroups({
135
+ exifTags: resolved.exifTags,
136
+ carriedGroups: resolved.carriedGroups,
137
+ parsedGroups,
138
+ expanded,
139
+ tagFilter,
140
+ tags,
141
+ deps,
142
+ merge: createCopyingMerge(deps),
143
+ });
136
144
  }
137
145
 
138
146
  if (Constants.USE_JXL && step.type === 'mergeBrobXmpDeferred') {
@@ -202,6 +210,7 @@ export function applyMergeStep({
202
210
  return addPngTextReadTagsToTagsAndGroups({
203
211
  readTags: step.readTags,
204
212
  embeddedExifTags: step.embeddedExifTags,
213
+ exifCarriedGroups: step.exifCarriedGroups,
205
214
  embeddedIptcTags: step.embeddedIptcTags,
206
215
  parsedGroups,
207
216
  expanded,
@@ -221,6 +230,7 @@ export function applyMergeStep({
221
230
  tags = addPngTextReadTagsToTagsAndGroups({
222
231
  readTags: entry.readTags || {},
223
232
  embeddedExifTags: entry.embeddedExifTags,
233
+ exifCarriedGroups: entry.exifCarriedGroups,
224
234
  embeddedIptcTags: entry.embeddedIptcTags,
225
235
  parsedGroups,
226
236
  expanded,
@@ -378,7 +388,7 @@ function hasImage(thumbnail) {
378
388
  }
379
389
 
380
390
  function getFirstEmbeddedExifThumbnailWithImage(embeddedExifThumbnails) {
381
- if (!(Constants.USE_PNG && Constants.USE_EXIF && Constants.USE_THUMBNAIL)) {
391
+ if (!((Constants.USE_PNG || Constants.USE_JXL) && Constants.USE_EXIF && Constants.USE_THUMBNAIL)) {
382
392
  return undefined;
383
393
  }
384
394
  return embeddedExifThumbnails.filter(hasImage)[0];
@@ -424,6 +434,7 @@ export function mergeMergeGroup(tags, groupKey, returnedTags, expanded, deps) {
424
434
  export function addPngTextReadTagsToTagsAndGroups({
425
435
  readTags,
426
436
  embeddedExifTags,
437
+ exifCarriedGroups,
427
438
  embeddedIptcTags,
428
439
  parsedGroups,
429
440
  expanded,
@@ -433,22 +444,16 @@ export function addPngTextReadTagsToTagsAndGroups({
433
444
  merge = createCopyingMerge(deps),
434
445
  }) {
435
446
  if (embeddedExifTags) {
436
- const parsedEmbeddedExifTags =
437
- deps.filterTagsForParse('exif', embeddedExifTags, tagFilter);
438
- merge.group(parsedGroups, 'exif', parsedEmbeddedExifTags);
439
-
440
- if (tagFilter.shouldReturnGroup('exif')) {
441
- const returnedEmbeddedExifTags = deps.filterTagsForReturn(
442
- 'exif',
443
- parsedEmbeddedExifTags,
444
- tagFilter
445
- );
446
- if (expanded) {
447
- merge.group(tags, 'exif', returnedEmbeddedExifTags);
448
- } else {
449
- tags = merge.topLevel(tags, returnedEmbeddedExifTags);
450
- }
451
- }
447
+ tags = addExifAndCarriedGroupsToTagsAndGroups({
448
+ exifTags: embeddedExifTags,
449
+ carriedGroups: exifCarriedGroups || {},
450
+ parsedGroups,
451
+ expanded,
452
+ tagFilter,
453
+ tags,
454
+ deps,
455
+ merge,
456
+ });
452
457
  }
453
458
 
454
459
  if (embeddedIptcTags) {
@@ -493,6 +498,79 @@ export function addPngTextReadTagsToTagsAndGroups({
493
498
  return tags;
494
499
  }
495
500
 
501
+ /**
502
+ * Merges one Exif block and the groups it carries in the order the main Exif
503
+ * block merges them: the carried groups, then Exif, then in flat output the
504
+ * ApplicationNotes XMP. The carried groups arrive parsed and are not filtered
505
+ * for parse again.
506
+ *
507
+ * @returns {object} The tags, which flat output replaces rather than mutates.
508
+ */
509
+ export function addExifAndCarriedGroupsToTagsAndGroups({
510
+ exifTags,
511
+ carriedGroups,
512
+ parsedGroups,
513
+ expanded,
514
+ tagFilter,
515
+ tags,
516
+ deps,
517
+ merge,
518
+ }) {
519
+ let flatXmpGroup = undefined;
520
+
521
+ for (let i = 0; i < EXIF_CARRIED_GROUP_ORDER.length; i++) {
522
+ const groupKey = EXIF_CARRIED_GROUP_ORDER[i];
523
+ const group = carriedGroups[groupKey];
524
+ if (group === undefined) {
525
+ continue;
526
+ }
527
+ if (groupKey === 'xmp' && !expanded) {
528
+ flatXmpGroup = group;
529
+ continue;
530
+ }
531
+ tags = addCarriedGroup(groupKey, group);
532
+ }
533
+
534
+ if (exifTags) {
535
+ const parsedExifTags = deps.filterTagsForParse('exif', exifTags, tagFilter);
536
+ merge.group(parsedGroups, 'exif', parsedExifTags);
537
+
538
+ if (tagFilter.shouldReturnGroup('exif')) {
539
+ const returnedExifTags = deps.filterTagsForReturn('exif', parsedExifTags, tagFilter);
540
+ if (expanded) {
541
+ merge.group(tags, 'exif', returnedExifTags);
542
+ } else {
543
+ tags = merge.topLevel(tags, returnedExifTags);
544
+ }
545
+ }
546
+ }
547
+
548
+ if (flatXmpGroup !== undefined) {
549
+ tags = addCarriedGroup('xmp', flatXmpGroup, true);
550
+ }
551
+
552
+ return tags;
553
+
554
+ function addCarriedGroup(groupKey, group, isFlatXmp) {
555
+ merge.group(parsedGroups, groupKey, group);
556
+ if (!tagFilter.shouldReturnGroup(groupKey)) {
557
+ return tags;
558
+ }
559
+ const returnedTags = deps.filterTagsForReturn(groupKey, group, tagFilter);
560
+ if (expanded) {
561
+ merge.group(tags, groupKey, returnedTags);
562
+ return tags;
563
+ }
564
+ return merge.topLevel(tags, isFlatXmp ? getXmpTagsWithoutRaw(returnedTags) : returnedTags);
565
+ }
566
+
567
+ function getXmpTagsWithoutRaw(xmpTags) {
568
+ const xmpTagsWithoutRaw = deps.objectAssign({}, xmpTags);
569
+ delete xmpTagsWithoutRaw._raw;
570
+ return xmpTagsWithoutRaw;
571
+ }
572
+ }
573
+
496
574
  function createCopyingMerge(deps) {
497
575
  return {
498
576
  group(holder, key, source) {
@@ -21,6 +21,10 @@ const SIGNATURE_SIZE = SIGNATURE.length;
21
21
  const TAG_NAME_MIN_SIZE = 2;
22
22
  const RESOURCE_BLOCK_MIN_HEADER_SIZE = SIGNATURE_SIZE + TAG_ID_SIZE + TAG_NAME_MIN_SIZE + RESOURCE_LENGTH_SIZE;
23
23
 
24
+ // Photoshop defines about 2,100 resource ids and real files carry a few dozen,
25
+ // so the cap never cuts a genuine file.
26
+ export const MAX_RESOURCES = 4096;
27
+
24
28
  function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
25
29
  if (!Array.isArray(bytes)) {
26
30
  return {};
@@ -31,7 +35,11 @@ function read(bytes, includeUnknown, tagFilter = NOOP_TAG_FILTER) {
31
35
  const tags = {};
32
36
  let offset = 0;
33
37
 
34
- while (offset + RESOURCE_BLOCK_MIN_HEADER_SIZE <= dataView.byteLength) {
38
+ for (
39
+ let resourceCount = 0;
40
+ resourceCount < MAX_RESOURCES && offset + RESOURCE_BLOCK_MIN_HEADER_SIZE <= dataView.byteLength;
41
+ resourceCount++
42
+ ) {
35
43
  const signature = getStringFromDataView(dataView, offset, SIGNATURE_SIZE);
36
44
  offset += SIGNATURE_SIZE;
37
45
  const tagId = Types.getShortAt(dataView, offset);
@@ -6,12 +6,12 @@
6
6
 
7
7
  import {getStringValueFromArray, getStringFromDataView, decompress, setProperty, getTagKey, objectAssign, COMPRESSION_METHOD_NONE, getHexDigitValue} from './utils.js';
8
8
  import TagDecoder from './tag-decoder.js';
9
- import {TYPE_TEXT, TYPE_ITXT, TYPE_ZTXT} from './image-header-png.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
10
  import Tags from './tags.js';
11
11
  import IptcTags from './iptc-tags.js';
12
12
  import Constants from './constants.js';
13
13
  import {NOOP_TAG_FILTER} from './tag-filter.js';
14
- import {addDecompressedValueAllowance, addDecompressedIptcAllowance} from './tags-helpers.js';
14
+ import {addDecompressedValueAllowance, addDecompressedIptcAllowance, takeEmbeddedExifThumbnail} from './tags-helpers.js';
15
15
 
16
16
  export default {
17
17
  read
@@ -29,22 +29,31 @@ const MAX_COMPRESSED_TEXT_CHUNKS = 255;
29
29
  const MAX_DECOMPRESSIONS_IN_FLIGHT = 4;
30
30
  // PNG spec: a keyword is 1-79 bytes.
31
31
  const MAX_KEYWORD_LENGTH = 79;
32
+ const IPTC_PROFILE_NAME = 'raw profile type iptc';
32
33
  // Our own bound: BCP 47 sets no maximum, but real tags are far shorter.
33
34
  const MAX_LANGUAGE_TAG_LENGTH = 79;
34
35
 
35
36
  /**
37
+ * @param {number[]} pngTextChunks Chunk start offsets, where each chunk's length field is.
36
38
  * @param {function(DataView, object, number): object} [getThumbnail] Called with a
37
39
  * decoded Exif raw profile, its thumbnail IFD tags and the TIFF header offset;
38
40
  * returns the thumbnail tags, with `image` set when the thumbnail was read.
39
41
  * No thumbnail is returned when it is not passed.
40
- * @returns {{readTags: object, embeddedExifTags: object|undefined, embeddedIptcTags: object|undefined, embeddedExifThumbnail: object|undefined, readTagsPromise: Promise<object[]>|undefined}}
41
- * `readTags`, the two embedded groups and `embeddedExifThumbnail` come from
42
- * uncompressed chunks and are ready at once; the embedded groups are undefined
43
- * when no uncompressed raw profile was parsed, and `embeddedExifThumbnail` is
44
- * the first thumbnail with an image. `readTagsPromise` resolves to one result
45
- * per compressed chunk, in chunk order, each with its own
46
- * `embeddedExifThumbnail` when it has one and no uncompressed chunk had one,
47
- * and is undefined when none is read.
42
+ * @param {function(DataView, object, number, number, object, {decompressed: boolean, hasIptcBlock: boolean}): object} [readCarriedGroups]
43
+ * Called with a decoded Exif raw profile, the Exif tags read from it, the TIFF
44
+ * header offset, the byte order, the value budget and where the profile came
45
+ * from: `decompressed` when the chunk was compressed, `hasIptcBlock` when the
46
+ * file has a `Raw profile type iptc` chunk. Returns the parsed groups the Exif
47
+ * carries, keyed by group name. No groups are read when it is not passed.
48
+ * @returns {{readTags: object, embeddedExifTags: object|undefined, embeddedIptcTags: object|undefined, embeddedExifThumbnail: object|undefined, exifCarriedGroups: object|undefined, readTagsPromise: Promise<object[]>|undefined}}
49
+ * `readTags`, the two embedded groups, `exifCarriedGroups` and
50
+ * `embeddedExifThumbnail` come from uncompressed chunks and are ready at
51
+ * once; the embedded groups and `exifCarriedGroups` are undefined when no
52
+ * uncompressed raw profile was parsed, and `embeddedExifThumbnail` is the
53
+ * first thumbnail with an image. `readTagsPromise` resolves to one result per
54
+ * compressed chunk, in chunk order, each with its own `exifCarriedGroups`, and
55
+ * its own `embeddedExifThumbnail` when it has one and no uncompressed chunk
56
+ * had one, and is undefined when none is read.
48
57
  */
49
58
  function read(
50
59
  dataView,
@@ -55,40 +64,29 @@ function read(
55
64
  tagFilter = NOOP_TAG_FILTER,
56
65
  decompressConfig,
57
66
  valueBudget,
58
- getThumbnail
67
+ getThumbnail,
68
+ readCarriedGroups
59
69
  ) {
60
70
  const tags = {};
61
71
  let embeddedExifTags;
62
72
  let embeddedIptcTags;
63
73
  let embeddedExifThumbnail;
74
+ let exifCarriedGroups;
75
+ const uncompressedRawProfiles = [];
64
76
  const compressedChunks = [];
65
77
 
66
78
  for (let i = 0; i < pngTextChunks.length; i++) {
67
- const {offset, length, type} = pngTextChunks[i];
68
- const textChunk = parseTextChunk(dataView, offset, length, type);
79
+ const chunkOffset = pngTextChunks[i];
80
+ const length = dataView.getUint32(chunkOffset + PNG_CHUNK_LENGTH_OFFSET);
81
+ const type = getStringFromDataView(dataView, chunkOffset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
82
+ const textChunk = parseTextChunk(dataView, chunkOffset + PNG_CHUNK_DATA_OFFSET, length, type);
69
83
  if (!textChunk) {
70
84
  continue;
71
85
  }
72
86
  if (textChunk.compressionMethod === COMPRESSION_METHOD_NONE) {
73
87
  const tag = getUncompressedTag(textChunk);
74
88
  if (isRawProfileTag(tag)) {
75
- const rawProfileTags = getTagsFromTextTag(
76
- tag,
77
- includeUnknown,
78
- computed,
79
- tagFilter,
80
- valueBudget,
81
- embeddedExifThumbnail ? undefined : getThumbnail
82
- );
83
- if (rawProfileTags.embeddedExifTags) {
84
- embeddedExifTags = objectAssign(embeddedExifTags || {}, rawProfileTags.embeddedExifTags);
85
- }
86
- if (rawProfileTags.embeddedExifThumbnail) {
87
- embeddedExifThumbnail = rawProfileTags.embeddedExifThumbnail;
88
- }
89
- if (rawProfileTags.embeddedIptcTags) {
90
- embeddedIptcTags = objectAssign(embeddedIptcTags || {}, rawProfileTags.embeddedIptcTags);
91
- }
89
+ uncompressedRawProfiles.push(tag);
92
90
  } else if (tag.name && tagFilter.shouldParseGroup('png')) {
93
91
  setProperty(tags, getTagKey(tag.name), {
94
92
  value: tag.value,
@@ -100,6 +98,35 @@ function read(
100
98
  }
101
99
  }
102
100
 
101
+ const hasIptcProfile = uncompressedRawProfiles.some((tag) => isIptcGroupTag(tag.name, tag.value))
102
+ || compressedChunks.some(isCompressedIptcProfile);
103
+
104
+ for (let i = 0; i < uncompressedRawProfiles.length; i++) {
105
+ const rawProfileTags = getTagsFromTextTag(
106
+ uncompressedRawProfiles[i],
107
+ includeUnknown,
108
+ computed,
109
+ tagFilter,
110
+ valueBudget,
111
+ embeddedExifThumbnail ? undefined : getThumbnail,
112
+ readCarriedGroups,
113
+ false,
114
+ hasIptcProfile
115
+ );
116
+ if (rawProfileTags.embeddedExifTags) {
117
+ embeddedExifTags = objectAssign(embeddedExifTags || {}, rawProfileTags.embeddedExifTags);
118
+ }
119
+ if (rawProfileTags.exifCarriedGroups) {
120
+ exifCarriedGroups = mergeCarriedGroups(exifCarriedGroups || {}, rawProfileTags.exifCarriedGroups);
121
+ }
122
+ if (rawProfileTags.embeddedExifThumbnail) {
123
+ embeddedExifThumbnail = rawProfileTags.embeddedExifThumbnail;
124
+ }
125
+ if (rawProfileTags.embeddedIptcTags) {
126
+ embeddedIptcTags = objectAssign(embeddedIptcTags || {}, rawProfileTags.embeddedIptcTags);
127
+ }
128
+ }
129
+
103
130
  const compressedThumbnailReader = embeddedExifThumbnail ? undefined : getThumbnail;
104
131
  const decompressionTasks = compressedChunks.map((textChunk) => () => decompressTextChunk(textChunk, decompressConfig)
105
132
  .then((tag) => getTagsFromTextTag(
@@ -109,7 +136,9 @@ function read(
109
136
  tagFilter,
110
137
  valueBudget,
111
138
  compressedThumbnailReader,
112
- true
139
+ readCarriedGroups,
140
+ true,
141
+ hasIptcProfile
113
142
  )));
114
143
 
115
144
  return {
@@ -117,10 +146,24 @@ function read(
117
146
  embeddedExifTags,
118
147
  embeddedIptcTags,
119
148
  embeddedExifThumbnail,
149
+ exifCarriedGroups,
120
150
  readTagsPromise: decompressionTasks.length > 0 ? runTasksInOrder(decompressionTasks, MAX_DECOMPRESSIONS_IN_FLIGHT) : undefined
121
151
  };
122
152
  }
123
153
 
154
+ // A compressed profile counts before its value can be checked.
155
+ function isCompressedIptcProfile({type, langChars, keywordChars}) {
156
+ return getName(type, langChars, keywordChars).toLowerCase() === IPTC_PROFILE_NAME;
157
+ }
158
+
159
+ function mergeCarriedGroups(target, source) {
160
+ const groupKeys = Object.keys(source);
161
+ for (let i = 0; i < groupKeys.length; i++) {
162
+ target[groupKeys[i]] = objectAssign(target[groupKeys[i]] || {}, source[groupKeys[i]]);
163
+ }
164
+ return target;
165
+ }
166
+
124
167
  function parseTextChunk(dataView, offset, length, type) {
125
168
  const keywordChars = [];
126
169
  const langChars = [];
@@ -173,7 +216,7 @@ function isRawProfileTag({name, value}) {
173
216
  return isExifGroupTag(name, value) || isIptcGroupTag(name, value);
174
217
  }
175
218
 
176
- function getTagsFromTextTag({name, value, description}, includeUnknown, computed, tagFilter, valueBudget, getThumbnail, isDecompressed) {
219
+ function getTagsFromTextTag({name, value, description}, includeUnknown, computed, tagFilter, valueBudget, getThumbnail, readCarriedGroups, isDecompressed, hasIptcProfile) {
177
220
  try {
178
221
  if (Constants.USE_EXIF && isExifGroupTag(name, value)) {
179
222
  if (!tagFilter.shouldParseGroup('exif')) {
@@ -183,18 +226,33 @@ function getTagsFromTextTag({name, value, description}, includeUnknown, computed
183
226
  if (isDecompressed) {
184
227
  addDecompressedValueAllowance(valueBudget, exifDataView.byteLength);
185
228
  }
186
- const embeddedExifTags = Tags.read(
229
+ const {tags: embeddedExifTags, byteOrder} = Tags.read(
187
230
  exifDataView,
188
231
  EXIF_OFFSET,
189
232
  includeUnknown,
190
233
  computed,
191
234
  tagFilter,
192
235
  valueBudget
193
- ).tags;
236
+ );
237
+ const result = {embeddedExifTags};
238
+ if (readCarriedGroups) {
239
+ result.exifCarriedGroups = readCarriedGroups(
240
+ exifDataView,
241
+ embeddedExifTags,
242
+ EXIF_OFFSET,
243
+ byteOrder,
244
+ valueBudget,
245
+ {decompressed: isDecompressed, hasIptcBlock: hasIptcProfile}
246
+ );
247
+ }
194
248
  if (embeddedExifTags['MakerNote']) {
195
249
  delete embeddedExifTags['MakerNote'].__offset;
196
250
  }
197
- return withEmbeddedExifThumbnail({embeddedExifTags}, exifDataView, getThumbnail);
251
+ const thumbnail = takeEmbeddedExifThumbnail(embeddedExifTags, exifDataView, EXIF_OFFSET, getThumbnail);
252
+ if (thumbnail) {
253
+ result.embeddedExifThumbnail = thumbnail;
254
+ }
255
+ return result;
198
256
  } else if (Constants.USE_IPTC && isIptcGroupTag(name, value)) {
199
257
  if (!tagFilter.shouldParseGroup('iptc')) {
200
258
  return {};
@@ -229,22 +287,6 @@ function getTagsFromTextTag({name, value, description}, includeUnknown, computed
229
287
  return {};
230
288
  }
231
289
 
232
- function withEmbeddedExifThumbnail(result, exifDataView, getThumbnail) {
233
- const thumbnailIfdTags = result.embeddedExifTags.Thumbnail;
234
- if (!thumbnailIfdTags) {
235
- return result;
236
- }
237
- delete result.embeddedExifTags.Thumbnail;
238
-
239
- if (getThumbnail) {
240
- const thumbnail = getThumbnail(exifDataView, thumbnailIfdTags, EXIF_OFFSET);
241
- if (thumbnail.image) {
242
- result.embeddedExifThumbnail = thumbnail;
243
- }
244
- }
245
- return result;
246
- }
247
-
248
290
  function decompressTextChunk({type, keywordChars, langChars, compressionMethod, valueChars}, decompressConfig) {
249
291
  if (valueChars === undefined) {
250
292
  return Promise.resolve(constructPlaceholderTag(type, langChars, keywordChars));
@@ -351,7 +393,7 @@ function isExifGroupTag(name, value) {
351
393
  }
352
394
 
353
395
  function isIptcGroupTag(name, value) {
354
- return name.toLowerCase() === 'raw profile type iptc' && value.substring(1, 5) === 'iptc';
396
+ return name.toLowerCase() === IPTC_PROFILE_NAME && value.substring(1, 5) === 'iptc';
355
397
  }
356
398
 
357
399
  function decodeRawData(value) {
@@ -3,7 +3,7 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import TextDecoder from './text-decoder.js';
6
- import {decodeUtf8ByteString, tryDecodeUtf8ByteString} from './utils.js';
6
+ import {decodeUtf8ByteString, getByteString, tryDecodeUtf8ByteString} from './utils.js';
7
7
 
8
8
  const TAG_HEADER_SIZE = 5;
9
9
 
@@ -42,7 +42,7 @@ function decode(encoding, tagValue) {
42
42
  }
43
43
  }
44
44
 
45
- const stringValue = tagValue.map((charCode) => String.fromCharCode(charCode)).join('');
45
+ const stringValue = getByteString(tagValue);
46
46
  const decoded = tryDecodeUtf8ByteString(stringValue);
47
47
  if (decoded !== undefined) {
48
48
  return decoded;
@@ -65,5 +65,5 @@ function decodeWindows1252(tagValue) {
65
65
  const mapped = WINDOWS_1252_C1_MAP[byte];
66
66
  codePoints[i] = mapped !== undefined ? mapped : byte;
67
67
  }
68
- return String.fromCharCode.apply(null, codePoints);
68
+ return getByteString(codePoints);
69
69
  }
@@ -34,6 +34,21 @@ export const EXIF_POINTER_TAGS = {
34
34
  interoperabilityIfdPointer: 'Interoperability IFD Pointer',
35
35
  };
36
36
 
37
+ // The two id sets mirror the keys of tag-names-gps-ifd.js and tag-names-interoperability-ifd.js.
38
+ // Importing those files here instead would bring them into builds that leave Exif out.
39
+ const GPS_IFD_TAG_IDS = getIdLookup([[0x0000, 0x001f]]);
40
+ const INTEROPERABILITY_IFD_TAG_IDS = getIdLookup([[0x0001, 0x0002], [0x1000, 0x1002]]);
41
+
42
+ function getIdLookup(idRanges) {
43
+ const lookup = Object.create(null);
44
+ for (let i = 0; i < idRanges.length; i++) {
45
+ for (let id = idRanges[i][0]; id <= idRanges[i][1]; id++) {
46
+ lookup[id] = true;
47
+ }
48
+ }
49
+ return lookup;
50
+ }
51
+
37
52
  export const EXIF_DEPENDENCY_TAGS = {
38
53
  thumbnail: [
39
54
  'JPEGInterchangeFormat',
@@ -99,6 +114,14 @@ export function getExifTagDependenciesForInclude(includeTags) {
99
114
  function addPointerDependenciesFromExifTagSelectors(pointerSet, selectors) {
100
115
  for (let i = 0; i < selectors.length; i++) {
101
116
  const selector = selectors[i];
117
+ if (typeof selector === 'number') {
118
+ if (GPS_IFD_TAG_IDS[selector]) {
119
+ pointerSet[EXIF_POINTER_TAGS.gpsInfoIfdPointer] = true;
120
+ }
121
+ if (INTEROPERABILITY_IFD_TAG_IDS[selector]) {
122
+ pointerSet[EXIF_POINTER_TAGS.interoperabilityIfdPointer] = true;
123
+ }
124
+ }
102
125
  if (typeof selector !== 'string') {
103
126
  continue;
104
127
  }