exifreader 4.48.1 → 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.
@@ -2,6 +2,8 @@
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 {joinMapped} from './utils.js';
6
+
5
7
  export default {
6
8
  ApertureValue: (value) => Math.pow(Math.sqrt(2), value[0] / value[1]).toFixed(2),
7
9
  ColorSpace(value) {
@@ -13,21 +15,7 @@ export default {
13
15
  return 'Unknown';
14
16
  },
15
17
  ComponentsConfiguration(value) {
16
- return value.map((character) => {
17
- if (character === 0x31) {
18
- return 'Y';
19
- } else if (character === 0x32) {
20
- return 'Cb';
21
- } else if (character === 0x33) {
22
- return 'Cr';
23
- } else if (character === 0x34) {
24
- return 'R';
25
- } else if (character === 0x35) {
26
- return 'G';
27
- } else if (character === 0x36) {
28
- return 'B';
29
- }
30
- }).join('');
18
+ return joinMapped(value, getComponentText, '');
31
19
  },
32
20
  Contrast(value) {
33
21
  if (value === 0) {
@@ -233,3 +221,19 @@ export default {
233
221
  XResolution: (value) => '' + Math.round(value[0] / value[1]),
234
222
  YResolution: (value) => '' + Math.round(value[0] / value[1])
235
223
  };
224
+
225
+ function getComponentText(component) {
226
+ if (component === 1) {
227
+ return 'Y';
228
+ } else if (component === 2) {
229
+ return 'Cb';
230
+ } else if (component === 3) {
231
+ return 'Cr';
232
+ } else if (component === 4) {
233
+ return 'R';
234
+ } else if (component === 5) {
235
+ return 'G';
236
+ } else if (component === 6) {
237
+ return 'B';
238
+ }
239
+ }
@@ -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 {getEncodedString, getVersionString} from './tag-names-utils.js';
5
+ import {getEncodedString, getTableDescription, getVersionString} from './tag-names-utils.js';
6
6
  import TagNamesCommon from './tag-names-common.js';
7
7
 
8
8
  export default {
@@ -28,7 +28,7 @@ export default {
28
28
  0x882b: 'SelfTimerMode',
29
29
  0x8830: {
30
30
  name: 'SensitivityType',
31
- description: (value) => ({
31
+ description: (value) => getTableDescription({
32
32
  1: 'Standard Output Sensitivity',
33
33
  2: 'Recommended Exposure Index',
34
34
  3: 'ISO Speed',
@@ -36,7 +36,7 @@ export default {
36
36
  5: 'Standard Output Sensitivity and ISO Speed',
37
37
  6: 'Recommended Exposure Index and ISO Speed',
38
38
  7: 'Standard Output Sensitivity, Recommended Exposure Index and ISO Speed'
39
- })[value] || 'Unknown'
39
+ }, value)
40
40
  },
41
41
  0x8831: 'StandardOutputSensitivity',
42
42
  0x8832: 'RecommendedExposureIndex',
@@ -144,13 +144,13 @@ export default {
144
144
  0x9211: 'ImageNumber',
145
145
  0x9212: {
146
146
  name: 'SecurityClassification',
147
- description: (value) => ({
147
+ description: (value) => getTableDescription({
148
148
  'C': 'Confidential',
149
149
  'R': 'Restricted',
150
150
  'S': 'Secret',
151
151
  'T': 'Top Secret',
152
152
  'U': 'Unclassified'
153
- })[value] || 'Unknown'
153
+ }, value)
154
154
  },
155
155
  0x9213: 'ImageHistory',
156
156
  0x9214: {
@@ -362,11 +362,11 @@ export default {
362
362
  0xa435: 'LensSerialNumber',
363
363
  0xa460: {
364
364
  name: 'CompositeImage',
365
- description: (value) => ({
365
+ description: (value) => getTableDescription({
366
366
  1: 'Not a Composite Image',
367
367
  2: 'General Composite Image',
368
368
  3: 'Composite Image Captured While Shooting',
369
- })[value] || 'Unknown'
369
+ }, value)
370
370
  },
371
371
  0xa461: 'SourceImageNumberOfCompositeImage',
372
372
  0xa462: 'SourceExposureTimesOfCompositeImage',
@@ -30,18 +30,39 @@ export function getEncodedString(value) {
30
30
 
31
31
  /**
32
32
  * Converts the bytes of a version tag, such as ExifVersion, to a string. A
33
- * value that is not an array (a single byte, or '<faulty value>') is returned
34
- * as it is.
35
- * @param {number[]|number|string} value
33
+ * value of strings (an ASCII-typed tag) is returned as its strings joined
34
+ * with ', ', as for other ASCII tags. A value that is not an array (a single
35
+ * byte, or '<faulty value>') is returned as it is.
36
+ * @param {number[]|string[]|number|string} value
36
37
  * @returns {string|number}
37
38
  */
38
39
  export function getVersionString(value) {
39
40
  if (Array.isArray(value)) {
41
+ if (value.some(isString)) {
42
+ return value.filter(isString).join(', ');
43
+ }
40
44
  return getByteString(value);
41
45
  }
42
46
  return value;
43
47
  }
44
48
 
49
+ function isString(item) {
50
+ return typeof item === 'string';
51
+ }
52
+
53
+ /**
54
+ * Looks a tag value up in a table of descriptions, own properties only, so a
55
+ * value such as 'constructor' or ['__proto__'] does not find a member of
56
+ * Object.prototype.
57
+ * @param {Object} table Descriptions keyed by tag value.
58
+ * @param {*} value The tag value, used as a property key.
59
+ * @returns {string} The description, or 'Unknown' when the value is not a key.
60
+ */
61
+ export function getTableDescription(table, value) {
62
+ const key = String(value);
63
+ return Object.prototype.hasOwnProperty.call(table, key) ? table[key] : 'Unknown';
64
+ }
65
+
45
66
  export function getCalculatedGpsValue(value) {
46
67
  return (value[0][0] / value[0][1]) + (value[1][0] / value[1][1]) / 60 + (value[2][0] / value[2][1]) / 3600;
47
68
  }
@@ -533,3 +533,23 @@ function getComputedRationalValue(rational) {
533
533
 
534
534
  return numerator / denominator;
535
535
  }
536
+
537
+ /**
538
+ * Removes the thumbnail IFD from the tags of an embedded Exif block and reads
539
+ * its image with `readThumbnail`, when given.
540
+ *
541
+ * @returns {object|undefined} The thumbnail tags when an image was read.
542
+ */
543
+ export function takeEmbeddedExifThumbnail(readTags, dataView, tiffHeaderOffset, readThumbnail) {
544
+ const thumbnailIfdTags = readTags.Thumbnail;
545
+ if (!thumbnailIfdTags) {
546
+ return undefined;
547
+ }
548
+ delete readTags.Thumbnail;
549
+
550
+ if (!readThumbnail) {
551
+ return undefined;
552
+ }
553
+ const thumbnail = readThumbnail(dataView, thumbnailIfdTags, tiffHeaderOffset);
554
+ return thumbnail.image ? thumbnail : undefined;
555
+ }
package/src/utils.js CHANGED
@@ -63,14 +63,41 @@ export function getPascalStringFromDataView(dataView, offset) {
63
63
  return [size, string];
64
64
  }
65
65
 
66
- export function getStringValueFromArray(charArray) {
67
- return charArray.map((charCode) => String.fromCharCode(charCode)).join('');
68
- }
69
-
70
66
  // Engines cap the number of arguments Function.prototype.apply can pass, and
71
67
  // some older ones reject a typed array there, so bytes go in as plain chunks.
72
68
  const MAX_CHARS_PER_CALL = 8192;
73
69
 
70
+ /**
71
+ * Converts character codes to a string, one character per element. Like
72
+ * `map`, it skips the holes of a sparse array.
73
+ * @param {Array} charArray
74
+ * @returns {string}
75
+ * @throws {TypeError} When `charArray` is not an array.
76
+ */
77
+ export function getStringValueFromArray(charArray) {
78
+ if (!Array.isArray(charArray)) {
79
+ throw new TypeError('Expected an array.');
80
+ }
81
+
82
+ const chunks = [];
83
+ // Overwritten by index, not emptied, so that its storage is reused across
84
+ // chunks. Emptying it measured about 20 MB more peak memory for 8 MiB.
85
+ const charCodes = [];
86
+ let count = 0;
87
+ for (let i = 0; i < charArray.length; i++) {
88
+ if (i in charArray) {
89
+ charCodes[count++] = charArray[i];
90
+ if (count === MAX_CHARS_PER_CALL) {
91
+ chunks.push(String.fromCharCode.apply(null, charCodes));
92
+ count = 0;
93
+ }
94
+ }
95
+ }
96
+ charCodes.length = count;
97
+ chunks.push(String.fromCharCode.apply(null, charCodes));
98
+ return chunks.join('');
99
+ }
100
+
74
101
  /**
75
102
  * Converts bytes to a string with one character per byte.
76
103
  * @param {Uint8Array|number[]} bytes
@@ -101,6 +128,41 @@ function getChunkString(bytes, start, end, charCodes) {
101
128
  return String.fromCharCode.apply(null, charCodes);
102
129
  }
103
130
 
131
+ // Smaller chunks measured a higher peak memory for 8 MiB values.
132
+ export const JOIN_CHUNK_SIZE = 65536;
133
+
134
+ /**
135
+ * Returns what `values.map(getText).join(separator)` returns, without building
136
+ * an array as long as `values`. Unlike `map`, a hole in a sparse array reaches
137
+ * `getText` as `undefined`.
138
+ * @param {Array} values
139
+ * @param {function(*): (string|undefined|null)} getText Called with the
140
+ * element only, in index order.
141
+ * @param {string} separator
142
+ * @returns {string}
143
+ * @throws {TypeError} When `values` is not an array.
144
+ */
145
+ export function joinMapped(values, getText, separator) {
146
+ if (!Array.isArray(values)) {
147
+ throw new TypeError('Expected an array.');
148
+ }
149
+
150
+ const texts = [];
151
+ const chunks = [];
152
+ for (let chunkStart = 0; chunkStart < values.length; chunkStart += JOIN_CHUNK_SIZE) {
153
+ chunks.push(getJoinedChunk(values, chunkStart, Math.min(chunkStart + JOIN_CHUNK_SIZE, values.length), getText, separator, texts));
154
+ }
155
+ return chunks.join(separator);
156
+ }
157
+
158
+ function getJoinedChunk(values, start, end, getText, separator, texts) {
159
+ for (let i = start; i < end; i++) {
160
+ texts[i - start] = getText(values[i]);
161
+ }
162
+ texts.length = end - start;
163
+ return texts.join(separator);
164
+ }
165
+
104
166
  /**
105
167
  * Decodes a byte string (one byte per character) as UTF-8. A string that is
106
168
  * not valid UTF-8 is returned as it is, so single-byte encoded text stays
@@ -44,7 +44,7 @@ export default {
44
44
  'exif:ColorSpace': (value) => TagNamesCommon.ColorSpace(parseNumber(value)),
45
45
  'exif:ComponentsConfiguration'(value, description) {
46
46
  if (/^\d, \d, \d, \d$/.test(description)) {
47
- const numbers = description.split(', ').map((number) => number.charCodeAt(0));
47
+ const numbers = description.split(', ').map((number) => parseInt(number, 10));
48
48
  return TagNamesCommon.ComponentsConfiguration(numbers);
49
49
  }
50
50
  return description;