exifreader 4.17.0 → 4.18.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 +17 -11
- package/dist/exif-reader.js +1 -1
- package/dist/exif-reader.js.map +1 -1
- package/exif-reader.d.ts +12 -1
- package/package.json +2 -2
- package/src/constants.js +1 -0
- package/src/exif-reader.js +10 -0
- package/src/photoshop-tag-names.js +1062 -0
- package/src/photoshop-tags.js +67 -0
- package/src/tag-names-exif-ifd.js +1 -0
- package/src/utils.js +33 -0
|
@@ -0,0 +1,67 @@
|
|
|
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: https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/
|
|
6
|
+
|
|
7
|
+
import {getDataView, getStringFromDataView, getPascalStringFromDataView} from './utils.js';
|
|
8
|
+
import Types from './types.js';
|
|
9
|
+
import TagNames from './photoshop-tag-names.js';
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
read
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const SIGNATURE = '8BIM';
|
|
16
|
+
const TAG_ID_SIZE = 2;
|
|
17
|
+
const RESOURCE_LENGTH_SIZE = 4;
|
|
18
|
+
|
|
19
|
+
const SIGNATURE_SIZE = SIGNATURE.length;
|
|
20
|
+
|
|
21
|
+
function read(bytes, includeUnknown) {
|
|
22
|
+
const dataView = getDataView(new Uint8Array(bytes).buffer);
|
|
23
|
+
const tags = {};
|
|
24
|
+
let offset = 0;
|
|
25
|
+
|
|
26
|
+
while (offset < bytes.length) {
|
|
27
|
+
const signature = getStringFromDataView(dataView, offset, SIGNATURE_SIZE);
|
|
28
|
+
offset += SIGNATURE_SIZE;
|
|
29
|
+
const tagId = Types.getShortAt(dataView, offset);
|
|
30
|
+
offset += TAG_ID_SIZE;
|
|
31
|
+
const {tagName, tagNameSize} = getTagName(dataView, offset);
|
|
32
|
+
offset += tagNameSize;
|
|
33
|
+
const resourceSize = Types.getLongAt(dataView, offset);
|
|
34
|
+
offset += RESOURCE_LENGTH_SIZE;
|
|
35
|
+
if (signature === SIGNATURE) {
|
|
36
|
+
const valueDataView = getDataView(dataView.buffer, offset, resourceSize);
|
|
37
|
+
const tag = {
|
|
38
|
+
id: tagId,
|
|
39
|
+
value: getStringFromDataView(valueDataView, 0, resourceSize),
|
|
40
|
+
};
|
|
41
|
+
if (TagNames[tagId]) {
|
|
42
|
+
try {
|
|
43
|
+
tag.description = TagNames[tagId].description(valueDataView);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
tag.description = '<no description formatter>';
|
|
46
|
+
}
|
|
47
|
+
tags[tagName ? tagName : TagNames[tagId].name] = tag;
|
|
48
|
+
} else if (includeUnknown) {
|
|
49
|
+
tags[`undefined-${tagId}`] = tag;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
offset += resourceSize + (resourceSize % 2);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return tags;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function getTagName(dataView, offset) {
|
|
59
|
+
// The name is encoded as a Pascal string (the string is prefixed with one
|
|
60
|
+
// byte containing the length of the string) and everything is padded with a
|
|
61
|
+
// null byte to make the size even.
|
|
62
|
+
const [stringSize, string] = getPascalStringFromDataView(dataView, offset);
|
|
63
|
+
return {
|
|
64
|
+
tagName: string,
|
|
65
|
+
tagNameSize: 1 + stringSize + (stringSize % 2 === 0 ? 1 : 0)
|
|
66
|
+
};
|
|
67
|
+
}
|
package/src/utils.js
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
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 DataViewWrapper from './dataview.js';
|
|
6
|
+
|
|
7
|
+
export function getDataView(data, byteOffset, byteLength) {
|
|
8
|
+
try {
|
|
9
|
+
return new DataView(data, byteOffset, byteLength);
|
|
10
|
+
} catch (error) {
|
|
11
|
+
return new DataViewWrapper(data, byteOffset, byteLength);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
5
15
|
export function getStringFromDataView(dataView, offset, length) {
|
|
6
16
|
const chars = [];
|
|
7
17
|
for (let i = 0; i < length && offset + i < dataView.byteLength; i++) {
|
|
@@ -15,9 +25,18 @@ export function getUnicodeStringFromDataView(dataView, offset, length) {
|
|
|
15
25
|
for (let i = 0; i < length && offset + i < dataView.byteLength; i += 2) {
|
|
16
26
|
chars.push(dataView.getUint16(offset + i));
|
|
17
27
|
}
|
|
28
|
+
if (chars[chars.length - 1] === 0) {
|
|
29
|
+
chars.pop();
|
|
30
|
+
}
|
|
18
31
|
return getStringValueFromArray(chars);
|
|
19
32
|
}
|
|
20
33
|
|
|
34
|
+
export function getPascalStringFromDataView(dataView, offset) {
|
|
35
|
+
const size = dataView.getUint8(offset);
|
|
36
|
+
const string = getStringFromDataView(dataView, offset + 1, size);
|
|
37
|
+
return [size, string];
|
|
38
|
+
}
|
|
39
|
+
|
|
21
40
|
export function getStringValueFromArray(charArray) {
|
|
22
41
|
return charArray.map((charCode) => String.fromCharCode(charCode)).join('');
|
|
23
42
|
}
|
|
@@ -99,3 +118,17 @@ export function dataUriToBuffer(dataUri) {
|
|
|
99
118
|
}
|
|
100
119
|
return Uint8Array.from(decodedData, (char) => char.charCodeAt(0)).buffer;
|
|
101
120
|
}
|
|
121
|
+
|
|
122
|
+
export function padStart(string, length, character) {
|
|
123
|
+
const padding = strRepeat(character, length - string.length);
|
|
124
|
+
return padding + string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function parseFloatRadix(string, radix) {
|
|
128
|
+
return parseInt(string.replace('.', ''), radix)
|
|
129
|
+
/ Math.pow(radix, (string.split('.')[1] || '').length);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function strRepeat(string, num) {
|
|
133
|
+
return new Array(num + 1).join(string);
|
|
134
|
+
}
|