exifreader 4.53.0 → 4.55.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.
Files changed (100) hide show
  1. package/README.md +404 -33
  2. package/bin/analyze.js +139 -9
  3. package/bin/build-pages.js +21 -35
  4. package/bin/cli.js +81 -13
  5. package/bin/generate-tag-modules.js +287 -0
  6. package/dist/exif-reader.js +1 -1
  7. package/dist/exif-reader.js.map +1 -1
  8. package/exif-reader.d.ts +213 -38
  9. package/package.json +71 -11
  10. package/src/base64-image.js +27 -0
  11. package/src/composable/brand.js +10 -0
  12. package/src/composable/composition-tag-filter.js +97 -0
  13. package/src/composable/composition.js +195 -0
  14. package/src/composable/core.d.ts +148 -0
  15. package/src/composable/core.js +526 -0
  16. package/src/composable/exif-block-reader.js +197 -0
  17. package/src/composable/formats/avif.js +19 -0
  18. package/src/composable/formats/gif.js +28 -0
  19. package/src/composable/formats/heic.js +19 -0
  20. package/src/composable/formats/iso-bmff-file-reader.js +14 -0
  21. package/src/composable/formats/jpeg.js +55 -0
  22. package/src/composable/formats/jxl.js +30 -0
  23. package/src/composable/formats/png.js +187 -0
  24. package/src/composable/formats/tiff.js +18 -0
  25. package/src/composable/formats/webp.js +28 -0
  26. package/src/composable/formats/xml.js +18 -0
  27. package/src/composable/formats.d.ts +17 -0
  28. package/src/composable/formats.js +17 -0
  29. package/src/composable/group-helpers.js +129 -0
  30. package/src/composable/groups/canon.js +26 -0
  31. package/src/composable/groups/composite.js +43 -0
  32. package/src/composable/groups/exif.js +48 -0
  33. package/src/composable/groups/gps.js +75 -0
  34. package/src/composable/groups/icc.js +52 -0
  35. package/src/composable/groups/iptc.js +86 -0
  36. package/src/composable/groups/maker-notes.js +21 -0
  37. package/src/composable/groups/mpf.js +25 -0
  38. package/src/composable/groups/pentax.js +34 -0
  39. package/src/composable/groups/photoshop.js +18 -0
  40. package/src/composable/groups/thumbnail.js +131 -0
  41. package/src/composable/groups/xmp.js +118 -0
  42. package/src/composable/groups.d.ts +20 -0
  43. package/src/composable/groups.js +105 -0
  44. package/src/composable/iptc-block-reader.js +44 -0
  45. package/src/composable/read-view.js +165 -0
  46. package/src/composable/tag-binding-name.js +17 -0
  47. package/src/composable/tags/exif.d.ts +295 -0
  48. package/src/composable/tags/exif.js +396 -0
  49. package/src/composable/tags/iptc.d.ts +84 -0
  50. package/src/composable/tags/iptc.js +109 -0
  51. package/src/composable/value-budget.js +24 -0
  52. package/src/composite.js +2 -2
  53. package/src/decompression.js +60 -0
  54. package/src/dom-parser.js +5 -6
  55. package/src/engine.js +332 -0
  56. package/src/errors.js +8 -2
  57. package/src/exif-reader.d.ts +8 -0
  58. package/src/exif-reader.js +118 -1121
  59. package/src/file-loaders.js +74 -1
  60. package/src/file-tags.js +9 -9
  61. package/src/image-header-avif.js +7 -8
  62. package/src/image-header-gif.js +9 -10
  63. package/src/image-header-heic.js +7 -8
  64. package/src/image-header-iso-bmff.js +71 -92
  65. package/src/image-header-jpeg.js +60 -59
  66. package/src/image-header-jxl.js +59 -65
  67. package/src/image-header-png.js +199 -76
  68. package/src/image-header-tiff.js +7 -4
  69. package/src/image-header-webp.js +29 -35
  70. package/src/image-header.js +25 -59
  71. package/src/iptc-tag-names-descriptions.js +334 -0
  72. package/src/iptc-tag-names.js +50 -306
  73. package/src/iptc-tags.js +41 -27
  74. package/src/jfif-tags.js +9 -9
  75. package/src/load-auto.js +2 -2
  76. package/src/loadview-pipeline.js +197 -498
  77. package/src/mpf-tags.js +10 -7
  78. package/src/node-require.js +15 -2
  79. package/src/png-text-tags.js +90 -327
  80. package/src/tag-filter-config.js +24 -15
  81. package/src/tag-filter.js +67 -3
  82. package/src/tag-names-0th-ifd-descriptions.js +232 -0
  83. package/src/tag-names-0th-ifd.js +49 -173
  84. package/src/tag-names-common.js +263 -211
  85. package/src/tag-names-exif-ifd-descriptions.js +249 -0
  86. package/src/tag-names-exif-ifd.js +55 -187
  87. package/src/tag-names-gps-ifd-descriptions.js +156 -0
  88. package/src/tag-names-gps-ifd.js +39 -146
  89. package/src/tag-names-utils.js +33 -10
  90. package/src/tags-helpers.js +14 -24
  91. package/src/tags.js +23 -11
  92. package/src/thumbnail.js +7 -5
  93. package/src/types.js +2 -0
  94. package/src/unsupported-format-error.js +21 -0
  95. package/src/utils.js +64 -119
  96. package/src/writer/exif-tag-table.js +321 -0
  97. package/src/xml.js +12 -15
  98. package/src/xmp-tag-names.js +42 -21
  99. package/src/xmp-tags.js +9 -10
  100. package/webpack.config.js +1 -1
