exifreader 4.41.1 → 4.41.3

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.
@@ -1,5 +1,5 @@
1
1
  import Constants from './constants.js';
2
- import {getNullTerminatedStringFromDataView, getStringFromDataView} from './utils.js';
2
+ import {getNullTerminatedStringFromDataView, getStringFromDataView, pushMetadataBlock} from './utils.js';
3
3
  // import {get64BitValue} from './image-header-iso-bmff-utils.js';
4
4
  import {parseItemLocationBox} from './image-header-iso-bmff-iloc.js';
5
5
  import {hasBytes} from './image-header-iso-bmff-utils.js';
@@ -35,6 +35,9 @@ export const ITEM_INFO_TYPE_EXIF = 0x45786966;
35
35
  export const ITEM_INFO_TYPE_MIME = 0x6d696d65;
36
36
  const ITEM_INFO_TYPE_URI = 0x75726920;
37
37
 
38
+ export const BOX_TYPE_OFFSET = 4;
39
+ export const BOX_MIN_LENGTH = 8;
40
+
38
41
  /**
39
42
  * Parses a ISO-BMFF box from the provided data view starting at the given offset.
40
43
  *
@@ -43,8 +46,6 @@ const ITEM_INFO_TYPE_URI = 0x75726920;
43
46
  * @returns {Object} The parsed box.
44
47
  */
