@ac-kit/format-tiff 0.0.0-stage → 0.1.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 (108) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/LICENSE +28 -0
  3. package/README.md +114 -2
  4. package/dist/codec/_deflate.d.ts +12 -0
  5. package/dist/codec/_deflate.d.ts.map +1 -0
  6. package/dist/codec/_deflate.js +33 -0
  7. package/dist/codec/_deflate.js.map +1 -0
  8. package/dist/codec/_lzw.d.ts +18 -0
  9. package/dist/codec/_lzw.d.ts.map +1 -0
  10. package/dist/codec/_lzw.js +134 -0
  11. package/dist/codec/_lzw.js.map +1 -0
  12. package/dist/codec/_packbits.d.ts +10 -0
  13. package/dist/codec/_packbits.d.ts.map +1 -0
  14. package/dist/codec/_packbits.js +34 -0
  15. package/dist/codec/_packbits.js.map +1 -0
  16. package/dist/codec/_predictor.d.ts +15 -0
  17. package/dist/codec/_predictor.d.ts.map +1 -0
  18. package/dist/codec/_predictor.js +68 -0
  19. package/dist/codec/_predictor.js.map +1 -0
  20. package/dist/codec/tiff-decoder.d.ts +54 -0
  21. package/dist/codec/tiff-decoder.d.ts.map +1 -0
  22. package/dist/codec/tiff-decoder.js +2 -0
  23. package/dist/codec/tiff-decoder.js.map +1 -0
  24. package/dist/container/_ifd.d.ts +45 -0
  25. package/dist/container/_ifd.d.ts.map +1 -0
  26. package/dist/container/_ifd.js +160 -0
  27. package/dist/container/_ifd.js.map +1 -0
  28. package/dist/container/_image.d.ts +37 -0
  29. package/dist/container/_image.d.ts.map +1 -0
  30. package/dist/container/_image.js +201 -0
  31. package/dist/container/_image.js.map +1 -0
  32. package/dist/container/_reader.d.ts +18 -0
  33. package/dist/container/_reader.d.ts.map +1 -0
  34. package/dist/container/_reader.js +53 -0
  35. package/dist/container/_reader.js.map +1 -0
  36. package/dist/container/open-tiff.d.ts +40 -0
  37. package/dist/container/open-tiff.d.ts.map +1 -0
  38. package/dist/container/open-tiff.js +209 -0
  39. package/dist/container/open-tiff.js.map +1 -0
  40. package/dist/container/read-tiff-gdal-metadata.d.ts +33 -0
  41. package/dist/container/read-tiff-gdal-metadata.d.ts.map +1 -0
  42. package/dist/container/read-tiff-gdal-metadata.js +82 -0
  43. package/dist/container/read-tiff-gdal-metadata.js.map +1 -0
  44. package/dist/container/tiff.d.ts +119 -0
  45. package/dist/container/tiff.d.ts.map +1 -0
  46. package/dist/container/tiff.js +2 -0
  47. package/dist/container/tiff.js.map +1 -0
  48. package/dist/errors.d.ts +34 -0
  49. package/dist/errors.d.ts.map +1 -0
  50. package/dist/errors.js +43 -0
  51. package/dist/errors.js.map +1 -0
  52. package/dist/field/_field-values.d.ts +10 -0
  53. package/dist/field/_field-values.d.ts.map +1 -0
  54. package/dist/field/_field-values.js +51 -0
  55. package/dist/field/_field-values.js.map +1 -0
  56. package/dist/field/tiff-field-type.d.ts +42 -0
  57. package/dist/field/tiff-field-type.d.ts.map +1 -0
  58. package/dist/field/tiff-field-type.js +68 -0
  59. package/dist/field/tiff-field-type.js.map +1 -0
  60. package/dist/field/tiff-field.d.ts +62 -0
  61. package/dist/field/tiff-field.d.ts.map +1 -0
  62. package/dist/field/tiff-field.js +125 -0
  63. package/dist/field/tiff-field.js.map +1 -0
  64. package/dist/field/tiff-tag.d.ts +120 -0
  65. package/dist/field/tiff-tag.d.ts.map +1 -0
  66. package/dist/field/tiff-tag.js +124 -0
  67. package/dist/field/tiff-tag.js.map +1 -0
  68. package/dist/geotiff/geo-key.d.ts +67 -0
  69. package/dist/geotiff/geo-key.d.ts.map +1 -0
  70. package/dist/geotiff/geo-key.js +69 -0
  71. package/dist/geotiff/geo-key.js.map +1 -0
  72. package/dist/geotiff/geotiff-raster-to-model.d.ts +36 -0
  73. package/dist/geotiff/geotiff-raster-to-model.d.ts.map +1 -0
  74. package/dist/geotiff/geotiff-raster-to-model.js +70 -0
  75. package/dist/geotiff/geotiff-raster-to-model.js.map +1 -0
  76. package/dist/geotiff/index.d.ts +4 -0
  77. package/dist/geotiff/index.d.ts.map +1 -0
  78. package/dist/geotiff/index.js +4 -0
  79. package/dist/geotiff/index.js.map +1 -0
  80. package/dist/geotiff/read-geotiff.d.ts +63 -0
  81. package/dist/geotiff/read-geotiff.d.ts.map +1 -0
  82. package/dist/geotiff/read-geotiff.js +150 -0
  83. package/dist/geotiff/read-geotiff.js.map +1 -0
  84. package/dist/index.d.ts +11 -0
  85. package/dist/index.d.ts.map +1 -0
  86. package/dist/index.js +11 -0
  87. package/dist/index.js.map +1 -0
  88. package/dist/raster/_decode-chunk.d.ts +29 -0
  89. package/dist/raster/_decode-chunk.d.ts.map +1 -0
  90. package/dist/raster/_decode-chunk.js +159 -0
  91. package/dist/raster/_decode-chunk.js.map +1 -0
  92. package/dist/raster/_layout.d.ts +27 -0
  93. package/dist/raster/_layout.d.ts.map +1 -0
  94. package/dist/raster/_layout.js +79 -0
  95. package/dist/raster/_layout.js.map +1 -0
  96. package/dist/raster/_samples.d.ts +52 -0
  97. package/dist/raster/_samples.d.ts.map +1 -0
  98. package/dist/raster/_samples.js +192 -0
  99. package/dist/raster/_samples.js.map +1 -0
  100. package/dist/raster/read-tiff-window.d.ts +109 -0
  101. package/dist/raster/read-tiff-window.d.ts.map +1 -0
  102. package/dist/raster/read-tiff-window.js +396 -0
  103. package/dist/raster/read-tiff-window.js.map +1 -0
  104. package/dist/raster/tiff-sample-array.d.ts +7 -0
  105. package/dist/raster/tiff-sample-array.d.ts.map +1 -0
  106. package/dist/raster/tiff-sample-array.js +2 -0
  107. package/dist/raster/tiff-sample-array.js.map +1 -0
  108. package/package.json +68 -3
