exifreader 4.48.1 → 4.50.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.
@@ -32,6 +32,10 @@ const TYPE_IINF = 0x69696e66;
32
32
  const TYPE_INFE = 0x696e6665;
33
33
  const TYPE_IPCO = 0x6970636f;
34
34
  const TYPE_COLR = 0x636f6c72;
35
+ const TYPE_PITM = 0x7069746d;
36
+ const TYPE_IPMA = 0x69706d61;
37
+ const TYPE_ISPE = 0x69737065;
38
+ const TYPE_PIXI = 0x70697869;
35
39
 
36
40
  // const EXTENSION_TYPE_FDEL = 0x6664656c;
37
41
 
@@ -46,6 +50,12 @@ const MAX_INFE_STRING_LENGTH = 65536;
46
50
  export const BOX_TYPE_OFFSET = 4;
47
51
  export const BOX_MIN_LENGTH = 8;
48
52
 
53
+ const FULL_BOX_VERSION_AND_FLAGS_SIZE = 4;
54
+
55
+ // Every ipco child the parser does not know shares this entry, so an ipco
56
+ // packed with them costs one array slot per child, not one object.
57
+ const UNKNOWN_PROPERTY = {type: undefined};
58
+
49
59
  /**
50
60
  * Parses a ISO-BMFF box from the provided data view starting at the given offset.
51
61
  *
@@ -67,7 +77,8 @@ export function parseBox(dataView, offset, containerEnd = dataView.byteLength) {
67
77
  // unparseable (return undefined) rather than throw a RangeError to the caller.
68
78
  try {
69
79
  const {length, contentOffset} = getBoxLength(dataView, offset);
70
- if (length < BOX_MIN_LENGTH) {
80
+ // An extended-size box has a 16-byte header, not 8.
81
+ if (length < contentOffset - offset || contentOffset > containerEnd) {
71
82
  return undefined;
72
83
  }
73
84
 
@@ -109,11 +120,20 @@ export function parseBox(dataView, offset, containerEnd = dataView.byteLength) {
109
120
  if (type === TYPE_INFE) {
110
121
  return parseItemInformationEntryBox(dataView, offset, version, contentOffset + VERSION_SIZE, length, containerEnd);
111
122
  }
123
+ if (type === TYPE_PITM) {
124
+ return parsePrimaryItemBox(dataView, offset, version, contentOffset, length, containerEnd);
125
+ }
126
+ if (type === TYPE_IPMA) {
127
+ return parseItemPropertyAssociationBox(dataView, offset, version, contentOffset, length, containerEnd);
128
+ }
129
+ if (type === TYPE_ISPE) {
130
+ return parseImageSpatialExtentsBox(dataView, offset, contentOffset, length, containerEnd);
131
+ }
132
+ if (type === TYPE_PIXI) {
133
+ return parsePixelInformationBox(dataView, offset, version, contentOffset, length, containerEnd);
134
+ }
112
135
 
113
- return {
114
- type: undefined,
115
- length
116
- };
136
+ return getUnknownBox(length);
117
137
  } catch (error) {
118
138
  return undefined;
119
139
  }
@@ -210,6 +230,12 @@ function hasEmptyHighBits(dataView, offset) {
210
230
  * rather than the source dataView.
211
231
  * @property {DataView=} xmpDataView Synthetic buffer when an XMP item
212
232
  * spans multiple extents. xmpChunks then references this view.
233
+ * @property {number=} imageDimensionsOffset Offset of the primary item's
234
+ * dimensions, the width and height fields of its ispe property; absent
235
+ * when it cannot be resolved.
236
+ * @property {number=} pixelInformationOffset Offset of the num_channels
237
+ * field of the primary item's pixi property; the per-channel bit depths
238
+ * follow it. Absent when it cannot be resolved.
213
239
  */
214
240
 
215
241
  /**
@@ -220,13 +246,49 @@ function hasEmptyHighBits(dataView, offset) {
220
246
  * receives one block per iloc extent (Exif/XMP) at the resolved file
221
247
  * offset (cm 0 direct, cm 1 via the idat box) plus the ICC profile
222
248
  * bytes. cm 2 (item_offset) items are skipped.
223
- * @returns {Offsets} The TIFF header offset, XMP chunks, ICC chunks, and
224
- * whether any were found.
249
+ * @returns {Offsets} The TIFF header offset, XMP chunks, ICC chunks,
250
+ * whether any of those were found, and the primary image's dimensions
251
+ * and pixel information offsets.
225
252
  */