45
48
  export function parseBox(dataView, offset) {
46
- const BOX_TYPE_OFFSET = 4;
47
- const BOX_MIN_LENGTH = 8;
48
49
  const VERSION_SIZE = 1;
49
50
 
50
51
  if (!hasBytes(dataView, offset, BOX_MIN_LENGTH)) {
@@ -107,6 +108,27 @@ export function parseBox(dataView, offset) {
107
108
  }
108
109
  }
109
110
 
111
+ /**
112
+ * Checks if the data view starts with an ftyp box whose major brand is one
113
+ * of the provided brands.
114
+ *
115
+ * @param {DataView} dataView The data view to check.
116
+ * @param {Array<string>} brands The major brands to accept.
117
+ * @returns {boolean} True if the file's major brand is in the list.
118
+ */
119
+ export function hasMajorBrand(dataView, brands) {
120
+ if (!dataView) {
121
+ return false;
122
+ }
123
+
124
+ try {
125
+ const headerBox = parseBox(dataView, 0);
126
+ return headerBox !== undefined && brands.indexOf(headerBox.majorBrand) !== -1;
127
+ } catch (error) {
128
+ return false;
129
+ }
130
+ }
131
+
110
132
  /**
111
133
  * @typedef {Object} BoxLength
112
134
  * @property {number} length The length of the box including length and type.
@@ -287,11 +309,7 @@ function pushIlocExtentBlocks(metadataBlocks, ilocItem, blockType, idatContentOf
287
309
  if (start === undefined) {
288
310
  continue;
289
311
  }
290
- metadataBlocks.push({
291
- type: blockType,
292
- start,
293
- end: start + extent.extentLength,
294
- });
312
+ pushMetadataBlock(metadataBlocks, blockType, start, start + extent.extentLength);
295
313
  }
296
314
  }
297
315
 
@@ -422,13 +440,7 @@ function findIccChunks(metaBox, metadataBlocks) {
422
440
  .properties.find((box) => box.type === 'colr')
423
441
  .icc;
424
442
  if (icc) {
425
- if (metadataBlocks) {
426
- metadataBlocks.push({
427
- type: 'icc',
428
- start: icc.offset,
429
- end: icc.offset + icc.length,
430
- });
431
- }
443
+ pushMetadataBlock(metadataBlocks, 'icc', icc.offset, icc.offset + icc.length);
432
444
  return [icc];
433
445
  }
434
446
  } catch (error) {
@@ -537,32 +549,17 @@ function parseSubBoxes(dataView, offset, length) {
537
549
  }
538
550
 
539
551
  function parseItemInformationBox(dataView, startOffset, version, contentOffset, length) {
540
- const {offsets} = getItemInformationBoxOffsetsAndSizes(version, contentOffset);
552
+ const FLAGS_SIZE = 3;
553
+ const entryCountSize = version === 0 ? 2 : 4;
554
+ const itemInfosOffset = contentOffset + FLAGS_SIZE + entryCountSize;
541
555
 
542
556
  return {
543
557
  type: 'iinf',
544
- itemInfos: parseSubBoxes(dataView, offsets.itemInfos, length - (offsets.itemInfos - startOffset)),
558
+ itemInfos: parseSubBoxes(dataView, itemInfosOffset, length - (itemInfosOffset - startOffset)),
545
559
  length
546
560
  };
547
561
  }
548
562
 
549
- function getItemInformationBoxOffsetsAndSizes(version, contentOffset) {
550
- const FLAGS_SIZE = 3;
551
-
552
- const offsets = {entryCount: contentOffset + FLAGS_SIZE};
553
- const sizes = {};
554
-
555
- if (version === 0) {
556
- sizes.entryCount = 2;
557
- } else {
558
- sizes.entryCount = 4;
559
- }
560
-
561
- offsets.itemInfos = offsets.entryCount + sizes.entryCount;
562
-
563
- return {offsets};
564
- }
565
-
566
563
  function parseItemInformationEntryBox(dataView, startOffset, version, contentOffset, length) {
567
564
  const FLAGS_SIZE = 3;
568
565
 
@@ -2,7 +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 {getStringFromDataView} from './utils.js';
5
+ import {getStringFromDataView, pushMetadataBlock} from './utils.js';
6
6
  import Constants from './constants.js';
7
7
  import ByteOrder from './byte-order.js';
8
8
  import {
@@ -97,10 +97,7 @@ function findJpegOffsets(dataView, metadataBlocks) {
97
97
  let iccChunks;
98
98
  let mpfDataOffset;
99
99
  let exifSegmentCount = 0;
100
- let bestExifSegmentScore;
101
- let bestExifSegmentTiffHeaderOffset;
102
- let bestExifSegmentAppMarkerPosition;
103
- let bestExifSegmentFieldLength;
100
+ let bestExifSegment;
104
101
  let sawSos = false;
105
102
 
106
103
  while (appMarkerPosition + APP_ID_OFFSET + 5 <= dataView.byteLength) {
@@ -128,45 +125,40 @@ function findJpegOffsets(dataView, metadataBlocks) {
128
125
  const currentTiffHeaderOffset = appMarkerPosition + TIFF_HEADER_OFFSET;
129
126
 
130
127
  if (exifSegmentCount === 1) {
131
- bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
132
- bestExifSegmentAppMarkerPosition = appMarkerPosition;
133
- bestExifSegmentFieldLength = fieldLength;
134
- } else if (exifSegmentCount === 2) {
135
- bestExifSegmentScore = getExifMarkerScore(
136
- dataView,
137
- bestExifSegmentAppMarkerPosition,
138
- bestExifSegmentFieldLength,
139
- bestExifSegmentTiffHeaderOffset
140
- );
141
- const currentExifSegmentScore = getExifMarkerScore(
142
- dataView,
128
+ // The single-segment case must stay cheap, so the first
129
+ // segment is only scored once a second one shows up.
130
+ bestExifSegment = {
143
131
  appMarkerPosition,
144
132
  fieldLength,
145
- currentTiffHeaderOffset
146
- );
147
-
148
- if (currentExifSegmentScore > bestExifSegmentScore) {
149
- bestExifSegmentScore = currentExifSegmentScore;
150
- bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
151
- bestExifSegmentAppMarkerPosition = appMarkerPosition;
152
- bestExifSegmentFieldLength = fieldLength;
153
- }
133
+ tiffHeaderOffset: currentTiffHeaderOffset,
134
+ score: undefined,
135
+ };
154
136
  } else {
137
+ if (bestExifSegment.score === undefined) {
138
+ bestExifSegment.score = getExifMarkerScore(
139
+ dataView,
140
+ bestExifSegment.appMarkerPosition,
141
+ bestExifSegment.fieldLength,
142
+ bestExifSegment.tiffHeaderOffset
143
+ );
144
+ }
155
145
  const currentExifSegmentScore = getExifMarkerScore(
156
146
  dataView,
157
147
  appMarkerPosition,
158
148
  fieldLength,
159
149
  currentTiffHeaderOffset
160
150
  );
161
- if (currentExifSegmentScore > bestExifSegmentScore) {
162
- bestExifSegmentScore = currentExifSegmentScore;
163
- bestExifSegmentTiffHeaderOffset = currentTiffHeaderOffset;
164
- bestExifSegmentAppMarkerPosition = appMarkerPosition;
165
- bestExifSegmentFieldLength = fieldLength;
151
+ if (currentExifSegmentScore > bestExifSegment.score) {
152
+ bestExifSegment = {
153
+ appMarkerPosition,
154
+ fieldLength,
155
+ tiffHeaderOffset: currentTiffHeaderOffset,
156
+ score: currentExifSegmentScore,
157
+ };
166
158
  }
167
159
  }
168
160
 
169
- tiffHeaderOffset = bestExifSegmentTiffHeaderOffset;
161
+ tiffHeaderOffset = bestExifSegment.tiffHeaderOffset;
170
162
  blockType = 'exif';
171
163
  } else if (Constants.USE_XMP && isApp1XmpMarker(dataView, appMarkerPosition)) {
172
164
  if (!xmpChunks) {
@@ -210,13 +202,7 @@ function findJpegOffsets(dataView, metadataBlocks) {
210
202
  } else {
211
203
  break;
212
204
  }
213
- if (metadataBlocks && blockType) {
214
- metadataBlocks.push({
215
- type: blockType,
216
- start: appMarkerPosition,
217
- end: appMarkerPosition + APP_MARKER_SIZE + fieldLength,
218
- });
219
- }
205
+ pushMetadataBlock(metadataBlocks, blockType, appMarkerPosition, appMarkerPosition + APP_MARKER_SIZE + fieldLength);
220
206
  appMarkerPosition += APP_MARKER_SIZE + fieldLength;
221
207
  }
222
208
 
@@ -3,7 +3,8 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import Constants from './constants.js';
6
- import {getBoxLength, getTiffHeaderOffset} from './image-header-iso-bmff.js';
6
+ import {getBoxLength, getTiffHeaderOffset, BOX_TYPE_OFFSET, BOX_MIN_LENGTH} from './image-header-iso-bmff.js';
7
+ import {pushMetadataBlock} from './utils.js';
7
8
 
8
9
  export default {
9
10
  isJxlFile,
@@ -17,8 +18,6 @@ const JXL_CONTAINER_SIGNATURE = [
17
18
  ];
18
19
  const JXL_CODESTREAM_SIGNATURE = [0xFF, 0x0A];
19
20
 
20
- const BOX_TYPE_OFFSET = 4;
21
- const BOX_HEADER_MIN_SIZE = 8;
22
21
  const TYPE_EXIF = 0x45786966;
23
22
  const TYPE_XML = 0x786D6C20;
24
23
  const TYPE_BROB = 0x62726F62;
@@ -97,9 +96,9 @@ function findJxlOffsets(dataView, metadataBlocks) {
97
96
  };
98
97
  }
99
98
 
100
- while (offset + BOX_HEADER_MIN_SIZE <= dataView.byteLength) {
99
+ while (offset + BOX_MIN_LENGTH <= dataView.byteLength) {
101
100
  const {length, contentOffset} = getBoxLength(dataView, offset);
102
- if (length < BOX_HEADER_MIN_SIZE) {
101
+ if (length < BOX_MIN_LENGTH) {
103
102
  break;
104
103
  }
105
104
 
@@ -167,13 +166,7 @@ function findJxlOffsets(dataView, metadataBlocks) {
167
166
  }
168
167
  }
169
168
 
170
- if (metadataBlocks && blockType) {
171
- metadataBlocks.push({
172
- type: blockType,
173
- start: offset,
174
- end: offset + length,
175
- });
176
- }
169
+ pushMetadataBlock(metadataBlocks, blockType, offset, offset + length);
177
170
 
178
171
  offset += length;
179
172
  }
@@ -4,7 +4,7 @@
4
4
 
5
5
  // Specification: http://www.libpng.org/pub/png/spec/1.2/
6
6
 
7
- import {getStringFromDataView, getNullTerminatedStringFromDataView} from './utils.js';
7
+ import {getStringFromDataView, getNullTerminatedStringFromDataView, pushMetadataBlock} from './utils.js';
8
8
  import Constants from './constants.js';
9
9
 
10
10
  export default {
@@ -130,13 +130,7 @@ function findPngOffsets(dataView, async, metadataBlocks) {
130
130
  + PNG_CHUNK_TYPE_SIZE
131
131
  + PNG_CRC_SIZE;
132
132
 
133
- if (metadataBlocks && blockType) {
134
- metadataBlocks.push({
135
- type: blockType,
136
- start: offset,
137
- end: offset + chunkTotalSize,
138
- });
139
- }
133
+ pushMetadataBlock(metadataBlocks, blockType, offset, offset + chunkTotalSize);
140
134
 
141
135
  offset += chunkTotalSize;
142
136
  }
@@ -2,7 +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 {getStringFromDataView} from './utils.js';
5
+ import {getStringFromDataView, pushMetadataBlock} from './utils.js';
6
6
  import Constants from './constants.js';
7
7
 
8
8
  export default {
@@ -85,13 +85,7 @@ function findOffsets(dataView, metadataBlocks) {
85
85
 
86
86
  const chunkTotalSize = CHUNK_HEADER_SIZE + (chunkSize % 2 === 0 ? chunkSize : chunkSize + 1);
87
87
 
88
- if (metadataBlocks && blockType) {
89
- metadataBlocks.push({
90
- type: blockType,
91
- start: offset,
92
- end: offset + chunkTotalSize,
93
- });
94
- }
88
+ pushMetadataBlock(metadataBlocks, blockType, offset, offset + chunkTotalSize);
95
89
 
96
90
  offset += chunkTotalSize;
97
91
  }
package/src/load-auto.js CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  concatBuffers, isFilePathOrURL, isBrowserFileObject, isDataUri,
10
10
  HTTP_STATUS_OK, HTTP_STATUS_RANGE_NOT_SATISFIABLE,
11
11
  } from './file-loaders.js';
12
- import {objectAssign, dataUriToBuffer} from './utils.js';
12
+ import {objectAssign, dataUriToBuffer, assertPromiseSupport} from './utils.js';
13
13
 
14
14
  const AUTO_INITIAL_LENGTH = 128 * 1024;
15
15
  const AUTO_MAX_ITERATIONS = 4;
@@ -42,9 +42,7 @@ export function validateAutoOptions(options) {
42
42
  */
43
43
  export function makeLoadAuto(loadFromData) {
44
44
  return function loadAuto(data, options) {
45
- if (typeof Promise === 'undefined') {
46
- throw new Error('Promise is required when async mode is enabled.');
47
- }
45
+ assertPromiseSupport();
48
46
  const asyncOptions = objectAssign({}, options, {async: true});
49
47
  if (isFilePathOrURL(data)) {
50
48
  return loadAdaptiveFromFilenameOrUrl(data, asyncOptions);
@@ -94,16 +94,7 @@ export function applyMergeStep({
94
94
  const returnedTags =
95
95
  deps.filterTagsForReturn('xmp', step.parsedTags, tagFilter);
96
96
 
97
- if (expanded) {
98
- tags.xmp = returnedTags;
99
-
100
- return tags;
101
- }
102
-
103
- const returnedTagsForFlat = deps.objectAssign({}, returnedTags);
104
- delete returnedTagsForFlat._raw;
105
-
106
- return deps.objectAssign({}, tags, returnedTagsForFlat);
97
+ return mergeXmpGroup(tags, returnedTags, expanded, deps);
107
98
  }
108
99
 
109
100
  if (Constants.USE_ICC && step.type === 'mergeIccDeferred') {
@@ -157,15 +148,7 @@ export function applyMergeStep({
157
148
  const returnedTags =
158
149
  deps.filterTagsForReturn('xmp', parsedXmpTags, tagFilter);
159
150
 
160
- if (expanded) {
161
- tags.xmp = returnedTags;
162
- return tags;
163
- }
164
-
165
- const returnedTagsForFlat = deps.objectAssign({}, returnedTags);
166
- delete returnedTagsForFlat._raw;
167
-
168
- return deps.objectAssign({}, tags, returnedTagsForFlat);
151
+ return mergeXmpGroup(tags, returnedTags, expanded, deps);
169
152
  }
170
153
 
171
154
  if (Constants.USE_PNG && step.type === 'mergePngFile') {
@@ -369,6 +352,19 @@ export function mergeAssignGroup(tags, groupKey, returnedTags, expanded, deps) {
369
352
  return deps.objectAssign({}, tags, returnedTags);
370
353
  }
371
354
 
355
+ function mergeXmpGroup(tags, returnedTags, expanded, deps) {
356
+ if (expanded) {
357
+ tags.xmp = returnedTags;
358
+
359
+ return tags;
360
+ }
361
+
362
+ const returnedTagsForFlat = deps.objectAssign({}, returnedTags);
363
+ delete returnedTagsForFlat._raw;
364
+
365
+ return deps.objectAssign({}, tags, returnedTagsForFlat);
366
+ }
367
+
372
368
  export function mergeMergeGroup(tags, groupKey, returnedTags, expanded, deps) {
373
369
  if (expanded) {
374
370
  if (!tags[groupKey]) {
package/src/package.json CHANGED
@@ -1 +1 @@
1
- {"type": "module"}
1
+ {"type": "module", "sideEffects": false}
@@ -8,130 +8,45 @@ export default {
8
8
  read
9
9
  };
10
10
 
11
- function read(dataView, fileDataOffset) {
12
- return {
13
- 'Image Width': getImageWidth(dataView, fileDataOffset),
14
- 'Image Height': getImageHeight(dataView, fileDataOffset),
15
- 'Bit Depth': getBitDepth(dataView, fileDataOffset),
16
- 'Color Type': getColorType(dataView, fileDataOffset),
17
- 'Compression': getCompression(dataView, fileDataOffset),
18
- 'Filter': getFilter(dataView, fileDataOffset),
19
- 'Interlace': getInterlace(dataView, fileDataOffset)
20
- };
21
- }
22
-
23
- function getImageWidth(dataView, fileDataOffset) {
24
- const OFFSET = 0;
25
- const SIZE = 4;
26
-
27
- if (fileDataOffset + OFFSET + SIZE > dataView.byteLength) {
28
- return undefined;
29
- }
30
-
31
- const value = Types.getLongAt(dataView, fileDataOffset);
32
- return {
33
- value,
34
- description: `${value}px`
35
- };
36
- }
37
-
38
- function getImageHeight(dataView, fileDataOffset) {
39
- const OFFSET = 4;
40
- const SIZE = 4;
41
-
42
- if (fileDataOffset + OFFSET + SIZE > dataView.byteLength) {
43
- return undefined;
44
- }
45
-
46
- const value = Types.getLongAt(dataView, fileDataOffset + OFFSET);
47
- return {
48
- value,
49
- description: `${value}px`
50
- };
51
- }
52
-
53
- function getBitDepth(dataView, fileDataOffset) {
54
- const OFFSET = 8;
55
- const SIZE = 1;
56
-
57
- if (fileDataOffset + OFFSET + SIZE > dataView.byteLength) {
58
- return undefined;
59
- }
60
-
61
- const value = Types.getByteAt(dataView, fileDataOffset + OFFSET);
62
- return {
63
- value,
64
- description: `${value}`
65
- };
66
- }
67
-
68
- function getColorType(dataView, fileDataOffset) {
69
- const OFFSET = 9;
70
- const SIZE = 1;
71
- const COLOR_TYPES = {
72
- 0: 'Grayscale',
73
- 2: 'RGB',
74
- 3: 'Palette',
75
- 4: 'Grayscale with Alpha',
76
- 6: 'RGB with Alpha'
77
- };
78
-
79
- if (fileDataOffset + OFFSET + SIZE > dataView.byteLength) {
80
- return undefined;
81
- }
82
-
83
- const value = Types.getByteAt(dataView, fileDataOffset + OFFSET);
84
- return {
85
- value,
86
- description: COLOR_TYPES[value] || 'Unknown'
87
- };
88
- }
89
-
90
- function getCompression(dataView, fileDataOffset) {
91
- const OFFSET = 10;
92
- const SIZE = 1;
93
-
94
- if (fileDataOffset + OFFSET + SIZE > dataView.byteLength) {
95
- return undefined;
96
- }
11
+ const COLOR_TYPES = {
12
+ 0: 'Grayscale',
13
+ 2: 'RGB',
14
+ 3: 'Palette',
15
+ 4: 'Grayscale with Alpha',
16
+ 6: 'RGB with Alpha'
17
+ };
97
18
 
98
- const value = Types.getByteAt(dataView, fileDataOffset + OFFSET);
99
- return {
100
- value,
101
- description: value === 0 ? 'Deflate/Inflate' : 'Unknown'
102
- };
103
- }
19
+ const INTERLACE_TYPES = {
20
+ 0: 'Noninterlaced',
21
+ 1: 'Adam7 Interlace'
22
+ };
104
23
 
105
- function getFilter(dataView, fileDataOffset) {
106
- const OFFSET = 11;
107
- const SIZE = 1;
24
+ const FIELDS = [
25
+ {name: 'Image Width', offset: 0, size: 4, read: Types.getLongAt, description: (value) => `${value}px`},
26
+ {name: 'Image Height', offset: 4, size: 4, read: Types.getLongAt, description: (value) => `${value}px`},
27
+ {name: 'Bit Depth', offset: 8, size: 1, read: Types.getByteAt, description: (value) => `${value}`},
28
+ {name: 'Color Type', offset: 9, size: 1, read: Types.getByteAt, description: (value) => COLOR_TYPES[value] || 'Unknown'},
29
+ {name: 'Compression', offset: 10, size: 1, read: Types.getByteAt, description: (value) => value === 0 ? 'Deflate/Inflate' : 'Unknown'},
30
+ {name: 'Filter', offset: 11, size: 1, read: Types.getByteAt, description: (value) => value === 0 ? 'Adaptive' : 'Unknown'},
31
+ {name: 'Interlace', offset: 12, size: 1, read: Types.getByteAt, description: (value) => INTERLACE_TYPES[value] || 'Unknown'}
32
+ ];
108
33
 
109
- if (fileDataOffset + OFFSET + SIZE > dataView.byteLength) {
110
- return undefined;
34
+ function read(dataView, fileDataOffset) {
35
+ const tags = {};
36
+ for (let i = 0; i < FIELDS.length; i++) {
37
+ tags[FIELDS[i].name] = getFieldTag(dataView, fileDataOffset, FIELDS[i]);
111
38
  }
112
-
113
- const value = Types.getByteAt(dataView, fileDataOffset + OFFSET);
114
- return {
115
- value,
116
- description: value === 0 ? 'Adaptive' : 'Unknown'
117
- };
39
+ return tags;
118
40
  }
119
41
 
120
- function getInterlace(dataView, fileDataOffset) {
121
- const OFFSET = 12;
122
- const SIZE = 1;
123
- const INTERLACE_TYPES = {
124
- 0: 'Noninterlaced',
125
- 1: 'Adam7 Interlace'
126
- };
127
-
128
- if (fileDataOffset + OFFSET + SIZE > dataView.byteLength) {
42
+ function getFieldTag(dataView, fileDataOffset, field) {
43
+ if (fileDataOffset + field.offset + field.size > dataView.byteLength) {
129
44
  return undefined;
130
45
  }
131
46
 
132
- const value = Types.getByteAt(dataView, fileDataOffset + OFFSET);
47
+ const value = field.read(dataView, fileDataOffset + field.offset);
133
48
  return {
134
49
  value,
135
- description: INTERLACE_TYPES[value] || 'Unknown'
50
+ description: field.description(value)
136
51
  };
137
52
  }
@@ -2,9 +2,9 @@
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
- export function getStringValue(value) {
6
- return value.map((charCode) => String.fromCharCode(charCode)).join('');
7
- }
5
+ import {getStringValueFromArray as getStringValue} from './utils.js';
6
+
7
+ export {getStringValue};
8
8
 
9
9
  export function getEncodedString(value) {
10
10
  if (value.length >= 8) {
package/src/tags.js CHANGED
@@ -7,9 +7,11 @@ import ByteOrder from './byte-order.js';
7
7
  import {IFD_TYPE_0TH, IFD_TYPE_EXIF, IFD_TYPE_GPS, IFD_TYPE_INTEROPERABILITY} from './tag-names.js';
8
8
  import {readIfd, get0thIfdOffset} from './tags-helpers.js';
9
9
 
10
- const EXIF_IFD_POINTER_KEY = 'Exif IFD Pointer';
11
- const GPS_INFO_IFD_POINTER_KEY = 'GPS Info IFD Pointer';
12
- const INTEROPERABILITY_IFD_POINTER_KEY = 'Interoperability IFD Pointer';
10
+ const SUB_IFDS = [
11
+ {pointerKey: 'Exif IFD Pointer', ifdType: IFD_TYPE_EXIF},
12
+ {pointerKey: 'GPS Info IFD Pointer', ifdType: IFD_TYPE_GPS},
13
+ {pointerKey: 'Interoperability IFD Pointer', ifdType: IFD_TYPE_INTEROPERABILITY}
14
+ ];
13
15
 
14
16
  export default {
15
17
  read,
@@ -18,9 +20,9 @@ export default {
18
20
  function read(dataView, tiffHeaderOffset, includeUnknown, computed = false, tagFilter = undefined) {
19
21
  const byteOrder = ByteOrder.getByteOrder(dataView, tiffHeaderOffset);
20
22
  let tags = read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter);
21
- tags = readExifIfd(tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter);
22
- tags = readGpsIfd(tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter);
23
- tags = readInteroperabilityIfd(tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter);
23
+ for (let i = 0; i < SUB_IFDS.length; i++) {
24
+ tags = readSubIfd(SUB_IFDS[i], tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter);
25
+ }
24
26
 
25
27
  return {tags, byteOrder};
26
28
  }
@@ -43,65 +45,24 @@ function read0thIfd(dataView, tiffHeaderOffset, byteOrder, includeUnknown, compu
43
45
  );
44
46
  }
45
47
 
46
- function readExifIfd(tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter) {
47
- if (tags[EXIF_IFD_POINTER_KEY] !== undefined) {
48
- return objectAssign(
49
- tags,
50
- readIfd(
51
- dataView,
52
- IFD_TYPE_EXIF,
53
- tiffHeaderOffset,
54
- tiffHeaderOffset + tags[EXIF_IFD_POINTER_KEY].value,
55
- byteOrder,
56
- includeUnknown,
57
- computed,
58
- tagFilter,
59
- 'exif'
60
- )
61
- );
62
- }
63
-
64
- return tags;
65
- }
66
-
67
- function readGpsIfd(tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter) {
68
- if (tags[GPS_INFO_IFD_POINTER_KEY] !== undefined) {
69
- return objectAssign(
70
- tags,
71
- readIfd(
72
- dataView,
73
- IFD_TYPE_GPS,
74
- tiffHeaderOffset,
75
- tiffHeaderOffset + tags[GPS_INFO_IFD_POINTER_KEY].value,
76
- byteOrder,
77
- includeUnknown,
78
- computed,
79
- tagFilter,
80
- 'exif'
81
- )
82
- );
48
+ function readSubIfd(subIfd, tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter) {
49
+ const pointerTag = tags[subIfd.pointerKey];
50
+ if (pointerTag === undefined) {
51
+ return tags;
83
52
  }
84
53
 
85
- return tags;
86
- }
87
-
88
- function readInteroperabilityIfd(tags, dataView, tiffHeaderOffset, byteOrder, includeUnknown, computed, tagFilter) {
89
- if (tags[INTEROPERABILITY_IFD_POINTER_KEY] !== undefined) {
90
- return objectAssign(
91
- tags,
92
- readIfd(
93
- dataView,
94
- IFD_TYPE_INTEROPERABILITY,
95
- tiffHeaderOffset,
96
- tiffHeaderOffset + tags[INTEROPERABILITY_IFD_POINTER_KEY].value,
97
- byteOrder,
98
- includeUnknown,
99
- computed,
100
- tagFilter,
101
- 'exif'
102
- )
103
- );
104
- }
105
-
106
- return tags;
54
+ return objectAssign(
55
+ tags,
56
+ readIfd(
57
+ dataView,
58
+ subIfd.ifdType,
59
+ tiffHeaderOffset,
60
+ tiffHeaderOffset + pointerTag.value,
61
+ byteOrder,
62
+ includeUnknown,
63
+ computed,
64
+ tagFilter,
65
+ 'exif'
66
+ )
67
+ );
107
68
  }