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.
@@ -3,12 +3,14 @@
3
3
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
4
 
5
5
  import TagNamesCommon from './tag-names-common.js';
6
+ import {joinMapped} from './utils.js';
7
+ import {getTableDescription} from './tag-names-utils.js';
6
8
 
7
9
  export default {
8
10
  0x000b: 'ProcessingSoftware',
9
11
  0x00fe: {
10
12
  name: 'SubfileType',
11
- description: (value) => ({
13
+ description: (value) => getTableDescription({
12
14
  0x0: 'Full-resolution image',
13
15
  0x1: 'Reduced-resolution image',
14
16
  0x2: 'Single page of multi-page image',
@@ -19,15 +21,15 @@ export default {
19
21
  0x7: 'Transparency mask of reduced-resolution multi-page image',
20
22
  0x10001: 'Alternate reduced-resolution image',
21
23
  0xffffffff: 'Invalid'
22
- })[value] || 'Unknown'
24
+ }, value)
23
25
  },
24
26
  0x00ff: {
25
27
  name: 'OldSubfileType',
26
- description: (value) => ({
28
+ description: (value) => getTableDescription({
27
29
  0: 'Full-resolution image',
28
30
  1: 'Reduced-resolution image',
29
31
  2: 'Single page of multi-page image'
30
- })[value] || 'Unknown'
32
+ }, value)
31
33
  },
32
34
  0x0100: 'ImageWidth',
33
35
  0x0101: 'ImageLength',
@@ -36,20 +38,20 @@ export default {
36
38
  0x0106: 'PhotometricInterpretation',
37
39
  0x0107: {
38
40
  name: 'Thresholding',
39
- description: (value) => ({
41
+ description: (value) => getTableDescription({
40
42
  1: 'No dithering or halftoning',
41
43
  2: 'Ordered dither or halfton',
42
44
  3: 'Randomized dither'
43
- })[value] || 'Unknown'
45
+ }, value)
44
46
  },
45
47
  0x0108: 'CellWidth',
46
48
  0x0109: 'CellLength',
47
49
  0x010a: {
48
50
  name: 'FillOrder',
49
- description: (value) => ({
51
+ description: (value) => getTableDescription({
50
52
  1: 'Normal',
51
53
  2: 'Reversed'
52
- })[value] || 'Unknown'
54
+ }, value)
53
55
  },
54
56
  0x010d: 'DocumentName',
55
57
  0x010e: 'ImageDescription',
@@ -115,13 +117,13 @@ export default {
115
117
  },
116
118
  0x0122: {
117
119
  name: 'GrayResponseUnit',
118
- description: (value) => ({
120
+ description: (value) => getTableDescription({
119
121
  1: '0.1',
120
122
  2: '0.001',
121
123
  3: '0.0001',
122
124
  4: '1e-05',
123
125
  5: '1e-06'
124
- })[value] || 'Unknown'
126
+ }, value)
125
127
  },
126
128
  0x0128: {
127
129
  name: 'ResolutionUnit',
@@ -136,15 +138,11 @@ export default {
136
138
  0x013d: 'Predictor',
137
139
  0x013e: {
138
140
  'name': 'WhitePoint',
139
- 'description': (values) => {
140
- return values.map((value) => `${value[0]}/${value[1]}`).join(', ');
141
- }
141
+ 'description': (values) => joinMapped(values, getRationalText, ', ')
142
142
  },
143
143
  0x013f: {
144
144
  'name': 'PrimaryChromaticities',
145
- 'description': (values) => {
146
- return values.map((value) => `${value[0]}/${value[1]}`).join(', ');
147
- }
145
+ 'description': (values) => joinMapped(values, getRationalText, ', ')
148
146
  },
149
147
  0x0141: 'HalftoneHints',
150
148
  0x0142: 'TileWidth',
@@ -152,35 +150,27 @@ export default {
152
150
  0x014a: 'A100DataOffset',
153
151
  0x014c: {
154
152
  name: 'InkSet',
155
- description: (value) => ({
153
+ description: (value) => getTableDescription({
156
154
  1: 'CMYK',
157
155
  2: 'Not CMYK'
158
- })[value] || 'Unknown'
156
+ }, value)
159
157
  },
160
158
  0x0151: 'TargetPrinter',
161
159
  0x0152: {
162
160
  name: 'ExtraSamples',
163
- description: (value) => ({
161
+ description: (value) => getTableDescription({
164
162
  0: 'Unspecified',
165
163
  1: 'Associated Alpha',
166
164
  2: 'Unassociated Alpha',
167
- })[value] || 'Unknown'
165
+ }, value)
168
166
  },
169
167
  0x0153: {
170
168
  name: 'SampleFormat',
171
169
  description: (value) => {
172
- const formats = {
173
- 1: 'Unsigned',
174
- 2: 'Signed',
175
- 3: 'Float',
176
- 4: 'Undefined',
177
- 5: 'Complex int',
178
- 6: 'Complex float',
179
- };
180
170
  if (!Array.isArray(value)) {
181
- return 'Unknown';
171
+ return getSampleFormatText(value);
182
172
  }
183
- return value.map((sample) => formats[sample] || 'Unknown').join(', ');
173
+ return value.map(getSampleFormatText).join(', ');
184
174
  }
185
175
  },
186
176
  0x0201: 'JPEGInterchangeFormat',
@@ -274,10 +264,10 @@ export default {
274
264
  0xc633: 'ShadowScale',
275
265
  0xc635: {
276
266
  name: 'MakerNoteSafety',
277
- description: (value) => ({
267
+ description: (value) => getTableDescription({
278
268
  0: 'Unsafe',
279
269
  1: 'Safe'
280
- })[value] || 'Unknown'
270
+ }, value)
281
271
  },
282
272
  0xc65a: {
283
273
  name: 'CalibrationIlluminant1',
@@ -309,12 +299,12 @@ export default {
309
299
  0xc6fc: 'ProfileToneCurve',
310
300
  0xc6fd: {
311
301
  name: 'ProfileEmbedPolicy',
312
- description: (value) => ({
302
+ description: (value) => getTableDescription({
313
303
  0: 'Allow Copying',
314
304
  1: 'Embed if Used',
315
305
  2: 'Never Embed',
316
306
  3: 'No Restrictions'
317
- })[value] || 'Unknown'
307
+ }, value)
318
308
  },
319
309
  0xc6fe: 'ProfileCopyright',
320
310
  0xc714: 'ForwardMatrix1',
@@ -325,12 +315,12 @@ export default {
325
315
  0xc719: 'PreviewSettingsDigest',
326
316
  0xc71a: {
327
317
  name: 'PreviewColorSpace',
328
- description: (value) => ({
318
+ description: (value) => getTableDescription({
329
319
  1: 'Gray Gamma 2.2',
330
320
  2: 'sRGB',
331
321
  3: 'Adobe RGB',
332
322
  4: 'ProPhoto RGB'
333
- })[value] || 'Unknown'
323
+ }, value)
334
324
  },
335
325
  0xc71b: 'PreviewDateTime',
336
326
  0xc71c: 'RawImageDigest',
@@ -347,30 +337,49 @@ export default {
347
337
  0xc7a1: 'CameraLabel',
348
338
  0xc7a3: {
349
339
  name: 'ProfileHueSatMapEncoding',
350
- description: (value) => ({
340
+ description: (value) => getTableDescription({
351
341
  0: 'Linear',
352
342
  1: 'sRGB'
353
- })[value] || 'Unknown'
343
+ }, value)
354
344
  },
355
345
  0xc7a4: {
356
346
  name: 'ProfileLookTableEncoding',
357
- description: (value) => ({
347
+ description: (value) => getTableDescription({
358
348
  0: 'Linear',
359
349
  1: 'sRGB'
360
- })[value] || 'Unknown'
350
+ }, value)
361
351
  },
362
352
  0xc7a5: 'BaselineExposureOffset',
363
353
  0xc7a6: {
364
354
  name: 'DefaultBlackRender',
365
- description: (value) => ({
355
+ description: (value) => getTableDescription({
366
356
  0: 'Auto',
367
357
  1: 'None'
368
- })[value] || 'Unknown'
358
+ }, value)
369
359
  },
370
360
  0xc7a7: 'NewRawImageDigest',
371
361
  0xc7a8: 'RawToPreviewGain'
372
362
  };
373
363
 
364
+ function getRationalText(value) {
365
+ if (!Array.isArray(value)) {
366
+ throw new TypeError('Expected a rational.');
367
+ }
368
+ return `${value[0]}/${value[1]}`;
369
+ }
370
+
371
+ function getSampleFormatText(sample) {
372
+ const formats = {
373
+ 1: 'Unsigned',
374
+ 2: 'Signed',
375
+ 3: 'Float',
376
+ 4: 'Undefined',
377
+ 5: 'Complex int',
378
+ 6: 'Complex float',
379
+ };
380
+ return getTableDescription(formats, sample);
381
+ }
382
+
374
383
  function decodeXPValue(value) {
375
384
  if (!Array.isArray(value)) {
376
385
  // A one-element value arrives as a bare number, which Uint8Array would take as a length.
@@ -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;