exifreader 4.19.0 → 4.20.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 +28 -5
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/exif-reader.d.ts +41 -9
- package/package.json +2 -2
- package/src/exif-reader.js +50 -13
- package/src/icc-tags.js +26 -3
- package/src/image-header-jpeg.js +1 -1
- package/src/image-header-png.js +45 -9
- package/src/image-header.js +2 -2
- package/src/png-text-tags.js +131 -30
- package/src/tag-decoder.js +1 -1
- package/src/utils.js +34 -0
package/exif-reader.d.ts
CHANGED
|
@@ -97,7 +97,34 @@ interface RiffTags {
|
|
|
97
97
|
'ImageHeight'?: {
|
|
98
98
|
value: number,
|
|
99
99
|
description: string
|
|
100
|
-
}
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface GifTags {
|
|
104
|
+
'GIF Version': {
|
|
105
|
+
value: '87a' | '89a',
|
|
106
|
+
description: '87a' | '89a'
|
|
107
|
+
},
|
|
108
|
+
'Image Width'?: {
|
|
109
|
+
value: number,
|
|
110
|
+
description: string
|
|
111
|
+
},
|
|
112
|
+
'Image Height'?: {
|
|
113
|
+
value: number,
|
|
114
|
+
description: string
|
|
115
|
+
},
|
|
116
|
+
'Global Color Map'?: {
|
|
117
|
+
value: 0 | 1,
|
|
118
|
+
description: 'No' | 'Yes'
|
|
119
|
+
},
|
|
120
|
+
'Bits Per Pixel'?: {
|
|
121
|
+
value: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8,
|
|
122
|
+
description: string
|
|
123
|
+
},
|
|
124
|
+
'Color Resolution Depth'?: {
|
|
125
|
+
value: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8,
|
|
126
|
+
description: string
|
|
127
|
+
},
|
|
101
128
|
}
|
|
102
129
|
|
|
103
130
|
interface NumberFileTag {
|
|
@@ -164,7 +191,7 @@ interface ThumbnailTags {
|
|
|
164
191
|
StripByteCounts?: NumberArrayTag
|
|
165
192
|
}
|
|
166
193
|
|
|
167
|
-
interface ExpandedTags {
|
|
194
|
+
export interface ExpandedTags {
|
|
168
195
|
file?: FileTags,
|
|
169
196
|
jfif?: JfifTags,
|
|
170
197
|
pngFile?: PngFileTags,
|
|
@@ -175,6 +202,7 @@ interface ExpandedTags {
|
|
|
175
202
|
xmp?: { _raw: string } & XmpTags,
|
|
176
203
|
icc?: IccTags,
|
|
177
204
|
riff?: RiffTags,
|
|
205
|
+
gif?: GifTags,
|
|
178
206
|
Thumbnail?: ThumbnailTags,
|
|
179
207
|
gps?: GpsTags
|
|
180
208
|
photoshop?: PhotoshopTags;
|
|
@@ -228,14 +256,18 @@ interface PhotoshopTags {
|
|
|
228
256
|
}
|
|
229
257
|
|
|
230
258
|
export function load(data: ArrayBuffer | SharedArrayBuffer | Buffer): Tags;
|
|
231
|
-
export function load(data: ArrayBuffer | SharedArrayBuffer | Buffer, options: {expanded: true, includeUnknown?: boolean, length?: number}): ExpandedTags;
|
|
232
|
-
export function load(data: ArrayBuffer | SharedArrayBuffer | Buffer, options: {expanded?: false, includeUnknown?: boolean, length?: number}): Tags;
|
|
259
|
+
export function load(data: ArrayBuffer | SharedArrayBuffer | Buffer, options: {expanded: true, includeUnknown?: boolean, length?: number, async?: false}): ExpandedTags;
|
|
260
|
+
export function load(data: ArrayBuffer | SharedArrayBuffer | Buffer, options: {expanded?: false, includeUnknown?: boolean, length?: number, async?: false}): Tags;
|
|
261
|
+
export function load(data: ArrayBuffer | SharedArrayBuffer | Buffer, options: {expanded: true, includeUnknown?: boolean, length?: number, async: true}): Promise<ExpandedTags>;
|
|
262
|
+
export function load(data: ArrayBuffer | SharedArrayBuffer | Buffer, options: {expanded?: false, includeUnknown?: boolean, length?: number, async: true}): Promise<Tags>;
|
|
233
263
|
export function load(data: string | File): Promise<Tags>;
|
|
234
|
-
export function load(data: string | File, options: {expanded: true, includeUnknown?: boolean, length?: number}): Promise<ExpandedTags>;
|
|
235
|
-
export function load(data: string | File, options: {expanded?: false, includeUnknown?: boolean, length?: number}): Promise<Tags>;
|
|
264
|
+
export function load(data: string | File, options: {expanded: true, includeUnknown?: boolean, length?: number, async?: boolean}): Promise<ExpandedTags>;
|
|
265
|
+
export function load(data: string | File, options: {expanded?: false, includeUnknown?: boolean, length?: number, async?: boolean}): Promise<Tags>;
|
|
236
266
|
export function loadView(data: DataView): Tags;
|
|
237
|
-
export function loadView(data: DataView, options: {expanded: true, includeUnknown?: boolean}): ExpandedTags;
|
|
238
|
-
export function loadView(data: DataView, options: {expanded?: false, includeUnknown?: boolean}): Tags;
|
|
267
|
+
export function loadView(data: DataView, options: {expanded: true, includeUnknown?: boolean, async?: false}): ExpandedTags;
|
|
268
|
+
export function loadView(data: DataView, options: {expanded?: false, includeUnknown?: boolean, async?: false}): Tags;
|
|
269
|
+
export function loadView(data: DataView, options: {expanded: true, includeUnknown?: boolean, async: true}): Promise<ExpandedTags>;
|
|
270
|
+
export function loadView(data: DataView, options: {expanded?: false, includeUnknown?: boolean, async: true}): Promise<Tags>;
|
|
239
271
|
|
|
240
272
|
export namespace errors {
|
|
241
273
|
export class MetadataMissingError extends Error {}
|
|
@@ -484,7 +516,7 @@ interface IccTags {
|
|
|
484
516
|
[name: string]: ValueTag;
|
|
485
517
|
}
|
|
486
518
|
|
|
487
|
-
export type Tags = XmpTags & IccTags & PngTags & RiffTags & PhotoshopTags & {
|
|
519
|
+
export type Tags = XmpTags & IccTags & PngTags & RiffTags & GifTags & PhotoshopTags & {
|
|
488
520
|
'Thumbnail'?: ThumbnailTags;
|
|
489
521
|
'Images'?: MPFImageTags[],
|
|
490
522
|
} & {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "exifreader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.20.0",
|
|
4
4
|
"description": "Library that parses Exif metadata in images.",
|
|
5
5
|
"author": "Mattias Wallander <mattias@wallander.eu>",
|
|
6
6
|
"repository": {
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"nyc": {
|
|
70
70
|
"check-coverage": true,
|
|
71
71
|
"statements": 95,
|
|
72
|
-
"branches":
|
|
72
|
+
"branches": 89,
|
|
73
73
|
"functions": 98,
|
|
74
74
|
"lines": 95,
|
|
75
75
|
"reporter": [
|
package/src/exif-reader.js
CHANGED
|
@@ -38,11 +38,13 @@ export default {
|
|
|
38
38
|
|
|
39
39
|
export const errors = exifErrors;
|
|
40
40
|
|
|
41
|
-
export function load(data, options) {
|
|
41
|
+
export function load(data, options = {}) {
|
|
42
42
|
if (isFilePathOrURL(data)) {
|
|
43
|
+
options.async = true;
|
|
43
44
|
return loadFile(data, options).then((fileContents) => loadFromData(fileContents, options));
|
|
44
45
|
}
|
|
45
46
|
if (isBrowserFileObject(data)) {
|
|
47
|
+
options.async = true;
|
|
46
48
|
return loadFileObject(data).then((fileContents) => loadFromData(fileContents, options));
|
|
47
49
|
}
|
|
48
50
|
return loadFromData(data, options);
|
|
@@ -195,9 +197,13 @@ function getDataView(data) {
|
|
|
195
197
|
}
|
|
196
198
|
}
|
|
197
199
|
|
|
198
|
-
export function loadView(
|
|
200
|
+
export function loadView(
|
|
201
|
+
dataView,
|
|
202
|
+
{expanded = false, async = false, includeUnknown = false} = {expanded: false, async: false, includeUnknown: false}
|
|
203
|
+
) {
|
|
199
204
|
let foundMetaData = false;
|
|
200
205
|
let tags = {};
|
|
206
|
+
const tagsPromises = [];
|
|
201
207
|
|
|
202
208
|
const {
|
|
203
209
|
fileType,
|
|
@@ -213,7 +219,7 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
|
|
|
213
219
|
pngChunkOffsets,
|
|
214
220
|
vp8xChunkOffset,
|
|
215
221
|
gifHeaderOffset
|
|
216
|
-
} = ImageHeader.parseAppMarkers(dataView);
|
|
222
|
+
} = ImageHeader.parseAppMarkers(dataView, async);
|
|
217
223
|
|
|
218
224
|
if (Constants.USE_JPEG && Constants.USE_FILE && hasFileData(fileDataOffset)) {
|
|
219
225
|
foundMetaData = true;
|
|
@@ -319,11 +325,11 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
|
|
|
319
325
|
|
|
320
326
|
if ((Constants.USE_JPEG || Constants.USE_WEBP) && Constants.USE_ICC && hasIccData(iccChunks)) {
|
|
321
327
|
foundMetaData = true;
|
|
322
|
-
const readTags = IccTags.read(dataView, iccChunks);
|
|
323
|
-
if (
|
|
324
|
-
|
|
328
|
+
const readTags = IccTags.read(dataView, iccChunks, async);
|
|
329
|
+
if (readTags instanceof Promise) {
|
|
330
|
+
tagsPromises.push(readTags.then(addIccTags));
|
|
325
331
|
} else {
|
|
326
|
-
|
|
332
|
+
addIccTags(readTags);
|
|
327
333
|
}
|
|
328
334
|
}
|
|
329
335
|
|
|
@@ -350,12 +356,10 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
|
|
|
350
356
|
|
|
351
357
|
if (Constants.USE_PNG && hasPngTextData(pngTextChunks)) {
|
|
352
358
|
foundMetaData = true;
|
|
353
|
-
const readTags = PngTextTags.read(dataView, pngTextChunks);
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
} else {
|
|
358
|
-
tags = objectAssign({}, tags, readTags);
|
|
359
|
+
const {readTags, readTagsPromise} = PngTextTags.read(dataView, pngTextChunks, async, includeUnknown);
|
|
360
|
+
addPngTextTags(readTags);
|
|
361
|
+
if (readTagsPromise) {
|
|
362
|
+
tagsPromises.push(readTagsPromise.then((tagList) => tagList.forEach(addPngTextTags)));
|
|
359
363
|
}
|
|
360
364
|
}
|
|
361
365
|
|
|
@@ -415,7 +419,40 @@ export function loadView(dataView, {expanded = false, includeUnknown = false} =
|
|
|
415
419
|
throw new exifErrors.MetadataMissingError();
|
|
416
420
|
}
|
|
417
421
|
|
|
422
|
+
if (async) {
|
|
423
|
+
return Promise.all(tagsPromises).then(() => tags);
|
|
424
|
+
}
|
|
418
425
|
return tags;
|
|
426
|
+
|
|
427
|
+
function addIccTags(readTags) {
|
|
428
|
+
if (expanded) {
|
|
429
|
+
tags.icc = readTags;
|
|
430
|
+
} else {
|
|
431
|
+
tags = objectAssign({}, tags, readTags);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function addPngTextTags(readTags) {
|
|
436
|
+
if (expanded) {
|
|
437
|
+
for (const group of ['exif', 'iptc']) {
|
|
438
|
+
const groupKey = `__${group}`;
|
|
439
|
+
if (readTags[groupKey]) {
|
|
440
|
+
tags[group] = !tags[group] ? readTags[groupKey] : objectAssign({}, tags.exif, readTags[groupKey]);
|
|
441
|
+
delete readTags[groupKey];
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
tags.png = !tags.png ? readTags : objectAssign({}, tags.png, readTags);
|
|
445
|
+
tags.pngText = !tags.pngText ? readTags : objectAssign({}, tags.png, readTags);
|
|
446
|
+
} else {
|
|
447
|
+
tags = objectAssign(
|
|
448
|
+
{},
|
|
449
|
+
tags,
|
|
450
|
+
readTags.__exif ? readTags.__exif : {},
|
|
451
|
+
readTags.__iptc ? readTags.__iptc : {},
|
|
452
|
+
readTags
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
419
456
|
}
|
|
420
457
|
|
|
421
458
|
function hasFileData(fileDataOffset) {
|
package/src/icc-tags.js
CHANGED
|
@@ -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
|
import {iccTags, iccProfile} from './icc-tag-names.js';
|
|
5
|
-
import {getStringFromDataView, getUnicodeStringFromDataView} from './utils.js';
|
|
5
|
+
import {getStringFromDataView, getUnicodeStringFromDataView, decompress, COMPRESSION_METHOD_NONE, COMPRESSION_METHOD_DEFLATE} from './utils.js';
|
|
6
6
|
|
|
7
7
|
export default {
|
|
8
8
|
read
|
|
@@ -20,8 +20,31 @@ const TAG_TABLE_SINGLE_TAG_DATA = 12;
|
|
|
20
20
|
// ICC profile data can be longer than application segment max length of ~64k.
|
|
21
21
|
// so it can be split into multiple APP2 segments. Each segment includes
|
|
22
22
|
// total chunk count and chunk number.
|
|
23
|
-
// Here we read all chunks into single
|
|
24
|
-
|
|
23
|
+
// Here we read all chunks into single continuous array of bytes.
|
|
24
|
+
// Compressed ICC profile data only has support for a single chunk.
|
|
25
|
+
function read(dataView, iccData, async) {
|
|
26
|
+
if (async && iccData[0].compressionMethod !== COMPRESSION_METHOD_NONE) {
|
|
27
|
+
return readCompressedIcc(dataView, iccData);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return readIcc(dataView, iccData);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function readCompressedIcc(dataView, iccData) {
|
|
34
|
+
if (!compressionMethodIsSupported(iccData[0].compressionMethod)) {
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
const compressedDataView = new DataView(dataView.buffer.slice(iccData[0].offset, iccData[0].offset + iccData[0].length));
|
|
38
|
+
return decompress(compressedDataView, iccData[0].compressionMethod, 'dataview')
|
|
39
|
+
.then(parseTags)
|
|
40
|
+
.catch(() => ({}));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function compressionMethodIsSupported(compressionMethod) {
|
|
44
|
+
return compressionMethod === COMPRESSION_METHOD_DEFLATE;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readIcc(dataView, iccData) {
|
|
25
48
|
try {
|
|
26
49
|
const totalIccProfileLength = iccData.reduce((sum, icc) => sum + icc.length, 0);
|
|
27
50
|
|
package/src/image-header-jpeg.js
CHANGED
|
@@ -155,7 +155,7 @@ function isApp2MPFMarker(dataView, appMarkerPosition) {
|
|
|
155
155
|
}
|
|
156
156
|
|
|
157
157
|
function isApp0JfifMarker(dataView, appMarkerPosition) {
|
|
158
|
-
const markerIdLength =
|
|
158
|
+
const markerIdLength = APP0_JFIF_IDENTIFIER.length;
|
|
159
159
|
|
|
160
160
|
return (dataView.getUint16(appMarkerPosition) === APP0_MARKER)
|
|
161
161
|
&& (getStringFromDataView(dataView, appMarkerPosition + APP_ID_OFFSET, markerIdLength) === APP0_JFIF_IDENTIFIER)
|
package/src/image-header-png.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
// Specification: http://www.libpng.org/pub/png/spec/1.2/
|
|
6
6
|
|
|
7
|
-
import {getStringFromDataView} from './utils.js';
|
|
7
|
+
import {getStringFromDataView, getNullTerminatedStringFromDataView} from './utils.js';
|
|
8
8
|
import Constants from './constants.js';
|
|
9
9
|
|
|
10
10
|
export default {
|
|
@@ -21,15 +21,17 @@ export const PNG_CHUNK_DATA_OFFSET = PNG_CHUNK_LENGTH_SIZE + PNG_CHUNK_TYPE_SIZE
|
|
|
21
21
|
const PNG_XMP_PREFIX = 'XML:com.adobe.xmp\x00';
|
|
22
22
|
export const TYPE_TEXT = 'tEXt';
|
|
23
23
|
export const TYPE_ITXT = 'iTXt';
|
|
24
|
+
export const TYPE_ZTXT = 'zTXt';
|
|
24
25
|
export const TYPE_PHYS = 'pHYs';
|
|
25
26
|
export const TYPE_TIME = 'tIME';
|
|
26
27
|
export const TYPE_EXIF = 'eXIf';
|
|
28
|
+
export const TYPE_ICCP = 'iCCP';
|
|
27
29
|
|
|
28
30
|
function isPngFile(dataView) {
|
|
29
31
|
return !!dataView && getStringFromDataView(dataView, 0, PNG_ID.length) === PNG_ID;
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
function findPngOffsets(dataView) {
|
|
34
|
+
function findPngOffsets(dataView, async) {
|
|
33
35
|
const PNG_CRC_SIZE = 4;
|
|
34
36
|
|
|
35
37
|
const offsets = {
|
|
@@ -51,7 +53,7 @@ function findPngOffsets(dataView) {
|
|
|
51
53
|
length: dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET) - (dataOffset - (offset + PNG_CHUNK_DATA_OFFSET))
|
|
52
54
|
}];
|
|
53
55
|
}
|
|
54
|
-
} else if (isPngTextChunk(dataView, offset)) {
|
|
56
|
+
} else if (isPngTextChunk(dataView, offset, async)) {
|
|
55
57
|
offsets.hasAppMarkers = true;
|
|
56
58
|
const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
|
|
57
59
|
if (!offsets.pngTextChunks) {
|
|
@@ -65,6 +67,22 @@ function findPngOffsets(dataView) {
|
|
|
65
67
|
} else if (isPngExifChunk(dataView, offset)) {
|
|
66
68
|
offsets.hasAppMarkers = true;
|
|
67
69
|
offsets.tiffHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
|
|
70
|
+
} else if (Constants.USE_ICC && async && isPngIccpChunk(dataView, offset)) {
|
|
71
|
+
offsets.hasAppMarkers = true;
|
|
72
|
+
const chunkDataLength = dataView.getUint32(offset + PNG_CHUNK_LENGTH_OFFSET);
|
|
73
|
+
const iccHeaderOffset = offset + PNG_CHUNK_DATA_OFFSET;
|
|
74
|
+
const {profileName, compressionMethod, compressedProfileOffset} = parseIccHeader(dataView, iccHeaderOffset);
|
|
75
|
+
if (!offsets.iccChunks) {
|
|
76
|
+
offsets.iccChunks = [];
|
|
77
|
+
}
|
|
78
|
+
offsets.iccChunks.push({
|
|
79
|
+
offset: compressedProfileOffset,
|
|
80
|
+
length: chunkDataLength - (compressedProfileOffset - iccHeaderOffset),
|
|
81
|
+
chunkNumber: 1,
|
|
82
|
+
chunksTotal: 1,
|
|
83
|
+
profileName,
|
|
84
|
+
compressionMethod
|
|
85
|
+
});
|
|
68
86
|
} else if (isPngChunk(dataView, offset)) {
|
|
69
87
|
offsets.hasAppMarkers = true;
|
|
70
88
|
if (!offsets.pngChunkOffsets) {
|
|
@@ -88,22 +106,23 @@ function isPngImageHeaderChunk(dataView, offset) {
|
|
|
88
106
|
}
|
|
89
107
|
|
|
90
108
|
function isPngXmpChunk(dataView, offset) {
|
|
91
|
-
|
|
92
|
-
return (getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === PNG_CHUNK_TYPE_INTERNATIONAL_TEXT)
|
|
109
|
+
return (getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_ITXT)
|
|
93
110
|
&& (getStringFromDataView(dataView, offset + PNG_CHUNK_DATA_OFFSET, PNG_XMP_PREFIX.length) === PNG_XMP_PREFIX);
|
|
94
111
|
}
|
|
95
112
|
|
|
96
|
-
function isPngTextChunk(dataView, offset) {
|
|
97
|
-
const PNG_CHUNK_TYPE_TEXT = 'tEXt';
|
|
98
|
-
const PNG_CHUNK_TYPE_ITXT = 'iTXt';
|
|
113
|
+
function isPngTextChunk(dataView, offset, async) {
|
|
99
114
|
const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
|
|
100
|
-
return chunkType ===
|
|
115
|
+
return chunkType === TYPE_TEXT || chunkType === TYPE_ITXT || (chunkType === TYPE_ZTXT && async);
|
|
101
116
|
}
|
|
102
117
|
|
|
103
118
|
function isPngExifChunk(dataView, offset) {
|
|
104
119
|
return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_EXIF;
|
|
105
120
|
}
|
|
106
121
|
|
|
122
|
+
function isPngIccpChunk(dataView, offset) {
|
|
123
|
+
return getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE) === TYPE_ICCP;
|
|
124
|
+
}
|
|
125
|
+
|
|
107
126
|
function isPngChunk(dataView, offset) {
|
|
108
127
|
const SUPPORTED_PNG_CHUNK_TYPES = [TYPE_PHYS, TYPE_TIME];
|
|
109
128
|
const chunkType = getStringFromDataView(dataView, offset + PNG_CHUNK_TYPE_OFFSET, PNG_CHUNK_TYPE_SIZE);
|
|
@@ -128,3 +147,20 @@ function getPngXmpDataOffset(dataView, offset) {
|
|
|
128
147
|
}
|
|
129
148
|
return offset;
|
|
130
149
|
}
|
|
150
|
+
|
|
151
|
+
function parseIccHeader(dataView, offset) {
|
|
152
|
+
const NULL_SEPARATOR_SIZE = 1;
|
|
153
|
+
const COMPRESSION_METHOD_SIZE = 1;
|
|
154
|
+
|
|
155
|
+
const profileName = getNullTerminatedStringFromDataView(dataView, offset);
|
|
156
|
+
offset += profileName.length + NULL_SEPARATOR_SIZE;
|
|
157
|
+
|
|
158
|
+
const compressionMethod = dataView.getUint8(offset);
|
|
159
|
+
offset += COMPRESSION_METHOD_SIZE;
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
profileName,
|
|
163
|
+
compressionMethod,
|
|
164
|
+
compressedProfileOffset: offset
|
|
165
|
+
};
|
|
166
|
+
}
|
package/src/image-header.js
CHANGED
|
@@ -15,7 +15,7 @@ export default {
|
|
|
15
15
|
parseAppMarkers
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
function parseAppMarkers(dataView) {
|
|
18
|
+
function parseAppMarkers(dataView, async) {
|
|
19
19
|
if (Constants.USE_TIFF && Tiff.isTiffFile(dataView)) {
|
|
20
20
|
return addFileType(Tiff.findTiffOffsets(), 'tiff', 'TIFF');
|
|
21
21
|
}
|
|
@@ -25,7 +25,7 @@ function parseAppMarkers(dataView) {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
if (Constants.USE_PNG && Png.isPngFile(dataView)) {
|
|
28
|
-
return addFileType(Png.findPngOffsets(dataView), 'png', 'PNG');
|
|
28
|
+
return addFileType(Png.findPngOffsets(dataView, async), 'png', 'PNG');
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
if (Constants.USE_HEIC && Heic.isHeicFile(dataView)) {
|
package/src/png-text-tags.js
CHANGED
|
@@ -2,9 +2,14 @@
|
|
|
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
|
-
|
|
5
|
+
// Specification: http://www.libpng.org/pub/png/spec/1.2/
|
|
6
|
+
|
|
7
|
+
import {getStringValueFromArray, getStringFromDataView, decompress, COMPRESSION_METHOD_NONE} from './utils.js';
|
|
6
8
|
import TagDecoder from './tag-decoder.js';
|
|
7
|
-
import {TYPE_TEXT, TYPE_ITXT} from './image-header-png.js';
|
|
9
|
+
import {TYPE_TEXT, TYPE_ITXT, TYPE_ZTXT} from './image-header-png.js';
|
|
10
|
+
import Tags from './tags.js';
|
|
11
|
+
import IptcTags from './iptc-tags.js';
|
|
12
|
+
import Constants from './constants.js';
|
|
8
13
|
|
|
9
14
|
export default {
|
|
10
15
|
read
|
|
@@ -15,36 +20,76 @@ const STATE_COMPRESSION = 'STATE_COMPRESSION';
|
|
|
15
20
|
const STATE_LANG = 'STATE_LANG';
|
|
16
21
|
const STATE_TRANSLATED_KEYWORD = 'STATE_TRANSLATED_KEYWORD';
|
|
17
22
|
const STATE_TEXT = 'STATE_TEXT';
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
const COMPRESSION_SECTION_ITXT_EXTRA_BYTE = 1;
|
|
24
|
+
const COMPRESSION_FLAG_COMPRESSED = 1;
|
|
25
|
+
const EXIF_OFFSET = 6;
|
|
26
|
+
|
|
27
|
+
function read(dataView, pngTextChunks, async, includeUnknown) {
|
|
28
|
+
const tags = {};
|
|
29
|
+
const tagsPromises = [];
|
|
30
|
+
for (let i = 0; i < pngTextChunks.length; i++) {
|
|
31
|
+
const {offset, length, type} = pngTextChunks[i];
|
|
32
|
+
const nameAndValue = getNameAndValue(dataView, offset, length, type, async);
|
|
33
|
+
if (nameAndValue instanceof Promise) {
|
|
34
|
+
tagsPromises.push(nameAndValue.then(({name, value, description}) => {
|
|
35
|
+
try {
|
|
36
|
+
if (Constants.USE_EXIF && isExifGroupTag(name, value)) {
|
|
37
|
+
return {
|
|
38
|
+
__exif: Tags.read(decodeRawData(value), EXIF_OFFSET, includeUnknown)
|
|
39
|
+
};
|
|
40
|
+
} else if (Constants.USE_IPTC && isIptcGroupTag(name, value)) {
|
|
41
|
+
return {
|
|
42
|
+
__iptc: IptcTags.read(decodeRawData(value), 0, includeUnknown)
|
|
43
|
+
};
|
|
44
|
+
} else if (name) {
|
|
45
|
+
return {
|
|
46
|
+
[name]: {
|
|
47
|
+
value,
|
|
48
|
+
description
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
} catch (error) {
|
|
53
|
+
// Ignore the broken tag.
|
|
54
|
+
}
|
|
55
|
+
return {};
|
|
56
|
+
}));
|
|
57
|
+
} else {
|
|
58
|
+
const {name, value, description} = nameAndValue;
|
|
59
|
+
if (name) {
|
|
60
|
+
tags[name] = {
|
|
61
|
+
value,
|
|
62
|
+
description
|
|
63
|
+
};
|
|
64
|
+
}
|
|
28
65
|
}
|
|
29
|
-
|
|
30
|
-
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
readTags: tags,
|
|
70
|
+
readTagsPromise: tagsPromises.length > 0 ? Promise.all(tagsPromises) : undefined
|
|
71
|
+
};
|
|
31
72
|
}
|
|
32
73
|
|
|
33
|
-
function getNameAndValue(dataView, offset, length, type) {
|
|
74
|
+
function getNameAndValue(dataView, offset, length, type, async) {
|
|
34
75
|
const keywordChars = [];
|
|
35
76
|
const langChars = [];
|
|
36
77
|
const translatedKeywordChars = [];
|
|
37
|
-
|
|
78
|
+
let valueChars;
|
|
38
79
|
let parsingState = STATE_KEYWORD;
|
|
80
|
+
let compressionMethod = COMPRESSION_METHOD_NONE;
|
|
39
81
|
|
|
40
82
|
for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
|
|
41
83
|
if (parsingState === STATE_COMPRESSION) {
|
|
42
|
-
|
|
43
|
-
if (
|
|
44
|
-
|
|
84
|
+
compressionMethod = getCompressionMethod({type, dataView, offset: offset + i});
|
|
85
|
+
if (type === TYPE_ITXT) {
|
|
86
|
+
i += COMPRESSION_SECTION_ITXT_EXTRA_BYTE;
|
|
45
87
|
}
|
|
46
|
-
i += COMPRESSION_SECTION_LENGTH;
|
|
47
88
|
parsingState = moveToNextState(type, parsingState);
|
|
89
|
+
continue;
|
|
90
|
+
} else if (parsingState === STATE_TEXT) {
|
|
91
|
+
valueChars = new DataView(dataView.buffer.slice(offset + i, offset + length));
|
|
92
|
+
break;
|
|
48
93
|
}
|
|
49
94
|
const byte = dataView.getUint8(offset + i);
|
|
50
95
|
if (byte === 0) {
|
|
@@ -55,26 +100,41 @@ function getNameAndValue(dataView, offset, length, type) {
|
|
|
55
100
|
langChars.push(byte);
|
|
56
101
|
} else if (parsingState === STATE_TRANSLATED_KEYWORD) {
|
|
57
102
|
translatedKeywordChars.push(byte);
|
|
58
|
-
} else {
|
|
59
|
-
valueChars.push(byte);
|
|
60
103
|
}
|
|
61
104
|
}
|
|
62
105
|
|
|
63
|
-
|
|
106
|
+
if (compressionMethod !== COMPRESSION_METHOD_NONE && !async) {
|
|
107
|
+
return {};
|
|
108
|
+
}
|
|
109
|
+
const decompressedValueChars = decompress(valueChars, compressionMethod);
|
|
110
|
+
if (decompressedValueChars instanceof Promise) {
|
|
111
|
+
return decompressedValueChars
|
|
112
|
+
.then((_decompressedValueChars) => constructTag(_decompressedValueChars, type, langChars, keywordChars))
|
|
113
|
+
.catch(() => constructTag('<text using unknown compression>'.split(''), type, langChars, keywordChars));
|
|
114
|
+
}
|
|
115
|
+
return constructTag(decompressedValueChars, type, langChars, keywordChars);
|
|
116
|
+
}
|
|
64
117
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
118
|
+
function getCompressionMethod({type, dataView, offset}) {
|
|
119
|
+
if (type === TYPE_ITXT) {
|
|
120
|
+
if (dataView.getUint8(offset) === COMPRESSION_FLAG_COMPRESSED) {
|
|
121
|
+
return dataView.getUint8(offset + 1);
|
|
122
|
+
}
|
|
123
|
+
} else if (type === TYPE_ZTXT) {
|
|
124
|
+
return dataView.getUint8(offset);
|
|
125
|
+
}
|
|
126
|
+
return COMPRESSION_METHOD_NONE;
|
|
70
127
|
}
|
|
71
128
|
|
|
72
129
|
function moveToNextState(type, parsingState) {
|
|
73
|
-
if (parsingState === STATE_KEYWORD && type
|
|
130
|
+
if (parsingState === STATE_KEYWORD && [TYPE_ITXT, TYPE_ZTXT].includes(type)) {
|
|
74
131
|
return STATE_COMPRESSION;
|
|
75
132
|
}
|
|
76
133
|
if (parsingState === STATE_COMPRESSION) {
|
|
77
|
-
|
|
134
|
+
if (type === TYPE_ITXT) {
|
|
135
|
+
return STATE_LANG;
|
|
136
|
+
}
|
|
137
|
+
return STATE_TEXT;
|
|
78
138
|
}
|
|
79
139
|
if (parsingState === STATE_LANG) {
|
|
80
140
|
return STATE_TRANSLATED_KEYWORD;
|
|
@@ -82,6 +142,15 @@ function moveToNextState(type, parsingState) {
|
|
|
82
142
|
return STATE_TEXT;
|
|
83
143
|
}
|
|
84
144
|
|
|
145
|
+
function constructTag(valueChars, type, langChars, keywordChars) {
|
|
146
|
+
const value = getValue(valueChars);
|
|
147
|
+
return {
|
|
148
|
+
name: getName(type, langChars, keywordChars),
|
|
149
|
+
value,
|
|
150
|
+
description: type === TYPE_ITXT ? getDescription(valueChars) : value
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
85
154
|
function getName(type, langChars, keywordChars) {
|
|
86
155
|
const name = getStringValueFromArray(keywordChars);
|
|
87
156
|
if (type === TYPE_TEXT || langChars.length === 0) {
|
|
@@ -90,3 +159,35 @@ function getName(type, langChars, keywordChars) {
|
|
|
90
159
|
const lang = getStringValueFromArray(langChars);
|
|
91
160
|
return `${name} (${lang})`;
|
|
92
161
|
}
|
|
162
|
+
|
|
163
|
+
function getValue(valueChars) {
|
|
164
|
+
if (valueChars instanceof DataView) {
|
|
165
|
+
return getStringFromDataView(valueChars, 0, valueChars.byteLength);
|
|
166
|
+
}
|
|
167
|
+
return valueChars;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function getDescription(valueChars) {
|
|
171
|
+
return TagDecoder.decode('UTF-8', valueChars);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function isExifGroupTag(name, value) {
|
|
175
|
+
return name.toLowerCase() === 'raw profile type exif' && value.substring(1, 5) === 'exif';
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function isIptcGroupTag(name, value) {
|
|
179
|
+
return name.toLowerCase() === 'raw profile type iptc' && value.substring(1, 5) === 'iptc';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function decodeRawData(value) {
|
|
183
|
+
const parts = value.match(/\n(exif|iptc)\n\s*\d+\n([\s\S]*)$/);
|
|
184
|
+
return hexToDataView(parts[2].replace(/\n/g, ''));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function hexToDataView(hex) {
|
|
188
|
+
const dataView = new DataView(new ArrayBuffer(hex.length / 2));
|
|
189
|
+
for (let i = 0; i < hex.length; i += 2) {
|
|
190
|
+
dataView.setUint8(i / 2, parseInt(hex.substring(i, i + 2), 16));
|
|
191
|
+
}
|
|
192
|
+
return dataView;
|
|
193
|
+
}
|
package/src/tag-decoder.js
CHANGED
|
@@ -15,7 +15,7 @@ function decode(encoding, tagValue) {
|
|
|
15
15
|
const Decoder = TextDecoder.get();
|
|
16
16
|
if ((typeof Decoder !== 'undefined') && (encoding !== undefined)) {
|
|
17
17
|
try {
|
|
18
|
-
return new Decoder(encoding).decode(Uint8Array.from(tagValue));
|
|
18
|
+
return new Decoder(encoding).decode(tagValue instanceof DataView ? tagValue.buffer : Uint8Array.from(tagValue));
|
|
19
19
|
} catch (error) {
|
|
20
20
|
// Pass through and fall back to ASCII decoding.
|
|
21
21
|
}
|
package/src/utils.js
CHANGED
|
@@ -20,6 +20,20 @@ export function getStringFromDataView(dataView, offset, length) {
|
|
|
20
20
|
return getStringValueFromArray(chars);
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
export function getNullTerminatedStringFromDataView(dataView, offset) {
|
|
24
|
+
const chars = [];
|
|
25
|
+
let i = 0;
|
|
26
|
+
while (offset + i < dataView.byteLength) {
|
|
27
|
+
const char = dataView.getUint8(offset + i);
|
|
28
|
+
if (char === 0) {
|
|
29
|
+
break;
|
|
30
|
+
}
|
|
31
|
+
chars.push(char);
|
|
32
|
+
i++;
|
|
33
|
+
}
|
|
34
|
+
return getStringValueFromArray(chars);
|
|
35
|
+
}
|
|
36
|
+
|
|
23
37
|
export function getUnicodeStringFromDataView(dataView, offset, length) {
|
|
24
38
|
const chars = [];
|
|
25
39
|
for (let i = 0; i < length && offset + i < dataView.byteLength; i += 2) {
|
|
@@ -132,3 +146,23 @@ export function parseFloatRadix(string, radix) {
|
|
|
132
146
|
export function strRepeat(string, num) {
|
|
133
147
|
return new Array(num + 1).join(string);
|
|
134
148
|
}
|
|
149
|
+
|
|
150
|
+
export const COMPRESSION_METHOD_NONE = undefined;
|
|
151
|
+
export const COMPRESSION_METHOD_DEFLATE = 0;
|
|
152
|
+
|
|
153
|
+
export function decompress(dataView, compressionMethod, returnType = 'string') {
|
|
154
|
+
if (compressionMethod === COMPRESSION_METHOD_DEFLATE) {
|
|
155
|
+
if (typeof DecompressionStream === 'function') {
|
|
156
|
+
const decompressionStream = new DecompressionStream('deflate');
|
|
157
|
+
const decompressedStream = new Blob([dataView]).stream().pipeThrough(decompressionStream);
|
|
158
|
+
if (returnType === 'dataview') {
|
|
159
|
+
return new Response(decompressedStream).arrayBuffer().then((arrayBuffer) => new DataView(arrayBuffer));
|
|
160
|
+
}
|
|
161
|
+
return new Response(decompressedStream).text();
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (compressionMethod !== undefined) {
|
|
165
|
+
return Promise.reject(`Unknown compression method ${compressionMethod}.`);
|
|
166
|
+
}
|
|
167
|
+
return dataView;
|
|
168
|
+
}
|