226
253
  export function findOffsets(dataView, metadataBlocks) {
254
+ const metaBox = findMetaBox(dataView);
255
+ const offsets = findMetadataOffsets(dataView, metaBox, metadataBlocks);
256
+ const {imageDimensionsOffset, pixelInformationOffset} = findPrimaryItemPropertyOffsets(dataView, metaBox);
257
+ if (imageDimensionsOffset !== undefined) {
258
+ offsets.imageDimensionsOffset = imageDimensionsOffset;
259
+ }
260
+ if (pixelInformationOffset !== undefined) {
261
+ offsets.pixelInformationOffset = pixelInformationOffset;
262
+ }
263
+ return offsets;
264
+ }
265
+
266
+ function findMetaBox(dataView) {
267
+ const BOX_LENGTH_SIZE = 4;
268
+ const BOX_TYPE_SIZE = 4;
269
+
270
+ let offset = 0;
271
+
272
+ while (offset + BOX_LENGTH_SIZE + BOX_TYPE_SIZE <= dataView.byteLength) {
273
+ const box = parseBox(dataView, offset);
274
+
275
+ if (box === undefined) {
276
+ break;
277
+ }
278
+
279
+ if (box.type === 'meta') {
280
+ return box;
281
+ }
282
+
283
+ offset += box.length;
284
+ }
285
+
286
+ return undefined;
287
+ }
288
+
289
+ function findMetadataOffsets(dataView, metaBox, metadataBlocks) {
227
290
  if (Constants.USE_EXIF || Constants.USE_XMP || Constants.USE_ICC) {
228
291
  const offsets = {};
229
- const metaBox = findMetaBox(dataView);
230
292
 
231
293
  if (!metaBox) {
232
294
  return {hasAppMarkers: false};
@@ -262,29 +324,6 @@ export function findOffsets(dataView, metadataBlocks) {
262
324
  return {};
263
325
  }
264
326
 
265
- function findMetaBox(dataView) {
266
- const BOX_LENGTH_SIZE = 4;
267
- const BOX_TYPE_SIZE = 4;
268
-
269
- let offset = 0;
270
-
271
- while (offset + BOX_LENGTH_SIZE + BOX_TYPE_SIZE <= dataView.byteLength) {
272
- const box = parseBox(dataView, offset);
273
-
274
- if (box === undefined) {
275
- break;
276
- }
277
-
278
- if (box.type === 'meta') {
279
- return box;
280
- }
281
-
282
- offset += box.length;
283
- }
284
-
285
- return undefined;
286
- }
287
-
288
327
  function warnIfUnsupportedFieldSize(metaBox) {
289
328
  const ilocBox = metaBox.subBoxes.find((box) => box.type === 'iloc');
290
329
  if (ilocBox && ilocBox.uses64BitFields) {
@@ -453,8 +492,7 @@ function findIccChunks(metaBox, metadataBlocks) {
453
492
  // This finds the first ICC chunk, but there could be one for each image
454
493
  // that is embedded in the file. If it turns out we need to match the ICC
455
494
  // chunk to a specific image, we need to check the "ipma" in addition to the
456
- // "ipco" (currently we only extract the "ipco" so more code would be
457
- // needed).
495
+ // "ipco", as findItemAssociations does for the primary item's ispe.
458
496
  try {
459
497
  const icc = metaBox.subBoxes.find((box) => box.type === 'iprp')
460
498
  .subBoxes.find((box) => box.type === 'ipco')
@@ -470,6 +508,95 @@ function findIccChunks(metaBox, metadataBlocks) {
470
508
  return undefined;
471
509
  }
472
510
 
511
+ // Resolves meta > pitm (the primary item) through meta > iprp > ipma (its
512
+ // property indexes) to the ispe and pixi among meta > iprp > ipco's children.
513
+ function findPrimaryItemPropertyOffsets(dataView, metaBox) {
514
+ // Every read below is bounded at parse time; the catch keeps a malformed
515
+ // box from failing the whole file when that bounding is ever wrong.
516
+ try {
517
+ if (!metaBox) {
518
+ return {};
519
+ }
520
+ const pitm = metaBox.subBoxes.find((box) => box.type === 'pitm');
521
+ const iprp = metaBox.subBoxes.find((box) => box.type === 'iprp');
522
+ if (!pitm || !iprp) {
523
+ return {};
524
+ }
525
+ const ipco = iprp.subBoxes.find((box) => box.type === 'ipco');
526
+ if (!ipco) {
527
+ return {};
528
+ }
529
+ const propertyIndexes = findItemPropertyIndexes(dataView, iprp.subBoxes, pitm.itemId);
530
+ if (!propertyIndexes) {
531
+ return {};
532
+ }
533
+ const ispe = findProperty(propertyIndexes, ipco.properties, 'ispe');
534
+ const pixi = findProperty(propertyIndexes, ipco.properties, 'pixi');
535
+ return {
536
+ imageDimensionsOffset: ispe && ispe.dimensionsOffset,
537
+ pixelInformationOffset: pixi && pixi.channelCountOffset,
538
+ };
539
+ } catch (error) {
540
+ return {};
541
+ }
542
+ }
543
+
544
+ function findItemPropertyIndexes(dataView, iprpSubBoxes, itemId) {
545
+ for (let i = 0; i < iprpSubBoxes.length; i++) {
546
+ if (iprpSubBoxes[i].type === 'ipma') {
547
+ const propertyIndexes = findItemPropertyIndexesInIpma(dataView, iprpSubBoxes[i], itemId);
548
+ if (propertyIndexes) {
549
+ return propertyIndexes;
550
+ }
551
+ }
552
+ }
553
+ return undefined;
554
+ }
555
+
556
+ // The scan is bounded by the box's bytes, not by the counts it declares.
557
+ function findItemPropertyIndexesInIpma(dataView, ipma, itemId) {
558
+ const ENTRY_COUNT_SIZE = 4;
559
+ const ASSOCIATION_COUNT_SIZE = 1;
560
+ const itemIdSize = ipma.version === 0 ? 2 : 4;
561
+ const associationSize = (ipma.flags & 1) === 1 ? 2 : 1;
562
+
563
+ const entryCount = dataView.getUint32(ipma.entryCountOffset);
564
+ let offset = ipma.entryCountOffset + ENTRY_COUNT_SIZE;
565
+ for (let i = 0; i < entryCount && offset + itemIdSize + ASSOCIATION_COUNT_SIZE <= ipma.end; i++) {
566
+ const entryItemId = itemIdSize === 2 ? dataView.getUint16(offset) : dataView.getUint32(offset);
567
+ const count = dataView.getUint8(offset + itemIdSize);
568
+ offset += itemIdSize + ASSOCIATION_COUNT_SIZE;
569
+ if (entryItemId === itemId) {
570
+ const countWithinBox = Math.min(count, Math.floor((ipma.end - offset) / associationSize));
571
+ return readPropertyIndexes(dataView, offset, countWithinBox, associationSize);
572
+ }
573
+ offset += count * associationSize;
574
+ }
575
+ return undefined;
576
+ }
577
+
578
+ function readPropertyIndexes(dataView, offset, count, associationSize) {
579
+ // The top bit marks the property as essential; the rest is the 1-based index, 0 meaning no property.
580
+ const indexMask = associationSize === 2 ? 0x7fff : 0x7f;
581
+ const propertyIndexes = [];
582
+ for (let i = 0; i < count; i++) {
583
+ const associationOffset = offset + i * associationSize;
584
+ const association = associationSize === 2 ? dataView.getUint16(associationOffset) : dataView.getUint8(associationOffset);
585
+ propertyIndexes.push(association & indexMask);
586
+ }
587
+ return propertyIndexes;
588
+ }
589
+
590
+ function findProperty(propertyIndexes, properties, type) {
591
+ for (let i = 0; i < propertyIndexes.length; i++) {
592
+ const property = properties[propertyIndexes[i] - 1];
593
+ if (property !== undefined && property.type === type) {
594
+ return property;
595
+ }
596
+ }
597
+ return undefined;
598
+ }
599
+
473
600
  function parseFileTypeBox(dataView, contentOffset, boxLength) {
474
601
  const MAJOR_BRAND_SIZE = 4;
475
602
  const majorBrand = getStringFromDataView(dataView, contentOffset, MAJOR_BRAND_SIZE);
@@ -492,11 +619,21 @@ function parseItemPropertiesBox(dataView, startOffset, contentOffset, length, co
492
619
  function parseItemPropertyContainerBox(dataView, startOffset, contentOffset, length, containerEnd) {
493
620
  return {
494
621
  type: 'ipco',
495
- properties: parseSubBoxes(dataView, contentOffset, length - (contentOffset - startOffset), containerEnd),
622
+ properties: parseProperties(dataView, contentOffset, length - (contentOffset - startOffset), containerEnd),
496
623
  length,
497
624
  };
498
625
  }
499
626
 
627
+ // ipma refers to a property by its 1-based position among all ipco children,
628
+ // so every child keeps its slot, known or not.
629
+ function parseProperties(dataView, offset, length, containerEnd) {
630
+ const properties = [];
631
+ walkSubBoxes(dataView, offset, length, containerEnd, (box) => {
632
+ properties.push(box.type !== undefined ? box : UNKNOWN_PROPERTY);
633
+ });
634
+ return properties;
635
+ }
636
+
500
637
  function parseColorInformationBox(dataView, contentOffset, length) {
501
638
  return {
502
639
  type: 'colr',
@@ -542,6 +679,85 @@ function parseItemDataBox(contentOffset, length) {
542
679
  };
543
680
  }
544
681
 
682
+ function parsePrimaryItemBox(dataView, startOffset, version, contentOffset, length, containerEnd) {
683
+ const itemIdOffset = contentOffset + FULL_BOX_VERSION_AND_FLAGS_SIZE;
684
+ const itemIdSize = version === 0 ? 2 : 4;
685
+ if (itemIdOffset + itemIdSize > getBoxEnd(dataView, startOffset, length, containerEnd)) {
686
+ return getUnknownBox(length);
687
+ }
688
+
689
+ return {
690
+ type: 'pitm',
691
+ itemId: version === 0 ? dataView.getUint16(itemIdOffset) : dataView.getUint32(itemIdOffset),
692
+ length
693
+ };
694
+ }
695
+
696
+ // Only the header is read here; the entries are scanned once the primary item
697
+ // is known, so nothing is built per entry.
698
+ function parseItemPropertyAssociationBox(dataView, startOffset, version, contentOffset, length, containerEnd) {
699
+ const ENTRY_COUNT_SIZE = 4;
700
+ const FLAGS_MASK = 0xffffff;
701
+ const entryCountOffset = contentOffset + FULL_BOX_VERSION_AND_FLAGS_SIZE;
702
+ const end = getBoxEnd(dataView, startOffset, length, containerEnd);
703
+ if (entryCountOffset + ENTRY_COUNT_SIZE > end) {
704
+ return getUnknownBox(length);
705
+ }
706
+
707
+ return {
708
+ type: 'ipma',
709
+ version,
710
+ flags: dataView.getUint32(contentOffset) & FLAGS_MASK,
711
+ entryCountOffset,
712
+ end,
713
+ length
714
+ };
715
+ }
716
+
717
+ function parseImageSpatialExtentsBox(dataView, startOffset, contentOffset, length, containerEnd) {
718
+ const DIMENSIONS_SIZE = 8;
719
+ const dimensionsOffset = contentOffset + FULL_BOX_VERSION_AND_FLAGS_SIZE;
720
+ if (dimensionsOffset + DIMENSIONS_SIZE > getBoxEnd(dataView, startOffset, length, containerEnd)) {
721
+ return getUnknownBox(length);
722
+ }
723
+
724
+ return {
725
+ type: 'ispe',
726
+ dimensionsOffset,
727
+ length
728
+ };
729
+ }
730
+
731
+ function parsePixelInformationBox(dataView, startOffset, version, contentOffset, length, containerEnd) {
732
+ const CHANNEL_COUNT_SIZE = 1;
733
+ const channelCountOffset = contentOffset + FULL_BOX_VERSION_AND_FLAGS_SIZE;
734
+ const end = getBoxEnd(dataView, startOffset, length, containerEnd);
735
+ if (version !== 0 || channelCountOffset + CHANNEL_COUNT_SIZE > end) {
736
+ return getUnknownBox(length);
737
+ }
738
+ const channelCount = dataView.getUint8(channelCountOffset);
739
+ if (channelCountOffset + CHANNEL_COUNT_SIZE + channelCount > end) {
740
+ return getUnknownBox(length);
741
+ }
742
+
743
+ return {
744
+ type: 'pixi',
745
+ channelCountOffset,
746
+ length
747
+ };
748
+ }
749
+
750
+ function getBoxEnd(dataView, startOffset, length, containerEnd) {
751
+ return Math.min(startOffset + length, containerEnd, dataView.byteLength);
752
+ }
753
+
754
+ function getUnknownBox(length) {
755
+ return {
756
+ type: undefined,
757
+ length
758
+ };
759
+ }
760
+
545
761
  /**
546
762
  * @param {DataView} dataView
547
763
  * @param {number} offset The offset to start parsing from.
@@ -555,13 +771,22 @@ function parseSubBoxes(dataView, offset, length, containerEnd) {
555
771
  ITEM_INFO_TYPE_MIME,
556
772
  ];
557
773
 
774
+ const subBoxes = [];
775
+ walkSubBoxes(dataView, offset, length, containerEnd, (box) => {
776
+ if (box.type !== undefined && (box.itemType === undefined || ACCEPTED_ITEM_INFO_TYPES.indexOf(box.itemType) !== -1)) {
777
+ subBoxes.push(box);
778
+ }
779
+ });
780
+ return subBoxes;
781
+ }
782
+
783
+ function walkSubBoxes(dataView, offset, length, containerEnd, onBox) {
558
784
  // A sub box may declare any length it likes, so bound it by the bytes this
559
785
  // container has, and by the ones that are really there when the file is
560
786
  // truncated. An overrunning box still gets parsed for what fits, which is
561
787
  // what keeps a cut-off file returning the metadata it does contain.
562
788
  const subBoxesEnd = Math.min(offset + length, containerEnd);
563
789
 
564
- const subBoxes = [];
565
790
  let currentOffset = offset;
566
791
  // Requiring room for a whole header keeps a box from taking its type from
567
792
  // the bytes after the container, which invents one out of two boxes.
@@ -570,12 +795,9 @@ function parseSubBoxes(dataView, offset, length, containerEnd) {
570
795
  if (box === undefined) {
571
796
  break;
572
797
  }
573
- if (box.type !== undefined && (box.itemType === undefined || ACCEPTED_ITEM_INFO_TYPES.indexOf(box.itemType) !== -1)) {
574
- subBoxes.push(box);
575
- }
798
+ onBox(box);
576
799
  currentOffset += box.length;
577
800
  }
578
- return subBoxes;
579
801
  }
580
802
 
581
803
  function parseItemInformationBox(dataView, startOffset, version, contentOffset, length, containerEnd) {
@@ -25,6 +25,7 @@ const TYPE_JXLC = 0x6a786c63;
25
25
  const TYPE_JXLP = 0x6a786c70;
26
26
  const JXLP_SEQUENCE_NUMBER_SIZE = 4;
27
27
  const BROB_ORIGINAL_TYPE_SIZE = 4;
28
+ const EXIF_TIFF_HEADER_OFFSET_SIZE = 4;
28
29
 
29
30
  /**
30
31
  * Checks if the provided data view represents a JPEG XL file (container or
@@ -98,18 +99,17 @@ function findJxlOffsets(dataView, metadataBlocks) {
98
99
 
99
100
  while (offset + BOX_MIN_LENGTH <= dataView.byteLength) {
100
101
  const {length, contentOffset} = getBoxLength(dataView, offset);
101
- if (length < BOX_MIN_LENGTH) {
102
+ if (length < contentOffset - offset) {
102
103
  break;
103
104
  }
105
+ const contentEnd = Math.min(offset + length, dataView.byteLength);
104
106
 
105
107
  const boxType = dataView.getUint32(offset + BOX_TYPE_OFFSET);
106
108
  let blockType;
107
109
 
108
110
  if (Constants.USE_EXIF && boxType === TYPE_EXIF) {
109
- try {
111
+ if (contentOffset + EXIF_TIFF_HEADER_OFFSET_SIZE <= contentEnd) {
110
112
  tiffHeaderOffset = getTiffHeaderOffset(dataView, contentOffset);
111
- } catch (_error) {
112
- // Truncated Exif data.
113
113
  }
114
114
  blockType = 'exif';
115
115
  }
@@ -129,7 +129,7 @@ function findJxlOffsets(dataView, metadataBlocks) {
129
129
  }
130
130
 
131
131
  if (boxType === TYPE_JXLP && jxlCodestreamOffset === undefined
132
- && contentOffset + JXLP_SEQUENCE_NUMBER_SIZE <= dataView.byteLength) {
132
+ && contentOffset + JXLP_SEQUENCE_NUMBER_SIZE <= contentEnd) {
133
133
  const sequenceNumber = dataView.getUint32(contentOffset) & 0x7FFFFFFF;
134
134
  if (sequenceNumber === 0) {
135
135
  jxlCodestreamOffset = contentOffset + JXLP_SEQUENCE_NUMBER_SIZE;
@@ -137,7 +137,7 @@ function findJxlOffsets(dataView, metadataBlocks) {
137
137
  }
138
138
  }
139
139
 
140
- if (boxType === TYPE_BROB && contentOffset + BROB_ORIGINAL_TYPE_SIZE <= dataView.byteLength) {
140
+ if (boxType === TYPE_BROB && contentOffset + BROB_ORIGINAL_TYPE_SIZE <= contentEnd) {
141
141
  const originalType = dataView.getUint32(contentOffset);
142
142
  const compressedDataOffset = contentOffset + BROB_ORIGINAL_TYPE_SIZE;
143
143
  const compressedDataLength = length - (compressedDataOffset - offset);
@@ -288,7 +288,7 @@ export default {
288
288
  },
289
289
  'description': (value) => {
290
290
  if (value.length === 2) {
291
- const intValue = (value[0] << 8) + value[1];
291
+ const intValue = getPreviewNumber(value);
292
292
  if (intValue === 0) {
293
293
  return 'No ObjectData';
294
294
  } else if (intValue === 1) {
@@ -360,28 +360,29 @@ export default {
360
360
  'description': (value, tags) => {
361
361
  // Format ID, Version ID, Version Description
362
362
  const formatVersions = {
363
- '00': {'00': '1'},
364
- '01': {'01': '1', '02': '2', '03': '3', '04': '4'},
365
- '02': {'04': '4'},
366
- '03': {'01': '5.0', '02': '6.0'},
367
- '04': {'01': '1.40'},
368
- '05': {'01': '2'},
369
- '06': {'01': '1'},
370
- '11': {'01': '1.02'},
371
- '20': {'01': '3.1', '02': '4.0', '03': '5.0', '04': '5.5'},
372
- '21': {'02': '2.0'}
363
+ 0: {0: '1'},
364
+ 1: {1: '1', 2: '2', 3: '3', 4: '4'},
365
+ 2: {4: '4'},
366
+ 3: {1: '5.0', 2: '6.0'},
367
+ 4: {1: '1.40'},
368
+ 5: {1: '2'},
369
+ 6: {1: '1'},
370
+ 11: {1: '1.02'},
371
+ 20: {1: '3.1', 2: '4.0', 3: '5.0', 4: '5.5'},
372
+ 21: {2: '2.0'}
373
373
  };
374
- const stringValue = getStringValue(value);
374
+ const version = getPreviewNumber(value);
375
375
 
376
376
  if (tags['ObjectData Preview File Format']) {
377
- const objectDataPreviewFileFormat = getStringValue(tags['ObjectData Preview File Format'].value);
378
- if (formatVersions[objectDataPreviewFileFormat]
379
- && formatVersions[objectDataPreviewFileFormat][stringValue]) {
380
- return formatVersions[objectDataPreviewFileFormat][stringValue];
377
+ const format = getPreviewNumber(tags['ObjectData Preview File Format'].value);
378
+ if (Object.prototype.hasOwnProperty.call(formatVersions, format)
379
+ && Object.prototype.hasOwnProperty.call(formatVersions[format], version)) {
380
+ return formatVersions[format][version];
381
381
  }
382
382
  }
383
383
 
384
- return stringValue;
384
+ const versionNumber = getBinaryNumber(value);
385
+ return versionNumber === null ? getStringValue(value) : String(versionNumber);
385
386
  }
386
387
  },
387
388
  0x02ca: 'ObjectData Preview Data',
@@ -479,3 +480,20 @@ function getEncodingName(value) {
479
480
  }
480
481
  return 'Unknown';
481
482
  }
483
+
484
+ // IIM stores 2:200 and 2:201 as 2-octet binary; two ASCII digits are the text form.
485
+ function getPreviewNumber(value) {
486
+ const number = getBinaryNumber(value);
487
+ return number === null ? parseInt(getStringValue(value), 10) : number;
488
+ }
489
+
490
+ function getBinaryNumber(value) {
491
+ if (value.length !== 2 || (isAsciiDigit(value[0]) && isAsciiDigit(value[1]))) {
492
+ return null;
493
+ }
494
+ return (value[0] << 8) + value[1];
495
+ }
496
+
497
+ function isAsciiDigit(octet) {
498
+ return octet >= 0x30 && octet <= 0x39;
499
+ }
@@ -0,0 +1,86 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ import {objectAssign} from './utils.js';
6
+ import {hasBytes} from './image-header-iso-bmff-utils.js';
7
+
8
+ export default {
9
+ read
10
+ };
11
+
12
+ const DIMENSIONS_SIZE = 8;
13
+ const HEIGHT_OFFSET = 4;
14
+ const CHANNEL_COUNT_SIZE = 1;
15
+
16
+ /**
17
+ * Reads the image width and height of an ispe property and the color
18
+ * components and bits per sample of a pixi property.
19
+ *
20
+ * @param {DataView} dataView
21
+ * @param {number=} dimensionsOffset Offset of the ispe width field; the
22
+ * height follows it.
23
+ * @param {number=} pixelInformationOffset Offset of the pixi num_channels
24
+ * field; the per-channel bit depths follow it.
25
+ * @returns {Object} Image Width and Image Height, then Color Components and
26
+ * Bits Per Sample, each part left out when its offset is not given or
27
+ * its bytes are not there.
28
+ */
29
+ function read(dataView, dimensionsOffset, pixelInformationOffset) {
30
+ const tags = {};
31
+ if (dimensionsOffset !== undefined) {
32
+ objectAssign(tags, readImageDimensions(dataView, dimensionsOffset));
33
+ }
34
+ if (pixelInformationOffset !== undefined) {
35
+ objectAssign(tags, readPixelInformation(dataView, pixelInformationOffset));
36
+ }
37
+ return tags;
38
+ }
39
+
40
+ function readImageDimensions(dataView, offset) {
41
+ if (!hasBytes(dataView, offset, DIMENSIONS_SIZE)) {
42
+ return {};
43
+ }
44
+
45
+ const width = dataView.getUint32(offset);
46
+ const height = dataView.getUint32(offset + HEIGHT_OFFSET);
47
+
48
+ return {
49
+ 'Image Width': {value: width, description: `${width}px`},
50
+ 'Image Height': {value: height, description: `${height}px`},
51
+ };
52
+ }
53
+
54
+ function readPixelInformation(dataView, offset) {
55
+ if (!hasBytes(dataView, offset, CHANNEL_COUNT_SIZE)) {
56
+ return {};
57
+ }
58
+ const channelCount = dataView.getUint8(offset);
59
+ const depthsOffset = offset + CHANNEL_COUNT_SIZE;
60
+ if (!hasBytes(dataView, depthsOffset, channelCount)) {
61
+ return {};
62
+ }
63
+
64
+ const tags = {
65
+ 'Color Components': {value: channelCount, description: `${channelCount}`},
66
+ };
67
+ const bitsPerSample = getSharedBitDepth(dataView, depthsOffset, channelCount);
68
+ // One number, as for JPEG; HDR files can mix depths, so mixed depths get no tag.
69
+ if (bitsPerSample !== undefined) {
70
+ tags['Bits Per Sample'] = {value: bitsPerSample, description: `${bitsPerSample}`};
71
+ }
72
+ return tags;
73
+ }
74
+
75
+ function getSharedBitDepth(dataView, offset, channelCount) {
76
+ if (channelCount === 0) {
77
+ return undefined;
78
+ }
79
+ const firstDepth = dataView.getUint8(offset);
80
+ for (let i = 1; i < channelCount; i++) {
81
+ if (dataView.getUint8(offset + i) !== firstDepth) {
82
+ return undefined;
83
+ }
84
+ }
85
+ return firstDepth;
86
+ }