@d0paminedriven/fs 10.0.0 → 11.0.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/dist/docs/index.d.ts +119 -0
- package/dist/docs/index.js +1027 -0
- package/dist/extract/client.d.ts +54 -0
- package/dist/extract/client.js +118 -0
- package/dist/extract/index.d.ts +69 -0
- package/dist/extract/index.js +622 -0
- package/dist/fs/index.d.ts +19 -55
- package/dist/fs/index.js +88 -19
- package/dist/fs-atomic/index.d.ts +28 -36
- package/dist/fs-atomic/index.js +88 -13
- package/dist/fs-base/index.d.ts +105 -111
- package/dist/fs-base/index.js +188 -12
- package/dist/fs-core/index.d.ts +20 -24
- package/dist/fs-core/index.js +44 -16
- package/dist/fs-fetch/index.d.ts +48 -62
- package/dist/fs-fetch/index.js +146 -18
- package/dist/fs-size/index.d.ts +16 -28
- package/dist/fs-size/index.js +64 -15
- package/dist/fs-tmp/index.d.ts +179 -192
- package/dist/fs-tmp/index.js +321 -17
- package/dist/images/index.d.ts +447 -0
- package/dist/images/index.js +899 -0
- package/dist/images/workup.d.ts +30 -0
- package/dist/images/workup.js +317 -0
- package/dist/index.d.ts +20 -22
- package/dist/index.js +20 -64
- package/dist/ld/index.d.ts +15 -13
- package/dist/ld/index.js +56 -6
- package/dist/mime/index.d.ts +299 -294
- package/dist/mime/index.js +348 -13
- package/dist/mixins/index.d.ts +34 -0
- package/dist/mixins/index.js +54 -0
- package/dist/types/index.d.ts +279 -215
- package/dist/types/index.js +11 -6
- package/dist/types/stream.d.ts +6 -4
- package/dist/types/stream.js +1 -1
- package/dist/url/index.d.ts +15 -18
- package/dist/url/index.js +21 -8
- package/dist/utils/index.d.ts +19 -23
- package/dist/utils/index.js +82 -7
- package/package.json +33 -13
- package/dist/chunk-5CACVZVF.js +0 -386
- package/dist/chunk-5H6GE5AO.js +0 -14
- package/dist/chunk-7JQ56XIH.js +0 -187
- package/dist/chunk-7LOAFIZM.js +0 -77
- package/dist/chunk-7QVYU63E.js +0 -7
- package/dist/chunk-DNR5ZI2T.js +0 -75
- package/dist/chunk-L2QMQOEJ.js +0 -366
- package/dist/chunk-NJHN5OE3.js +0 -33
- package/dist/chunk-OHKRQJZH.js +0 -82
- package/dist/chunk-OT5JEQKW.js +0 -139
- package/dist/chunk-PATMSIQK.js +0 -367
- package/dist/chunk-PB5A7ZFD.js +0 -79
- package/dist/chunk-VDKUXJE7.js +0 -240
- package/dist/chunk-Y5QWE4EG.js +0 -1335
- package/dist/chunk-YRCJWFZD.js +0 -107
- package/dist/image/index.d.ts +0 -182
- package/dist/image/index.js +0 -13
- package/dist/image/workup.d.ts +0 -40
- package/dist/image/workup.js +0 -12
|
@@ -0,0 +1,899 @@
|
|
|
1
|
+
import { ImgMetadataExtractorWorkup } from "./workup.js";
|
|
2
|
+
import { createReadStream } from "node:fs";
|
|
3
|
+
import { relative } from "node:path";
|
|
4
|
+
import { inflateSync } from "fflate";
|
|
5
|
+
|
|
6
|
+
//#region src/images/index.ts
|
|
7
|
+
var ImgMetadataExtractor = class extends ImgMetadataExtractorWorkup {
|
|
8
|
+
constructor() {
|
|
9
|
+
super();
|
|
10
|
+
}
|
|
11
|
+
png(buffer) {
|
|
12
|
+
if (buffer.readUInt32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) throw new Error("IHDR Chunk of png not found or incorrect.");
|
|
13
|
+
const colorType = buffer[25];
|
|
14
|
+
let colorModel = "unknown", colorSpace = "unknown", hasAlpha = false;
|
|
15
|
+
switch (colorType) {
|
|
16
|
+
case 0:
|
|
17
|
+
colorModel = "grayscale";
|
|
18
|
+
colorSpace = "gray";
|
|
19
|
+
break;
|
|
20
|
+
case 2:
|
|
21
|
+
colorModel = "rgb";
|
|
22
|
+
colorSpace = "srgb";
|
|
23
|
+
break;
|
|
24
|
+
case 3:
|
|
25
|
+
colorModel = "indexed";
|
|
26
|
+
colorSpace = "srgb";
|
|
27
|
+
break;
|
|
28
|
+
case 4:
|
|
29
|
+
colorModel = "grayscale-alpha";
|
|
30
|
+
colorSpace = "gray";
|
|
31
|
+
hasAlpha = true;
|
|
32
|
+
break;
|
|
33
|
+
case 6:
|
|
34
|
+
colorModel = "rgba";
|
|
35
|
+
colorSpace = "srgb";
|
|
36
|
+
hasAlpha = true;
|
|
37
|
+
break;
|
|
38
|
+
default: colorModel = "unknown";
|
|
39
|
+
}
|
|
40
|
+
const width = buffer.readUInt32BE(16);
|
|
41
|
+
const height = buffer.readUInt32BE(20);
|
|
42
|
+
let frames = 1;
|
|
43
|
+
let animated = false;
|
|
44
|
+
let iccProfile = null;
|
|
45
|
+
let exifDateTimeOriginal = null;
|
|
46
|
+
let orientation = null;
|
|
47
|
+
let metadata = {};
|
|
48
|
+
let pos = 33;
|
|
49
|
+
while (pos < buffer.length - 12) {
|
|
50
|
+
const chunkLen = buffer.readUInt32BE(pos);
|
|
51
|
+
const chunkType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
52
|
+
const chunkData = pos + 8;
|
|
53
|
+
const nextPos = pos + 12 + chunkLen;
|
|
54
|
+
if (chunkType === "acTL") {
|
|
55
|
+
animated = true;
|
|
56
|
+
frames = buffer.readUInt32BE(chunkData);
|
|
57
|
+
} else if (chunkType === "iCCP") {
|
|
58
|
+
const nameEnd = buffer.indexOf(0, chunkData);
|
|
59
|
+
const profileName = buffer.toString("ascii", chunkData, nameEnd);
|
|
60
|
+
iccProfile = profileName || "embedded";
|
|
61
|
+
colorSpace = this.mapProfileToColorSpace(profileName, colorSpace);
|
|
62
|
+
} else if (chunkType === "sRGB") {
|
|
63
|
+
if (iccProfile === null) {
|
|
64
|
+
iccProfile = "sRGB";
|
|
65
|
+
colorSpace = "srgb";
|
|
66
|
+
}
|
|
67
|
+
} else if (chunkType === "cHRM") {
|
|
68
|
+
if (iccProfile === null && colorModel !== "grayscale" && colorModel !== "grayscale-alpha") {
|
|
69
|
+
const white_x = buffer.readUInt32BE(chunkData), white_y = buffer.readUInt32BE(chunkData + 4), red_x = buffer.readUInt32BE(chunkData + 8), red_y = buffer.readUInt32BE(chunkData + 12), green_x = buffer.readUInt32BE(chunkData + 16), green_y = buffer.readUInt32BE(chunkData + 20), blue_x = buffer.readUInt32BE(chunkData + 24), blue_y = buffer.readUInt32BE(chunkData + 28);
|
|
70
|
+
colorSpace = this.mapChrmToColorSpace({
|
|
71
|
+
white_x,
|
|
72
|
+
white_y,
|
|
73
|
+
red_x,
|
|
74
|
+
red_y,
|
|
75
|
+
green_x,
|
|
76
|
+
green_y,
|
|
77
|
+
blue_x,
|
|
78
|
+
blue_y
|
|
79
|
+
}, colorSpace);
|
|
80
|
+
}
|
|
81
|
+
} else if (chunkType === "iTXt") {
|
|
82
|
+
let offset = chunkData;
|
|
83
|
+
const keywordEnd = buffer.indexOf(0, offset);
|
|
84
|
+
if (keywordEnd === -1) {
|
|
85
|
+
pos = nextPos;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const keyword = buffer.toString("ascii", offset, keywordEnd);
|
|
89
|
+
offset = keywordEnd + 1;
|
|
90
|
+
const compressionFlag = buffer[offset];
|
|
91
|
+
buffer[offset + 1];
|
|
92
|
+
offset += 2;
|
|
93
|
+
const langEnd = buffer.indexOf(0, offset);
|
|
94
|
+
if (langEnd === -1) {
|
|
95
|
+
pos = nextPos;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
offset = langEnd + 1;
|
|
99
|
+
const transEnd = buffer.indexOf(0, offset);
|
|
100
|
+
if (transEnd === -1) {
|
|
101
|
+
pos = nextPos;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
offset = transEnd + 1;
|
|
105
|
+
let textBuffer = buffer.subarray(offset, chunkData + chunkLen);
|
|
106
|
+
if (compressionFlag === 1) try {
|
|
107
|
+
textBuffer = Buffer.from(inflateSync(textBuffer));
|
|
108
|
+
} catch (e) {
|
|
109
|
+
console.error("Failed to decompress iTXt:", e);
|
|
110
|
+
pos = nextPos;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const text = textBuffer.toString("utf8");
|
|
114
|
+
metadata[keyword] = text;
|
|
115
|
+
if (keyword === "XML:com.adobe.xmp" && !iccProfile) colorSpace = this.mapXmpToColorSpace(text, colorSpace);
|
|
116
|
+
if (keyword === "Creation Time" && !exifDateTimeOriginal) exifDateTimeOriginal = text.trim();
|
|
117
|
+
} else if (chunkType === "zTXt") {
|
|
118
|
+
const chunkStart = chunkData;
|
|
119
|
+
const chunkEnd = chunkData + chunkLen;
|
|
120
|
+
let offset = chunkStart;
|
|
121
|
+
const keywordEnd = buffer.indexOf(0, offset);
|
|
122
|
+
if (keywordEnd === -1 || keywordEnd >= chunkEnd) {
|
|
123
|
+
pos = nextPos;
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
const keyword = buffer.toString("latin1", offset, keywordEnd);
|
|
127
|
+
offset = keywordEnd + 1;
|
|
128
|
+
if (offset >= chunkEnd) {
|
|
129
|
+
pos = nextPos;
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const compressionMethod = buffer[offset];
|
|
133
|
+
offset += 1;
|
|
134
|
+
if (offset >= chunkEnd) {
|
|
135
|
+
pos = nextPos;
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
if (chunkEnd - offset <= 0) {
|
|
139
|
+
pos = nextPos;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
let textBuffer = buffer.subarray(offset, chunkEnd);
|
|
143
|
+
if (compressionMethod === 0) try {
|
|
144
|
+
if (textBuffer.length < 2) {
|
|
145
|
+
pos = nextPos;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const cmf = textBuffer[0];
|
|
149
|
+
const flg = textBuffer[1];
|
|
150
|
+
if ((cmf * 256 + flg) % 31 !== 0) {
|
|
151
|
+
pos = nextPos;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
textBuffer = Buffer.from(inflateSync(textBuffer));
|
|
155
|
+
} catch {
|
|
156
|
+
pos = nextPos;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
pos = nextPos;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
const text = textBuffer.toString("latin1");
|
|
164
|
+
metadata[keyword] = text;
|
|
165
|
+
if (keyword === "Creation Time" && !exifDateTimeOriginal) exifDateTimeOriginal = text.trim();
|
|
166
|
+
} else if (chunkType === "tIME") {
|
|
167
|
+
const month = buffer?.[chunkData + 2], day = buffer?.[chunkData + 3], hour = buffer?.[chunkData + 4], minute = buffer?.[chunkData + 5], second = buffer?.[chunkData + 6];
|
|
168
|
+
if (typeof month !== "undefined" && typeof day !== "undefined" && typeof hour !== "undefined" && typeof minute !== "undefined" && typeof second !== "undefined") exifDateTimeOriginal = `${buffer.readUInt16BE(chunkData)}:${month.toString().padStart(2, "0")}:${day.toString().padStart(2, "0")} ${hour.toString().padStart(2, "0")}:${minute.toString().padStart(2, "0")}:${second.toString().padStart(2, "0")}`;
|
|
169
|
+
} else if (chunkType === "eXIf") {
|
|
170
|
+
const { orientation: ori, dateTimeOriginal } = this.parseExif(buffer, chunkData - 4, chunkLen);
|
|
171
|
+
orientation = ori;
|
|
172
|
+
exifDateTimeOriginal = dateTimeOriginal ?? exifDateTimeOriginal;
|
|
173
|
+
} else if (chunkType === "IDAT") break;
|
|
174
|
+
pos = nextPos;
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
type: "IMAGE",
|
|
178
|
+
width,
|
|
179
|
+
height,
|
|
180
|
+
format: animated === true ? "apng" : "png",
|
|
181
|
+
frames,
|
|
182
|
+
animated,
|
|
183
|
+
hasAlpha,
|
|
184
|
+
colorModel,
|
|
185
|
+
orientation,
|
|
186
|
+
aspectRatio: width / height,
|
|
187
|
+
colorSpace,
|
|
188
|
+
iccProfile,
|
|
189
|
+
exifDateTimeOriginal,
|
|
190
|
+
metadata: Object.entries(metadata).length > 0 ? metadata : {}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
jpeg(buffer) {
|
|
194
|
+
let colorSpace = "srgb", hasAlpha = false;
|
|
195
|
+
let pos = 2, colorModel = "unknown", width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
|
|
196
|
+
while (pos < buffer.length - 10) {
|
|
197
|
+
if (buffer[pos] !== 255) {
|
|
198
|
+
const nextFF = buffer.indexOf(255, pos + 1);
|
|
199
|
+
if (nextFF === -1 || nextFF >= buffer.length - 4) break;
|
|
200
|
+
pos = nextFF;
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
const marker = buffer[pos + 1];
|
|
204
|
+
if (marker === 218) break;
|
|
205
|
+
if (pos + 4 > buffer.length) break;
|
|
206
|
+
const segmentSize = buffer.readUInt16BE(pos + 2);
|
|
207
|
+
if (segmentSize < 2) {
|
|
208
|
+
pos += 2;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const segmentEnd = pos + 2 + segmentSize;
|
|
212
|
+
if (segmentEnd > buffer.length) {
|
|
213
|
+
if (marker && marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
214
|
+
if (pos + 9 <= buffer.length) {
|
|
215
|
+
switch (buffer[pos + 9]) {
|
|
216
|
+
case 1:
|
|
217
|
+
colorModel = "grayscale";
|
|
218
|
+
break;
|
|
219
|
+
case 3:
|
|
220
|
+
colorModel = "ycbcr";
|
|
221
|
+
break;
|
|
222
|
+
case 4:
|
|
223
|
+
colorModel = "ycck";
|
|
224
|
+
break;
|
|
225
|
+
default: colorModel = "unknown";
|
|
226
|
+
}
|
|
227
|
+
if (pos + 7 <= buffer.length - 2) width = buffer.readUInt16BE(pos + 7);
|
|
228
|
+
if (pos + 5 <= buffer.length - 2) height = buffer.readUInt16BE(pos + 5);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
if (marker && marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
234
|
+
switch (buffer[pos + 9]) {
|
|
235
|
+
case 1:
|
|
236
|
+
colorModel = "grayscale";
|
|
237
|
+
break;
|
|
238
|
+
case 3:
|
|
239
|
+
colorModel = "ycbcr";
|
|
240
|
+
break;
|
|
241
|
+
case 4:
|
|
242
|
+
colorModel = "ycck";
|
|
243
|
+
break;
|
|
244
|
+
default: colorModel = "unknown";
|
|
245
|
+
}
|
|
246
|
+
width = buffer.readUInt16BE(pos + 7);
|
|
247
|
+
height = buffer.readUInt16BE(pos + 5);
|
|
248
|
+
} else if (marker === 225) {
|
|
249
|
+
const { orientation: ori, dateTimeOriginal } = this.parseExif(buffer, pos, segmentSize);
|
|
250
|
+
orientation = ori;
|
|
251
|
+
exifDateTimeOriginal = dateTimeOriginal;
|
|
252
|
+
} else if (marker === 226) {
|
|
253
|
+
if (pos + 18 <= buffer.length) {
|
|
254
|
+
if (buffer.toString("ascii", pos + 4, pos + 18).startsWith("ICC_PROFILE")) {
|
|
255
|
+
iccProfile = "embedded";
|
|
256
|
+
colorSpace = "unknown";
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
} else if (marker === 255) {
|
|
260
|
+
pos++;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
pos = segmentEnd;
|
|
264
|
+
while (pos < buffer.length && buffer[pos] === 0) pos++;
|
|
265
|
+
while (pos < buffer.length - 1 && buffer[pos] === 255 && buffer[pos + 1] === 255) pos++;
|
|
266
|
+
}
|
|
267
|
+
if (width === 0) throw new Error("No dimensions found in JPEG file");
|
|
268
|
+
return {
|
|
269
|
+
type: "IMAGE",
|
|
270
|
+
width,
|
|
271
|
+
height,
|
|
272
|
+
format: "jpeg",
|
|
273
|
+
frames: 1,
|
|
274
|
+
animated: false,
|
|
275
|
+
hasAlpha,
|
|
276
|
+
orientation,
|
|
277
|
+
aspectRatio: width / height,
|
|
278
|
+
colorSpace,
|
|
279
|
+
colorModel,
|
|
280
|
+
iccProfile,
|
|
281
|
+
exifDateTimeOriginal
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
gif(buffer) {
|
|
285
|
+
const width = buffer.readUInt16LE(6), height = buffer.readUInt16LE(8);
|
|
286
|
+
let frames = 0;
|
|
287
|
+
let pos = 13;
|
|
288
|
+
const bufTen = buffer?.[10];
|
|
289
|
+
if (typeof bufTen === "undefined") {
|
|
290
|
+
pos = 13;
|
|
291
|
+
frames = 0;
|
|
292
|
+
} else if (bufTen & 128) pos += 3 << (bufTen & 7) + 1;
|
|
293
|
+
while (pos < buffer.length) if (buffer[pos] === 33) {
|
|
294
|
+
pos += 2;
|
|
295
|
+
let subSize = buffer?.[pos];
|
|
296
|
+
while (subSize && subSize > 0) {
|
|
297
|
+
pos += subSize + 1;
|
|
298
|
+
subSize = buffer?.[pos];
|
|
299
|
+
}
|
|
300
|
+
pos++;
|
|
301
|
+
} else if (buffer?.[pos] && buffer?.[pos] === 44) {
|
|
302
|
+
frames++;
|
|
303
|
+
pos += 10;
|
|
304
|
+
const b = buffer?.[pos - 1];
|
|
305
|
+
if (typeof b !== "undefined" && b & 128) pos += 3 << (b & 7) + 1;
|
|
306
|
+
pos++;
|
|
307
|
+
let dataSize = buffer?.[pos];
|
|
308
|
+
while (dataSize && dataSize > 0) {
|
|
309
|
+
pos += dataSize + 1;
|
|
310
|
+
dataSize = buffer[pos];
|
|
311
|
+
}
|
|
312
|
+
pos++;
|
|
313
|
+
} else if (buffer[pos] === 59) break;
|
|
314
|
+
else pos++;
|
|
315
|
+
return {
|
|
316
|
+
type: "IMAGE",
|
|
317
|
+
width,
|
|
318
|
+
height,
|
|
319
|
+
format: "gif",
|
|
320
|
+
frames,
|
|
321
|
+
animated: frames > 1,
|
|
322
|
+
hasAlpha: null,
|
|
323
|
+
orientation: null,
|
|
324
|
+
aspectRatio: width / height,
|
|
325
|
+
colorModel: "indexed",
|
|
326
|
+
colorSpace: "unknown",
|
|
327
|
+
iccProfile: null,
|
|
328
|
+
exifDateTimeOriginal: null
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
bmp(buffer) {
|
|
332
|
+
const width = buffer.readInt32LE(18);
|
|
333
|
+
const height = Math.abs(buffer.readInt32LE(22));
|
|
334
|
+
const colorModel = buffer.readUInt16LE(28) <= 8 ? "indexed" : "rgb";
|
|
335
|
+
return {
|
|
336
|
+
type: "IMAGE",
|
|
337
|
+
width,
|
|
338
|
+
height,
|
|
339
|
+
format: "bmp",
|
|
340
|
+
frames: 1,
|
|
341
|
+
animated: false,
|
|
342
|
+
hasAlpha: null,
|
|
343
|
+
orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
|
|
344
|
+
aspectRatio: width / height,
|
|
345
|
+
colorModel,
|
|
346
|
+
colorSpace: "unknown",
|
|
347
|
+
iccProfile: null,
|
|
348
|
+
exifDateTimeOriginal: null
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
webp(buffer) {
|
|
352
|
+
const chunkType = buffer.toString("ascii", 12, 16);
|
|
353
|
+
let colorSpace = "srgb";
|
|
354
|
+
let colorModel = "unknown", hasAlpha = false, width = 0, height = 0, frames = 1, animated = false, iccProfile = null;
|
|
355
|
+
if (chunkType === "VP8X") {
|
|
356
|
+
const flags = buffer?.[20];
|
|
357
|
+
colorModel = flags ? flags & 2 ? "rgba" : "rgb" : "unknown";
|
|
358
|
+
hasAlpha = flags ? !!(flags & 2) : false;
|
|
359
|
+
animated = flags ? !!(flags & 1) : false;
|
|
360
|
+
let widthSubtractOne = 0;
|
|
361
|
+
let heightSubtractOne = 0;
|
|
362
|
+
if (buffer?.[24] && buffer?.[25] && buffer?.[26]) widthSubtractOne = buffer[24] | buffer[25] << 8 | buffer[26] << 16;
|
|
363
|
+
if (buffer?.[27] && buffer?.[28] && buffer?.[29]) heightSubtractOne = buffer[27] | buffer[28] << 8 | buffer[29] << 16;
|
|
364
|
+
width = widthSubtractOne + 1;
|
|
365
|
+
height = heightSubtractOne + 1;
|
|
366
|
+
let pos = 30;
|
|
367
|
+
if (animated) frames = 0;
|
|
368
|
+
while (pos < buffer.length - 8) {
|
|
369
|
+
const subChunkType = buffer.toString("ascii", pos, pos + 4);
|
|
370
|
+
const subChunkSize = buffer.readUInt32LE(pos + 4);
|
|
371
|
+
if (subChunkType === "ICCP") {
|
|
372
|
+
iccProfile = "embedded";
|
|
373
|
+
colorSpace = "unknown";
|
|
374
|
+
} else if (subChunkType === "ANMF" && animated) frames++;
|
|
375
|
+
pos += 8 + subChunkSize + subChunkSize % 2;
|
|
376
|
+
}
|
|
377
|
+
} else if (chunkType === "VP8 ") {
|
|
378
|
+
colorModel = "rgb";
|
|
379
|
+
hasAlpha = false;
|
|
380
|
+
const dataStart = 20;
|
|
381
|
+
if (buffer?.[dataStart + 3] !== 157 || buffer?.[dataStart + 4] !== 1 || buffer?.[dataStart + 5] !== 42) throw new Error("Invalid VP8 keyframe");
|
|
382
|
+
width = buffer.readUInt16LE(dataStart + 6) & 16383;
|
|
383
|
+
height = buffer.readUInt16LE(dataStart + 8) & 16383;
|
|
384
|
+
} else if (chunkType === "VP8L") {
|
|
385
|
+
const dataStart = 20;
|
|
386
|
+
if (buffer?.[dataStart] !== 47) throw new Error("Invalid VP8L signature");
|
|
387
|
+
const bits = buffer.readUInt32LE(dataStart + 1);
|
|
388
|
+
if (bits >>> 29 !== 0) throw new Error("Invalid VP8L version");
|
|
389
|
+
colorModel = bits & 256 ? "rgba" : "rgb";
|
|
390
|
+
hasAlpha = !!(bits & 256);
|
|
391
|
+
width = 1 + (bits & 16383);
|
|
392
|
+
height = 1 + (bits >> 14 & 16383);
|
|
393
|
+
} else throw new Error("Unsupported WebP chunk");
|
|
394
|
+
return {
|
|
395
|
+
type: "IMAGE",
|
|
396
|
+
width,
|
|
397
|
+
height,
|
|
398
|
+
format: "webp",
|
|
399
|
+
frames,
|
|
400
|
+
animated,
|
|
401
|
+
hasAlpha,
|
|
402
|
+
colorModel,
|
|
403
|
+
orientation: null,
|
|
404
|
+
aspectRatio: width / height,
|
|
405
|
+
colorSpace,
|
|
406
|
+
iccProfile,
|
|
407
|
+
exifDateTimeOriginal: null
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
avif(buffer) {
|
|
411
|
+
const ftyp = this.findBox(buffer, "ftyp");
|
|
412
|
+
if (!ftyp) throw new Error("Invalid AVIF: No ftyp");
|
|
413
|
+
const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size);
|
|
414
|
+
const isAvif = brands.includes("avif");
|
|
415
|
+
const isAvis = brands.includes("avis");
|
|
416
|
+
if (!isAvif && !isAvis) throw new Error("Not an AVIF file");
|
|
417
|
+
const meta = this.findBox(buffer, "meta");
|
|
418
|
+
if (!meta) throw new Error("Invalid AVIF: No meta");
|
|
419
|
+
const metaSubStart = meta.pos + 4;
|
|
420
|
+
const metaSubEnd = meta.pos + meta.size;
|
|
421
|
+
const iprp = this.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
|
|
422
|
+
if (!iprp) throw new Error("Invalid AVIF: No iprp");
|
|
423
|
+
const ipco = this.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
|
|
424
|
+
if (!ipco) throw new Error("Invalid AVIF: No ipco");
|
|
425
|
+
const ispe = this.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
|
|
426
|
+
if (!ispe) throw new Error("Invalid AVIF: No ispe");
|
|
427
|
+
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
428
|
+
const width = buffer.readUInt32BE(ispe.pos + 4);
|
|
429
|
+
const height = buffer.readUInt32BE(ispe.pos + 8);
|
|
430
|
+
let colorSpace = "rgb", colorModel = "rgb", hasAlpha = false, iccProfile = null;
|
|
431
|
+
const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
|
|
432
|
+
if (colr) {
|
|
433
|
+
const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
|
|
434
|
+
if (colrType === "nclx") colorModel = buffer.readUInt16BE(colr.pos + 6) === 2 ? "rgb" : "ycbcr";
|
|
435
|
+
else if (colrType === "rICC" || colrType === "prof") {
|
|
436
|
+
colorSpace = "unknown";
|
|
437
|
+
iccProfile = "embedded";
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
|
|
441
|
+
if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
|
|
442
|
+
hasAlpha = true;
|
|
443
|
+
if (colorModel === "rgb") colorModel = "rgba";
|
|
444
|
+
else if (colorModel === "ycbcr") colorModel = "ycck";
|
|
445
|
+
else if (colorModel === "grayscale" || colorModel === "unknown") colorModel = "grayscale-alpha";
|
|
446
|
+
}
|
|
447
|
+
const xmpXml = this.parseXmpFromAvif(buffer, meta, ipco);
|
|
448
|
+
let exifDateTimeOriginal = null;
|
|
449
|
+
if (xmpXml) {
|
|
450
|
+
let dateStart = xmpXml.indexOf("xmp:CreateDate=\"");
|
|
451
|
+
if (dateStart === -1) dateStart = xmpXml.indexOf("photoshop:DateCreated=\"");
|
|
452
|
+
if (dateStart !== -1) {
|
|
453
|
+
dateStart += 16;
|
|
454
|
+
const dateEnd = xmpXml.indexOf("\"", dateStart);
|
|
455
|
+
if (dateEnd !== -1) exifDateTimeOriginal = xmpXml.substring(dateStart, dateEnd);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
let frames = 1;
|
|
459
|
+
const animated = isAvis;
|
|
460
|
+
if (animated) {
|
|
461
|
+
const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
|
|
462
|
+
if (iloc) {
|
|
463
|
+
const version = buffer?.[iloc.pos] ?? 0;
|
|
464
|
+
const itemCountPos = iloc.pos + (version < 2 ? 4 : 6);
|
|
465
|
+
let itemCount = buffer?.readUInt16BE(itemCountPos) ?? 0;
|
|
466
|
+
if (version >= 2) itemCount = buffer?.readUInt32BE(itemCountPos) ?? 0;
|
|
467
|
+
frames = Math.max(1, Math.floor(itemCount / (hasAlpha ? 2 : 1)));
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
return {
|
|
471
|
+
type: "IMAGE",
|
|
472
|
+
width,
|
|
473
|
+
height,
|
|
474
|
+
format: "avif",
|
|
475
|
+
frames,
|
|
476
|
+
animated,
|
|
477
|
+
colorModel,
|
|
478
|
+
hasAlpha: hasAlpha ? true : null,
|
|
479
|
+
orientation: null,
|
|
480
|
+
aspectRatio: width / height,
|
|
481
|
+
colorSpace,
|
|
482
|
+
iccProfile,
|
|
483
|
+
exifDateTimeOriginal
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
heic(buffer) {
|
|
487
|
+
const ftyp = this.findBox(buffer, "ftyp");
|
|
488
|
+
if (!ftyp) throw new Error("Invalid HEIC: No ftyp");
|
|
489
|
+
const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size).toLowerCase();
|
|
490
|
+
if (!(brands.includes("heic") || brands.includes("heix") || brands.includes("heim") || brands.includes("heis") || brands.includes("hevc") || brands.includes("hevx") || brands.includes("mif1"))) throw new Error("Not a HEIC file");
|
|
491
|
+
const meta = this.findBox(buffer, "meta");
|
|
492
|
+
if (!meta) throw new Error("Invalid HEIC: No meta");
|
|
493
|
+
const metaSubStart = meta.pos + 4;
|
|
494
|
+
const metaSubEnd = meta.pos + meta.size;
|
|
495
|
+
const iprp = this.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
|
|
496
|
+
if (!iprp) throw new Error("Invalid HEIC: No iprp");
|
|
497
|
+
const ipco = this.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
|
|
498
|
+
if (!ipco) throw new Error("Invalid HEIC: No ipco");
|
|
499
|
+
const ispe = this.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
|
|
500
|
+
if (!ispe) throw new Error("Invalid HEIC: No ispe");
|
|
501
|
+
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
502
|
+
const width = buffer.readUInt32BE(ispe.pos + 4);
|
|
503
|
+
const height = buffer.readUInt32BE(ispe.pos + 8);
|
|
504
|
+
let colorSpace = "rgb", colorModel = "rgb", hasAlpha = false, iccProfile = null;
|
|
505
|
+
const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
|
|
506
|
+
if (colr) {
|
|
507
|
+
const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
|
|
508
|
+
if (colrType === "nclx") colorModel = buffer.readUInt16BE(colr.pos + 6) === 2 ? "rgb" : "ycbcr";
|
|
509
|
+
else if (colrType === "rICC" || colrType === "prof") {
|
|
510
|
+
colorSpace = "unknown";
|
|
511
|
+
iccProfile = "embedded";
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
|
|
515
|
+
if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
|
|
516
|
+
hasAlpha = true;
|
|
517
|
+
if (colorModel === "rgb") colorModel = "rgba";
|
|
518
|
+
else if (colorModel === "ycbcr") colorModel = "ycck";
|
|
519
|
+
else if (colorModel === "grayscale" || colorModel === "unknown") colorModel = "grayscale-alpha";
|
|
520
|
+
}
|
|
521
|
+
const xmpXml = this.parseXmpFromAvif(buffer, meta, ipco);
|
|
522
|
+
let exifDateTimeOriginal = null;
|
|
523
|
+
if (xmpXml) {
|
|
524
|
+
let dateStart = xmpXml.indexOf("xmp:CreateDate=\"");
|
|
525
|
+
if (dateStart === -1) dateStart = xmpXml.indexOf("photoshop:DateCreated=\"");
|
|
526
|
+
if (dateStart !== -1) {
|
|
527
|
+
dateStart += 16;
|
|
528
|
+
const dateEnd = xmpXml.indexOf("\"", dateStart);
|
|
529
|
+
if (dateEnd !== -1) exifDateTimeOriginal = xmpXml.substring(dateStart, dateEnd);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
let frames = 1;
|
|
533
|
+
const animated = brands.includes("heim") || brands.includes("heis");
|
|
534
|
+
if (animated) {
|
|
535
|
+
const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
|
|
536
|
+
if (iloc) {
|
|
537
|
+
const version = buffer?.[iloc.pos] ?? 0;
|
|
538
|
+
const itemCountPos = iloc.pos + (version < 2 ? 4 : 6);
|
|
539
|
+
let itemCount = buffer?.readUInt16BE(itemCountPos) ?? 0;
|
|
540
|
+
if (version >= 2) itemCount = buffer?.readUInt32BE(itemCountPos) ?? 0;
|
|
541
|
+
frames = Math.max(1, Math.floor(itemCount / (hasAlpha ? 2 : 1)));
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
return {
|
|
545
|
+
type: "IMAGE",
|
|
546
|
+
width,
|
|
547
|
+
height,
|
|
548
|
+
format: "heic",
|
|
549
|
+
frames,
|
|
550
|
+
animated,
|
|
551
|
+
colorModel,
|
|
552
|
+
hasAlpha: hasAlpha ? true : null,
|
|
553
|
+
orientation: null,
|
|
554
|
+
aspectRatio: width / height,
|
|
555
|
+
colorSpace,
|
|
556
|
+
iccProfile,
|
|
557
|
+
exifDateTimeOriginal
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
svg(headerText) {
|
|
561
|
+
const widthMatch = headerText.match(/width\s*=\s*["']?(\d+(?:\.\d+)?)(?:px)?["']?/i);
|
|
562
|
+
const heightMatch = headerText.match(/height\s*=\s*["']?(\d+(?:\.\d+)?)(?:px)?["']?/i);
|
|
563
|
+
const viewBoxMatch = headerText.match(/viewBox\s*=\s*["']?(\d+(?:\.\d+)?\s+\d+(?:\.\d+)?\s+\d+(?:\.\d+)?\s+\d+(?:\.\d+)?)["']?/i);
|
|
564
|
+
let width = 0;
|
|
565
|
+
let height = 0;
|
|
566
|
+
if (widthMatch?.[1] && heightMatch?.[1]) {
|
|
567
|
+
width = parseFloat(widthMatch[1]);
|
|
568
|
+
height = parseFloat(heightMatch[1]);
|
|
569
|
+
} else if (viewBoxMatch) {
|
|
570
|
+
const [_x0, _y0, vbWidth, vbHeight] = viewBoxMatch?.[1]?.split(/\s+/).map((t) => Number.parseFloat(t));
|
|
571
|
+
width = vbWidth;
|
|
572
|
+
height = vbHeight;
|
|
573
|
+
} else {
|
|
574
|
+
width = 0;
|
|
575
|
+
height = 0;
|
|
576
|
+
throw new Error("SVG has no defined width/height or viewBox");
|
|
577
|
+
}
|
|
578
|
+
const animated = headerText.includes("<animate") || headerText.includes("<animation") || headerText.includes("<motion.");
|
|
579
|
+
return {
|
|
580
|
+
type: "IMAGE",
|
|
581
|
+
width,
|
|
582
|
+
height,
|
|
583
|
+
format: "svg",
|
|
584
|
+
frames: 1,
|
|
585
|
+
animated,
|
|
586
|
+
hasAlpha: true,
|
|
587
|
+
orientation: null,
|
|
588
|
+
aspectRatio: width / height || 1,
|
|
589
|
+
colorModel: "vector",
|
|
590
|
+
colorSpace: "srgb",
|
|
591
|
+
iccProfile: null,
|
|
592
|
+
exifDateTimeOriginal: null
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
ico(rawbuffer, buffer) {
|
|
596
|
+
const frames = buffer.readUInt16LE(4);
|
|
597
|
+
if (frames === 0) throw new Error("Invalid ICO: No images");
|
|
598
|
+
let maxWidth = 0;
|
|
599
|
+
let maxHeight = 0;
|
|
600
|
+
let hasAlpha = null;
|
|
601
|
+
let colorModel = "unknown";
|
|
602
|
+
for (let i = 0; i < frames; i++) {
|
|
603
|
+
const entryPos = 6 + i * 16;
|
|
604
|
+
if (entryPos + 16 > buffer.length) break;
|
|
605
|
+
let entryWidth = buffer[entryPos];
|
|
606
|
+
let entryHeight = buffer[entryPos + 1];
|
|
607
|
+
entryWidth = entryWidth === 0 ? 256 : entryWidth;
|
|
608
|
+
entryHeight = entryHeight === 0 ? 256 : entryHeight;
|
|
609
|
+
const bpp = buffer.readUInt16LE(entryPos + 6);
|
|
610
|
+
buffer.readUInt32LE(entryPos + 8);
|
|
611
|
+
const offset = buffer.readUInt32LE(entryPos + 12);
|
|
612
|
+
if (bpp <= 8) colorModel = "indexed";
|
|
613
|
+
else if (bpp === 24) colorModel = "rgb";
|
|
614
|
+
else if (bpp === 32) {
|
|
615
|
+
colorModel = "rgba";
|
|
616
|
+
hasAlpha = true;
|
|
617
|
+
}
|
|
618
|
+
if (offset + 8 <= rawbuffer.length && rawbuffer[offset] === 137 && rawbuffer[offset + 1] === 80 && rawbuffer[offset + 2] === 78 && rawbuffer[offset + 3] === 71 && rawbuffer[offset + 4] === 13 && rawbuffer[offset + 5] === 10 && rawbuffer[offset + 6] === 26 && rawbuffer[offset + 7] === 10) {
|
|
619
|
+
hasAlpha = true;
|
|
620
|
+
colorModel = "rgba";
|
|
621
|
+
}
|
|
622
|
+
if (entryWidth && entryWidth > maxWidth) maxWidth = entryWidth;
|
|
623
|
+
if (entryHeight && entryHeight > maxHeight) maxHeight = entryHeight;
|
|
624
|
+
}
|
|
625
|
+
return {
|
|
626
|
+
type: "IMAGE",
|
|
627
|
+
width: maxWidth,
|
|
628
|
+
height: maxHeight,
|
|
629
|
+
format: "ico",
|
|
630
|
+
frames,
|
|
631
|
+
animated: false,
|
|
632
|
+
hasAlpha,
|
|
633
|
+
orientation: null,
|
|
634
|
+
aspectRatio: maxWidth / maxHeight,
|
|
635
|
+
colorModel,
|
|
636
|
+
colorSpace: "srgb",
|
|
637
|
+
iccProfile: null,
|
|
638
|
+
exifDateTimeOriginal: null
|
|
639
|
+
};
|
|
640
|
+
}
|
|
641
|
+
tiff(rawbuffer, buffer) {
|
|
642
|
+
const little = buffer[0] === 73;
|
|
643
|
+
if (buffer[2] === 43 || buffer[3] === 43) throw new Error("BigTIFF (0x2B) detected — not supported in lightweight header parser.");
|
|
644
|
+
const src = rawbuffer;
|
|
645
|
+
const readU16 = (off) => {
|
|
646
|
+
if (off < 0 || off + 2 > src.length) return 0;
|
|
647
|
+
return little ? src.readUInt16LE(off) : src.readUInt16BE(off);
|
|
648
|
+
};
|
|
649
|
+
const readU32 = (off) => {
|
|
650
|
+
if (off < 0 || off + 4 > src.length) return 0;
|
|
651
|
+
return little ? src.readUInt32LE(off) : src.readUInt32BE(off);
|
|
652
|
+
};
|
|
653
|
+
const TIFF_START = 0;
|
|
654
|
+
const firstIFDOff = readU32(4);
|
|
655
|
+
let ifdOff = TIFF_START + firstIFDOff;
|
|
656
|
+
let frames = 0;
|
|
657
|
+
let width = null;
|
|
658
|
+
let height = null;
|
|
659
|
+
let samplesPerPixel = null;
|
|
660
|
+
let extraSamples = [];
|
|
661
|
+
let photometric = null;
|
|
662
|
+
let orientation = null;
|
|
663
|
+
let iccPresent = false;
|
|
664
|
+
let exifIFDOffset = null;
|
|
665
|
+
const TAG = {
|
|
666
|
+
ImageWidth: 256,
|
|
667
|
+
ImageLength: 257,
|
|
668
|
+
BitsPerSample: 258,
|
|
669
|
+
Compression: 259,
|
|
670
|
+
PhotometricInterpretation: 262,
|
|
671
|
+
StripOffsets: 273,
|
|
672
|
+
Orientation: 274,
|
|
673
|
+
SamplesPerPixel: 277,
|
|
674
|
+
RowsPerStrip: 278,
|
|
675
|
+
PlanarConfiguration: 284,
|
|
676
|
+
ExtraSamples: 338,
|
|
677
|
+
ICCProfile: 34675,
|
|
678
|
+
ExifIFDPointer: 34665,
|
|
679
|
+
DateTimeOriginal: 36867
|
|
680
|
+
};
|
|
681
|
+
const TYPE = {
|
|
682
|
+
BYTE: 1,
|
|
683
|
+
ASCII: 2,
|
|
684
|
+
SHORT: 3,
|
|
685
|
+
LONG: 4,
|
|
686
|
+
RATIONAL: 5,
|
|
687
|
+
SBYTE: 6,
|
|
688
|
+
UNDEFINED: 7,
|
|
689
|
+
SSHORT: 8,
|
|
690
|
+
SLONG: 9,
|
|
691
|
+
SRATIONAL: 10,
|
|
692
|
+
FLOAT: 11,
|
|
693
|
+
DOUBLE: 12
|
|
694
|
+
};
|
|
695
|
+
function readIFDValue(entryOff, type, count) {
|
|
696
|
+
const valueOrOff = readU32(entryOff + 8);
|
|
697
|
+
const valueFitsInline = (type === TYPE.BYTE || type === TYPE.SBYTE || type === TYPE.UNDEFINED || type === TYPE.ASCII) && count <= 4 || type === TYPE.SHORT && count <= 2 || type === TYPE.LONG && count <= 1;
|
|
698
|
+
let dataOff = valueOrOff;
|
|
699
|
+
if (valueFitsInline) dataOff = entryOff + 8;
|
|
700
|
+
else dataOff = TIFF_START + valueOrOff;
|
|
701
|
+
if (dataOff < 0 || dataOff >= src.length) return null;
|
|
702
|
+
const readShortN = (o) => readU16(o);
|
|
703
|
+
const readLongN = (o) => readU32(o);
|
|
704
|
+
switch (type) {
|
|
705
|
+
case TYPE.ASCII: {
|
|
706
|
+
const end = Math.min(src.indexOf(0, dataOff) === -1 ? dataOff + count : src.indexOf(0, dataOff), src.length);
|
|
707
|
+
try {
|
|
708
|
+
return src.toString("ascii", dataOff, end);
|
|
709
|
+
} catch {
|
|
710
|
+
return null;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
case TYPE.BYTE:
|
|
714
|
+
case TYPE.SBYTE:
|
|
715
|
+
case TYPE.UNDEFINED: {
|
|
716
|
+
const out = [];
|
|
717
|
+
for (let i = 0; i < count; i++) {
|
|
718
|
+
const off = dataOff + i;
|
|
719
|
+
if (off >= src.length || !src[off]) break;
|
|
720
|
+
out.push(src[off]);
|
|
721
|
+
}
|
|
722
|
+
return count === 1 ? out[0] ?? null : out;
|
|
723
|
+
}
|
|
724
|
+
case TYPE.SHORT: {
|
|
725
|
+
const out = [];
|
|
726
|
+
for (let i = 0; i < count; i++) {
|
|
727
|
+
const val = readShortN(dataOff + i * 2);
|
|
728
|
+
if (val === 0) break;
|
|
729
|
+
out.push(val);
|
|
730
|
+
}
|
|
731
|
+
return count === 1 ? out[0] ?? null : out;
|
|
732
|
+
}
|
|
733
|
+
case TYPE.LONG: {
|
|
734
|
+
const out = [];
|
|
735
|
+
for (let i = 0; i < count; i++) {
|
|
736
|
+
const val = readLongN(dataOff + i * 4);
|
|
737
|
+
if (val === 0) break;
|
|
738
|
+
out.push(val);
|
|
739
|
+
}
|
|
740
|
+
return count === 1 ? out[0] ?? null : out;
|
|
741
|
+
}
|
|
742
|
+
case TYPE.RATIONAL: {
|
|
743
|
+
const out = [];
|
|
744
|
+
for (let i = 0; i < count; i++) {
|
|
745
|
+
const num = readLongN(dataOff + i * 8);
|
|
746
|
+
const den = readLongN(dataOff + i * 8 + 4);
|
|
747
|
+
if (num === 0 && den === 0) break;
|
|
748
|
+
out.push(den ? num / den : 0);
|
|
749
|
+
}
|
|
750
|
+
return count === 1 ? out[0] ?? null : out;
|
|
751
|
+
}
|
|
752
|
+
default: return null;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
const MAX_IFDS = 32;
|
|
756
|
+
while (ifdOff > 0 && ifdOff + 2 <= src.length && frames < MAX_IFDS) {
|
|
757
|
+
const numEntries = readU16(ifdOff);
|
|
758
|
+
const entriesBase = ifdOff + 2;
|
|
759
|
+
const ifdSize = numEntries * 12 + 4;
|
|
760
|
+
if (entriesBase + ifdSize > src.length) console.warn(`TIFF IFD at offset ${ifdOff} requires ${ifdSize} bytes but only ${src.length - entriesBase} available`);
|
|
761
|
+
for (let i = 0; i < numEntries; i++) {
|
|
762
|
+
const entry = entriesBase + i * 12;
|
|
763
|
+
if (entry + 12 > src.length) {
|
|
764
|
+
console.warn(`TIFF entry ${i} at offset ${entry} truncated`);
|
|
765
|
+
break;
|
|
766
|
+
}
|
|
767
|
+
const tag = readU16(entry);
|
|
768
|
+
const type = readU16(entry + 2);
|
|
769
|
+
const count = readU32(entry + 4);
|
|
770
|
+
if (tag === TAG.ImageWidth && width == null) {
|
|
771
|
+
const v = readIFDValue(entry, type, count);
|
|
772
|
+
if (typeof v === "number") width = v;
|
|
773
|
+
if (Array.isArray(v) && typeof v[0] === "number") width = v[0];
|
|
774
|
+
} else if (tag === TAG.ImageLength && height == null) {
|
|
775
|
+
const v = readIFDValue(entry, type, count);
|
|
776
|
+
if (typeof v === "number") height = v;
|
|
777
|
+
if (Array.isArray(v) && typeof v[0] === "number") height = v[0];
|
|
778
|
+
} else if (tag === TAG.SamplesPerPixel && samplesPerPixel == null) {
|
|
779
|
+
const v = readIFDValue(entry, type, count);
|
|
780
|
+
if (typeof v === "number") samplesPerPixel = v;
|
|
781
|
+
} else if (tag === TAG.ExtraSamples) {
|
|
782
|
+
const v = readIFDValue(entry, type, count);
|
|
783
|
+
if (typeof v === "number") extraSamples = [v];
|
|
784
|
+
else if (Array.isArray(v)) extraSamples = v.filter((n) => typeof n === "number");
|
|
785
|
+
} else if (tag === TAG.PhotometricInterpretation && photometric == null) {
|
|
786
|
+
const v = readIFDValue(entry, type, count);
|
|
787
|
+
if (typeof v === "number") photometric = v;
|
|
788
|
+
} else if (tag === TAG.Orientation && orientation == null) {
|
|
789
|
+
const v = readIFDValue(entry, type, count);
|
|
790
|
+
if (typeof v === "number") orientation = v;
|
|
791
|
+
} else if (tag === TAG.ICCProfile) iccPresent = true;
|
|
792
|
+
else if (tag === TAG.ExifIFDPointer && exifIFDOffset == null) {
|
|
793
|
+
const v = readIFDValue(entry, type, count);
|
|
794
|
+
if (typeof v === "number") exifIFDOffset = TIFF_START + v;
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
frames++;
|
|
798
|
+
const nextPtrOff = entriesBase + numEntries * 12;
|
|
799
|
+
if (nextPtrOff + 4 > src.length) break;
|
|
800
|
+
const nextRel = readU32(nextPtrOff);
|
|
801
|
+
ifdOff = nextRel ? TIFF_START + nextRel : 0;
|
|
802
|
+
}
|
|
803
|
+
let exifDateTimeOriginal = null;
|
|
804
|
+
if (exifIFDOffset && exifIFDOffset + 2 <= src.length) try {
|
|
805
|
+
const num = readU16(exifIFDOffset);
|
|
806
|
+
const base = exifIFDOffset + 2;
|
|
807
|
+
for (let i = 0; i < num; i++) {
|
|
808
|
+
const entry = base + i * 12;
|
|
809
|
+
if (entry + 12 > src.length) break;
|
|
810
|
+
if (readU16(entry) === TAG.DateTimeOriginal) {
|
|
811
|
+
const val = readIFDValue(entry, readU16(entry + 2), readU32(entry + 4));
|
|
812
|
+
if (typeof val === "string" && /^\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}/.test(val)) exifDateTimeOriginal = val;
|
|
813
|
+
break;
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
} catch {}
|
|
817
|
+
if (!width || !height) {
|
|
818
|
+
console.warn(`TIFF: Could not find ImageWidth/ImageLength in buffer of ${src.length} bytes. IFD at offset ${firstIFDOff} may require more data.`);
|
|
819
|
+
if (firstIFDOff >= src.length - 100) throw new Error(`TIFF IFD at offset ${firstIFDOff} is beyond buffer (${src.length} bytes). This file requires reading more data to parse.`);
|
|
820
|
+
throw new Error("Invalid TIFF: missing ImageWidth/ImageLength tags in accessible IFDs.");
|
|
821
|
+
}
|
|
822
|
+
let colorModel = "unknown";
|
|
823
|
+
let colorSpace = "unknown";
|
|
824
|
+
switch (photometric) {
|
|
825
|
+
case 0:
|
|
826
|
+
case 1:
|
|
827
|
+
colorModel = samplesPerPixel === 2 || extraSamples.length ? "grayscale-alpha" : "grayscale";
|
|
828
|
+
colorSpace = "gray";
|
|
829
|
+
break;
|
|
830
|
+
case 2:
|
|
831
|
+
colorModel = samplesPerPixel === 4 || extraSamples.length ? "rgba" : "rgb";
|
|
832
|
+
colorSpace = "srgb";
|
|
833
|
+
break;
|
|
834
|
+
case 3:
|
|
835
|
+
colorModel = "indexed";
|
|
836
|
+
colorSpace = "srgb";
|
|
837
|
+
break;
|
|
838
|
+
case 5:
|
|
839
|
+
colorModel = "cmyk";
|
|
840
|
+
colorSpace = "cmyk";
|
|
841
|
+
break;
|
|
842
|
+
case 6:
|
|
843
|
+
colorModel = "ycbcr";
|
|
844
|
+
colorSpace = "srgb";
|
|
845
|
+
break;
|
|
846
|
+
case 8:
|
|
847
|
+
colorModel = "lab";
|
|
848
|
+
colorSpace = "lab";
|
|
849
|
+
break;
|
|
850
|
+
default: break;
|
|
851
|
+
}
|
|
852
|
+
const hasAlpha = extraSamples.some((v) => v === 1 || v === 2) || photometric === 2 && samplesPerPixel === 4 || (photometric === 0 || photometric === 1) && samplesPerPixel === 2;
|
|
853
|
+
return {
|
|
854
|
+
type: "IMAGE",
|
|
855
|
+
width,
|
|
856
|
+
height,
|
|
857
|
+
format: "tiff",
|
|
858
|
+
frames,
|
|
859
|
+
animated: false,
|
|
860
|
+
hasAlpha,
|
|
861
|
+
orientation: orientation ?? null,
|
|
862
|
+
aspectRatio: width / height,
|
|
863
|
+
colorModel,
|
|
864
|
+
colorSpace,
|
|
865
|
+
iccProfile: iccPresent ? "embedded" : null,
|
|
866
|
+
exifDateTimeOriginal
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
async extractImgSpecsViaStream(stream, size = 4096) {
|
|
870
|
+
const headerSize = size;
|
|
871
|
+
const header = await this.readBytes(stream, headerSize);
|
|
872
|
+
return this.getImageSpecsWorkup(header, size);
|
|
873
|
+
}
|
|
874
|
+
async extractImgSpecsViaPath(filePath, size = 4096) {
|
|
875
|
+
const stream = createReadStream(relative(process.cwd(), filePath), { highWaterMark: size });
|
|
876
|
+
return await this.extractImgSpecsViaStream(stream, size);
|
|
877
|
+
}
|
|
878
|
+
getImageSpecsWorkup(rawbuffer, size = 4096 * 6) {
|
|
879
|
+
const MAX_HEADER_SIZE = size;
|
|
880
|
+
const buffer = rawbuffer.subarray(0, Math.min(rawbuffer.length, MAX_HEADER_SIZE));
|
|
881
|
+
if (buffer?.length >= 24 && buffer?.[0] === 137 && buffer?.[1] === 80 && buffer?.[2] === 78 && buffer?.[3] === 71 && buffer?.[4] === 13 && buffer?.[5] === 10 && buffer?.[6] === 26 && buffer?.[7] === 10) return this.png(buffer);
|
|
882
|
+
if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) return this.jpeg(buffer);
|
|
883
|
+
const gifHeader = buffer.toString("ascii", 0, 6);
|
|
884
|
+
if (buffer.length >= 10 && (gifHeader === "GIF87a" || gifHeader === "GIF89a")) return this.gif(buffer);
|
|
885
|
+
if (buffer.length >= 26 && buffer?.[0] === 66 && buffer?.[1] === 77) return this.bmp(buffer);
|
|
886
|
+
if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") return this.webp(buffer);
|
|
887
|
+
if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") return this.avif(buffer);
|
|
888
|
+
if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") return this.heic(buffer);
|
|
889
|
+
const headerText = buffer.toString("utf-8", 0, Math.min(1024, buffer.length)).trim();
|
|
890
|
+
if (headerText.startsWith("<svg") || headerText.startsWith("<?xml") && headerText.includes("<svg")) return this.svg(headerText);
|
|
891
|
+
if (buffer.length >= 6 && buffer?.[0] === 0 && buffer?.[1] === 0 && buffer?.[2] === 1 && buffer?.[3] === 0) return this.ico(rawbuffer, buffer);
|
|
892
|
+
if (buffer.length >= 8 && (buffer?.[0] === 73 && buffer?.[1] === 73 && buffer?.[2] === 42 && buffer?.[3] === 0 || buffer?.[0] === 77 && buffer?.[1] === 77 && buffer?.[2] === 0 && buffer?.[3] === 42 || buffer?.[0] === 73 && buffer?.[1] === 73 && buffer?.[2] === 43 && buffer?.[3] === 0 || buffer?.[0] === 77 && buffer?.[1] === 77 && buffer?.[2] === 0 && buffer?.[3] === 43)) return this.tiff(rawbuffer, buffer);
|
|
893
|
+
throw new Error("Unsupported image format or invalid file");
|
|
894
|
+
}
|
|
895
|
+
};
|
|
896
|
+
|
|
897
|
+
//#endregion
|
|
898
|
+
export { ImgMetadataExtractor };
|
|
899
|
+
//# sourceMappingURL=index.js.map
|