@@ -0,0 +1,209 @@
1
+ import { TiffFormatError, TiffLimitError } from "../errors.js";
2
+ import { fieldNumberAt_, fieldValueCount_ } from "../field/_field-values.js";
3
+ import { TiffTag } from "../field/tiff-tag.js";
4
+ import { ifdSizes_, parseHeader_, parseIfd_, spendTagBytes_, } from "./_ifd.js";
5
+ import { createImage_, groupImages_, linkSubImages_, setImageContext_, } from "./_image.js";
6
+ import { createReader_, SpanCache_ } from "./_reader.js";
7
+ /** The first read: header, ghost area and, in a COG, every IFD. */
8
+ const PREFIX_LENGTH_ = 16 * 1024;
9
+ /** Values this close together are read in one request. */
10
+ const VALUE_GAP_ = 4 * 1024;
11
+ function checkLimits_(limits) {
12
+ for (const name of [
13
+ "maxImages",
14
+ "maxTagBytes",
15
+ "maxTileBytes",
16
+ "maxDecodedBytes",
17
+ ]) {
18
+ const value = limits[name];
19
+ if (!Number.isSafeInteger(value) || value < 0) {
20
+ throw new TypeError(`TIFF limit ${name} of ${value}`);
21
+ }
22
+ }
23
+ }
24
+ /** The bytes of `[offset, offset + length)`, from the cache or one read. */
25
+ async function ensure_(walk, offset, length) {
26
+ const cached = walk.cache.get(offset, length);
27
+ if (cached !== undefined) {
28
+ return cached;
29
+ }
30
+ const { size } = walk.reader;
31
+ if (offset + length > size) {
32
+ throw new TiffFormatError("An IFD past the end of the file", offset);
33
+ }
34
+ const readLength = Math.min(size - offset, Math.max(length, PREFIX_LENGTH_));
35
+ const bytes = await walk.reader.read(offset, readLength, walk.signal);
36
+ walk.cache.add(offset, bytes);
37
+ return bytes.subarray(0, length);
38
+ }
39
+ async function readIfd_(walk, offset) {
40
+ if (walk.visited.has(offset)) {
41
+ throw new TiffFormatError("A loop in the IFD chain", offset);
42
+ }
43
+ if (walk.visited.size >= walk.limits.maxImages) {
44
+ throw new TiffLimitError(`The file has more than maxImages (${walk.limits.maxImages}) IFDs`);
45
+ }
46
+ walk.visited.add(offset);
47
+ const { bigTiff, littleEndian } = walk.header;
48
+ const sizes = ifdSizes_(bigTiff);
49
+ const countBytes = await ensure_(walk, offset, sizes.count);
50
+ const countView = new DataView(countBytes.buffer, countBytes.byteOffset, countBytes.length);
51
+ const entryCount = bigTiff
52
+ ? Number(countView.getBigUint64(0, littleEndian))
53
+ : countView.getUint16(0, littleEndian);
54
+ const length = sizes.count + entryCount * sizes.entry + sizes.next;
55
+ if (!Number.isSafeInteger(length)) {
56
+ throw new TiffFormatError(`An IFD of ${entryCount} entries`, offset);
57
+ }
58
+ spendTagBytes_(walk.budget, length);
59
+ const bytes = await ensure_(walk, offset, length);
60
+ return parseIfd_(bytes, offset, walk.header, walk.reader.size, walk.budget);
61
+ }
62
+ /**
63
+ * Reads every pending value: those the cache holds at once, the others in as
64
+ * few reads as their gaps allow.
65
+ */
66
+ async function resolvePending_(walk, pending) {
67
+ const missing = pending
68
+ .filter((value) => walk.cache.get(value.offset, value.length) === undefined)
69
+ .sort((left, right) => left.offset - right.offset);
70
+ let index = 0;
71
+ while (index < missing.length) {
72
+ const start = missing[index].offset;
73
+ let end = start + missing[index].length;
74
+ index++;
75
+ while (index < missing.length &&
76
+ missing[index].offset - end <= VALUE_GAP_) {
77
+ end = Math.max(end, missing[index].offset + missing[index].length);
78
+ index++;
79
+ }
80
+ walk.cache.add(start, await walk.reader.read(start, end - start, walk.signal));
81
+ }
82
+ for (const value of pending) {
83
+ value.fields.set(value.tag, {
84
+ tag: value.tag,
85
+ type: value.type,
86
+ count: value.count,
87
+ bytes: walk.cache.get(value.offset, value.length).slice(),
88
+ littleEndian: walk.header.littleEndian,
89
+ });
90
+ }
91
+ }
92
+ function subIfdOffsets_(image) {
93
+ const field = image.fields.get(TiffTag.SUB_IFDS);
94
+ if (field === undefined) {
95
+ return [];
96
+ }
97
+ const offsets = [];
98
+ const count = fieldValueCount_(field);
99
+ for (let index = 0; index < count; index++) {
100
+ const offset = fieldNumberAt_(field, index);
101
+ if (Number.isSafeInteger(offset) && offset > 0) {
102
+ offsets.push(offset);
103
+ }
104
+ }
105
+ return offsets;
106
+ }
107
+ /**
108
+ * Opens a TIFF 6.0, BigTIFF or Cloud Optimized GeoTIFF file by ranges, without
109
+ * reading its image data: the header and GDAL's ghost area, every IFD of the
110
+ * chain and every SubIFD, and every tag value, so each {@link TiffImage} is
111
+ * complete when this resolves.
112
+ *
113
+ * The first read takes the first 16 KiB, which in a COG holds every IFD
114
+ * (`LAYOUT=IFDS_BEFORE_DATA`); the values those IFDs point to outside it are
115
+ * then read together, in one read when they are close together, as a COG's are.
116
+ * An IFD outside what was read costs one more read. Images are then read with
117
+ * {@link readTiffWindow} and {@link readTiffTile}.
118
+ *
119
+ * A `Blob` (a `File`, Node's `fs.openAsBlob`) is read with `slice`, bytes in
120
+ * memory as views; any other {@link ByteRangeSource} (HTTP `Range`, object
121
+ * storage) through `read`.
122
+ *
123
+ * Memory is linear in the tag bytes, bounded by `maxTagBytes`.
124
+ *
125
+ * @example
126
+ * const tiff = await openTiff(await fs.openAsBlob(path), {
127
+ * maxImages: 64,
128
+ * maxTagBytes: 16 * 1024 * 1024,
129
+ * maxTileBytes: 64 * 1024 * 1024,
130
+ * maxDecodedBytes: 256 * 1024 * 1024,
131
+ * });
132
+ * const [image] = tiff.images;
133
+ *
134
+ * @throws {TiffFormatError} When the header is not TIFF's, an IFD or a value
135
+ * lies past the end, the chain loops, an image lacks its size, or the source
136
+ * gives fewer bytes than asked.
137
+ * @throws {TiffLimitError} When the file has more than `maxImages` IFDs or more
138
+ * than `maxTagBytes` of IFDs and values.
139
+ * @throws {TypeError} When a limit or the source's size is not a safe
140
+ * non-negative integer.
141
+ * @throws When `signal` aborts, its reason; the source's own read error.
142
+ */
143
+ export async function openTiff(source, limits, signal) {
144
+ checkLimits_(limits);
145
+ signal?.throwIfAborted();
146
+ const reader = createReader_(source);
147
+ const cache = new SpanCache_();
148
+ const prefix = await reader.read(0, Math.min(reader.size, PREFIX_LENGTH_), signal);
149
+ cache.add(0, prefix);
150
+ const header = parseHeader_(prefix);
151
+ if (header.firstIfd === 0) {
152
+ throw new TiffFormatError("A file without an IFD", header.bigTiff ? 8 : 4);
153
+ }
154
+ const walk = {
155
+ reader,
156
+ cache,
157
+ header,
158
+ budget: { spent: 0, max: limits.maxTagBytes },
159
+ limits,
160
+ visited: new Set(),
161
+ signal,
162
+ };
163
+ const chain = [];
164
+ for (let offset = header.firstIfd; offset !== 0;) {
165
+ const ifd = await readIfd_(walk, offset);
166
+ chain.push(ifd);
167
+ offset = ifd.next;
168
+ }
169
+ await resolvePending_(walk, chain.flatMap((ifd) => ifd.pending));
170
+ const images = chain.map((ifd) => createImage_(ifd.offset, ifd.fields));
171
+ groupImages_(images);
172
+ let parents = images;
173
+ while (parents.length > 0) {
174
+ const level = [];
175
+ for (const parent of parents) {
176
+ const ifds = [];
177
+ for (const offset of subIfdOffsets_(parent)) {
178
+ ifds.push(await readIfd_(walk, offset));
179
+ }
180
+ level.push({ parent, ifds });
181
+ }
182
+ await resolvePending_(walk, level.flatMap(({ ifds }) => ifds.flatMap((ifd) => ifd.pending)));
183
+ const next = [];
184
+ for (const { parent, ifds } of level) {
185
+ const subImages = ifds.map((ifd) => createImage_(ifd.offset, ifd.fields));
186
+ linkSubImages_(parent, subImages);
187
+ next.push(...subImages);
188
+ }
189
+ parents = next;
190
+ }
191
+ const context = { reader, limits, littleEndian: header.littleEndian };
192
+ const register = (image) => {
193
+ setImageContext_(image, context);
194
+ for (const subImage of image.subImages) {
195
+ register(subImage);
196
+ }
197
+ };
198
+ for (const image of images) {
199
+ register(image);
200
+ }
201
+ return {
202
+ littleEndian: header.littleEndian,
203
+ bigTiff: header.bigTiff,
204
+ size: reader.size,
205
+ images,
206
+ structuralMetadata: header.structuralMetadata,
207
+ };
208
+ }
209
+ //# sourceMappingURL=open-tiff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"open-tiff.js","sourceRoot":"","sources":["../../src/container/open-tiff.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7E,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAC/C,OAAO,EAEN,SAAS,EAET,YAAY,EACZ,SAAS,EAET,cAAc,GAEd,MAAM,WAAW,CAAC;AACnB,OAAO,EACN,YAAY,E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C,CAC/D,CAAC;QACF,MAAM,IAAI,GAAoB,EAAE,CAAC;QACjC,KAAK,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,KAAK,EAAE,CAAC;YACtC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,YAAY,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;YAC1E,cAAc,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;YAClC,IAAI,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAC;QACzB,CAAC;QACD,OAAO,GAAG,IAAI,CAAC;IAChB,CAAC;IAED,MAAM,OAAO,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,CAAC,YAAY,EAAE,CAAC;IACtE,MAAM,QAAQ,GAAG,CAAC,KAAoB,EAAQ,EAAE;QAC/C,gBAAgB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QACjC,KAAK,MAAM,QAAQ,IAAI,KAAK,CAAC,SAAS,EAAE,CAAC;YACxC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACpB,CAAC;IACF,CAAC,CAAC;IACF,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC5B,QAAQ,CAAC,KAAK,CAAC,CAAC;IACjB,CAAC;IACD,OAAO;QACN,YAAY,EAAE,MAAM,CAAC,YAAY;QACjC,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM;QACN,kBAAkB,EAAE,MAAM,CAAC,kBAAkB;KAC7C,CAAC;AACH,CAAC"}
@@ -0,0 +1,33 @@
1
+ import type { TiffImage } from "./tiff.js";
2
+ /** One `Item` of GDAL's `GDAL_METADATA` tag. */
3
+ export type TiffGdalMetadataItem = {
4
+ readonly name: string;
5
+ /** The text content, references expanded. */
6
+ readonly value: string;
7
+ /** The band it belongs to, from 0; `undefined` for the dataset. */
8
+ readonly sample: number | undefined;
9
+ /** What GDAL reads it as: `scale`, `offset`, `description`, `unittype`, … */
10
+ readonly role: string | undefined;
11
+ /** Its metadata domain; `undefined` for the default one. */
12
+ readonly domain: string | undefined;
13
+ };
14
+ /**
15
+ * The items of the image's `GDAL_METADATA` tag (42112): dataset and band
16
+ * metadata, among them each band's `scale`, `offset`, `description` and
17
+ * `unittype` roles, as GDAL writes them. `undefined` when the tag is absent.
18
+ *
19
+ * The document is parsed with no external entity fetched and entity expansion
20
+ * bounded; its size is already bounded by `maxTagBytes`.
21
+ *
22
+ * Time and memory linear in the tag.
23
+ *
24
+ * @example
25
+ * const scale = readTiffGdalMetadata(image)?.find(
26
+ * (item) => item.role === "scale" && item.sample === 0,
27
+ * )?.value;
28
+ *
29
+ * @throws {TiffFormatError} When the tag is not a well-formed `<GDALMetadata>`
30
+ * document.
31
+ */
32
+ export declare function readTiffGdalMetadata(image: TiffImage): TiffGdalMetadataItem[] | undefined;
33
+ //# sourceMappingURL=read-tiff-gdal-metadata.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-tiff-gdal-metadata.d.ts","sourceRoot":"","sources":["../../src/container/read-tiff-gdal-metadata.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE3C,gDAAgD;AAChD,MAAM,MAAM,oBAAoB,GAAG;IAClC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,6CAA6C;IAC7C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,mEAAmE;IACnE,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC,6EAA6E;IAC7E,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,4DAA4D;IAC5D,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;CACpC,CAAC;AAqBF;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,SAAS,GACd,oBAAoB,EAAE,GAAG,SAAS,CAmDpC"}
@@ -0,0 +1,82 @@
1
+ import { parseXml, XmlSyntaxError, xmlDocumentRoot, } from "@ac-kit/format-xml";
2
+ import { TiffFormatError } from "../errors.js";
3
+ import { tiffFieldText } from "../field/tiff-field.js";
4
+ import { TiffTag } from "../field/tiff-tag.js";
5
+ function attribute_(element, name) {
6
+ return element.attributes.find((attribute) => attribute.name.prefix === null && attribute.name.local === name)?.value;
7
+ }
8
+ function text_(nodes) {
9
+ let text = "";
10
+ for (const node of nodes) {
11
+ if (node.kind === "text" || node.kind === "cdata") {
12
+ text += node.value;
13
+ }
14
+ else if (node.kind === "element") {
15
+ text += text_(node.children);
16
+ }
17
+ }
18
+ return text;
19
+ }
20
+ /**
21
+ * The items of the image's `GDAL_METADATA` tag (42112): dataset and band
22
+ * metadata, among them each band's `scale`, `offset`, `description` and
23
+ * `unittype` roles, as GDAL writes them. `undefined` when the tag is absent.
24
+ *
25
+ * The document is parsed with no external entity fetched and entity expansion
26
+ * bounded; its size is already bounded by `maxTagBytes`.
27
+ *
28
+ * Time and memory linear in the tag.
29
+ *
30
+ * @example
31
+ * const scale = readTiffGdalMetadata(image)?.find(
32
+ * (item) => item.role === "scale" && item.sample === 0,
33
+ * )?.value;
34
+ *
35
+ * @throws {TiffFormatError} When the tag is not a well-formed `<GDALMetadata>`
36
+ * document.
37
+ */
38
+ export function readTiffGdalMetadata(image) {
39
+ const field = image.fields.get(TiffTag.GDAL_METADATA);
40
+ if (field === undefined) {
41
+ return undefined;
42
+ }
43
+ let root;
44
+ try {
45
+ root = xmlDocumentRoot(parseXml(tiffFieldText(field).join(""), {
46
+ maxEntityExpansion: 0,
47
+ maxDepth: 16,
48
+ }));
49
+ }
50
+ catch (error) {
51
+ if (error instanceof XmlSyntaxError || error instanceof TypeError) {
52
+ throw new TiffFormatError(`GDAL_METADATA is not XML (${error.message})`, image.offset);
53
+ }
54
+ throw error;
55
+ }
56
+ if (root.name.local !== "GDALMetadata") {
57
+ throw new TiffFormatError(`GDAL_METADATA rooted at <${root.name.local}>`, image.offset);
58
+ }
59
+ const items = [];
60
+ for (const child of root.children) {
61
+ if (child.kind !== "element" || child.name.local !== "Item") {
62
+ continue;
63
+ }
64
+ const name = attribute_(child, "name");
65
+ if (name === undefined) {
66
+ continue;
67
+ }
68
+ const sampleText = attribute_(child, "sample");
69
+ const sample = sampleText !== undefined && /^\d+$/u.test(sampleText)
70
+ ? Number(sampleText)
71
+ : undefined;
72
+ items.push({
73
+ name,
74
+ value: text_(child.children),
75
+ sample,
76
+ role: attribute_(child, "role"),
77
+ domain: attribute_(child, "domain"),
78
+ });
79
+ }
80
+ return items;
81
+ }
82
+ //# sourceMappingURL=read-tiff-gdal-metadata.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"read-tiff-gdal-metadata.js","sourceRoot":"","sources":["../../src/container/read-tiff-gdal-metadata.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,QAAQ,EACR,cAAc,EACd,eAAe,GAGf,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAgB/C,SAAS,UAAU,CAAC,OAAmB,EAAE,IAAY;IACpD,OAAO,OAAO,CAAC,UAAU,CAAC,IAAI,CAC7B,CAAC,SAAS,EAAE,EAAE,CACb,SAAS,CAAC,IAAI,CAAC,MAAM,KAAK,IAAI,IAAI,SAAS,CAAC,IAAI,CAAC,KAAK,KAAK,IAAI,CAChE,EAAE,KAAK,CAAC;AACV,CAAC;AAED,SAAS,KAAK,CAAC,KAAyB;IACvC,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,IAAI,IAAI,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C;IACzC,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;QACnC,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,KAAK,MAAM,EAAE,CAAC;YAC7D,SAAS;QACV,CAAC;QACD,MAAM,IAAI,GAAG,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;QACvC,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,SAAS;QACV,CAAC;QACD,MAAM,UAAU,GAAG,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;QAC/C,MAAM,MAAM,GACX,UAAU,KAAK,SAAS,IAAI,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC;YACpD,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC;YACpB,CAAC,CAAC,SAAS,CAAC;QACd,KAAK,CAAC,IAAI,CAAC;YACV,IAAI;YACJ,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,QAAQ,CAAC;YAC5B,MAAM;YACN,IAAI,EAAE,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC;YAC/B,MAAM,EAAE,UAAU,CAAC,KAAK,EAAE,QAAQ,CAAC;SACnC,CAAC,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC"}
@@ -0,0 +1,119 @@
1
+ import type { TiffField } from "../field/tiff-field.js";
2
+ import type { TiffCompression, TiffPredictor, TiffSampleFormat } from "../field/tiff-tag.js";
3
+ /**
4
+ * The limits {@link openTiff} and the readers hold a file to. A file declares
5
+ * its own counts and sizes, so each is checked against these before the work or
6
+ * the read it would cause.
7
+ */
8
+ export type TiffLimits = {
9
+ /** Most IFDs read, SubIFDs included. */
10
+ readonly maxImages: number;
11
+ /**
12
+ * Most bytes of IFD entries and tag values read, summed over the file: the
13
+ * strip and tile offset tables are tag values, so this bounds the chunk count
14
+ * too.
15
+ */
16
+ readonly maxTagBytes: number;
17
+ /** Largest stored strip or tile, in bytes, read to decode it. */
18
+ readonly maxTileBytes: number;
19
+ /**
20
+ * Largest decoded strip or tile, in bytes: the rows it holds times their byte
21
+ * length. A decoder never produces more than this.
22
+ */
23
+ readonly maxDecodedBytes: number;
24
+ };
25
+ /** A TIFF, BigTIFF or COG file {@link openTiff} read the structure of. */
26
+ export type Tiff = {
27
+ /** Whether the file is little-endian (`II`) rather than big-endian (`MM`). */
28
+ readonly littleEndian: boolean;
29
+ /** Whether it is a BigTIFF (version 43, 64-bit offsets). */
30
+ readonly bigTiff: boolean;
31
+ /** The file's length in bytes. */
32
+ readonly size: number;
33
+ /**
34
+ * Every image of the IFD chain, in file order; a COG's full-resolution image
35
+ * first, then its overviews and masks. SubIFDs are under their parent's
36
+ * {@link TiffImage.subImages}.
37
+ */
38
+ readonly images: readonly TiffImage[];
39
+ /**
40
+ * GDAL's "ghost area" after the header, as `KEY=VALUE` pairs (`LAYOUT`,
41
+ * `BLOCK_ORDER`, `BLOCK_LEADER`, `BLOCK_TRAILER`, …) when the file has one,
42
+ * which a COG written by GDAL does.
43
+ */
44
+ readonly structuralMetadata: ReadonlyMap<string, string> | undefined;
45
+ };
46
+ /**
47
+ * One image: an IFD with every tag raw in {@link fields}, the baseline ones
48
+ * decoded with their TIFF 6.0 defaults, and its place among the overviews and
49
+ * masks.
50
+ *
51
+ * Overviews and masks are grouped as GDAL writes and reads them: an image that
52
+ * is neither reduced-resolution nor a mask starts a group, the reduced-
53
+ * resolution images after it in the chain are its overviews, and a mask
54
+ * (`NewSubfileType` bit 2) is the mask of the group's image of its size. The
55
+ * reduced-resolution images among an image's SubIFDs are its overviews too.
56
+ */
57
+ export type TiffImage = {
58
+ /** The offset of its IFD in the file. */
59
+ readonly offset: number;
60
+ /** Every entry of the IFD, by tag; the first of a duplicated tag. */
61
+ readonly fields: ReadonlyMap<number, TiffField>;
62
+ readonly width: number;
63
+ readonly height: number;
64
+ readonly samplesPerPixel: number;
65
+ /** Bits of each sample, one per sample (`BitsPerSample`, default 1). */
66
+ readonly bitsPerSample: readonly number[];
67
+ /** Format of each sample, one per sample (`SampleFormat`, default 1). */
68
+ readonly sampleFormat: readonly TiffSampleFormat[];
69
+ /** `Compression`, 1 by default. */
70
+ readonly compression: TiffCompression;
71
+ /** `Predictor`, 1 by default. */
72
+ readonly predictor: TiffPredictor;
73
+ /** `PhotometricInterpretation`, which has no default. */
74
+ readonly photometric: number | undefined;
75
+ /** Whether each sample has its own planes (`PlanarConfiguration` 2). */
76
+ readonly planar: boolean;
77
+ /** `FillOrder`: 1 (most significant bit first, the default) or 2. */
78
+ readonly fillOrder: number;
79
+ /** `ExtraSamples`, empty by default. */
80
+ readonly extraSamples: readonly number[];
81
+ /** `NewSubfileType`, or the flags `SubfileType` stands for. */
82
+ readonly newSubfileType: number;
83
+ /** `NewSubfileType` bit 0: a reduced-resolution version of another image. */
84
+ readonly isReducedResolution: boolean;
85
+ /** `NewSubfileType` bit 1: one page of a multi-page document. */
86
+ readonly isPage: boolean;
87
+ /** `NewSubfileType` bit 2: a transparency mask for another image. */
88
+ readonly isMask: boolean;
89
+ /** Whether the data is in tiles rather than strips. */
90
+ readonly tiled: boolean;
91
+ /** Width of a tile, or the image's width for strips. */
92
+ readonly chunkWidth: number;
93
+ /** Height of a tile, or `RowsPerStrip` (at most the image's height). */
94
+ readonly chunkHeight: number;
95
+ /** Tiles across, or 1 for strips. */
96
+ readonly chunksAcross: number;
97
+ /** Tiles down, or the strip count of one plane. */
98
+ readonly chunksDown: number;
99
+ /** `GDAL_NODATA` as written, when present. */
100
+ readonly noData: string | undefined;
101
+ /**
102
+ * {@link noData} as a number: `nan`, `inf` and `-inf` as GDAL writes them, any
103
+ * other text as `Number` reads it; `undefined` when absent or not a number. A
104
+ * 64-bit integer value may round: parse {@link noData} with `BigInt` for it.
105
+ */
106
+ readonly noDataValue: number | undefined;
107
+ /** Its overviews, largest first. */
108
+ readonly overviews: readonly TiffImage[];
109
+ /** Its transparency mask, when it has one. */
110
+ readonly mask: TiffImage | undefined;
111
+ /** The images its `SubIFDs` tag points to, in order. */
112
+ readonly subImages: readonly TiffImage[];
113
+ /**
114
+ * The image an overview, a mask or a SubIFD belongs to; `undefined` for an
115
+ * image that starts a group.
116
+ */
117
+ readonly parent: TiffImage | undefined;
118
+ };
119
+ //# sourceMappingURL=tiff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tiff.d.ts","sourceRoot":"","sources":["../../src/container/tiff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,KAAK,EACX,eAAe,EACf,aAAa,EACb,gBAAgB,EAChB,MAAM,sBAAsB,CAAC;AAE9B;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG;IACxB,wCAAwC;IACxC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,iEAAiE;IACjE,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B;;;OAGG;IACH,QAAQ,CAAC,eAAe,EAAE,MAAM,CAAC;CACjC,CAAC;AAEF,0EAA0E;AAC1E,MAAM,MAAM,IAAI,GAAG;IAClB,8EAA8E;IAC9E,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAC/B,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,kCAAkC;IAClC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB;;;;OAIG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,CAAC;IACtC;;;;OAIG;IACH,QAAQ,CAAC,kBAAkB,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAAC;CACrE,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,MAAM,SAAS,GAAG;IACvB,yCAAyC;IACzC,QAAQ,CAAC,MAAM,EAAE,MAA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QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,8CAA8C;IAC9C,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IACpC;;;;OAIG;IACH,QAAQ,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IACzC,oCAAoC;IACpC,QAAQ,CAAC,SAAS,EAAE,SAAS,SAAS,EAAE,CAAC;IACzC,8CAA8C;IAC9C,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,SAAS,CAAC;IACrC,wDAAwD;IACxD,QAAQ,CAAC,SAAS,EAAE,SAAS,SAAS,EAAE,CAAC;IACzC;;;OAGG;IACH,QAAQ,CAAC,MAAM,EAAE,SAAS,GAAG,SAAS,CAAC;CACvC,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=tiff.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tiff.js","sourceRoot":"","sources":["../../src/container/tiff.ts"],"names":[],"mappings":""}
@@ -0,0 +1,34 @@
1
+ import { UnsupportedError } from "@ac-kit/core";
2
+ /**
3
+ * A TIFF file whose structure is broken or contradicts itself: not a TIFF
4
+ * header, an IFD or a value past the end, a loop in the IFD chain, a required
5
+ * tag missing or out of range, a chunk that decodes to fewer bytes than its
6
+ * rows hold, or a read that gave fewer bytes than asked.
7
+ */
8
+ export declare class TiffFormatError extends SyntaxError {
9
+ /** Byte offset in the file where reading gave up. */
10
+ readonly position: number;
11
+ constructor(message: string,
12
+ /** Byte offset in the file where reading gave up. */
13
+ position: number);
14
+ }
15
+ /**
16
+ * A file past one of the {@link TiffLimits} given: more images, more tag bytes,
17
+ * a larger stored chunk or a larger decoded chunk than allowed.
18
+ */
19
+ export declare class TiffLimitError extends RangeError {
20
+ constructor(message: string);
21
+ }
22
+ /**
23
+ * A valid file using what this reader does not decode: a compression with no
24
+ * built-in or caller decoder (`code` is the `Compression` value), a predictor
25
+ * other than 1, 2 and 3, or a sample layout outside TIFF 6.0's.
26
+ */
27
+ export declare class TiffUnsupportedError extends UnsupportedError {
28
+ /** The tag value naming what is unsupported, when there is one. */
29
+ readonly code?: number | undefined;
30
+ constructor(message: string,
31
+ /** The tag value naming what is unsupported, when there is one. */
32
+ code?: number | undefined);
33
+ }
34
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD;;;;;GAKG;AACH,qBAAa,eAAgB,SAAQ,WAAW;IAG9C,qDAAqD;IACrD,QAAQ,CAAC,QAAQ,EAAE,MAAM;IAH1B,YACC,OAAO,EAAE,MAAM;IACf,qDAAqD;IAC5C,QAAQ,EAAE,MAAM,EAIzB;CACD;AAED;;;GAGG;AACH,qBAAa,cAAe,SAAQ,UAAU;IAC7C,YAAY,OAAO,EAAE,MAAM,EAG1B;CACD;AAED;;;;GAIG;AACH,qBAAa,oBAAqB,SAAQ,gBAAgB;IAGxD,mEAAmE;IACnE,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM;IAHvB,YACC,OAAO,EAAE,MAAM;IACf,mEAAmE;IAC1D,IAAI,CAAC,EAAE,MAAM,YAAA,EAItB;CACD"}
package/dist/errors.js ADDED
@@ -0,0 +1,43 @@
1
+ import { UnsupportedError } from "@ac-kit/core";
2
+ /**
3
+ * A TIFF file whose structure is broken or contradicts itself: not a TIFF
4
+ * header, an IFD or a value past the end, a loop in the IFD chain, a required
5
+ * tag missing or out of range, a chunk that decodes to fewer bytes than its
6
+ * rows hold, or a read that gave fewer bytes than asked.
7
+ */
8
+ export class TiffFormatError extends SyntaxError {
9
+ position;
10
+ constructor(message,
11
+ /** Byte offset in the file where reading gave up. */
12
+ position) {
13
+ super(`${message} at ${position}`);
14
+ this.position = position;
15
+ this.name = "TiffFormatError";
16
+ }
17
+ }
18
+ /**
19
+ * A file past one of the {@link TiffLimits} given: more images, more tag bytes,
20
+ * a larger stored chunk or a larger decoded chunk than allowed.
21
+ */
22
+ export class TiffLimitError extends RangeError {
23
+ constructor(message) {
24
+ super(message);
25
+ this.name = "TiffLimitError";
26
+ }
27
+ }
28
+ /**
29
+ * A valid file using what this reader does not decode: a compression with no
30
+ * built-in or caller decoder (`code` is the `Compression` value), a predictor
31
+ * other than 1, 2 and 3, or a sample layout outside TIFF 6.0's.
32
+ */
33
+ export class TiffUnsupportedError extends UnsupportedError {
34
+ code;
35
+ constructor(message,
36
+ /** The tag value naming what is unsupported, when there is one. */
37
+ code) {
38
+ super(message);
39
+ this.code = code;
40
+ this.name = "TiffUnsupportedError";
41
+ }
42
+ }
43
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEhD;;;;;GAKG;AACH,MAAM,OAAO,eAAgB,SAAQ,WAAW;IAIrC,QAAQ;IAHlB,YACC,OAAe;IACf,qDAAqD;IAC5C,QAAgB;QAEzB,KAAK,CAAC,GAAG,OAAO,OAAO,QAAQ,EAAE,CAAC,CAAC;wBAF1B,QAAQ;QAGjB,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAC/B,CAAC;CACD;AAED;;;GAGG;AACH,MAAM,OAAO,cAAe,SAAQ,UAAU;IAC7C,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC9B,CAAC;CACD;AAED;;;;GAIG;AACH,MAAM,OAAO,oBAAqB,SAAQ,gBAAgB;IAI/C,IAAI;IAHd,YACC,OAAe;IACf,mEAAmE;IAC1D,IAAa;QAEtB,KAAK,CAAC,OAAO,CAAC,CAAC;oBAFN,IAAI;QAGb,IAAI,CAAC,IAAI,GAAG,sBAAsB,CAAC;IACpC,CAAC;CACD"}
@@ -0,0 +1,10 @@
1
+ import type { TiffField } from "./tiff-field.js";
2
+ export declare function fieldView_(field: TiffField): DataView;
3
+ /** How many whole values of its type the field's bytes hold. */
4
+ export declare function fieldValueCount_(field: TiffField): number;
5
+ /**
6
+ * Reads value `index` of `field` as a number, `NaN` when its type holds no
7
+ * number. A 64-bit integer past `Number.MAX_SAFE_INTEGER` rounds.
8
+ */
9
+ export declare function fieldNumberAt_(field: TiffField, index: number): number;
10
+ //# sourceMappingURL=_field-values.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_field-values.d.ts","sourceRoot":"","sources":["../../src/field/_field-values.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAEjD,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,GAAG,QAAQ,CAMrD;AAED,gEAAgE;AAChE,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,CAIzD;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAuCtE"}
@@ -0,0 +1,51 @@
1
+ import { TiffFieldType, tiffFieldTypeSize } from "./tiff-field-type.js";
2
+ export function fieldView_(field) {
3
+ return new DataView(field.bytes.buffer, field.bytes.byteOffset, field.bytes.byteLength);
4
+ }
5
+ /** How many whole values of its type the field's bytes hold. */
6
+ export function fieldValueCount_(field) {
7
+ const size = tiffFieldTypeSize(field.type);
8
+ const quotient = size === 0 ? 0 : field.bytes.length / size;
9
+ return Math.floor(quotient);
10
+ }
11
+ /**
12
+ * Reads value `index` of `field` as a number, `NaN` when its type holds no
13
+ * number. A 64-bit integer past `Number.MAX_SAFE_INTEGER` rounds.
14
+ */
15
+ export function fieldNumberAt_(field, index) {
16
+ const data = fieldView_(field);
17
+ const little = field.littleEndian;
18
+ switch (field.type) {
19
+ case TiffFieldType.BYTE:
20
+ case TiffFieldType.UNDEFINED:
21
+ return data.getUint8(index);
22
+ case TiffFieldType.SBYTE:
23
+ return data.getInt8(index);
24
+ case TiffFieldType.SHORT:
25
+ return data.getUint16(index * 2, little);
26
+ case TiffFieldType.SSHORT:
27
+ return data.getInt16(index * 2, little);
28
+ case TiffFieldType.LONG:
29
+ case TiffFieldType.IFD:
30
+ return data.getUint32(index * 4, little);
31
+ case TiffFieldType.SLONG:
32
+ return data.getInt32(index * 4, little);
33
+ case TiffFieldType.FLOAT:
34
+ return data.getFloat32(index * 4, little);
35
+ case TiffFieldType.DOUBLE:
36
+ return data.getFloat64(index * 8, little);
37
+ case TiffFieldType.RATIONAL:
38
+ return (data.getUint32(index * 8, little) /
39
+ data.getUint32(index * 8 + 4, little));
40
+ case TiffFieldType.SRATIONAL:
41
+ return (data.getInt32(index * 8, little) / data.getInt32(index * 8 + 4, little));
42
+ case TiffFieldType.LONG8:
43
+ case TiffFieldType.IFD8:
44
+ return Number(data.getBigUint64(index * 8, little));
45
+ case TiffFieldType.SLONG8:
46
+ return Number(data.getBigInt64(index * 8, little));
47
+ default:
48
+ return Number.NaN;
49
+ }
50
+ }
51
+ //# sourceMappingURL=_field-values.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_field-values.js","sourceRoot":"","sources":["../../src/field/_field-values.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAGxE,MAAM,UAAU,UAAU,CAAC,KAAgB;IAC1C,OAAO,IAAI,QAAQ,CAClB,KAAK,CAAC,KAAK,CAAC,MAAM,EAClB,KAAK,CAAC,KAAK,CAAC,UAAU,EACtB,KAAK,CAAC,KAAK,CAAC,UAAU,CACtB,CAAC;AACH,CAAC;AAED,gEAAgE;AAChE,MAAM,UAAU,gBAAgB,CAAC,KAAgB;IAChD,MAAM,IAAI,GAAG,iBAAiB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC3C,MAAM,QAAQ,GAAG,IAAI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