exifreader 4.14.0 → 4.15.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.
package/exif-reader.d.ts CHANGED
@@ -81,6 +81,25 @@ interface PngTextTags {
81
81
  [name: string]: PngTextTag
82
82
  }
83
83
 
84
+ interface RiffTags {
85
+ 'Alpha'?: {
86
+ value: 0 | 1,
87
+ description: 'No' | 'Yes'
88
+ },
89
+ 'Animation'?: {
90
+ value: 0 | 1,
91
+ description: 'No' | 'Yes'
92
+ },
93
+ 'ImageWidth'?: {
94
+ value: number,
95
+ description: string
96
+ },
97
+ 'ImageHeight'?: {
98
+ value: number,
99
+ description: string
100
+ }
101
+ }
102
+
84
103
  interface NumberFileTag {
85
104
  description: string,
86
105
  value: number
@@ -153,6 +172,7 @@ interface ExpandedTags {
153
172
  iptc?: ExifTags,
154
173
  xmp?: { _raw: string } & XmpTags,
155
174
  icc?: IccTags,
175
+ riff?: RiffTags,
156
176
  Thumbnail?: ThumbnailTags,
157
177
  gps?: GpsTags
158
178
  }
@@ -453,9 +473,9 @@ interface IccTags {
453
473
  [name: string]: ValueTag;
454
474
  }
455
475
 
456
- export type Tags = XmpTags & IccTags & PngTags & {
476
+ export type Tags = XmpTags & IccTags & PngTags & RiffTags & {
457
477
  'Thumbnail'?: ThumbnailTags;
458
478
  'Images'?: MPFImageTags[],
459
479
  } & {
460
480
  [K in keyof ExifTags]: ExifTags[K] | XmpTag;
461
- }
481
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exifreader",
3
- "version": "4.14.0",
3
+ "version": "4.15.0",
4
4
  "description": "Library that parses Exif metadata in images.",
5
5
  "author": "Mattias Wallander <mattias@wallander.eu>",
6
6
  "repository": {
@@ -22,38 +22,37 @@
22
22
  ],
23
23
  "types": "./exif-reader.d.ts",
24
24
  "optionalDependencies": {
25
- "@xmldom/xmldom": "^0.8.8"
25
+ "@xmldom/xmldom": "^0.8.10"
26
26
  },
27
27
  "devDependencies": {
28
- "@babel/core": "^7.15.8",
29
- "@babel/preset-env": "^7.15.8",
30
- "@babel/register": "^7.15.3",
31
- "@types/node": "^14.14.36",
32
- "babel-loader": "^8.2.2",
28
+ "@babel/core": "^7.23.2",
29
+ "@babel/preset-env": "^7.23.2",
30
+ "@babel/register": "^7.22.15",
31
+ "@types/node": "^18.11.9",
32
+ "babel-loader": "^8.2.5",
33
33
  "babel-plugin-rewire": "^1.2.0",
34
- "chai": "^4.3.4",
34
+ "chai": "^4.3.10",
35
35
  "cross-env": "^7.0.3",
36
- "cypress": "^8.5.0",
37
- "cypress-file-upload": "^5.0.8",
38
- "eslint": "^8.0.0",
39
- "eslint-plugin-cypress": "^2.12.1",
40
- "husky": "^4.3.8",
41
- "mocha": "^9.1.2",
36
+ "cypress": "^13.3.2",
37
+ "eslint": "^8.52.0",
38
+ "eslint-plugin-cypress": "^2.15.1",
39
+ "husky": "^8.0.0",
40
+ "mocha": "^10.2.0",
42
41
  "npm-run-all": "^4.1.5",
43
42
  "nyc": "^15.1.0",
44
- "rimraf": "^3.0.2",
45
- "string-replace-loader": "^3.0.3",
46
- "terser-webpack-plugin": "^5.2.4",
47
- "typescript": "^5.0.2",
48
- "webpack": "^5.74.0",
49
- "webpack-cli": "^4.10.0",
50
- "webpack-dev-server": "^4.11.1"
43
+ "rimraf": "^5.0.5",
44
+ "string-replace-loader": "^3.1.0",
45
+ "terser-webpack-plugin": "^5.3.9",
46
+ "typescript": "^5.2.2",
47
+ "webpack": "^5.89.0",
48
+ "webpack-cli": "^5.1.4",
49
+ "webpack-dev-server": "^4.15.1"
51
50
  },
52
51
  "scripts": {
53
52
  "build": "node bin/build.js",
54
53
  "build:pages": "node bin/build-pages.js",
55
54
  "coverage": "nyc npm test",
56
- "cypress:open": "cypress open",
55
+ "cypress:open": "cypress open --e2e",
57
56
  "lint": "eslint .",
58
57
  "start": "cross-env NODE_ENV=development webpack serve",
59
58
  "test": "cross-env BABEL_ENV=test mocha --require @babel/register --recursive \"test/unit/\"",
@@ -62,13 +61,15 @@
62
61
  "test:build:update": "node test/build/update.js",
63
62
  "test:e2e": "cypress run",
64
63
  "test:types": "tsc --noEmit --project test/types",
65
- "test:all": "npm-run-all lint coverage test:e2e test:build test:build:custom test:types",
66
- "postinstall": "node bin/build.js --only-with-config"
64
+ "test:all": "npm-run-all lint test:types coverage test:e2e test:build test:build:custom",
65
+ "pre-commit": "npm-run-all lint test test:build",
66
+ "postinstall": "node bin/build.js --only-with-config",
67
+ "prepare": "husky install"
67
68
  },
68
69
  "nyc": {
69
70
  "check-coverage": true,
70
71
  "statements": 95,
71
- "branches": 92,
72
+ "branches": 91,
72
73
  "functions": 98,
73
74
  "lines": 95,
74
75
  "reporter": [
@@ -90,10 +91,5 @@
90
91
  "Edge >= 12",
91
92
  "Safari >= 5.1",
92
93
  "Opera >= 12.1"
93
- ],
94
- "husky": {
95
- "hooks": {
96
- "pre-commit": "npm-run-all lint test test:build"
97
- }
98
- }
94
+ ]
99
95
  }
@@ -15,6 +15,7 @@ import {getStringValueFromArray} from './utils.js';
15
15
  import {getCalculatedGpsValue} from './tag-names-utils.js';
16
16
  import ImageHeader from './image-header.js';
17
17
  import Tags from './tags.js';
18
+ import MpfTags from './mpf-tags.js';
18
19
  import FileTags from './file-tags.js';
19
20
  import JfifTags from './jfif-tags.js';
20
21
  import IptcTags from './iptc-tags.js';
@@ -316,7 +317,7 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
316
317
 
317
318
  if (Constants.USE_MPF && hasMpfData(mpfDataOffset)) {
318
319
  foundMetaData = true;
319
- const readMpfTags = Tags.readMpf(dataView, mpfDataOffset, includeUnknown);
320
+ const readMpfTags = MpfTags.read(dataView, mpfDataOffset, includeUnknown);
320
321
  if (expanded) {
321
322
  tags.mpf = readMpfTags;
322
323
  } else {
@@ -0,0 +1,150 @@
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 ByteOrder from './byte-order.js';
6
+ import Types from './types.js';
7
+ import {IFD_TYPE_MPF} from './tag-names.js';
8
+ import {deferInit, getBase64Image} from './utils.js';
9
+ import {readIfd, get0thIfdOffset} from './tags-helpers.js';
10
+
11
+ export default {
12
+ read
13
+ };
14
+
15
+ const ENTRY_SIZE = 16;
16
+
17
+ function read(dataView, dataOffset, includeUnknown) {
18
+ const byteOrder = ByteOrder.getByteOrder(dataView, dataOffset);
19
+ const tags = readIfd(dataView, IFD_TYPE_MPF, dataOffset, get0thIfdOffset(dataView, dataOffset, byteOrder), byteOrder, includeUnknown);
20
+ return addMpfImages(dataView, dataOffset, tags, byteOrder);
21
+ }
22
+
23
+ function addMpfImages(dataView, dataOffset, tags, byteOrder) {
24
+ if (!tags['MPEntry']) {
25
+ return tags;
26
+ }
27
+
28
+ const images = [];
29
+ for (let i = 0; i < Math.ceil(tags['MPEntry'].value.length / ENTRY_SIZE); i++) {
30
+ images[i] = {};
31
+
32
+ const attributes = getImageNumberValue(tags['MPEntry'].value, i * ENTRY_SIZE, Types.getTypeSize('LONG'), byteOrder);
33
+ images[i]['ImageFlags'] = getImageFlags(attributes);
34
+ images[i]['ImageFormat'] = getImageFormat(attributes);
35
+ images[i]['ImageType'] = getImageType(attributes);
36
+
37
+ const imageSize = getImageNumberValue(tags['MPEntry'].value, i * ENTRY_SIZE + 4, Types.getTypeSize('LONG'), byteOrder);
38
+ images[i]['ImageSize'] = {
39
+ value: imageSize,
40
+ description: '' + imageSize
41
+ };
42
+
43
+ const imageOffset = getImageOffset(i, tags['MPEntry'], byteOrder, dataOffset);
44
+ images[i]['ImageOffset'] = {
45
+ value: imageOffset,
46
+ description: '' + imageOffset
47
+ };
48
+
49
+ const dependentImage1EntryNumber =
50
+ getImageNumberValue(tags['MPEntry'].value, i * ENTRY_SIZE + 12, Types.getTypeSize('SHORT'), byteOrder);
51
+ images[i]['DependentImage1EntryNumber'] = {
52
+ value: dependentImage1EntryNumber,
53
+ description: '' + dependentImage1EntryNumber
54
+ };
55
+
56
+ const dependentImage2EntryNumber =
57
+ getImageNumberValue(tags['MPEntry'].value, i * ENTRY_SIZE + 14, Types.getTypeSize('SHORT'), byteOrder);
58
+ images[i]['DependentImage2EntryNumber'] = {
59
+ value: dependentImage2EntryNumber,
60
+ description: '' + dependentImage2EntryNumber
61
+ };
62
+
63
+ images[i].image = dataView.buffer.slice(imageOffset, imageOffset + imageSize);
64
+ deferInit(images[i], 'base64', function () {
65
+ return getBase64Image(this.image);
66
+ });
67
+ }
68
+
69
+ tags['Images'] = images;
70
+
71
+ return tags;
72
+ }
73
+
74
+ function getImageNumberValue(entries, offset, size, byteOrder) {
75
+ if (byteOrder === ByteOrder.LITTLE_ENDIAN) {
76
+ let value = 0;
77
+ for (let i = 0; i < size; i++) {
78
+ value += entries[offset + i] << (8 * i);
79
+ }
80
+ return value;
81
+ }
82
+
83
+ let value = 0;
84
+ for (let i = 0; i < size; i++) {
85
+ value += entries[offset + i] << (8 * (size - 1 - i));
86
+ }
87
+ return value;
88
+ }
89
+
90
+ function getImageFlags(attributes) {
91
+ const flags = [
92
+ (attributes >> 31) & 0x1,
93
+ (attributes >> 30) & 0x1,
94
+ (attributes >> 29) & 0x1
95
+ ];
96
+
97
+ const flagsDescription = [];
98
+
99
+ if (flags[0]) {
100
+ flagsDescription.push('Dependent Parent Image');
101
+ }
102
+ if (flags[1]) {
103
+ flagsDescription.push('Dependent Child Image');
104
+ }
105
+ if (flags[2]) {
106
+ flagsDescription.push('Representative Image');
107
+ }
108
+
109
+ return {
110
+ value: flags,
111
+ description: flagsDescription.join(', ') || 'None'
112
+ };
113
+ }
114
+
115
+ function getImageFormat(attributes) {
116
+ const imageFormat = attributes >> 24 & 0x7;
117
+ return {
118
+ value: imageFormat,
119
+ description: imageFormat === 0 ? 'JPEG' : 'Unknown'
120
+ };
121
+ }
122
+
123
+ function getImageType(attributes) {
124
+ const type = attributes & 0xffffff;
125
+ const descriptions = {
126
+ 0x30000: 'Baseline MP Primary Image',
127
+ 0x10001: 'Large Thumbnail (VGA equivalent)',
128
+ 0x10002: 'Large Thumbnail (Full HD equivalent)',
129
+ 0x20001: 'Multi-Frame Image (Panorama)',
130
+ 0x20002: 'Multi-Frame Image (Disparity)',
131
+ 0x20003: 'Multi-Frame Image (Multi-Angle)',
132
+ 0x0: 'Undefined',
133
+ };
134
+
135
+ return {
136
+ value: type,
137
+ description: descriptions[type] || 'Unknown'
138
+ };
139
+ }
140
+
141
+ function getImageOffset(imageIndex, mpEntry, byteOrder, dataOffset) {
142
+ if (isFirstIndividualImage(imageIndex)) {
143
+ return 0;
144
+ }
145
+ return getImageNumberValue(mpEntry.value, imageIndex * ENTRY_SIZE + 8, Types.getTypeSize('LONG'), byteOrder) + dataOffset;
146
+ }
147
+
148
+ function isFirstIndividualImage(imageIndex) {
149
+ return imageIndex === 0;
150
+ }
@@ -0,0 +1,187 @@
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 Constants from './constants.js';
6
+ import Types from './types.js';
7
+ import TagNames, {IFD_TYPE_0TH, IFD_TYPE_1ST} from './tag-names.js';
8
+
9
+ const getTagValueAt = {
10
+ 1: Types.getByteAt,
11
+ 2: Types.getAsciiAt,
12
+ 3: Types.getShortAt,
13
+ 4: Types.getLongAt,
14
+ 5: Types.getRationalAt,
15
+ 7: Types.getUndefinedAt,
16
+ 9: Types.getSlongAt,
17
+ 10: Types.getSrationalAt,
18
+ 13: Types.getIfdPointerAt
19
+ };
20
+
21
+ export function get0thIfdOffset(dataView, tiffHeaderOffset, byteOrder) {
22
+ return tiffHeaderOffset + Types.getLongAt(dataView, tiffHeaderOffset + 4, byteOrder);
23
+ }
24
+
25
+ export function readIfd(dataView, ifdType, tiffHeaderOffset, offset, byteOrder, includeUnknown) {
26
+ const FIELD_COUNT_SIZE = Types.getTypeSize('SHORT');
27
+ const FIELD_SIZE = 12;
28
+
29
+ const tags = {};
30
+ const numberOfFields = getNumberOfFields(dataView, offset, byteOrder);
31
+
32
+ offset += FIELD_COUNT_SIZE;
33
+ for (let fieldIndex = 0; fieldIndex < numberOfFields; fieldIndex++) {
34
+ if (offset + FIELD_SIZE > dataView.byteLength) {
35
+ break;
36
+ }
37
+
38
+ const tag = readTag(dataView, ifdType, tiffHeaderOffset, offset, byteOrder, includeUnknown);
39
+ if (tag !== undefined) {
40
+ tags[tag.name] = {
41
+ 'id': tag.id,
42
+ 'value': tag.value,
43
+ 'description': tag.description
44
+ };
45
+ }
46
+
47
+ offset += FIELD_SIZE;
48
+ }
49
+
50
+ if (Constants.USE_THUMBNAIL && (offset < dataView.byteLength - Types.getTypeSize('LONG'))) {
51
+ const nextIfdOffset = Types.getLongAt(dataView, offset, byteOrder);
52
+ if (nextIfdOffset !== 0 && ifdType === IFD_TYPE_0TH) {
53
+ tags['Thumbnail'] = readIfd(dataView, IFD_TYPE_1ST, tiffHeaderOffset, tiffHeaderOffset + nextIfdOffset, byteOrder, true);
54
+ }
55
+ }
56
+
57
+ return tags;
58
+ }
59
+
60
+ function getNumberOfFields(dataView, offset, byteOrder) {
61
+ if (offset + Types.getTypeSize('SHORT') <= dataView.byteLength) {
62
+ return Types.getShortAt(dataView, offset, byteOrder);
63
+ }
64
+ return 0;
65
+ }
66
+
67
+ function readTag(dataView, ifdType, tiffHeaderOffset, offset, byteOrder, includeUnknown) {
68
+ const TAG_CODE_IPTC_NAA = 0x83bb;
69
+ const TAG_TYPE_OFFSET = Types.getTypeSize('SHORT');
70
+ const TAG_COUNT_OFFSET = TAG_TYPE_OFFSET + Types.getTypeSize('SHORT');
71
+ const TAG_VALUE_OFFSET = TAG_COUNT_OFFSET + Types.getTypeSize('LONG');
72
+
73
+ const tagCode = Types.getShortAt(dataView, offset, byteOrder);
74
+ const tagType = Types.getShortAt(dataView, offset + TAG_TYPE_OFFSET, byteOrder);
75
+ const tagCount = Types.getLongAt(dataView, offset + TAG_COUNT_OFFSET, byteOrder);
76
+ let tagValue;
77
+
78
+ if (Types.typeSizes[tagType] === undefined || (!includeUnknown && TagNames[ifdType][tagCode] === undefined)) {
79
+ return undefined;
80
+ }
81
+
82
+ if (tagValueFitsInOffsetSlot(tagType, tagCount)) {
83
+ tagValue = getTagValue(dataView, offset + TAG_VALUE_OFFSET, tagType, tagCount, byteOrder);
84
+ } else {
85
+ const tagValueOffset = Types.getLongAt(dataView, offset + TAG_VALUE_OFFSET, byteOrder);
86
+ if (tagValueFitsInDataView(dataView, tiffHeaderOffset, tagValueOffset, tagType, tagCount)) {
87
+ const forceByteType = tagCode === TAG_CODE_IPTC_NAA;
88
+ tagValue = getTagValue(dataView, tiffHeaderOffset + tagValueOffset, tagType, tagCount, byteOrder, forceByteType);
89
+ } else {
90
+ tagValue = '<faulty value>';
91
+ }
92
+ }
93
+
94
+ if (tagType === Types.tagTypes['ASCII']) {
95
+ tagValue = splitNullSeparatedAsciiString(tagValue);
96
+ tagValue = decodeAsciiValue(tagValue);
97
+ }
98
+
99
+ let tagName = `undefined-${tagCode}`;
100
+ let tagDescription = tagValue;
101
+
102
+ if (TagNames[ifdType][tagCode] !== undefined) {
103
+ if ((TagNames[ifdType][tagCode]['name'] !== undefined) && (TagNames[ifdType][tagCode]['description'] !== undefined)) {
104
+ tagName = TagNames[ifdType][tagCode]['name'];
105
+ try {
106
+ tagDescription = TagNames[ifdType][tagCode]['description'](tagValue);
107
+ } catch (error) {
108
+ tagDescription = getDescriptionFromTagValue(tagValue);
109
+ }
110
+ } else if ((tagType === Types.tagTypes['RATIONAL']) || (tagType === Types.tagTypes['SRATIONAL'])) {
111
+ tagName = TagNames[ifdType][tagCode];
112
+ tagDescription = '' + (tagValue[0] / tagValue[1]);
113
+ } else {
114
+ tagName = TagNames[ifdType][tagCode];
115
+ tagDescription = getDescriptionFromTagValue(tagValue);
116
+ }
117
+ }
118
+
119
+ return {
120
+ id: tagCode,
121
+ name: tagName,
122
+ value: tagValue,
123
+ description: tagDescription
124
+ };
125
+ }
126
+
127
+ function tagValueFitsInOffsetSlot(tagType, tagCount) {
128
+ return Types.typeSizes[tagType] * tagCount <= Types.getTypeSize('LONG');
129
+ }
130
+
131
+ function getTagValue(dataView, offset, type, count, byteOrder, forceByteType = false) {
132
+ let value = [];
133
+
134
+ if (forceByteType) {
135
+ count = count * Types.typeSizes[type];
136
+ type = Types.tagTypes['BYTE'];
137
+ }
138
+ for (let valueIndex = 0; valueIndex < count; valueIndex++) {
139
+ value.push(getTagValueAt[type](dataView, offset, byteOrder));
140
+ offset += Types.typeSizes[type];
141
+ }
142
+
143
+ if (type === Types.tagTypes['ASCII']) {
144
+ value = Types.getAsciiValue(value);
145
+ } else if (value.length === 1) {
146
+ value = value[0];
147
+ }
148
+
149
+ return value;
150
+ }
151
+
152
+ function tagValueFitsInDataView(dataView, tiffHeaderOffset, tagValueOffset, tagType, tagCount) {
153
+ return tiffHeaderOffset + tagValueOffset + Types.typeSizes[tagType] * tagCount <= dataView.byteLength;
154
+ }
155
+
156
+ function splitNullSeparatedAsciiString(string) {
157
+ const tagValue = [];
158
+ let i = 0;
159
+
160
+ for (let j = 0; j < string.length; j++) {
161
+ if (string[j] === '\x00') {
162
+ i++;
163
+ continue;
164
+ }
165
+ if (tagValue[i] === undefined) {
166
+ tagValue[i] = '';
167
+ }
168
+ tagValue[i] += string[j];
169
+ }
170
+
171
+ return tagValue;
172
+ }
173
+
174
+ function decodeAsciiValue(asciiValue) {
175
+ try {
176
+ return asciiValue.map((value) => decodeURIComponent(escape(value)));
177
+ } catch (error) {
178
+ return asciiValue;
179
+ }
180
+ }
181
+
182
+ function getDescriptionFromTagValue(tagValue) {
183
+ if (tagValue instanceof Array) {
184
+ return tagValue.join(', ');
185
+ }
186
+ return tagValue;
187
+ }