exifreader 4.21.1 → 4.22.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/README.md +5 -3
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/exif-reader.d.ts +1 -0
- package/package.json +6 -6
- package/src/constants.js +1 -0
- package/src/exif-reader.js +1 -0
- package/src/image-header-avif.js +42 -0
- package/src/image-header-heic.js +20 -167
- package/src/image-header-iso-bmff-iloc.js +142 -0
- package/src/image-header-iso-bmff-utils.js +5 -0
- package/src/image-header-iso-bmff.js +460 -0
- package/src/image-header.js +5 -0
- package/webpack.config.js +2 -1
package/exif-reader.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exifreader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.22.0",
|
|
4
4
|
"description": "Library that parses Exif metadata in images.",
|
|
5
5
|
"author": "Mattias Wallander <mattias@wallander.eu>",
|
|
6
6
|
"repository": {
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"cypress": "^13.3.2",
|
|
37
37
|
"eslint": "^8.52.0",
|
|
38
38
|
"eslint-plugin-cypress": "^2.15.1",
|
|
39
|
-
"husky": "^
|
|
39
|
+
"husky": "^9.0.11",
|
|
40
40
|
"mocha": "^10.2.0",
|
|
41
41
|
"npm-run-all": "^4.1.5",
|
|
42
42
|
"nyc": "^15.1.0",
|
|
@@ -64,14 +64,14 @@
|
|
|
64
64
|
"test:all": "npm-run-all lint test:types coverage test:e2e test:build test:build:custom",
|
|
65
65
|
"pre-commit": "npm-run-all lint test:types coverage test:build",
|
|
66
66
|
"postinstall": "node bin/build.js --only-with-config",
|
|
67
|
-
"prepare": "husky
|
|
67
|
+
"prepare": "husky"
|
|
68
68
|
},
|
|
69
69
|
"nyc": {
|
|
70
70
|
"check-coverage": true,
|
|
71
|
-
"statements":
|
|
72
|
-
"branches":
|
|
71
|
+
"statements": 93,
|
|
72
|
+
"branches": 88,
|
|
73
73
|
"functions": 98,
|
|
74
|
-
"lines":
|
|
74
|
+
"lines": 93,
|
|
75
75
|
"reporter": [
|
|
76
76
|
"lcov",
|
|
77
77
|
"text"
|
package/src/constants.js
CHANGED
package/src/exif-reader.js
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
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
|
+
// Specification:
|
|
6
|
+
// https://aomediacodec.github.io/av1-avif
|
|
7
|
+
|
|
8
|
+
import {parseBox, findOffsets} from './image-header-iso-bmff.js';
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
isAvifFile,
|
|
12
|
+
findAvifOffsets
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the provided data view represents an AVIF file.
|
|
17
|
+
*
|
|
18
|
+
* @param {DataView} dataView - The data view to check.
|
|
19
|
+
* @returns {boolean} True if the data view represents an AVIF file, false otherwise.
|
|
20
|
+
*/
|
|
21
|
+
function isAvifFile(dataView) {
|
|
22
|
+
if (!dataView) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const headerBox = parseBox(dataView, 0);
|
|
28
|
+
return headerBox && headerBox.majorBrand === 'avif';
|
|
29
|
+
} catch (error) {
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Finds the offsets of an AVIF file in the provided data view.
|
|
36
|
+
*
|
|
37
|
+
* @param {DataView} dataView - The data view to find offsets in.
|
|
38
|
+
* @returns {Object} An object containing the offsets of the TIFF header, XMP chunks, ICC chunks, and a boolean indicating if any of these exist.
|
|
39
|
+
*/
|
|
40
|
+
function findAvifOffsets(dataView) {
|
|
41
|
+
return findOffsets(dataView);
|
|
42
|
+
}
|
package/src/image-header-heic.js
CHANGED
|
@@ -2,187 +2,40 @@
|
|
|
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 {
|
|
6
|
-
import Constants from './constants.js';
|
|
5
|
+
import {parseBox, findOffsets} from './image-header-iso-bmff.js';
|
|
7
6
|
|
|
8
7
|
export default {
|
|
9
8
|
isHeicFile,
|
|
10
9
|
findHeicOffsets
|
|
11
10
|
};
|
|
12
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the provided data view represents a HEIC/HEIF file.
|
|
14
|
+
*
|
|
15
|
+
* @param {DataView} dataView - The data view to check.
|
|
16
|
+
* @returns {boolean} True if the data view represents a HEIC/HEIF file, false otherwise.
|
|
17
|
+
*/
|
|
13
18
|
function isHeicFile(dataView) {
|
|
14
19
|
if (!dataView) {
|
|
15
20
|
return false;
|
|
16
21
|
}
|
|
17
22
|
|
|
18
|
-
const HEIC_ID = 'ftyp';
|
|
19
|
-
const HEIC_ID_OFFSET = 4;
|
|
20
23
|
const HEIC_MAJOR_BRANDS = ['heic', 'heix', 'hevc', 'hevx', 'heim', 'heis', 'hevm', 'hevs', 'mif1'];
|
|
21
|
-
const HEIC_MAJOR_BRAND_LENGTH = 4;
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
function findHeicOffsets(dataView) {
|
|
30
|
-
if (Constants.USE_EXIF || Constants.USE_ICC) {
|
|
31
|
-
const {offset: metaOffset, length: metaLength} = findMetaAtom(dataView);
|
|
32
|
-
if (metaOffset === undefined) {
|
|
33
|
-
return {hasAppMarkers: false};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const metaEndOffset = Math.min(metaOffset + metaLength, dataView.byteLength);
|
|
37
|
-
const {exifItemOffset, ilocOffset, colrOffset} = findMetaItems(dataView, metaOffset, metaEndOffset);
|
|
38
|
-
|
|
39
|
-
const exifOffset = findExifOffset(dataView, exifItemOffset, ilocOffset, metaEndOffset);
|
|
40
|
-
const iccChunks = findIccChunks(dataView, colrOffset, metaEndOffset);
|
|
41
|
-
return {
|
|
42
|
-
hasAppMarkers: (exifOffset !== undefined) || (iccChunks !== undefined),
|
|
43
|
-
tiffHeaderOffset: exifOffset,
|
|
44
|
-
iccChunks
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return {hasAppMarkers: false};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function findMetaAtom(dataView) {
|
|
52
|
-
const ATOM_LENGTH_SIZE = 4;
|
|
53
|
-
const ATOM_TYPE_SIZE = 4;
|
|
54
|
-
const ATOM_MIN_LENGTH = 8;
|
|
55
|
-
const ATOM_TYPE_OFFSET = 4;
|
|
56
|
-
|
|
57
|
-
let offset = 0;
|
|
58
|
-
|
|
59
|
-
while (offset + ATOM_LENGTH_SIZE + ATOM_TYPE_SIZE <= dataView.byteLength) {
|
|
60
|
-
const atomLength = getAtomLength(dataView, offset);
|
|
61
|
-
if (atomLength >= ATOM_MIN_LENGTH) {
|
|
62
|
-
const atomType = getStringFromDataView(dataView, offset + ATOM_TYPE_OFFSET, ATOM_TYPE_SIZE);
|
|
63
|
-
if (atomType === 'meta') {
|
|
64
|
-
return {
|
|
65
|
-
offset,
|
|
66
|
-
length: atomLength
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
offset += atomLength;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
offset: undefined,
|
|
76
|
-
length: 0
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
function getAtomLength(dataView, offset) {
|
|
81
|
-
const ATOM_EXTENDED_SIZE_LOW_OFFSET = 12;
|
|
82
|
-
|
|
83
|
-
const atomLength = dataView.getUint32(offset);
|
|
84
|
-
if (extendsToEndOfFile(atomLength)) {
|
|
85
|
-
return dataView.byteLength - offset;
|
|
86
|
-
}
|
|
87
|
-
if (hasExtendedSize(atomLength)) {
|
|
88
|
-
if (hasEmptyHighBits(dataView, offset)) {
|
|
89
|
-
// It's a bit tricky to handle 64 bit numbers in JavaScript. Let's
|
|
90
|
-
// wait until there are real-world examples where it is necessary.
|
|
91
|
-
return dataView.getUint32(offset + ATOM_EXTENDED_SIZE_LOW_OFFSET);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return atomLength;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function extendsToEndOfFile(atomLength) {
|
|
99
|
-
return atomLength === 0;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function hasExtendedSize(atomLength) {
|
|
103
|
-
return atomLength === 1;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function hasEmptyHighBits(dataView, offset) {
|
|
107
|
-
const ATOM_EXTENDED_SIZE_OFFSET = 8;
|
|
108
|
-
return dataView.getUint32(offset + ATOM_EXTENDED_SIZE_OFFSET) === 0;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function findMetaItems(dataView, offset, metaEndOffset) {
|
|
112
|
-
const STRING_SIZE = 4;
|
|
113
|
-
const ITEM_INDEX_REL_OFFSET = -4;
|
|
114
|
-
const offsets = {
|
|
115
|
-
ilocOffset: undefined,
|
|
116
|
-
exifItemOffset: undefined,
|
|
117
|
-
colrOffset: undefined
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
while ((offset + STRING_SIZE <= metaEndOffset)
|
|
121
|
-
&& (!offsets.ilocOffset || !offsets.exifItemOffset || !offsets.colrOffset)) {
|
|
122
|
-
const itemName = getStringFromDataView(dataView, offset, STRING_SIZE);
|
|
123
|
-
if (Constants.USE_EXIF && (itemName === 'iloc')) {
|
|
124
|
-
offsets.ilocOffset = offset;
|
|
125
|
-
} else if (Constants.USE_EXIF && (itemName === 'Exif')) {
|
|
126
|
-
offsets.exifItemOffset = offset + ITEM_INDEX_REL_OFFSET;
|
|
127
|
-
} else if (Constants.USE_ICC && (itemName === 'colr')) {
|
|
128
|
-
offsets.colrOffset = offset + ITEM_INDEX_REL_OFFSET;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
offset++;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return offsets;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function findExifOffset(dataView, exifItemOffset, offset, metaEndOffset) {
|
|
138
|
-
const EXIF_ITEM_OFFSET_SIZE = 2;
|
|
139
|
-
const ILOC_DATA_OFFSET = 12;
|
|
140
|
-
const EXIF_POINTER_OFFSET = 8;
|
|
141
|
-
const EXIF_POINTER_SIZE = 4;
|
|
142
|
-
const EXIF_PREFIX_LENGTH_OFFSET = 4;
|
|
143
|
-
const ILOC_ITEM_SIZE = 16;
|
|
144
|
-
|
|
145
|
-
if (!offset || !exifItemOffset || (exifItemOffset + EXIF_ITEM_OFFSET_SIZE > metaEndOffset)) {
|
|
146
|
-
return undefined;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
const exifItemIndex = dataView.getUint16(exifItemOffset);
|
|
150
|
-
offset += ILOC_DATA_OFFSET;
|
|
151
|
-
|
|
152
|
-
while (offset + ILOC_ITEM_SIZE <= metaEndOffset) {
|
|
153
|
-
const itemIndex = dataView.getUint16(offset);
|
|
154
|
-
if (itemIndex === exifItemIndex) {
|
|
155
|
-
const exifPointer = dataView.getUint32(offset + EXIF_POINTER_OFFSET);
|
|
156
|
-
if (exifPointer + EXIF_POINTER_SIZE <= dataView.byteLength) {
|
|
157
|
-
const exifOffset = dataView.getUint32(exifPointer);
|
|
158
|
-
const prefixLength = exifOffset + EXIF_PREFIX_LENGTH_OFFSET;
|
|
159
|
-
return exifPointer + prefixLength;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
offset += ILOC_ITEM_SIZE;
|
|
25
|
+
try {
|
|
26
|
+
const headerBox = parseBox(dataView, 0);
|
|
27
|
+
return headerBox && HEIC_MAJOR_BRANDS.indexOf(headerBox.majorBrand) !== -1;
|
|
28
|
+
} catch (error) {
|
|
29
|
+
return false;
|
|
163
30
|
}
|
|
164
|
-
|
|
165
|
-
return undefined;
|
|
166
31
|
}
|
|
167
32
|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const colorType = getStringFromDataView(dataView, offset + ITEM_TYPE_OFFSET, ITEM_TYPE_SIZE);
|
|
178
|
-
if ((colorType !== 'prof') && (colorType !== 'rICC')) {
|
|
179
|
-
return undefined;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return [{
|
|
183
|
-
offset: offset + ITEM_CONTENT_OFFSET,
|
|
184
|
-
length: getAtomLength(dataView, offset) - ITEM_CONTENT_OFFSET,
|
|
185
|
-
chunkNumber: 1,
|
|
186
|
-
chunksTotal: 1
|
|
187
|
-
}];
|
|
33
|
+
/**
|
|
34
|
+
* Finds the offsets of a HEIC file in the provided data view.
|
|
35
|
+
*
|
|
36
|
+
* @param {DataView} dataView - The data view to find offsets in.
|
|
37
|
+
* @returns {Object} An object containing the offsets of the TIFF header, XMP chunks, ICC chunks, and a boolean indicating if any of these exist.
|
|
38
|
+
*/
|
|
39
|
+
function findHeicOffsets(dataView) {
|
|
40
|
+
return findOffsets(dataView);
|
|
188
41
|
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import {get64BitValue} from './image-header-iso-bmff-utils.js';
|
|
2
|
+
|
|
3
|
+
export function parseItemLocationBox(dataView, version, contentOffset, boxLength) {
|
|
4
|
+
const FLAGS_SIZE = 3;
|
|
5
|
+
|
|
6
|
+
const {offsets, sizes} = getItemLocationBoxOffsetsAndSizes(version, contentOffset + FLAGS_SIZE);
|
|
7
|
+
|
|
8
|
+
const offsetSize = dataView.getUint8(offsets.offsetSize) >> 4;
|
|
9
|
+
sizes.item.extent.extentOffset = offsetSize;
|
|
10
|
+
const lengthSize = dataView.getUint8(offsets.lengthSize) & 0x0f;
|
|
11
|
+
sizes.item.extent.extentLength = lengthSize;
|
|
12
|
+
const baseOffsetSize = dataView.getUint8(offsets.baseOffsetSize) >> 4;
|
|
13
|
+
sizes.item.baseOffset = baseOffsetSize;
|
|
14
|
+
const indexSize = getIndexSize(dataView, offsets.indexSize, version);
|
|
15
|
+
sizes.item.extent.extentIndex = indexSize !== undefined ? indexSize : 0;
|
|
16
|
+
const itemCount = getItemCount(dataView, offsets.itemCount, version);
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
type: 'iloc',
|
|
20
|
+
items: getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, indexSize, itemCount),
|
|
21
|
+
length: boxLength
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getItemLocationBoxOffsetsAndSizes(version, contentOffset) {
|
|
26
|
+
const sizes = {
|
|
27
|
+
item: {
|
|
28
|
+
dataReferenceIndex: 2,
|
|
29
|
+
extentCount: 2,
|
|
30
|
+
extent: {}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
if (version < 2) {
|
|
34
|
+
sizes.itemCount = 2;
|
|
35
|
+
sizes.item.itemId = 2;
|
|
36
|
+
} else if (version === 2) {
|
|
37
|
+
sizes.itemCount = 4;
|
|
38
|
+
sizes.item.itemId = 4;
|
|
39
|
+
}
|
|
40
|
+
if (version === 1 || version === 2) {
|
|
41
|
+
sizes.item.constructionMethod = 2;
|
|
42
|
+
} else {
|
|
43
|
+
sizes.item.constructionMethod = 0;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const offsets = {
|
|
47
|
+
offsetSize: contentOffset,
|
|
48
|
+
lengthSize: contentOffset,
|
|
49
|
+
baseOffsetSize: contentOffset + 1,
|
|
50
|
+
indexSize: contentOffset + 1
|
|
51
|
+
};
|
|
52
|
+
offsets.itemCount = contentOffset + 2;
|
|
53
|
+
offsets.items = offsets.itemCount + sizes.itemCount;
|
|
54
|
+
offsets.item = {
|
|
55
|
+
itemId: 0
|
|
56
|
+
};
|
|
57
|
+
offsets.item.constructionMethod = offsets.item.itemId + sizes.item.itemId;
|
|
58
|
+
offsets.item.dataReferenceIndex = offsets.item.constructionMethod + sizes.item.constructionMethod;
|
|
59
|
+
|
|
60
|
+
return {offsets, sizes};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function getIndexSize(dataView, offset, version) {
|
|
64
|
+
if (version === 1 || version === 2) {
|
|
65
|
+
return dataView.getUint8(offset) & 0x0f;
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getItemCount(dataView, offset, version) {
|
|
71
|
+
if (version < 2) {
|
|
72
|
+
return dataView.getUint16(offset);
|
|
73
|
+
} else if (version === 2) {
|
|
74
|
+
return dataView.getUint32(offset);
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function getItems(dataView, version, offsets, sizes, offsetSize, lengthSize, indexSize, itemCount) {
|
|
80
|
+
if (itemCount === undefined) {
|
|
81
|
+
return [];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const items = [];
|
|
85
|
+
let offset = offsets.items;
|
|
86
|
+
|
|
87
|
+
for (let i = 0; i < itemCount; i++) {
|
|
88
|
+
const item = {extents: []};
|
|
89
|
+
item.itemId = getItemId(dataView, offset, version);
|
|
90
|
+
offset += sizes.item.itemId;
|
|
91
|
+
item.constructionMethod = (version === 1) || (version === 2) ? dataView.getUint16(offset) & 0x0f : undefined;
|
|
92
|
+
offset += sizes.item.constructionMethod;
|
|
93
|
+
item.dataReferenceIndex = dataView.getUint16(offset);
|
|
94
|
+
offset += sizes.item.dataReferenceIndex;
|
|
95
|
+
item.baseOffset = getVariableSizedValue(dataView, offset, sizes.item.baseOffset);
|
|
96
|
+
offset += sizes.item.baseOffset;
|
|
97
|
+
item.extentCount = dataView.getUint16(offset);
|
|
98
|
+
offset += sizes.item.extentCount;
|
|
99
|
+
for (let j = 0; j < item.extentCount; j++) {
|
|
100
|
+
const extent = {};
|
|
101
|
+
|
|
102
|
+
extent.extentIndex = getExtentIndex(dataView, version, offset, indexSize);
|
|
103
|
+
offset += sizes.item.extent.extentIndex;
|
|
104
|
+
extent.extentOffset = getVariableSizedValue(dataView, offset, offsetSize);
|
|
105
|
+
offset += sizes.item.extent.extentOffset;
|
|
106
|
+
extent.extentLength = getVariableSizedValue(dataView, offset, lengthSize);
|
|
107
|
+
offset += sizes.item.extent.extentLength;
|
|
108
|
+
|
|
109
|
+
item.extents.push(extent);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
items.push(item);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return items;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function getItemId(dataView, offset, version) {
|
|
119
|
+
if (version < 2) {
|
|
120
|
+
return dataView.getUint16(offset);
|
|
121
|
+
} else if (version === 2) {
|
|
122
|
+
return dataView.getUint32(offset);
|
|
123
|
+
}
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function getExtentIndex(dataView, version, offset, indexSize) {
|
|
128
|
+
if ((version === 1 || version === 2) && indexSize > 0) {
|
|
129
|
+
return getVariableSizedValue(dataView, offset, indexSize);
|
|
130
|
+
}
|
|
131
|
+
return undefined;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getVariableSizedValue(dataView, offset, size) {
|
|
135
|
+
if (size === 4) {
|
|
136
|
+
return dataView.getUint32(offset);
|
|
137
|
+
}
|
|
138
|
+
if (size === 8) {
|
|
139
|
+
return get64BitValue(dataView, offset);
|
|
140
|
+
}
|
|
141
|
+
return 0;
|
|
142
|
+
}
|