@d0paminedriven/fs 7.0.0 → 7.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.
- package/README.md +14 -3
- package/dist/{chunk-CTA73JRX.js → chunk-3REIEDSC.js} +33 -426
- package/dist/{chunk-CN45SMBB.js → chunk-3WB2TSBO.js} +26 -11
- package/dist/chunk-LFHV4PPA.js +577 -0
- package/dist/{chunk-G27YIWW7.js → chunk-RJ2YQE3C.js} +1 -1
- package/dist/fs/index.cjs +743 -555
- package/dist/fs/index.d.cts +157 -85
- package/dist/fs/index.d.ts +157 -85
- package/dist/fs/index.js +4 -3
- package/dist/image/index.cjs +1071 -0
- package/dist/image/index.d.cts +88 -0
- package/dist/image/index.d.ts +88 -0
- package/dist/image/index.js +11 -0
- package/dist/index.cjs +745 -555
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +7 -3
- package/dist/mime/index.cjs +25 -10
- package/dist/mime/index.d.cts +51 -21
- package/dist/mime/index.d.ts +51 -21
- package/dist/mime/index.js +1 -1
- package/dist/types/index.d.cts +5 -5
- package/dist/types/index.d.ts +5 -5
- package/dist/types/index.js +1 -1
- package/package.json +14 -1
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MimeService
|
|
3
|
+
} from "./chunk-3WB2TSBO.js";
|
|
4
|
+
import {
|
|
5
|
+
__name
|
|
6
|
+
} from "./chunk-7QVYU63E.js";
|
|
7
|
+
|
|
8
|
+
// src/image/index.ts
|
|
9
|
+
var ImageService = class extends MimeService {
|
|
10
|
+
static {
|
|
11
|
+
__name(this, "ImageService");
|
|
12
|
+
}
|
|
13
|
+
parseExif(buffer, app1Pos, app1Size) {
|
|
14
|
+
const exifHeader = buffer.toString("ascii", app1Pos + 2, app1Pos + 8);
|
|
15
|
+
if (exifHeader !== "Exif\0\0")
|
|
16
|
+
return { orientation: null, dateTimeOriginal: null };
|
|
17
|
+
let tiffPos = app1Pos + 8;
|
|
18
|
+
const app1End = app1Pos + app1Size;
|
|
19
|
+
if (tiffPos + 10 > app1End)
|
|
20
|
+
return { orientation: null, dateTimeOriginal: null };
|
|
21
|
+
const byteOrder = buffer.toString("ascii", tiffPos, tiffPos + 2);
|
|
22
|
+
const littleEndian = byteOrder === "II";
|
|
23
|
+
const readUInt16 = littleEndian ? buffer.readUInt16LE.bind(buffer) : buffer.readUInt16BE.bind(buffer);
|
|
24
|
+
const readUInt32 = littleEndian ? buffer.readUInt32LE.bind(buffer) : buffer.readUInt32BE.bind(buffer);
|
|
25
|
+
if (readUInt16(tiffPos + 2) !== 42)
|
|
26
|
+
return { orientation: null, dateTimeOriginal: null };
|
|
27
|
+
let ifdOffset = readUInt32(tiffPos + 4);
|
|
28
|
+
tiffPos += ifdOffset;
|
|
29
|
+
const numEntries = readUInt16(tiffPos);
|
|
30
|
+
tiffPos += 2;
|
|
31
|
+
let orientation = null;
|
|
32
|
+
let dateTimeOriginal = null;
|
|
33
|
+
for (let i = 0; i < numEntries; i++) {
|
|
34
|
+
const tag = readUInt16(tiffPos), type = readUInt16(tiffPos + 2), count = readUInt32(tiffPos + 4), valueOffset = tiffPos + 8;
|
|
35
|
+
if (tag === 274 && type === 3 && count === 1) {
|
|
36
|
+
orientation = readUInt16(valueOffset);
|
|
37
|
+
} else if (tag === 36867 && type === 2 && count === 20) {
|
|
38
|
+
const offset = readUInt32(valueOffset);
|
|
39
|
+
if (app1Pos + 2 + offset + 19 > app1End) {
|
|
40
|
+
dateTimeOriginal = null;
|
|
41
|
+
} else {
|
|
42
|
+
dateTimeOriginal = buffer.toString("ascii", app1Pos + 2 + offset, app1Pos + 2 + offset + 19).trim();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
tiffPos += 12;
|
|
46
|
+
}
|
|
47
|
+
return { orientation, dateTimeOriginal };
|
|
48
|
+
}
|
|
49
|
+
parseXmpFromAvif(buffer, meta, _ipco) {
|
|
50
|
+
const iinf = this.findBox(
|
|
51
|
+
buffer,
|
|
52
|
+
"iinf",
|
|
53
|
+
meta.pos + 4,
|
|
54
|
+
meta.pos + meta.size
|
|
55
|
+
);
|
|
56
|
+
if (!iinf) return null;
|
|
57
|
+
let pos = iinf.pos;
|
|
58
|
+
const version = buffer[pos++];
|
|
59
|
+
const itemCount = version === 0 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
60
|
+
pos += version === 0 ? 2 : 4;
|
|
61
|
+
let xmpItemId;
|
|
62
|
+
for (let i = 0; i < itemCount; i++) {
|
|
63
|
+
const infeStart = pos;
|
|
64
|
+
const infeSize = buffer.readUInt32BE(pos);
|
|
65
|
+
if (buffer.toString("ascii", pos + 4, pos + 8) !== "infe") break;
|
|
66
|
+
pos += 8;
|
|
67
|
+
const infeVersion = buffer?.[pos++];
|
|
68
|
+
if (!infeVersion) return null;
|
|
69
|
+
const itemId = infeVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
70
|
+
pos += infeVersion < 2 ? 2 : 4;
|
|
71
|
+
pos += 2;
|
|
72
|
+
const itemType = buffer.toString("ascii", pos, pos + 4);
|
|
73
|
+
pos += 4;
|
|
74
|
+
let nameEnd = pos;
|
|
75
|
+
while (buffer[nameEnd] !== 0 && nameEnd < infeStart + infeSize) nameEnd++;
|
|
76
|
+
const _itemName = buffer.toString("ascii", pos, nameEnd);
|
|
77
|
+
pos = nameEnd + 1;
|
|
78
|
+
if (itemType === "mime") {
|
|
79
|
+
let ctEnd = pos;
|
|
80
|
+
while (buffer[ctEnd] !== 0 && ctEnd < infeStart + infeSize) ctEnd++;
|
|
81
|
+
const contentType = buffer.toString("ascii", pos, ctEnd);
|
|
82
|
+
if (contentType === "application/rdf+xml") {
|
|
83
|
+
xmpItemId = itemId;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
pos = ctEnd + 1;
|
|
87
|
+
}
|
|
88
|
+
pos = infeStart + infeSize;
|
|
89
|
+
}
|
|
90
|
+
if (!xmpItemId) return null;
|
|
91
|
+
const iloc = this.findBox(
|
|
92
|
+
buffer,
|
|
93
|
+
"iloc",
|
|
94
|
+
meta.pos + 4,
|
|
95
|
+
meta.pos + meta.size
|
|
96
|
+
);
|
|
97
|
+
if (!iloc) return null;
|
|
98
|
+
pos = iloc.pos;
|
|
99
|
+
const ilocVersion = buffer[pos++];
|
|
100
|
+
const sizes = buffer?.[pos++];
|
|
101
|
+
if (!sizes) return null;
|
|
102
|
+
const offsetSize = sizes >> 4 & 15;
|
|
103
|
+
const lengthSize = sizes & 15;
|
|
104
|
+
let indexSize = 0;
|
|
105
|
+
if (ilocVersion === 1 || ilocVersion === 2) {
|
|
106
|
+
const indexSizes = buffer?.[pos++];
|
|
107
|
+
if (!indexSizes) return null;
|
|
108
|
+
indexSize = indexSizes >> 4 & 15;
|
|
109
|
+
}
|
|
110
|
+
if (!ilocVersion) return null;
|
|
111
|
+
let buffpos = buffer?.[pos++];
|
|
112
|
+
if (!buffpos) return null;
|
|
113
|
+
const baseOffsetSize = ilocVersion < 2 ? 0 : buffpos >> 4 & 15;
|
|
114
|
+
const itemCountIloc = ilocVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
115
|
+
pos += ilocVersion < 2 ? 2 : 4;
|
|
116
|
+
let xmpOffset = 0;
|
|
117
|
+
let xmpLength = 0;
|
|
118
|
+
for (let i = 0; i < itemCountIloc; i++) {
|
|
119
|
+
const curItemId = ilocVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
120
|
+
pos += ilocVersion < 2 ? 2 : 4;
|
|
121
|
+
if (curItemId !== xmpItemId) {
|
|
122
|
+
if (ilocVersion === 1 || ilocVersion === 2) pos += 2;
|
|
123
|
+
pos += 2;
|
|
124
|
+
pos += baseOffsetSize;
|
|
125
|
+
const extentCount2 = buffer.readUInt16BE(pos);
|
|
126
|
+
pos += 2;
|
|
127
|
+
for (let e = 0; e < extentCount2; e++) {
|
|
128
|
+
if (indexSize > 0) pos += indexSize;
|
|
129
|
+
pos += offsetSize + lengthSize;
|
|
130
|
+
}
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (ilocVersion === 1 || ilocVersion === 2) {
|
|
134
|
+
const constructionMethod = buffer.readUInt16BE(pos);
|
|
135
|
+
pos += 2;
|
|
136
|
+
if (constructionMethod !== 0) return null;
|
|
137
|
+
}
|
|
138
|
+
pos += 2;
|
|
139
|
+
const baseOffset = this.readVarSize(buffer, pos, baseOffsetSize);
|
|
140
|
+
pos += baseOffsetSize;
|
|
141
|
+
const extentCount = buffer.readUInt16BE(pos);
|
|
142
|
+
pos += 2;
|
|
143
|
+
if (extentCount !== 1) return null;
|
|
144
|
+
if (indexSize > 0) pos += indexSize;
|
|
145
|
+
const extentOffset = this.readVarSize(buffer, pos, offsetSize);
|
|
146
|
+
pos += offsetSize;
|
|
147
|
+
const extentLength = this.readVarSize(buffer, pos, lengthSize);
|
|
148
|
+
xmpOffset = baseOffset + extentOffset;
|
|
149
|
+
xmpLength = extentLength;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
if (xmpOffset === 0 || xmpLength === 0 || xmpOffset + xmpLength > buffer.length)
|
|
153
|
+
return null;
|
|
154
|
+
return buffer.toString("utf8", xmpOffset, xmpOffset + xmpLength);
|
|
155
|
+
}
|
|
156
|
+
readVarSize(buffer, pos, size) {
|
|
157
|
+
if (size === 0) return 0;
|
|
158
|
+
if (size === 1 && buffer?.[pos]) return buffer?.[pos];
|
|
159
|
+
if (size === 2) return buffer.readUInt16BE(pos);
|
|
160
|
+
if (size === 3) {
|
|
161
|
+
let pos1 = buffer?.[pos + 1], pos2 = buffer?.[pos + 2];
|
|
162
|
+
if (buffer?.[pos] && pos1 && pos2) {
|
|
163
|
+
return buffer[pos] << 16 | pos1 << 8 | pos2;
|
|
164
|
+
}
|
|
165
|
+
throw new Error("Invalid var size");
|
|
166
|
+
}
|
|
167
|
+
if (size === 4) return buffer.readUInt32BE(pos);
|
|
168
|
+
if (size === 8) return Number(buffer.readBigUInt64BE(pos));
|
|
169
|
+
throw new Error("Invalid var size");
|
|
170
|
+
}
|
|
171
|
+
findBox(buffer, type, start = 0, end = buffer.length) {
|
|
172
|
+
let pos = start;
|
|
173
|
+
while (pos < end) {
|
|
174
|
+
let boxSize = buffer.readUInt32BE(pos);
|
|
175
|
+
let boxType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
176
|
+
let hdrSize = 8;
|
|
177
|
+
if (boxSize === 1) {
|
|
178
|
+
boxSize = Number(buffer.readBigUInt64BE(pos + 8));
|
|
179
|
+
hdrSize = 16;
|
|
180
|
+
} else if (boxSize === 0) {
|
|
181
|
+
boxSize = end - pos;
|
|
182
|
+
}
|
|
183
|
+
if (boxType === type) {
|
|
184
|
+
return { pos: pos + hdrSize, size: boxSize - hdrSize };
|
|
185
|
+
}
|
|
186
|
+
pos += boxSize;
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
getImageSpecsWorkup(buffer) {
|
|
191
|
+
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) {
|
|
192
|
+
if (buffer.readUint32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) {
|
|
193
|
+
throw new Error("IHDR Chunk of png not found or incorrect.");
|
|
194
|
+
}
|
|
195
|
+
const colorType = buffer[25];
|
|
196
|
+
let colorSpace;
|
|
197
|
+
let hasAlpha = false;
|
|
198
|
+
switch (colorType) {
|
|
199
|
+
case 0:
|
|
200
|
+
colorSpace = "grayscale";
|
|
201
|
+
break;
|
|
202
|
+
case 2:
|
|
203
|
+
colorSpace = "rgb";
|
|
204
|
+
break;
|
|
205
|
+
case 3:
|
|
206
|
+
colorSpace = "indexed";
|
|
207
|
+
break;
|
|
208
|
+
case 4:
|
|
209
|
+
colorSpace = "grayscale-alpha";
|
|
210
|
+
hasAlpha = true;
|
|
211
|
+
break;
|
|
212
|
+
case 6:
|
|
213
|
+
colorSpace = "rgba";
|
|
214
|
+
hasAlpha = true;
|
|
215
|
+
break;
|
|
216
|
+
default:
|
|
217
|
+
colorSpace = "unknown";
|
|
218
|
+
}
|
|
219
|
+
const width = buffer.readUInt32BE(16);
|
|
220
|
+
const height = buffer.readUInt32BE(20);
|
|
221
|
+
let frames = 1;
|
|
222
|
+
let animated = false;
|
|
223
|
+
let iccProfile = null;
|
|
224
|
+
let exifDateTimeOriginal = null;
|
|
225
|
+
let orientation = null;
|
|
226
|
+
let pos = 33;
|
|
227
|
+
while (pos < buffer.length - 12) {
|
|
228
|
+
const chunkLen = buffer.readUInt32BE(pos);
|
|
229
|
+
const chunkType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
230
|
+
const chunkData = pos + 8;
|
|
231
|
+
if (chunkType === "acTL") {
|
|
232
|
+
animated = true;
|
|
233
|
+
frames = buffer.readUInt32BE(chunkData);
|
|
234
|
+
} else if (chunkType === "iCCP") {
|
|
235
|
+
const nameEnd = buffer.indexOf(0, chunkData);
|
|
236
|
+
const profileName = buffer.toString("ascii", chunkData, nameEnd);
|
|
237
|
+
iccProfile = profileName || "embedded";
|
|
238
|
+
} else if (chunkType === "tIME") {
|
|
239
|
+
const month = buffer?.[chunkData + 2], day = buffer?.[chunkData + 3], hour = buffer?.[chunkData + 4], minute = buffer?.[chunkData + 5], second = buffer?.[chunkData + 6];
|
|
240
|
+
if (typeof month !== "undefined" && typeof day !== "undefined" && typeof hour !== "undefined" && typeof minute !== "undefined" && typeof second !== "undefined") {
|
|
241
|
+
const year = buffer.readUInt16BE(chunkData);
|
|
242
|
+
exifDateTimeOriginal = `${year}:${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")}`;
|
|
243
|
+
}
|
|
244
|
+
} else if (chunkType === "IDAT") {
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
pos += 12 + chunkLen;
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
width,
|
|
251
|
+
height,
|
|
252
|
+
format: "png",
|
|
253
|
+
frames,
|
|
254
|
+
animated,
|
|
255
|
+
hasAlpha,
|
|
256
|
+
orientation,
|
|
257
|
+
aspectRatio: width / height,
|
|
258
|
+
colorSpace,
|
|
259
|
+
iccProfile,
|
|
260
|
+
exifDateTimeOriginal
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) {
|
|
264
|
+
let pos = 2, colorSpace = "unknown", hasAlpha = false, width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
|
|
265
|
+
while (pos < buffer.length - 10) {
|
|
266
|
+
if (buffer[pos] !== 255) {
|
|
267
|
+
throw new Error("Invalid JPEG file");
|
|
268
|
+
}
|
|
269
|
+
const marker = buffer[pos + 1];
|
|
270
|
+
if (marker === 218) break;
|
|
271
|
+
const segmentSize = buffer.readUInt16BE(pos + 2);
|
|
272
|
+
if (!marker) {
|
|
273
|
+
throw new Error("no marker for jpeg");
|
|
274
|
+
}
|
|
275
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
276
|
+
const numComponents = buffer[pos + 4];
|
|
277
|
+
switch (numComponents) {
|
|
278
|
+
case 1:
|
|
279
|
+
colorSpace = "grayscale";
|
|
280
|
+
break;
|
|
281
|
+
case 3:
|
|
282
|
+
colorSpace = "ycbcr";
|
|
283
|
+
break;
|
|
284
|
+
// Most common for RGB JPEGs (stored as YCbCr)
|
|
285
|
+
case 4:
|
|
286
|
+
colorSpace = "ycck";
|
|
287
|
+
break;
|
|
288
|
+
// YCCK for CMYK with alpha-like, but no true alpha
|
|
289
|
+
default:
|
|
290
|
+
colorSpace = "unknown";
|
|
291
|
+
}
|
|
292
|
+
width = buffer.readUInt16BE(pos + 7);
|
|
293
|
+
height = buffer.readUInt16BE(pos + 5);
|
|
294
|
+
} else if (marker === 225) {
|
|
295
|
+
const { orientation: ori, dateTimeOriginal } = this.parseExif(
|
|
296
|
+
buffer,
|
|
297
|
+
pos,
|
|
298
|
+
segmentSize
|
|
299
|
+
);
|
|
300
|
+
orientation = ori;
|
|
301
|
+
exifDateTimeOriginal = dateTimeOriginal;
|
|
302
|
+
} else if (marker === 226) {
|
|
303
|
+
const iccHeader = buffer.toString("ascii", pos + 4, pos + 18);
|
|
304
|
+
if (iccHeader.startsWith("ICC_PROFILE")) {
|
|
305
|
+
iccProfile = "embedded";
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
pos += segmentSize + 2;
|
|
309
|
+
}
|
|
310
|
+
if (width === 0) throw new Error("No dimensions found in JPEG file");
|
|
311
|
+
return {
|
|
312
|
+
width,
|
|
313
|
+
height,
|
|
314
|
+
format: "jpeg",
|
|
315
|
+
frames: 1,
|
|
316
|
+
animated: false,
|
|
317
|
+
// JPEG not animated
|
|
318
|
+
hasAlpha,
|
|
319
|
+
orientation,
|
|
320
|
+
aspectRatio: width / height,
|
|
321
|
+
colorSpace,
|
|
322
|
+
iccProfile,
|
|
323
|
+
exifDateTimeOriginal
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
const gifHeader = buffer.toString("ascii", 0, 6);
|
|
327
|
+
if (buffer.length >= 10 && (gifHeader === "GIF87a" || gifHeader === "GIF89a")) {
|
|
328
|
+
const width = buffer.readUInt16LE(6), height = buffer.readUInt16LE(8);
|
|
329
|
+
let frames = 0;
|
|
330
|
+
let pos = 13;
|
|
331
|
+
let bufTen = buffer?.[10];
|
|
332
|
+
if (typeof bufTen === "undefined") {
|
|
333
|
+
pos = 13;
|
|
334
|
+
frames = 0;
|
|
335
|
+
} else if (bufTen & 128) pos += 3 << (bufTen & 7) + 1;
|
|
336
|
+
while (pos < buffer.length) {
|
|
337
|
+
if (buffer[pos] === 33) {
|
|
338
|
+
pos += 2;
|
|
339
|
+
let subSize = buffer?.[pos];
|
|
340
|
+
while (subSize && subSize > 0) {
|
|
341
|
+
pos += subSize + 1;
|
|
342
|
+
subSize = buffer?.[pos];
|
|
343
|
+
}
|
|
344
|
+
pos++;
|
|
345
|
+
} else if (buffer?.[pos] && buffer?.[pos] === 44) {
|
|
346
|
+
frames++;
|
|
347
|
+
pos += 10;
|
|
348
|
+
let b = buffer?.[pos - 1];
|
|
349
|
+
if (typeof b !== "undefined" && b & 128)
|
|
350
|
+
pos += 3 << (b & 7) + 1;
|
|
351
|
+
pos++;
|
|
352
|
+
let dataSize = buffer?.[pos];
|
|
353
|
+
while (dataSize && dataSize > 0) {
|
|
354
|
+
pos += dataSize + 1;
|
|
355
|
+
dataSize = buffer[pos];
|
|
356
|
+
}
|
|
357
|
+
pos++;
|
|
358
|
+
} else if (buffer[pos] === 59) {
|
|
359
|
+
break;
|
|
360
|
+
} else {
|
|
361
|
+
pos++;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return {
|
|
365
|
+
width,
|
|
366
|
+
height,
|
|
367
|
+
format: "gif",
|
|
368
|
+
frames,
|
|
369
|
+
animated: frames > 1,
|
|
370
|
+
hasAlpha: null,
|
|
371
|
+
// GIF transparency is per-pixel binary, not full alpha; set null or true if transparent color exists, but complex
|
|
372
|
+
orientation: null,
|
|
373
|
+
// No orientation in GIF
|
|
374
|
+
aspectRatio: width / height,
|
|
375
|
+
colorSpace: "indexed",
|
|
376
|
+
iccProfile: null,
|
|
377
|
+
// No ICC in GIF
|
|
378
|
+
exifDateTimeOriginal: null
|
|
379
|
+
// No EXIF in GIF
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
if (buffer.length >= 26 && buffer?.[0] === 66 && buffer?.[1] === 77) {
|
|
383
|
+
const width = buffer.readInt32LE(18);
|
|
384
|
+
const height = Math.abs(buffer.readInt32LE(22));
|
|
385
|
+
const bitDepth = buffer.readUInt16LE(28);
|
|
386
|
+
let colorSpace = bitDepth <= 8 ? "indexed" : "rgb";
|
|
387
|
+
return {
|
|
388
|
+
width,
|
|
389
|
+
height,
|
|
390
|
+
format: "bmp",
|
|
391
|
+
frames: 1,
|
|
392
|
+
animated: false,
|
|
393
|
+
hasAlpha: null,
|
|
394
|
+
// Some BMP have alpha in 32bpp, check if bitDepth===32 && compression===3 (bitfields with alpha)
|
|
395
|
+
orientation: buffer.readInt32LE(22) < 0 ? 1 : 6,
|
|
396
|
+
// Negative height means top-down (orientation 1), positive bottom-up (like 6, but simplified)
|
|
397
|
+
aspectRatio: width / height,
|
|
398
|
+
colorSpace,
|
|
399
|
+
iccProfile: null,
|
|
400
|
+
// Rare in BMP
|
|
401
|
+
exifDateTimeOriginal: null
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
|
|
405
|
+
const chunkType = buffer.toString("ascii", 12, 16);
|
|
406
|
+
let colorSpace = "unknown";
|
|
407
|
+
let hasAlpha = false;
|
|
408
|
+
let width = 0;
|
|
409
|
+
let height = 0;
|
|
410
|
+
let frames = 1;
|
|
411
|
+
let animated = false;
|
|
412
|
+
let iccProfile = null;
|
|
413
|
+
if (chunkType === "VP8X") {
|
|
414
|
+
const flags = buffer?.[20];
|
|
415
|
+
colorSpace = flags ? flags & 2 ? "rgba" : "rgb" : "unknown";
|
|
416
|
+
hasAlpha = flags ? !!(flags & 2) : false;
|
|
417
|
+
animated = flags ? !!(flags & 1) : false;
|
|
418
|
+
let widthSubtractOne = 0;
|
|
419
|
+
let heightSubtractOne = 0;
|
|
420
|
+
if (buffer?.[24] && buffer?.[25] && buffer?.[26]) {
|
|
421
|
+
widthSubtractOne = buffer[24] | buffer[25] << 8 | buffer[26] << 16;
|
|
422
|
+
}
|
|
423
|
+
if (buffer?.[27] && buffer?.[28] && buffer?.[29]) {
|
|
424
|
+
heightSubtractOne = buffer[27] | buffer[28] << 8 | buffer[29] << 16;
|
|
425
|
+
}
|
|
426
|
+
width = widthSubtractOne + 1;
|
|
427
|
+
height = heightSubtractOne + 1;
|
|
428
|
+
if (animated) {
|
|
429
|
+
let pos = 30;
|
|
430
|
+
frames = 0;
|
|
431
|
+
while (pos < buffer.length - 8) {
|
|
432
|
+
const subChunkType = buffer.toString("ascii", pos, pos + 4);
|
|
433
|
+
const subChunkSize = buffer.readUInt32LE(pos + 4);
|
|
434
|
+
if (subChunkType === "ICCP") {
|
|
435
|
+
iccProfile = "embedded";
|
|
436
|
+
} else if (subChunkType === "ANMF") {
|
|
437
|
+
frames++;
|
|
438
|
+
}
|
|
439
|
+
pos += 8 + subChunkSize + subChunkSize % 2;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
} else if (chunkType === "VP8 ") {
|
|
443
|
+
colorSpace = "rgb";
|
|
444
|
+
hasAlpha = false;
|
|
445
|
+
const dataStart = 20;
|
|
446
|
+
if (buffer?.[dataStart + 3] !== 157 || buffer?.[dataStart + 4] !== 1 || buffer?.[dataStart + 5] !== 42) {
|
|
447
|
+
throw new Error("Invalid VP8 keyframe");
|
|
448
|
+
}
|
|
449
|
+
width = buffer.readUInt16LE(dataStart + 6) & 16383;
|
|
450
|
+
height = buffer.readUInt16LE(dataStart + 8) & 16383;
|
|
451
|
+
} else if (chunkType === "VP8L") {
|
|
452
|
+
const dataStart = 20;
|
|
453
|
+
if (buffer?.[dataStart] !== 47) {
|
|
454
|
+
throw new Error("Invalid VP8L signature");
|
|
455
|
+
}
|
|
456
|
+
const bits = buffer.readUInt32LE(dataStart + 1);
|
|
457
|
+
if (bits >>> 29 !== 0) {
|
|
458
|
+
throw new Error("Invalid VP8L version");
|
|
459
|
+
}
|
|
460
|
+
colorSpace = bits & 1 << 8 ? "rgba" : "rgb";
|
|
461
|
+
hasAlpha = !!(bits & 1 << 8);
|
|
462
|
+
width = 1 + (bits & 16383);
|
|
463
|
+
height = 1 + (bits >> 14 & 16383);
|
|
464
|
+
} else {
|
|
465
|
+
throw new Error("Unsupported WebP chunk");
|
|
466
|
+
}
|
|
467
|
+
return {
|
|
468
|
+
width,
|
|
469
|
+
height,
|
|
470
|
+
format: "webp",
|
|
471
|
+
frames,
|
|
472
|
+
animated,
|
|
473
|
+
hasAlpha,
|
|
474
|
+
orientation: null,
|
|
475
|
+
// No standard orientation in WebP
|
|
476
|
+
aspectRatio: width / height,
|
|
477
|
+
colorSpace,
|
|
478
|
+
iccProfile,
|
|
479
|
+
exifDateTimeOriginal: null
|
|
480
|
+
// Can have EXIF chunk, but rare; add if needed
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") {
|
|
484
|
+
const ftyp = this.findBox(buffer, "ftyp");
|
|
485
|
+
if (!ftyp) throw new Error("Invalid AVIF: No ftyp");
|
|
486
|
+
const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size);
|
|
487
|
+
const isAvif = brands.includes("avif");
|
|
488
|
+
const isAvis = brands.includes("avis");
|
|
489
|
+
if (!isAvif && !isAvis) throw new Error("Not an AVIF file");
|
|
490
|
+
const meta = this.findBox(buffer, "meta");
|
|
491
|
+
if (!meta) throw new Error("Invalid AVIF: No meta");
|
|
492
|
+
const metaSubStart = meta.pos + 4;
|
|
493
|
+
const metaSubEnd = meta.pos + meta.size;
|
|
494
|
+
const iprp = this.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
|
|
495
|
+
if (!iprp) throw new Error("Invalid AVIF: No iprp");
|
|
496
|
+
const ipco = this.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
|
|
497
|
+
if (!ipco) throw new Error("Invalid AVIF: No ipco");
|
|
498
|
+
const ispe = this.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
|
|
499
|
+
if (!ispe) throw new Error("Invalid AVIF: No ispe");
|
|
500
|
+
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
501
|
+
const width = buffer.readUInt32BE(ispe.pos + 4);
|
|
502
|
+
const height = buffer.readUInt32BE(ispe.pos + 8);
|
|
503
|
+
let colorSpace = "rgb";
|
|
504
|
+
let hasAlpha = false;
|
|
505
|
+
let iccProfile = null;
|
|
506
|
+
const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
|
|
507
|
+
if (colr) {
|
|
508
|
+
const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
|
|
509
|
+
if (colrType === "nclx") {
|
|
510
|
+
const matrix = buffer.readUInt16BE(colr.pos + 6);
|
|
511
|
+
colorSpace = matrix === 2 ? "rgb" : "ycbcr";
|
|
512
|
+
} else if (colrType === "rICC" || colrType === "prof") {
|
|
513
|
+
colorSpace = "unknown";
|
|
514
|
+
iccProfile = "embedded";
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
|
|
518
|
+
if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
|
|
519
|
+
hasAlpha = true;
|
|
520
|
+
if (colorSpace === "rgb") {
|
|
521
|
+
colorSpace = "rgba";
|
|
522
|
+
} else if (colorSpace === "ycbcr") {
|
|
523
|
+
colorSpace = "ycck";
|
|
524
|
+
} else if (colorSpace === "grayscale" || colorSpace === "unknown") {
|
|
525
|
+
colorSpace = "grayscale-alpha";
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
const xmpXml = this.parseXmpFromAvif(buffer, meta, ipco);
|
|
529
|
+
let exifDateTimeOriginal = null;
|
|
530
|
+
if (xmpXml) {
|
|
531
|
+
let dateStart = xmpXml.indexOf('xmp:CreateDate="');
|
|
532
|
+
if (dateStart === -1)
|
|
533
|
+
dateStart = xmpXml.indexOf('photoshop:DateCreated="');
|
|
534
|
+
if (dateStart !== -1) {
|
|
535
|
+
dateStart += 16;
|
|
536
|
+
const dateEnd = xmpXml.indexOf('"', dateStart);
|
|
537
|
+
if (dateEnd !== -1) {
|
|
538
|
+
exifDateTimeOriginal = xmpXml.substring(dateStart, dateEnd);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
let frames = 1;
|
|
543
|
+
let animated = isAvis;
|
|
544
|
+
if (animated) {
|
|
545
|
+
const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
|
|
546
|
+
if (iloc) {
|
|
547
|
+
const version = buffer?.[iloc.pos] ?? 0;
|
|
548
|
+
const itemCountPos = iloc.pos + (version < 2 ? 4 : 6);
|
|
549
|
+
let itemCount = buffer?.readUInt16BE(itemCountPos) ?? 0;
|
|
550
|
+
if (version >= 2) itemCount = buffer?.readUInt32BE(itemCountPos) ?? 0;
|
|
551
|
+
frames = Math.max(1, Math.floor(itemCount / (hasAlpha ? 2 : 1)));
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return {
|
|
555
|
+
width,
|
|
556
|
+
height,
|
|
557
|
+
format: "avif",
|
|
558
|
+
frames,
|
|
559
|
+
animated,
|
|
560
|
+
hasAlpha: hasAlpha ? true : null,
|
|
561
|
+
orientation: null,
|
|
562
|
+
// Can have 'irot' or 'imir' transforms, but rare
|
|
563
|
+
aspectRatio: width / height,
|
|
564
|
+
colorSpace,
|
|
565
|
+
iccProfile,
|
|
566
|
+
exifDateTimeOriginal
|
|
567
|
+
// Can have 'XMP' box, but parse XMP for date if needed
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
throw new Error("Unsupported image format or invalid file");
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
|
|
574
|
+
export {
|
|
575
|
+
ImageService
|
|
576
|
+
};
|
|
577
|
+
//# sourceMappingURL=chunk-LFHV4PPA.js.map
|