@@ -0,0 +1,131 @@
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 Thumbnail from '../../thumbnail.js';
6
+ import {filterTagsForParse} from '../../tag-filter.js';
7
+ import {getInputSizeBound} from '../group-helpers.js';
8
+ import {GROUP_BRAND} from '../brand.js';
9
+ import {removeThumbnailTag} from '../../loadview-pipeline.js';
10
+
11
+ // An embedded thumbnail is sliced from a decoded or decompressed view that can be
12
+ // far larger than the file. A real JPEG thumbnail shrinks only a little when
13
+ // compressed, so bound the embedded thumbnails of a file by twice its size in
14
+ // total, plus room for a typical thumbnail.
15
+ const EMBEDDED_THUMBNAIL_SIZE_PER_INPUT_SIZE = 2;
16
+ const EMBEDDED_THUMBNAIL_SIZE_ALLOWANCE = 64 * 1024;
17
+
18
+ export const thumbnail = {
19
+ key: 'thumbnail',
20
+ consumes: ['thumbnail'],
21
+ emits: [],
22
+ read: readThumbnailBlock,
23
+ merge: (block) => ({slot: 'thumbnail', mode: 'thumbnailCandidate', rank: block.rank}),
24
+ mergeModes: {thumbnailCandidate: addThumbnailCandidate, thumbnail: addThumbnail},
25
+ fromExif: getThumbnailBlockFromExif,
26
+ brand: GROUP_BRAND,
27
+ };
28
+
29
+ // The main block's thumbnail is a candidate with or without an image. An
30
+ // embedded one is a candidate only with an image, within the copy bound, and
31
+ // not once an uncompressed block gave one; the decompressed blocks of a file
32
+ // all get to try, since they are read after the uncompressed ones.
33
+ function readThumbnailBlock(block, ctx) {
34
+ if (!ctx.tagFilter.shouldReturnTag('thumbnail', 'Thumbnail')) {
35
+ return undefined;
36
+ }
37
+ const parsed = filterTagsForParse('thumbnail', block.ifd1Tags, ctx.tagFilter);
38
+ if (block.rank === 'main') {
39
+ return {tags: Thumbnail.get(block.dataView, parsed, block.tiffHeaderOffset, ctx.getBase64Image)};
40
+ }
41
+ if (ctx.hasEmbeddedThumbnailImage) {
42
+ return undefined;
43
+ }
44
+ const lengthTag = parsed.JPEGInterchangeFormatLength;
45
+ const copiedSize = ctx.copiedThumbnailSize || 0;
46
+ const room = getInputSizeBound(ctx.dataView, EMBEDDED_THUMBNAIL_SIZE_PER_INPUT_SIZE, EMBEDDED_THUMBNAIL_SIZE_ALLOWANCE) - copiedSize;
47
+ if (!(lengthTag && isNonNegativeInteger(lengthTag.value) && lengthTag.value <= room)) {
48
+ return undefined;
49
+ }
50
+ const thumbnailTags = Thumbnail.get(block.dataView, parsed, block.tiffHeaderOffset, ctx.getBase64Image);
51
+ if (!thumbnailTags.image) {
52
+ return undefined;
53
+ }
54
+ ctx.copiedThumbnailSize = copiedSize + lengthTag.value;
55
+ if (!block.decompressed) {
56
+ ctx.hasEmbeddedThumbnailImage = true;
57
+ }
58
+ return {tags: thumbnailTags};
59
+ }
60
+
61
+ function isNonNegativeInteger(value) {
62
+ return (typeof value === 'number') && (value >= 0) && (value % 1 === 0);
63
+ }
64
+
65
+ function addThumbnailCandidate(tags, block, context) {
66
+ context.thumbnails.push({rank: block.rank, thumbnail: block.tags});
67
+ return tags;
68
+ }
69
+
70
+ // The composed readers' thumbnail mode; the default entry replaces it with its
71
+ // own, which keeps the Thumbnail key.
72
+ function addThumbnail(tags, block, context) {
73
+ removeThumbnailTag(tags);
74
+ // Flat output can already hold an XMP or PNG text tag named thumbnail.
75
+ delete tags.thumbnail;
76
+
77
+ const selected = getSelectedThumbnail(context);
78
+ if (selected) {
79
+ tags.thumbnail = selected;
80
+ }
81
+
82
+ return tags;
83
+ }
84
+
85
+ /**
86
+ * Selects the thumbnail a load returns: the main thumbnail when it has an
87
+ * image, else the first embedded Exif thumbnail with one, else the main
88
+ * thumbnail without an image.
89
+ *
90
+ * @param {object} context The merge context, with `tagFilter` and the
91
+ * `thumbnails` candidates.
92
+ * @returns {object|undefined} The thumbnail tags, or undefined when the
93
+ * thumbnail group or its Thumbnail tag is filtered out or no candidate
94
+ * qualifies.
95
+ */
96
+ export function getSelectedThumbnail(context) {
97
+ if (
98
+ !context.tagFilter.shouldReturnGroup('thumbnail')
99
+ || !context.tagFilter.shouldReturnTag('thumbnail', 'Thumbnail')
100
+ ) {
101
+ return undefined;
102
+ }
103
+ return selectThumbnail(context.thumbnails);
104
+ }
105
+
106
+ function selectThumbnail(candidates) {
107
+ const main = candidates.filter((candidate) => candidate.rank === 'main')[0];
108
+ if (main && hasImage(main.thumbnail)) {
109
+ return main.thumbnail;
110
+ }
111
+ const embedded = candidates.filter((candidate) => candidate.rank === 'embedded' && hasImage(candidate.thumbnail))[0];
112
+ if (embedded) {
113
+ return embedded.thumbnail;
114
+ }
115
+ return main ? main.thumbnail : undefined;
116
+ }
117
+
118
+ function hasImage(thumbnailTags) {
119
+ return !!thumbnailTags && !!thumbnailTags.image;
120
+ }
121
+
122
+ function getThumbnailBlockFromExif(parsed, exif) {
123
+ return exif.thumbnailIfdTags && {
124
+ kind: 'thumbnail',
125
+ dataView: exif.view,
126
+ tiffHeaderOffset: exif.tiffHeaderOffset,
127
+ ifd1Tags: exif.thumbnailIfdTags,
128
+ decompressed: exif.decompressed,
129
+ rank: exif.carried === 'main' ? 'main' : 'embedded',
130
+ };
131
+ }
@@ -0,0 +1,118 @@
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 {getCompressionMethod, getByteString} from '../../utils.js';
6
+ import XmpTags from '../../xmp-tags.js';
7
+ import {parsedTags, getBlockDataView, getBrobDataView, getInputSizeBound, getEmbeddableMergeDescriptor} from '../group-helpers.js';
8
+ import {GROUP_BRAND} from '../brand.js';
9
+ import {assignGroupTags} from '../../loadview-pipeline.js';
10
+
11
+ // Brob XMP is bounded by the file size, as brob Exif is, plus room for one JPEG
12
+ // standard XMP segment so that a small file can still carry a typical packet.
13
+ const BROB_XMP_SIZE_PER_INPUT_SIZE = 4;
14
+ const BROB_XMP_SIZE_ALLOWANCE = 64 * 1024;
15
+
16
+ export const xmp = {
17
+ key: 'xmp',
18
+ consumes: ['xmp'],
19
+ emits: [],
20
+ read: readXmpBlock,
21
+ merge: getXmpMergeDescriptor,
22
+ suppressions: [{kind: 'xmp', isSuppressed: isCarriedXmpSuppressed}],
23
+ mergeModes: {brobXmp: assignBrobXmpTags},
24
+ fromExif: getXmpBlockFromExif,
25
+ brand: GROUP_BRAND,
26
+ };
27
+
28
+ // Only the JPEG XL finder makes a compressed (brob) block.
29
+ function readXmpBlock(block, ctx) {
30
+ const {domParser} = ctx.options;
31
+ if (block.compression !== undefined) {
32
+ if (!ctx.composed.formats.jxl) {
33
+ return undefined;
34
+ }
35
+ return ctx.decompress(getBrobDataView(ctx.dataView, block), getCompressionMethod(block.compression), undefined, 'dataview')
36
+ .then((view) => readBrobXmpView(view, ctx));
37
+ }
38
+ if (block.carried !== undefined) {
39
+ if (block.decompressed && !takeDecompressedApplicationNotesAllowance(ctx, block.byteLength)) {
40
+ return undefined;
41
+ }
42
+ return parsedTags('xmp', XmpTags.read(getBlockDataView(block, ctx), undefined, domParser, ctx.getDomParser), ctx);
43
+ }
44
+ if (!hasXmpData(block)) {
45
+ return undefined;
46
+ }
47
+ return parsedTags('xmp', XmpTags.read(block.dataView || ctx.dataView, block.chunks, domParser, ctx.getDomParser), ctx);
48
+ }
49
+
50
+ function readBrobXmpView(view, ctx) {
51
+ if (exceedsBrobXmpSizeBound(ctx, view.byteLength)) {
52
+ return parsedTags('xmp', {}, ctx);
53
+ }
54
+ return parsedTags('xmp', XmpTags.read(view, [{dataOffset: 0, length: view.byteLength}], ctx.options.domParser, ctx.getDomParser), ctx);
55
+ }
56
+
57
+ // The brob XMP bound covers the decompressed Exif blocks of a file together, so
58
+ // that several cannot each pass it.
59
+ function takeDecompressedApplicationNotesAllowance(ctx, byteLength) {
60
+ if (!(typeof byteLength === 'number' && byteLength >= 0)) {
61
+ return false;
62
+ }
63
+ const total = (ctx.decompressedApplicationNotesSize || 0) + byteLength;
64
+ if (exceedsBrobXmpSizeBound(ctx, total)) {
65
+ return false;
66
+ }
67
+ ctx.decompressedApplicationNotesSize = total;
68
+ return true;
69
+ }
70
+
71
+ function exceedsBrobXmpSizeBound(ctx, byteLength) {
72
+ return !(typeof byteLength === 'number' && byteLength <= getInputSizeBound(ctx.dataView, BROB_XMP_SIZE_PER_INPUT_SIZE, BROB_XMP_SIZE_ALLOWANCE));
73
+ }
74
+
75
+ function getXmpMergeDescriptor(block) {
76
+ if (block.compression !== undefined) {
77
+ return {slot: 'brobXmp', mode: 'brobXmp'};
78
+ }
79
+ return getEmbeddableMergeDescriptor(block, 'xmp', 'carriedXmp');
80
+ }
81
+
82
+ // A brob XMP box has no chunks, so it does not suppress.
83
+ function isCarriedXmpSuppressed(block, finderBlocks) {
84
+ return block.carried !== undefined && finderBlocks.some((finderBlock) => finderBlock.kind === 'xmp' && hasXmpData(finderBlock));
85
+ }
86
+
87
+ // In expanded output a brob XMP replaces the xmp group, in flat output it
88
+ // merges key by key, and an empty, failed or over-bound one leaves the earlier
89
+ // xmp group as it is.
90
+ function assignBrobXmpTags(tags, block, context) {
91
+ if (!block.tags || Object.keys(block.tags).length === 0) {
92
+ return tags;
93
+ }
94
+
95
+ return assignGroupTags(tags, block, context);
96
+ }
97
+
98
+ // A brob block carries compressed XMP, not chunks.
99
+ function hasXmpData(xmpBlock) {
100
+ return xmpBlock !== undefined && xmpBlock.compression === undefined && xmpBlock.chunks.length > 0;
101
+ }
102
+
103
+ function getXmpBlockFromExif(parsed, exif) {
104
+ const applicationNotes = parsed['ApplicationNotes'] && parsed['ApplicationNotes'].value;
105
+ return isNumberArray(applicationNotes) && {
106
+ kind: 'xmp',
107
+ carried: exif.carried,
108
+ decompressed: exif.decompressed,
109
+ byteLength: applicationNotes.length,
110
+ getDataView: () => getByteString(applicationNotes),
111
+ };
112
+ }
113
+
114
+ // ASCII values hold strings and holes, values of two or more rationals hold
115
+ // number pairs: neither converts to the character codes of an XMP packet.
116
+ function isNumberArray(value) {
117
+ return Array.isArray(value) && typeof value[0] === 'number';
118
+ }
@@ -0,0 +1,20 @@
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 type {Group} from './core.js';
6
+
7
+ export declare const exif: Group<'exif'>;
8
+ export declare const iptc: Group<'iptc'>;
9
+ export declare const xmp: Group<'xmp'>;
10
+ export declare const icc: Group<'icc'>;
11
+ export declare const photoshop: Group<'photoshop'>;
12
+ export declare const mpf: Group<'mpf'>;
13
+ export declare const canon: Group<'makerNotes', 'canon'>;
14
+ export declare const pentax: Group<'makerNotes', 'pentax'>;
15
+ export declare const makerNotes: Group<'makerNotes', 'canon' | 'pentax'>;
16
+ export declare const gps: Group<'gps'>;
17
+ export declare const composite: Group<'composite'>;
18
+ export declare const thumbnail: Group<'thumbnail'>;
19
+
20
+ export declare const allGroups: readonly Group[];
@@ -0,0 +1,105 @@
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 {objectAssign, getByteString} from '../utils.js';
6
+ import TagNames0thIfd from '../tag-names-0th-ifd.js';
7
+ import {exif} from './groups/exif.js';
8
+ import {composedIptc as iptcGroup} from './groups/iptc.js';
9
+ import {xmp as xmpGroup} from './groups/xmp.js';
10
+ import {icc as iccGroup} from './groups/icc.js';
11
+ import {photoshop as photoshopGroup} from './groups/photoshop.js';
12
+ import {mpf as mpfGroup} from './groups/mpf.js';
13
+ import {canon as canonGroup} from './groups/canon.js';
14
+ import {pentax as pentaxGroup} from './groups/pentax.js';
15
+ import {makerNotes as makerNotesGroup} from './groups/maker-notes.js';
16
+ import {gps as gpsGroup} from './groups/gps.js';
17
+ import {composite as compositeGroup} from './groups/composite.js';
18
+ import {thumbnail as thumbnailGroup} from './groups/thumbnail.js';
19
+ import {
20
+ JPEGInterchangeFormat,
21
+ JPEGInterchangeFormatLength,
22
+ IPTCNAA,
23
+ ApplicationNotes,
24
+ ICC_Profile,
25
+ ImageSourceData,
26
+ PhotoshopSettings,
27
+ MakerNote,
28
+ Make,
29
+ GPSLatitude,
30
+ GPSLatitudeRef,
31
+ GPSLongitude,
32
+ GPSLongitudeRef,
33
+ GPSAltitude,
34
+ GPSAltitudeRef,
35
+ FocalLength,
36
+ FocalPlaneXResolution,
37
+ FocalPlaneYResolution,
38
+ FocalPlaneResolutionUnit,
39
+ FocalLengthIn35mmFilm,
40
+ PixelXDimension,
41
+ PixelYDimension,
42
+ } from './tags/exif.js';
43
+
44
+ // These copies carry the descriptors of the Exif tags their groups read through
45
+ // Exif and, for xmp, thumbnail and mpf, what the composable reader needs from
46
+ // its environment, here and not in the modules, which the default entry also
47
+ // imports.
48
+ export const iptc = /* #__PURE__ */ withAdditions(iptcGroup, {exifDependencies: [IPTCNAA]});
49
+ export const xmp = /* #__PURE__ */ withAdditions(xmpGroup, {
50
+ exifDependencies: [ApplicationNotes],
51
+ environment: {getDomParser},
52
+ assertEnvironment: assertDomParserAvailable,
53
+ });
54
+ export const icc = /* #__PURE__ */ withAdditions(iccGroup, {exifDependencies: [ICC_Profile]});
55
+ export const photoshop = /* #__PURE__ */ withAdditions(photoshopGroup, {exifDependencies: [ImageSourceData, PhotoshopSettings]});
56
+ export const canon = /* #__PURE__ */ withAdditions(canonGroup, {exifDependencies: [MakerNote, Make]});
57
+ export const pentax = /* #__PURE__ */ withAdditions(pentaxGroup, {exifDependencies: [MakerNote, Make]});
58
+ export const makerNotes = /* #__PURE__ */ withAdditions(makerNotesGroup, {exifDependencies: [MakerNote, Make]});
59
+ export const gps = /* #__PURE__ */ withAdditions(gpsGroup, {
60
+ exifDependencies: [GPSLatitude, GPSLatitudeRef, GPSLongitude, GPSLongitudeRef, GPSAltitude, GPSAltitudeRef],
61
+ });
62
+ export const composite = /* #__PURE__ */ withAdditions(compositeGroup, {
63
+ exifDependencies: [
64
+ FocalLength,
65
+ FocalPlaneXResolution,
66
+ FocalPlaneYResolution,
67
+ FocalPlaneResolutionUnit,
68
+ FocalLengthIn35mmFilm,
69
+ PixelXDimension,
70
+ PixelYDimension,
71
+ ],
72
+ });
73
+ export const thumbnail = /* #__PURE__ */ withAdditions(thumbnailGroup, {
74
+ exifDependencies: [JPEGInterchangeFormat, JPEGInterchangeFormatLength],
75
+ ifd1TagNames: TagNames0thIfd,
76
+ environment: {getBase64Image},
77
+ });
78
+ export const mpf = /* #__PURE__ */ withAdditions(mpfGroup, {environment: {getBase64Image}});
79
+
80
+ export {exif};
81
+
82
+ // Without canon and pentax, since makerNotes is their union: leaving makerNotes
83
+ // out of this list leaves out every maker note.
84
+ export const allGroups = [exif, iptc, xmp, icc, photoshop, mpf, makerNotes, gps, composite, thumbnail];
85
+
86
+ function withAdditions(group, additions) {
87
+ return objectAssign({}, group, additions);
88
+ }
89
+
90
+ function getDomParser(domParser) {
91
+ return {parser: domParser || new DOMParser()};
92
+ }
93
+
94
+ function assertDomParserAvailable(domParser) {
95
+ if (!domParser && typeof DOMParser === 'undefined') {
96
+ throw new Error('reader.load: xmp is in groups but no DOM parser is available: pass domParser to createReader, for example new DOMParser({onError: onErrorStopParsing}) from @xmldom/xmldom, or leave xmp out of groups');
97
+ }
98
+ }
99
+
100
+ function getBase64Image(image) {
101
+ if (typeof btoa === 'undefined') {
102
+ return undefined;
103
+ }
104
+ return btoa(getByteString(new Uint8Array(image)));
105
+ }
@@ -0,0 +1,44 @@
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 {objectAssign} from '../utils.js';
6
+ import {readIptcTags} from '../iptc-tags.js';
7
+ import {createIptcGroup, withRepeatableAsArrays} from './groups/iptc.js';
8
+ import {IPTCNAA} from './tags/exif.js';
9
+
10
+ // The generated IPTC tag descriptors link to this, so a createReader bundle
11
+ // carries IPTC parsing only when it composes one.
12
+ export const iptcBlockReader = {createParseOnlyGroup, isDescriptor};
13
+
14
+ /**
15
+ * An iptc group that reads and returns the datasets of the given descriptors
16
+ * only, with the unknown datasets left out whatever includeUnknown says.
17
+ *
18
+ * @param {Array<object>} descriptors Generated IPTC tag descriptors.
19
+ * @returns {object} An engine group keyed iptc, with the Exif tag that carries
20
+ * IPTC as its Exif dependency.
21
+ */
22
+ function createParseOnlyGroup(descriptors) {
23
+ const tagNames = getTagNames(descriptors);
24
+ const readWithTagNames = (view, dataOffset, includeUnknown, tagFilter, valueBudget) => withRepeatableAsArrays(
25
+ readIptcTags(view, dataOffset, tagNames, false, tagFilter, valueBudget),
26
+ tagNames
27
+ );
28
+ return objectAssign(createIptcGroup(readWithTagNames), {exifDependencies: [IPTCNAA]});
29
+ }
30
+
31
+ // In the iptc dictionary's own shape.
32
+ function getTagNames(descriptors) {
33
+ const tagNames = {};
34
+ descriptors.forEach((descriptor) => {
35
+ descriptor.ids.forEach((id) => {
36
+ tagNames[id] = {name: descriptor.name, description: descriptor.description, repeatable: descriptor.repeatable};
37
+ });
38
+ });
39
+ return tagNames;
40
+ }
41
+
42
+ function isDescriptor(tag) {
43
+ return tag.group === 'iptc' && tag.dictionary === 'iptc';
44
+ }
@@ -0,0 +1,165 @@
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 {objectAssign, getDataView, assertPromiseSupport, MAX_METADATA_BLOCKS} from '../utils.js';
6
+ import ImageHeader from '../image-header.js';
7
+ import {filterTagsForReturn} from '../tag-filter.js';
8
+ import {buildTagsFromMergeBlocks} from '../loadview-pipeline.js';
9
+ import {run, createContext} from '../engine.js';
10
+ import {isPngTextChunkBlock} from './group-helpers.js';
11
+
12
+ /**
13
+ * Runs a composition over an image's bytes and builds its tags.
14
+ *
15
+ * @param {object} composition From Composition.createComposition; a sync load
16
+ * also needs its `isHiddenInSyncMode` from createIsHiddenInSyncMode.
17
+ * @param {DataView} dataView The image. A window into a larger buffer is
18
+ * copied out first.
19
+ * @param {object} options The load options (`expanded`, `async`, `computed`,
20
+ * `includeUnknown`, `includeOffsets`, `domParser`, `includeTags`,
21
+ * `excludeTags`, `decompress`).
22
+ * @param {{getDomParser: (function|undefined), getBase64Image: (function|undefined), createTagFilter: function, includeTagsIsLimit: (boolean|undefined)}} environment
23
+ * How XMP gets a DOM parser and a thumbnail or an MPF image its base64,
24
+ * what builds the tag filter from includeTags, excludeTags and
25
+ * includeTagsIsLimit, and whether includeTags only caps what is read (see
26
+ * createTagFilter in tag-filter.js).
27
+ * @returns {object|Promise<object>} The tags, or a promise of them when
28
+ * `options.async` is set.
29
+ * @throws {UnsupportedFormatError} Synchronously, when no composed format
30
+ * recognizes the image.
31
+ */
32
+ export function readView(composition, dataView, options, environment) {
33
+ const {
34
+ expanded = false,
35
+ async = false,
36
+ computed = false,
37
+ includeUnknown = false,
38
+ includeOffsets = false,
39
+ domParser = undefined,
40
+ includeTags = undefined,
41
+ excludeTags = undefined,
42
+ decompress: decompressConfig = undefined
43
+ } = options;
44
+ dataView = getSelfContainedDataView(dataView);
45
+
46
+ const {fileType, blocks: allBlocks, ranges, truncated} = ImageHeader.parseAppMarkers(dataView, expanded && includeOffsets, composition.formats);
47
+ if (async) {
48
+ assertPromiseSupport();
49
+ }
50
+ const blocks = async ? allBlocks : allBlocks.filter((block) => !composition.isHiddenInSyncMode(block));
51
+
52
+ const tagFilter = environment.createTagFilter({includeTags, excludeTags, includeTagsIsLimit: environment.includeTagsIsLimit});
53
+ const tagFilterIsReadCap = Boolean(environment.includeTagsIsLimit) && !excludeTags;
54
+ const context = createContext({
55
+ dataView,
56
+ options: {expanded, async, computed, includeUnknown, includeOffsets, domParser, includeTags, excludeTags},
57
+ tagFilter,
58
+ decompressConfig: composition.decompression ? composition.decompression.withDecompressBudget(decompressConfig) : undefined,
59
+ getBase64Image: environment.getBase64Image,
60
+ getDomParser: environment.getDomParser,
61
+ composed: composition.composed,
62
+ decompression: composition.decompression,
63
+ });
64
+ const result = run(composition, blocks, context);
65
+
66
+ return async ? result.then(finish) : finish(result);
67
+
68
+ function finish({mergeBlocks}) {
69
+ for (let i = 0; i < composition.prepareMergeBlocks.length; i++) {
70
+ mergeBlocks = composition.prepareMergeBlocks[i](mergeBlocks);
71
+ }
72
+ let tags = buildTagsFromMergeBlocks({
73
+ mergeBlocks: mergeBlocks.concat(getDerivedBlocks(composition, {expanded, includeOffsets, async, ranges, truncated})),
74
+ parsedGroups: Object.create(null),
75
+ expanded,
76
+ tagFilter,
77
+ tagFilterIsReadCap,
78
+ dataView,
79
+ fileType,
80
+ modes: composition.mergeModes,
81
+ deps: {objectAssign, filterTagsForReturn},
82
+ });
83
+ for (let i = 0; i < composition.finishTags.length; i++) {
84
+ tags = composition.finishTags[i](tags, {expanded, async, tagFilter, blocks});
85
+ }
86
+ return tags;
87
+ }
88
+ }
89
+
90
+ // A caller-supplied DataView can be a window into a larger buffer. Extractors
91
+ // slice dataView.buffer, so copy the window out to keep them inside the image.
92
+ function getSelfContainedDataView(dataView) {
93
+ if (isWindowIntoLargerBuffer(dataView)) {
94
+ return getDataView(dataView.buffer.slice(dataView.byteOffset, dataView.byteOffset + dataView.byteLength));
95
+ }
96
+ return dataView;
97
+ }
98
+
99
+ function isWindowIntoLargerBuffer(dataView) {
100
+ return isDataViewLike(dataView)
101
+ && ((dataView.byteOffset > 0) || (dataView.byteLength < dataView.buffer.byteLength));
102
+ }
103
+
104
+ // A DataView from another realm, an iframe for example, fails instanceof, so
105
+ // match on the shape instead. A typed array has no getUint8, so it is not
106
+ // treated as a window here.
107
+ function isDataViewLike(dataView) {
108
+ return !!dataView
109
+ && (typeof dataView.getUint8 === 'function')
110
+ && (typeof dataView.byteOffset === 'number')
111
+ && (typeof dataView.byteLength === 'number')
112
+ && !!dataView.buffer
113
+ && (typeof dataView.buffer.slice === 'function');
114
+ }
115
+
116
+ function getDerivedBlocks(composition, {expanded, includeOffsets, async, ranges, truncated}) {
117
+ const blocks = composition.derived.map((group) => group.merge());
118
+ blocks.push({slot: 'thumbnail', mode: 'thumbnail'});
119
+ blocks.push({slot: 'fileType', mode: 'fileType'});
120
+ if (expanded && includeOffsets) {
121
+ blocks.push({
122
+ slot: 'metadataRange',
123
+ mode: 'metadataRange',
124
+ metadataBlocks: getMetadataRangeBlocks(composition, ranges, async),
125
+ metadataTruncated: !!truncated,
126
+ });
127
+ }
128
+ return blocks;
129
+ }
130
+
131
+ // Finders report every kind they find; list only the kinds this build parses,
132
+ // and in sync mode only the blocks it reads (isHiddenInSyncMode).
133
+ function getMetadataRangeBlocks(composition, ranges, async) {
134
+ const entries = ranges || [];
135
+ const isCapped = entries.length >= MAX_METADATA_BLOCKS;
136
+ const lastIndex = entries.length - 1;
137
+ const metadataBlocks = [];
138
+ for (let i = 0; i < entries.length; i++) {
139
+ const {block, start, end} = entries[i];
140
+ // At the cap the last entry was widened over every later block, so it
141
+ // stays whatever its own block is, hidden in sync mode or not parsed.
142
+ const isWidenedLast = isCapped && i === lastIndex;
143
+ if (isWidenedLast || ((async || !composition.isHiddenInSyncMode(block)) && isParsedRangeType(composition, block))) {
144
+ metadataBlocks.push({type: getRangeType(block), start, end});
145
+ }
146
+ }
147
+ return metadataBlocks;
148
+ }
149
+
150
+ // A kind no composed group consumes is not parsed; a text chunk is parsed
151
+ // whenever the png format is composed.
152
+ function isParsedRangeType(composition, block) {
153
+ return composition.composed.kinds[composition.dispatchKind(block)] === true;
154
+ }
155
+
156
+ // The range types predate the block kinds and stay as they are.
157
+ function getRangeType(block) {
158
+ if (isPngTextChunkBlock(block) || block.kind === 'pngChunk') {
159
+ return 'png';
160
+ }
161
+ if (block.kind === 'pngFile') {
162
+ return 'file';
163
+ }
164
+ return block.kind;
165
+ }
@@ -0,0 +1,17 @@
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
+ /**
6
+ * The JS identifier a tag name is exported as: the name split into words on
7
+ * every non-identifier character, each word's first letter upper-cased.
8
+ *
9
+ * @param {string} name A tag name as the dictionaries give it.
10
+ * @returns {string}
11
+ */
12
+ export function getTagBindingName(name) {
13
+ return name.split(/[^A-Za-z0-9_$]+/)
14
+ .filter((word) => word !== '')
15
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
16
+ .join('');
17
+ }