exifreader 4.39.0 → 4.40.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 +324 -166
- package/bin/profile-diff.js +171 -0
- package/bin/profile.js +852 -0
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/exif-reader.d.ts +53 -10
- package/package.json +7 -7
- package/src/exif-reader.js +36 -142
- package/src/file-loaders.js +308 -0
- package/src/image-header-avif.js +7 -4
- package/src/image-header-gif.js +21 -1
- package/src/image-header-heic.js +7 -4
- package/src/image-header-iso-bmff.js +169 -18
- package/src/image-header-jpeg.js +53 -1
- package/src/image-header-jxl.js +63 -12
- package/src/image-header-png.js +51 -5
- package/src/image-header-webp.js +32 -2
- package/src/image-header.js +32 -12
- package/src/load-auto.js +310 -0
- package/src/loadview-pipeline.js +66 -1
- package/src/tag-decoder.js +40 -1
- package/src/xml.js +39 -1
package/src/image-header-jpeg.js
CHANGED
|
@@ -65,11 +65,27 @@ const APP1_XMP_EXTENDED_IDENTIFIER = 'http://ns.adobe.com/xmp/extension/\x00';
|
|
|
65
65
|
const APP13_IPTC_IDENTIFIER = 'Photoshop 3.0';
|
|
66
66
|
const APP13_RESOURCE_BLOCK_IDENTIFIER = '8BIM';
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Checks if the provided data view represents a JPEG file.
|
|
70
|
+
*
|
|
71
|
+
* @param {DataView} dataView The data view to check.
|
|
72
|
+
* @returns {boolean} True if the data view starts with the JPEG SOI marker.
|
|
73
|
+
*/
|
|
68
74
|
function isJpegFile(dataView) {
|
|
69
75
|
return !!dataView && (dataView.byteLength >= MIN_JPEG_DATA_BUFFER_LENGTH) && (dataView.getUint16(0) === JPEG_ID);
|
|
70
76
|
}
|
|
71
77
|
|
|
72
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Walks the JPEG APP/SOF markers and reports the offsets ExifReader needs.
|
|
80
|
+
*
|
|
81
|
+
* @param {DataView} dataView The data view to find offsets in.
|
|
82
|
+
* @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
|
|
83
|
+
* receives one block per recognized segment (Exif/XMP/IPTC/ICC/MPF/JFIF
|
|
84
|
+
* and SOF for file tags) and gets `truncated` set to true if the loop
|
|
85
|
+
* never saw the SOS marker.
|
|
86
|
+
* @returns {Object} Per-segment offsets plus `hasAppMarkers`.
|
|
87
|
+
*/
|
|
88
|
+
function findJpegOffsets(dataView, metadataBlocks) {
|
|
73
89
|
let appMarkerPosition = JPEG_ID_SIZE;
|
|
74
90
|
let fieldLength;
|
|
75
91
|
let sof0DataOffset;
|
|
@@ -85,17 +101,26 @@ function findJpegOffsets(dataView) {
|
|
|
85
101
|
let bestExifSegmentTiffHeaderOffset;
|
|
86
102
|
let bestExifSegmentAppMarkerPosition;
|
|
87
103
|
let bestExifSegmentFieldLength;
|
|
104
|
+
let sawSos = false;
|
|
88
105
|
|
|
89
106
|
while (appMarkerPosition + APP_ID_OFFSET + 5 <= dataView.byteLength) {
|
|
107
|
+
let blockType;
|
|
108
|
+
if (dataView.getUint16(appMarkerPosition) === SOS_MARKER) {
|
|
109
|
+
// Reaching SOS means the metadata region is closed.
|
|
110
|
+
sawSos = true;
|
|
111
|
+
}
|
|
90
112
|
if (Constants.USE_FILE && isSOF0Marker(dataView, appMarkerPosition)) {
|
|
91
113
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
92
114
|
sof0DataOffset = appMarkerPosition + APP_MARKER_SIZE;
|
|
115
|
+
blockType = 'file';
|
|
93
116
|
} else if (Constants.USE_FILE && isSOF2Marker(dataView, appMarkerPosition)) {
|
|
94
117
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
95
118
|
sof2DataOffset = appMarkerPosition + APP_MARKER_SIZE;
|
|
119
|
+
blockType = 'file';
|
|
96
120
|
} else if (Constants.USE_JFIF && isApp0JfifMarker(dataView, appMarkerPosition)) {
|
|
97
121
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
98
122
|
jfifDataOffset = appMarkerPosition + JFIF_DATA_OFFSET;
|
|
123
|
+
blockType = 'jfif';
|
|
99
124
|
} else if (Constants.USE_EXIF && isApp1ExifMarker(dataView, appMarkerPosition)) {
|
|
100
125
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
101
126
|
exifSegmentCount++;
|
|
@@ -142,21 +167,25 @@ function findJpegOffsets(dataView) {
|
|
|
142
167
|
}
|
|
143
168
|
|
|
144
169
|
tiffHeaderOffset = bestExifSegmentTiffHeaderOffset;
|
|
170
|
+
blockType = 'exif';
|
|
145
171
|
} else if (Constants.USE_XMP && isApp1XmpMarker(dataView, appMarkerPosition)) {
|
|
146
172
|
if (!xmpChunks) {
|
|
147
173
|
xmpChunks = [];
|
|
148
174
|
}
|
|
149
175
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
150
176
|
xmpChunks.push(getXmpChunkDetails(appMarkerPosition, fieldLength));
|
|
177
|
+
blockType = 'xmp';
|
|
151
178
|
} else if (Constants.USE_XMP && isApp1ExtendedXmpMarker(dataView, appMarkerPosition)) {
|
|
152
179
|
if (!xmpChunks) {
|
|
153
180
|
xmpChunks = [];
|
|
154
181
|
}
|
|
155
182
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
156
183
|
xmpChunks.push(getExtendedXmpChunkDetails(appMarkerPosition, fieldLength));
|
|
184
|
+
blockType = 'xmp';
|
|
157
185
|
} else if (Constants.USE_IPTC && isApp13PhotoshopMarker(dataView, appMarkerPosition)) {
|
|
158
186
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
159
187
|
iptcDataOffset = appMarkerPosition + IPTC_DATA_OFFSET;
|
|
188
|
+
blockType = 'iptc';
|
|
160
189
|
} else if (Constants.USE_ICC && isApp2ICCMarker(dataView, appMarkerPosition)) {
|
|
161
190
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
162
191
|
const iccDataOffset = appMarkerPosition + APP2_ICC_DATA_OFFSET;
|
|
@@ -168,9 +197,11 @@ function findJpegOffsets(dataView) {
|
|
|
168
197
|
iccChunks = [];
|
|
169
198
|
}
|
|
170
199
|
iccChunks.push({offset: iccDataOffset, length: iccDataLength, chunkNumber: iccChunkNumber, chunksTotal: iccChunksTotal});
|
|
200
|
+
blockType = 'icc';
|
|
171
201
|
} else if (Constants.USE_MPF && isApp2MPFMarker(dataView, appMarkerPosition)) {
|
|
172
202
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
173
203
|
mpfDataOffset = appMarkerPosition + MPF_DATA_OFFSET;
|
|
204
|
+
blockType = 'mpf';
|
|
174
205
|
} else if (isAppMarker(dataView, appMarkerPosition)) {
|
|
175
206
|
fieldLength = dataView.getUint16(appMarkerPosition + APP_MARKER_SIZE);
|
|
176
207
|
} else if (isFillByte(dataView, appMarkerPosition)) {
|
|
@@ -179,6 +210,13 @@ function findJpegOffsets(dataView) {
|
|
|
179
210
|
} else {
|
|
180
211
|
break;
|
|
181
212
|
}
|
|
213
|
+
if (metadataBlocks && blockType) {
|
|
214
|
+
metadataBlocks.push({
|
|
215
|
+
type: blockType,
|
|
216
|
+
start: appMarkerPosition,
|
|
217
|
+
end: appMarkerPosition + APP_MARKER_SIZE + fieldLength,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
182
220
|
appMarkerPosition += APP_MARKER_SIZE + fieldLength;
|
|
183
221
|
}
|
|
184
222
|
|
|
@@ -186,6 +224,20 @@ function findJpegOffsets(dataView) {
|
|
|
186
224
|
warnAboutMultipleExifSegments(exifSegmentCount);
|
|
187
225
|
}
|
|
188
226
|
|
|
227
|
+
if (metadataBlocks) {
|
|
228
|
+
// The while-condition requires +5 bytes to enter, so the SOS marker
|
|
229
|
+
// can sit right at the boundary where the loop exits without
|
|
230
|
+
// entering. Catch that here.
|
|
231
|
+
if (!sawSos
|
|
232
|
+
&& appMarkerPosition + APP_MARKER_SIZE <= dataView.byteLength
|
|
233
|
+
&& dataView.getUint16(appMarkerPosition) === SOS_MARKER) {
|
|
234
|
+
sawSos = true;
|
|
235
|
+
}
|
|
236
|
+
// The pipeline combines this with the per-block end <= byteLength
|
|
237
|
+
// check to compute the overall `complete` flag.
|
|
238
|
+
metadataBlocks.truncated = !sawSos;
|
|
239
|
+
}
|
|
240
|
+
|
|
189
241
|
return {
|
|
190
242
|
hasAppMarkers: appMarkerPosition > JPEG_ID_SIZE,
|
|
191
243
|
fileDataOffset: sof0DataOffset || sof2DataOffset,
|
package/src/image-header-jxl.js
CHANGED
|
@@ -27,6 +27,14 @@ const TYPE_JXLP = 0x6a786c70;
|
|
|
27
27
|
const JXLP_SEQUENCE_NUMBER_SIZE = 4;
|
|
28
28
|
const BROB_ORIGINAL_TYPE_SIZE = 4;
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Checks if the provided data view represents a JPEG XL file (container or
|
|
32
|
+
* bare codestream).
|
|
33
|
+
*
|
|
34
|
+
* @param {DataView} dataView The data view to check.
|
|
35
|
+
* @returns {boolean} True for either a JXL container signature or a bare
|
|
36
|
+
* codestream signature.
|
|
37
|
+
*/
|
|
30
38
|
function isJxlFile(dataView) {
|
|
31
39
|
try {
|
|
32
40
|
return isJxlContainer(dataView) || isJxlCodestream(dataView);
|
|
@@ -58,7 +66,17 @@ function isJxlCodestream(dataView) {
|
|
|
58
66
|
&& dataView.getUint8(1) === JXL_CODESTREAM_SIGNATURE[1];
|
|
59
67
|
}
|
|
60
68
|
|
|
61
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Walks the JPEG XL box list and reports the offsets ExifReader needs.
|
|
71
|
+
*
|
|
72
|
+
* @param {DataView} dataView The data view to find offsets in.
|
|
73
|
+
* @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
|
|
74
|
+
* receives one block per recognized box (Exif, xml, brob with Exif or
|
|
75
|
+
* xml original type). The codestream box (`jxlc`/`jxlp`) does not emit
|
|
76
|
+
* a block because it wraps the encoded image, not metadata.
|
|
77
|
+
* @returns {Object} hasAppMarkers and the per-section offsets.
|
|
78
|
+
*/
|
|
79
|
+
function findJxlOffsets(dataView, metadataBlocks) {
|
|
62
80
|
let offset = 0;
|
|
63
81
|
let tiffHeaderOffset;
|
|
64
82
|
let xmpChunks;
|
|
@@ -67,7 +85,16 @@ function findJxlOffsets(dataView) {
|
|
|
67
85
|
let jxlCodestreamOffset;
|
|
68
86
|
|
|
69
87
|
if (isJxlCodestream(dataView)) {
|
|
70
|
-
|
|
88
|
+
// A bare codestream has no box structure, so the box walk below
|
|
89
|
+
// would only ever misinterpret codestream bytes as box headers.
|
|
90
|
+
return {
|
|
91
|
+
hasAppMarkers: true,
|
|
92
|
+
tiffHeaderOffset: undefined,
|
|
93
|
+
xmpChunks: undefined,
|
|
94
|
+
brobExifChunk: undefined,
|
|
95
|
+
brobXmpChunk: undefined,
|
|
96
|
+
jxlCodestreamOffset: 0,
|
|
97
|
+
};
|
|
71
98
|
}
|
|
72
99
|
|
|
73
100
|
while (offset + BOX_HEADER_MIN_SIZE <= dataView.byteLength) {
|
|
@@ -77,6 +104,7 @@ function findJxlOffsets(dataView) {
|
|
|
77
104
|
}
|
|
78
105
|
|
|
79
106
|
const boxType = dataView.getUint32(offset + BOX_TYPE_OFFSET);
|
|
107
|
+
let blockType;
|
|
80
108
|
|
|
81
109
|
if (Constants.USE_EXIF && boxType === TYPE_EXIF) {
|
|
82
110
|
try {
|
|
@@ -84,6 +112,7 @@ function findJxlOffsets(dataView) {
|
|
|
84
112
|
} catch (_error) {
|
|
85
113
|
// Truncated Exif data.
|
|
86
114
|
}
|
|
115
|
+
blockType = 'exif';
|
|
87
116
|
}
|
|
88
117
|
|
|
89
118
|
if (Constants.USE_XMP && boxType === TYPE_XML) {
|
|
@@ -91,10 +120,13 @@ function findJxlOffsets(dataView) {
|
|
|
91
120
|
dataOffset: contentOffset,
|
|
92
121
|
length: length - (contentOffset - offset)
|
|
93
122
|
}];
|
|
123
|
+
blockType = 'xmp';
|
|
94
124
|
}
|
|
95
125
|
|
|
96
126
|
if (boxType === TYPE_JXLC && jxlCodestreamOffset === undefined) {
|
|
97
127
|
jxlCodestreamOffset = contentOffset;
|
|
128
|
+
// No block emitted: the codestream box wraps the encoded image,
|
|
129
|
+
// marking it would inflate metadataRange.end past pixel data.
|
|
98
130
|
}
|
|
99
131
|
|
|
100
132
|
if (boxType === TYPE_JXLP && jxlCodestreamOffset === undefined
|
|
@@ -102,6 +134,7 @@ function findJxlOffsets(dataView) {
|
|
|
102
134
|
const sequenceNumber = dataView.getUint32(contentOffset) & 0x7FFFFFFF;
|
|
103
135
|
if (sequenceNumber === 0) {
|
|
104
136
|
jxlCodestreamOffset = contentOffset + JXLP_SEQUENCE_NUMBER_SIZE;
|
|
137
|
+
// Same reason as TYPE_JXLC above.
|
|
105
138
|
}
|
|
106
139
|
}
|
|
107
140
|
|
|
@@ -110,20 +143,38 @@ function findJxlOffsets(dataView) {
|
|
|
110
143
|
const compressedDataOffset = contentOffset + BROB_ORIGINAL_TYPE_SIZE;
|
|
111
144
|
const compressedDataLength = length - (compressedDataOffset - offset);
|
|
112
145
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
146
|
+
// Decouple block emission from parsing-source selection. A
|
|
147
|
+
// brob(Exif) box must surface as an `exif` block even if a
|
|
148
|
+
// plain Exif box already won the parsing race. The brob*Chunk
|
|
149
|
+
// variables stay guarded to avoid double-parsing.
|
|
150
|
+
if (Constants.USE_EXIF && originalType === TYPE_EXIF) {
|
|
151
|
+
blockType = 'exif';
|
|
152
|
+
if (tiffHeaderOffset === undefined && !brobExifChunk) {
|
|
153
|
+
brobExifChunk = {
|
|
154
|
+
dataOffset: compressedDataOffset,
|
|
155
|
+
length: compressedDataLength
|
|
156
|
+
};
|
|
157
|
+
}
|
|
118
158
|
}
|
|
119
|
-
if (Constants.USE_XMP && originalType === TYPE_XML
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
159
|
+
if (Constants.USE_XMP && originalType === TYPE_XML) {
|
|
160
|
+
blockType = 'xmp';
|
|
161
|
+
if (!xmpChunks && !brobXmpChunk) {
|
|
162
|
+
brobXmpChunk = {
|
|
163
|
+
dataOffset: compressedDataOffset,
|
|
164
|
+
length: compressedDataLength
|
|
165
|
+
};
|
|
166
|
+
}
|
|
124
167
|
}
|
|
125
168
|
}
|
|
126
169
|
|
|
170
|
+
if (metadataBlocks && blockType) {
|
|
171
|
+
metadataBlocks.push({
|
|
172
|
+
type: blockType,
|
|
173
|
+
start: offset,
|
|
174
|
+
end: offset + length,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
127
178
|
offset += length;
|
|
128
179
|
}
|
|
129
180
|
|
package/src/image-header-png.js
CHANGED
|
@@ -27,31 +27,60 @@ export const TYPE_TIME = 'tIME';
|
|
|
27
27
|
export const TYPE_EXIF = 'eXIf';
|
|
28
28
|
export const TYPE_ICCP = 'iCCP';
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Checks if the provided data view represents a PNG file.
|
|
32
|
+
*
|
|
33
|
+
* @param {DataView} dataView The data view to check.
|
|
34
|
+
* @returns {boolean} True if the data view starts with the PNG signature.
|
|
35
|
+
*/
|
|
30
36
|
function isPngFile(dataView) {
|
|
31
37
|
return !!dataView && getStringFromDataView(dataView, 0, PNG_ID.length) === PNG_ID;
|
|
32
38
|
}
|
|
33
39
|
|
|
34
|
-
|
|
40
|
+
/**
|
|
41
|
+
* Walks the PNG chunk list and reports the offsets ExifReader needs.
|
|
42
|
+
*
|
|
43
|
+
* @param {DataView} dataView The data view to find offsets in.
|
|
44
|
+
* @param {boolean} async Whether async parsing is enabled (gates zTXt/iCCP).
|
|
45
|
+
* @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
|
|
46
|
+
* receives one block per recognized chunk and gets `truncated` set to
|
|
47
|
+
* true if the loop never saw an IEND chunk.
|
|
48
|
+
* @returns {Object} Per-section offsets plus `hasAppMarkers`.
|
|
49
|
+
*/
|
|
50
|
+
function findPngOffsets(dataView, async, metadataBlocks) {
|
|
35
51
|
const PNG_CRC_SIZE = 4;
|
|
52
|
+
const TYPE_IEND = 'IEND';
|
|
36
53
|
|
|
37
54
|
const offsets = {
|
|
38
55
|
hasAppMarkers: false
|
|
39
56
|
};
|
|
40
57
|
|
|
41
58
|
let offset = PNG_ID.length;
|
|
59
|
+
let sawIEnd = false;
|
|
42
60
|
|
|
43
61
|
while (offset + PNG_CHUNK_LENGTH_SIZE + PNG_CHUNK_TYPE_SIZE <= dataView.byteLength) {
|
|
62
|
+
let blockType;
|
|
63
|
+
const chunkDataLength = dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET);
|
|
64
|
+
|
|
65
|
+
// The IEND check is only needed for the truncation signal, so skip
|
|
66
|
+
// the per-chunk string read for default callers.
|
|
67
|
+
if (metadataBlocks
|
|
68
|
+
&& getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_IEND) {
|
|
69
|
+
sawIEnd = true;
|
|
70
|
+
}
|
|
44
71
|
if (Constants.USE_PNG_FILE && isPngImageHeaderChunk(dataView, offset)) {
|
|
45
72
|
offsets.hasAppMarkers = true;
|
|
46
73
|
offsets.pngHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
|
|
74
|
+
blockType = 'file';
|
|
47
75
|
} else if (Constants.USE_XMP && isPngXmpChunk(dataView, offset)) {
|
|
48
76
|
const dataOffset = getPngXmpDataOffset(dataView, offset);
|
|
49
77
|
if (dataOffset !== undefined) {
|
|
50
78
|
offsets.hasAppMarkers = true;
|
|
51
79
|
offsets.xmpChunks = [{
|
|
52
80
|
dataOffset,
|
|
53
|
-
length:
|
|
81
|
+
length: chunkDataLength - (dataOffset - (offset + PNG_CHUNK_DATA_OFFSET))
|
|
54
82
|
}];
|
|
83
|
+
blockType = 'xmp';
|
|
55
84
|
}
|
|
56
85
|
} else if (isPngTextChunk(dataView, offset, async)) {
|
|
57
86
|
offsets.hasAppMarkers = true;
|
|
@@ -60,16 +89,17 @@ function findPngOffsets(dataView, async) {
|
|
|
60
89
|
offsets.pngTextChunks = [];
|
|
61
90
|
}
|
|
62
91
|
offsets.pngTextChunks.push({
|
|
63
|
-
length:
|
|
92
|
+
length: chunkDataLength,
|
|
64
93
|
type: chunkType,
|
|
65
94
|
offset: offset + PNG_CHUNK_DATA_OFFSET
|
|
66
95
|
});
|
|
96
|
+
blockType = 'png';
|
|
67
97
|
} else if (isPngExifChunk(dataView, offset)) {
|
|
68
98
|
offsets.hasAppMarkers = true;
|
|
69
99
|
offsets.tiffHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
|
|
100
|
+
blockType = 'exif';
|
|
70
101
|
} else if (Constants.USE_ICC && async && isPngIccpChunk(dataView, offset)) {
|
|
71
102
|
offsets.hasAppMarkers = true;
|
|
72
|
-
const chunkDataLength = dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET);
|
|
73
103
|
const iccHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
|
|
74
104
|
const {profileName, compressionMethod, compressedProfileOffset} = parseIccHeader(dataView, iccHeaderOffset);
|
|
75
105
|
if (!offsets.iccChunks) {
|
|
@@ -83,18 +113,34 @@ function findPngOffsets(dataView, async) {
|
|
|
83
113
|
profileName,
|
|
84
114
|
compressionMethod
|
|
85
115
|
});
|
|
116
|
+
blockType = 'icc';
|
|
86
117
|
} else if (isPngChunk(dataView, offset)) {
|
|
87
118
|
offsets.hasAppMarkers = true;
|
|
88
119
|
if (!offsets.pngChunkOffsets) {
|
|
89
120
|
offsets.pngChunkOffsets = [];
|
|
90
121
|
}
|
|
91
122
|
offsets.pngChunkOffsets.push(offset + PNG_CHUNK_LENGTH_OFFSET);
|
|
123
|
+
blockType = 'png';
|
|
92
124
|
}
|
|
93
125
|
|
|
94
|
-
|
|
126
|
+
const chunkTotalSize = chunkDataLength
|
|
95
127
|
+ PNG_CHUNK_LENGTH_SIZE
|
|
96
128
|
+ PNG_CHUNK_TYPE_SIZE
|
|
97
129
|
+ PNG_CRC_SIZE;
|
|
130
|
+
|
|
131
|
+
if (metadataBlocks && blockType) {
|
|
132
|
+
metadataBlocks.push({
|
|
133
|
+
type: blockType,
|
|
134
|
+
start: offset,
|
|
135
|
+
end: offset + chunkTotalSize,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
offset += chunkTotalSize;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (metadataBlocks) {
|
|
143
|
+
metadataBlocks.truncated = !sawIEnd;
|
|
98
144
|
}
|
|
99
145
|
|
|
100
146
|
return offsets;
|
package/src/image-header-webp.js
CHANGED
|
@@ -10,6 +10,12 @@ export default {
|
|
|
10
10
|
findOffsets
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Checks if the provided data view represents a WebP file.
|
|
15
|
+
*
|
|
16
|
+
* @param {DataView} dataView The data view to check.
|
|
17
|
+
* @returns {boolean} True for a RIFF container with the WEBP marker.
|
|
18
|
+
*/
|
|
13
19
|
function isWebpFile(dataView) {
|
|
14
20
|
const RIFF_ID_OFFSET = 0;
|
|
15
21
|
const RIFF_ID = 'RIFF';
|
|
@@ -20,7 +26,16 @@ function isWebpFile(dataView) {
|
|
|
20
26
|
&& getStringFromDataView(dataView, WEBP_MARKER_OFFSET, WEBP_MARKER.length) === WEBP_MARKER;
|
|
21
27
|
}
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
/**
|
|
30
|
+
* Finds the offsets of metadata chunks in a WebP file.
|
|
31
|
+
*
|
|
32
|
+
* @param {DataView} dataView The data view to find offsets in.
|
|
33
|
+
* @param {Array<Object>=} metadataBlocks Optional out-array. When provided,
|
|
34
|
+
* receives one block per recognized chunk (EXIF/XMP/ICCP/VP8X) with
|
|
35
|
+
* `start` and `end` covering the full RIFF chunk header and content.
|
|
36
|
+
* @returns {Object} hasAppMarkers and the per-section offsets.
|
|
37
|
+
*/
|
|
38
|
+
function findOffsets(dataView, metadataBlocks) {
|
|
24
39
|
const SUB_CHUNK_START_OFFSET = 12;
|
|
25
40
|
const CHUNK_SIZE_OFFSET = 4;
|
|
26
41
|
const EXIF_IDENTIFIER = 'Exif\x00\x00';
|
|
@@ -36,6 +51,7 @@ function findOffsets(dataView) {
|
|
|
36
51
|
while (offset + CHUNK_HEADER_SIZE < dataView.byteLength) {
|
|
37
52
|
const chunkId = getStringFromDataView(dataView, offset, 4);
|
|
38
53
|
const chunkSize = dataView.getUint32(offset + CHUNK_SIZE_OFFSET, true);
|
|
54
|
+
let blockType;
|
|
39
55
|
|
|
40
56
|
if (Constants.USE_EXIF && (chunkId === 'EXIF')) {
|
|
41
57
|
hasAppMarkers = true;
|
|
@@ -44,12 +60,14 @@ function findOffsets(dataView) {
|
|
|
44
60
|
} else {
|
|
45
61
|
tiffHeaderOffset = offset + CHUNK_HEADER_SIZE;
|
|
46
62
|
}
|
|
63
|
+
blockType = 'exif';
|
|
47
64
|
} else if (Constants.USE_XMP && (chunkId === 'XMP ')) {
|
|
48
65
|
hasAppMarkers = true;
|
|
49
66
|
xmpChunks = [{
|
|
50
67
|
dataOffset: offset + CHUNK_HEADER_SIZE,
|
|
51
68
|
length: chunkSize
|
|
52
69
|
}];
|
|
70
|
+
blockType = 'xmp';
|
|
53
71
|
} else if (Constants.USE_ICC && (chunkId === 'ICCP')) {
|
|
54
72
|
hasAppMarkers = true;
|
|
55
73
|
iccChunks = [{
|
|
@@ -58,12 +76,24 @@ function findOffsets(dataView) {
|
|
|
58
76
|
chunkNumber: 1,
|
|
59
77
|
chunksTotal: 1
|
|
60
78
|
}];
|
|
79
|
+
blockType = 'icc';
|
|
61
80
|
} else if (chunkId === 'VP8X') {
|
|
62
81
|
hasAppMarkers = true;
|
|
63
82
|
vp8xChunkOffset = offset + CHUNK_HEADER_SIZE;
|
|
83
|
+
blockType = 'riff';
|
|
64
84
|
}
|
|
65
85
|
|
|
66
|
-
|
|
86
|
+
const chunkTotalSize = CHUNK_HEADER_SIZE + (chunkSize % 2 === 0 ? chunkSize : chunkSize + 1);
|
|
87
|
+
|
|
88
|
+
if (metadataBlocks && blockType) {
|
|
89
|
+
metadataBlocks.push({
|
|
90
|
+
type: blockType,
|
|
91
|
+
start: offset,
|
|
92
|
+
end: offset + chunkTotalSize,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
offset += chunkTotalSize;
|
|
67
97
|
}
|
|
68
98
|
|
|
69
99
|
return {
|
package/src/image-header.js
CHANGED
|
@@ -18,46 +18,66 @@ export default {
|
|
|
18
18
|
parseAppMarkers
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Detects the image format and delegates to the matching finder.
|
|
23
|
+
*
|
|
24
|
+
* @param {DataView} dataView The data view to inspect.
|
|
25
|
+
* @param {boolean=} async Whether async parsing is enabled.
|
|
26
|
+
* @param {boolean=} includeMetadataBlocks When true, the finder collects a
|
|
27
|
+
* block list describing where metadata sits in the file. The result
|
|
28
|
+
* gains `metadataBlocks` and `metadataTruncated` fields.
|
|
29
|
+
* @returns {Object} Per-format offsets plus `fileType`.
|
|
30
|
+
* @throws {Error} When the data view is not a recognized image format.
|
|
31
|
+
*/
|
|
32
|
+
function parseAppMarkers(dataView, async, includeMetadataBlocks) {
|
|
33
|
+
const metadataBlocks = includeMetadataBlocks ? [] : undefined;
|
|
34
|
+
|
|
22
35
|
if (Constants.USE_TIFF && Tiff.isTiffFile(dataView)) {
|
|
23
|
-
return
|
|
36
|
+
return finalize(Tiff.findTiffOffsets(), 'tiff', 'TIFF', metadataBlocks);
|
|
24
37
|
}
|
|
25
38
|
|
|
26
39
|
if (Constants.USE_JPEG && Jpeg.isJpegFile(dataView)) {
|
|
27
|
-
return
|
|
40
|
+
return finalize(Jpeg.findJpegOffsets(dataView, metadataBlocks), 'jpeg', 'JPEG', metadataBlocks);
|
|
28
41
|
}
|
|
29
42
|
|
|
30
43
|
if (Constants.USE_PNG && Png.isPngFile(dataView)) {
|
|
31
|
-
return
|
|
44
|
+
return finalize(Png.findPngOffsets(dataView, async, metadataBlocks), 'png', 'PNG', metadataBlocks);
|
|
32
45
|
}
|
|
33
46
|
|
|
34
47
|
if (Constants.USE_HEIC && Heic.isHeicFile(dataView)) {
|
|
35
|
-
return
|
|
48
|
+
return finalize(Heic.findHeicOffsets(dataView, metadataBlocks), 'heic', 'HEIC', metadataBlocks);
|
|
36
49
|
}
|
|
37
50
|
|
|
38
51
|
if (Constants.USE_AVIF && Avif.isAvifFile(dataView)) {
|
|
39
|
-
return
|
|
52
|
+
return finalize(Avif.findAvifOffsets(dataView, metadataBlocks), 'avif', 'AVIF', metadataBlocks);
|
|
40
53
|
}
|
|
41
54
|
|
|
42
55
|
if (Constants.USE_JXL && Jxl.isJxlFile(dataView)) {
|
|
43
|
-
return
|
|
56
|
+
return finalize(Jxl.findJxlOffsets(dataView, metadataBlocks), 'jxl', 'JPEG XL', metadataBlocks);
|
|
44
57
|
}
|
|
45
58
|
|
|
46
59
|
if (Constants.USE_WEBP && Webp.isWebpFile(dataView)) {
|
|
47
|
-
return
|
|
60
|
+
return finalize(Webp.findOffsets(dataView, metadataBlocks), 'webp', 'WebP', metadataBlocks);
|
|
48
61
|
}
|
|
49
62
|
|
|
50
63
|
if (Constants.USE_GIF && Gif.isGifFile(dataView)) {
|
|
51
|
-
return
|
|
64
|
+
return finalize(Gif.findOffsets(dataView, metadataBlocks), 'gif', 'GIF', metadataBlocks);
|
|
52
65
|
}
|
|
53
66
|
|
|
54
67
|
if (Constants.USE_XMP && Xml.isXMLFile(dataView)) {
|
|
55
|
-
return
|
|
68
|
+
return finalize(Xml.findOffsets(dataView, metadataBlocks), 'xml', 'XML', metadataBlocks);
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
throw new Error('Invalid image format');
|
|
59
72
|
}
|
|
60
73
|
|
|
61
|
-
function
|
|
62
|
-
|
|
74
|
+
function finalize(offsets, fileType, fileTypeDescription, metadataBlocks) {
|
|
75
|
+
const result = objectAssign({}, offsets, {fileType: {value: fileType, description: fileTypeDescription}});
|
|
76
|
+
if (metadataBlocks !== undefined) {
|
|
77
|
+
result.metadataBlocks = metadataBlocks;
|
|
78
|
+
// Surface the finder's .truncated array convention as a sibling
|
|
79
|
+
// field so consumers don't depend on properties on the array.
|
|
80
|
+
result.metadataTruncated = !!metadataBlocks.truncated;
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
63
83
|
}
|