@d0paminedriven/fs 7.5.0 → 7.6.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/{chunk-6UANVLO3.js → chunk-3RC3WC7A.js} +2 -2
- package/dist/{chunk-LYQ3KQSL.js → chunk-4LZQHSBL.js} +2 -2
- package/dist/chunk-6R36YN3W.js +386 -0
- package/dist/{chunk-HQ2FHXLJ.js → chunk-CTSJFQMC.js} +2 -2
- package/dist/{chunk-DSH3OL5Q.js → chunk-I4ITMOIV.js} +2 -2
- package/dist/{chunk-F57L5MBU.js → chunk-MUFEXR25.js} +2 -2
- package/dist/{chunk-KNLXF622.js → chunk-PHTZ7NRL.js} +26 -6
- package/dist/{chunk-4DZLRFGH.js → chunk-Q6QZ5XEP.js} +1 -1
- package/dist/{chunk-N33N54TN.js → chunk-QKZIGBLD.js} +2 -2
- package/dist/{chunk-3MPIZJSN.js → chunk-TUY7Y4DL.js} +502 -326
- package/dist/{chunk-CJBGTYSB.js → chunk-ZQRB46YA.js} +3 -3
- package/dist/fs/index.d.ts +60 -7
- package/dist/fs/index.js +11 -10
- package/dist/fs-atomic/index.d.ts +1 -0
- package/dist/fs-atomic/index.js +5 -4
- package/dist/fs-base/index.d.ts +1 -0
- package/dist/fs-base/index.js +4 -3
- package/dist/fs-core/index.d.ts +2 -1
- package/dist/fs-core/index.js +8 -7
- package/dist/fs-fetch/index.d.ts +4 -3
- package/dist/fs-fetch/index.js +10 -9
- package/dist/fs-size/index.d.ts +1 -0
- package/dist/fs-size/index.js +7 -6
- package/dist/fs-tmp/index.d.ts +1 -0
- package/dist/fs-tmp/index.js +9 -8
- package/dist/image/index.d.ts +180 -37
- package/dist/image/index.js +3 -2
- package/dist/image/workup.d.ts +39 -0
- package/dist/image/workup.js +12 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +14 -10
- package/dist/mime/index.d.ts +45 -15
- package/dist/mime/index.js +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/index.js +1 -1
- package/package.json +14 -10
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "./chunk-
|
|
2
|
+
ImageServiceWorkup
|
|
3
|
+
} from "./chunk-6R36YN3W.js";
|
|
4
4
|
import {
|
|
5
5
|
__name
|
|
6
6
|
} from "./chunk-7QVYU63E.js";
|
|
@@ -9,50 +9,14 @@ import {
|
|
|
9
9
|
import { createReadStream } from "fs";
|
|
10
10
|
import { relative } from "path";
|
|
11
11
|
import { inflateSync } from "fflate";
|
|
12
|
-
var ImageService = class extends
|
|
12
|
+
var ImageService = class extends ImageServiceWorkup {
|
|
13
13
|
constructor(cwd) {
|
|
14
|
-
super();
|
|
14
|
+
super(cwd ??= process.cwd());
|
|
15
15
|
this.cwd = cwd;
|
|
16
16
|
}
|
|
17
17
|
static {
|
|
18
18
|
__name(this, "ImageService");
|
|
19
19
|
}
|
|
20
|
-
/**
|
|
21
|
-
* Read exactly N bytes from a stream, returning a Buffer
|
|
22
|
-
* Closes stream after reading or on error
|
|
23
|
-
*/
|
|
24
|
-
async readBytes(stream, bytes) {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
const chunks = [];
|
|
27
|
-
let totalBytes = 0;
|
|
28
|
-
const cleanup = /* @__PURE__ */ __name(() => {
|
|
29
|
-
stream.removeAllListeners();
|
|
30
|
-
stream.destroy();
|
|
31
|
-
}, "cleanup");
|
|
32
|
-
stream.on("error", (err) => {
|
|
33
|
-
cleanup();
|
|
34
|
-
reject(err);
|
|
35
|
-
});
|
|
36
|
-
stream.on("data", (chunk) => {
|
|
37
|
-
const remaining = bytes - totalBytes;
|
|
38
|
-
if (chunk.length <= remaining) {
|
|
39
|
-
chunks.push(chunk);
|
|
40
|
-
totalBytes += chunk.length;
|
|
41
|
-
} else {
|
|
42
|
-
chunks.push(chunk.subarray(0, remaining));
|
|
43
|
-
totalBytes = bytes;
|
|
44
|
-
}
|
|
45
|
-
if (totalBytes >= bytes) {
|
|
46
|
-
cleanup();
|
|
47
|
-
resolve(Buffer.concat(chunks));
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
stream.on("end", () => {
|
|
51
|
-
cleanup();
|
|
52
|
-
resolve(Buffer.concat(chunks));
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
20
|
/**
|
|
57
21
|
* Extract image metadata from a stream, reading only ~4KB for most formats
|
|
58
22
|
* Automatically handles PNG, JPEG, GIF, BMP, WebP, and AVIF
|
|
@@ -352,241 +316,6 @@ var ImageService = class extends MimeService {
|
|
|
352
316
|
exifDateTimeOriginal: null
|
|
353
317
|
};
|
|
354
318
|
}
|
|
355
|
-
parseExif(buffer, app1Pos, app1Size) {
|
|
356
|
-
const exifHeader = buffer.toString("ascii", app1Pos + 2, app1Pos + 8);
|
|
357
|
-
if (exifHeader !== "Exif\0\0")
|
|
358
|
-
return { orientation: null, dateTimeOriginal: null };
|
|
359
|
-
let tiffPos = app1Pos + 8;
|
|
360
|
-
const app1End = app1Pos + app1Size;
|
|
361
|
-
if (tiffPos + 10 > app1End)
|
|
362
|
-
return { orientation: null, dateTimeOriginal: null };
|
|
363
|
-
const byteOrder = buffer.toString("ascii", tiffPos, tiffPos + 2);
|
|
364
|
-
const littleEndian = byteOrder === "II";
|
|
365
|
-
const readUInt16 = littleEndian ? buffer.readUInt16LE.bind(buffer) : buffer.readUInt16BE.bind(buffer);
|
|
366
|
-
const readUInt32 = littleEndian ? buffer.readUInt32LE.bind(buffer) : buffer.readUInt32BE.bind(buffer);
|
|
367
|
-
if (readUInt16(tiffPos + 2) !== 42)
|
|
368
|
-
return { orientation: null, dateTimeOriginal: null };
|
|
369
|
-
let ifdOffset = readUInt32(tiffPos + 4);
|
|
370
|
-
tiffPos += ifdOffset;
|
|
371
|
-
const numEntries = readUInt16(tiffPos);
|
|
372
|
-
tiffPos += 2;
|
|
373
|
-
let orientation = null;
|
|
374
|
-
let dateTimeOriginal = null;
|
|
375
|
-
for (let i = 0; i < numEntries; i++) {
|
|
376
|
-
const tag = readUInt16(tiffPos), type = readUInt16(tiffPos + 2), count = readUInt32(tiffPos + 4), valueOffset = tiffPos + 8;
|
|
377
|
-
if (tag === 274 && type === 3 && count === 1) {
|
|
378
|
-
orientation = readUInt16(valueOffset);
|
|
379
|
-
} else if (tag === 36867 && type === 2 && count === 20) {
|
|
380
|
-
const offset = readUInt32(valueOffset);
|
|
381
|
-
if (app1Pos + 2 + offset + 19 > app1End) {
|
|
382
|
-
dateTimeOriginal = null;
|
|
383
|
-
} else {
|
|
384
|
-
dateTimeOriginal = buffer.toString("ascii", app1Pos + 2 + offset, app1Pos + 2 + offset + 19).trim();
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
tiffPos += 12;
|
|
388
|
-
}
|
|
389
|
-
return { orientation, dateTimeOriginal };
|
|
390
|
-
}
|
|
391
|
-
parseXmpFromAvif(buffer, meta, _ipco) {
|
|
392
|
-
const iinf = this.findBox(
|
|
393
|
-
buffer,
|
|
394
|
-
"iinf",
|
|
395
|
-
meta.pos + 4,
|
|
396
|
-
meta.pos + meta.size
|
|
397
|
-
);
|
|
398
|
-
if (!iinf) return null;
|
|
399
|
-
let pos = iinf.pos;
|
|
400
|
-
const version = buffer[pos++];
|
|
401
|
-
const itemCount = version === 0 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
402
|
-
pos += version === 0 ? 2 : 4;
|
|
403
|
-
let xmpItemId;
|
|
404
|
-
for (let i = 0; i < itemCount; i++) {
|
|
405
|
-
const infeStart = pos;
|
|
406
|
-
const infeSize = buffer.readUInt32BE(pos);
|
|
407
|
-
if (buffer.toString("ascii", pos + 4, pos + 8) !== "infe") break;
|
|
408
|
-
pos += 8;
|
|
409
|
-
const infeVersion = buffer?.[pos++];
|
|
410
|
-
if (!infeVersion) return null;
|
|
411
|
-
const itemId = infeVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
412
|
-
pos += infeVersion < 2 ? 2 : 4;
|
|
413
|
-
pos += 2;
|
|
414
|
-
const itemType = buffer.toString("ascii", pos, pos + 4);
|
|
415
|
-
pos += 4;
|
|
416
|
-
let nameEnd = pos;
|
|
417
|
-
while (buffer[nameEnd] !== 0 && nameEnd < infeStart + infeSize) nameEnd++;
|
|
418
|
-
pos = nameEnd + 1;
|
|
419
|
-
if (itemType === "mime") {
|
|
420
|
-
let ctEnd = pos;
|
|
421
|
-
while (buffer[ctEnd] !== 0 && ctEnd < infeStart + infeSize) ctEnd++;
|
|
422
|
-
const contentType = buffer.toString("ascii", pos, ctEnd);
|
|
423
|
-
if (contentType === "application/rdf+xml") {
|
|
424
|
-
xmpItemId = itemId;
|
|
425
|
-
break;
|
|
426
|
-
}
|
|
427
|
-
pos = ctEnd + 1;
|
|
428
|
-
}
|
|
429
|
-
pos = infeStart + infeSize;
|
|
430
|
-
}
|
|
431
|
-
if (!xmpItemId) return null;
|
|
432
|
-
const iloc = this.findBox(
|
|
433
|
-
buffer,
|
|
434
|
-
"iloc",
|
|
435
|
-
meta.pos + 4,
|
|
436
|
-
meta.pos + meta.size
|
|
437
|
-
);
|
|
438
|
-
if (!iloc) return null;
|
|
439
|
-
pos = iloc.pos;
|
|
440
|
-
const ilocVersion = buffer[pos++];
|
|
441
|
-
const sizes = buffer?.[pos++];
|
|
442
|
-
if (!sizes) return null;
|
|
443
|
-
const offsetSize = sizes >> 4 & 15;
|
|
444
|
-
const lengthSize = sizes & 15;
|
|
445
|
-
let indexSize = 0;
|
|
446
|
-
if (ilocVersion === 1 || ilocVersion === 2) {
|
|
447
|
-
const indexSizes = buffer?.[pos++];
|
|
448
|
-
if (!indexSizes) return null;
|
|
449
|
-
indexSize = indexSizes >> 4 & 15;
|
|
450
|
-
}
|
|
451
|
-
if (!ilocVersion) return null;
|
|
452
|
-
let buffpos = buffer?.[pos++];
|
|
453
|
-
if (!buffpos) return null;
|
|
454
|
-
const baseOffsetSize = ilocVersion < 2 ? 0 : buffpos >> 4 & 15;
|
|
455
|
-
const itemCountIloc = ilocVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
456
|
-
pos += ilocVersion < 2 ? 2 : 4;
|
|
457
|
-
let xmpOffset = 0;
|
|
458
|
-
let xmpLength = 0;
|
|
459
|
-
for (let i = 0; i < itemCountIloc; i++) {
|
|
460
|
-
const curItemId = ilocVersion < 2 ? buffer.readUInt16BE(pos) : buffer.readUInt32BE(pos);
|
|
461
|
-
pos += ilocVersion < 2 ? 2 : 4;
|
|
462
|
-
if (curItemId !== xmpItemId) {
|
|
463
|
-
if (ilocVersion === 1 || ilocVersion === 2) pos += 2;
|
|
464
|
-
pos += 2;
|
|
465
|
-
pos += baseOffsetSize;
|
|
466
|
-
const extentCount2 = buffer.readUInt16BE(pos);
|
|
467
|
-
pos += 2;
|
|
468
|
-
for (let e = 0; e < extentCount2; e++) {
|
|
469
|
-
if (indexSize > 0) pos += indexSize;
|
|
470
|
-
pos += offsetSize + lengthSize;
|
|
471
|
-
}
|
|
472
|
-
continue;
|
|
473
|
-
}
|
|
474
|
-
if (ilocVersion === 1 || ilocVersion === 2) {
|
|
475
|
-
const constructionMethod = buffer.readUInt16BE(pos);
|
|
476
|
-
pos += 2;
|
|
477
|
-
if (constructionMethod !== 0) return null;
|
|
478
|
-
}
|
|
479
|
-
pos += 2;
|
|
480
|
-
const baseOffset = this.readVarSize(buffer, pos, baseOffsetSize);
|
|
481
|
-
pos += baseOffsetSize;
|
|
482
|
-
const extentCount = buffer.readUInt16BE(pos);
|
|
483
|
-
pos += 2;
|
|
484
|
-
if (extentCount !== 1) return null;
|
|
485
|
-
if (indexSize > 0) pos += indexSize;
|
|
486
|
-
const extentOffset = this.readVarSize(buffer, pos, offsetSize);
|
|
487
|
-
pos += offsetSize;
|
|
488
|
-
const extentLength = this.readVarSize(buffer, pos, lengthSize);
|
|
489
|
-
xmpOffset = baseOffset + extentOffset;
|
|
490
|
-
xmpLength = extentLength;
|
|
491
|
-
break;
|
|
492
|
-
}
|
|
493
|
-
if (xmpOffset === 0 || xmpLength === 0 || xmpOffset + xmpLength > buffer.length)
|
|
494
|
-
return null;
|
|
495
|
-
return buffer.toString("utf8", xmpOffset, xmpOffset + xmpLength);
|
|
496
|
-
}
|
|
497
|
-
readVarSize(buffer, pos, size) {
|
|
498
|
-
if (size === 0) return 0;
|
|
499
|
-
if (size === 1 && buffer?.[pos]) return buffer?.[pos];
|
|
500
|
-
if (size === 2) return buffer.readUInt16BE(pos);
|
|
501
|
-
if (size === 3) {
|
|
502
|
-
let pos1 = buffer?.[pos + 1], pos2 = buffer?.[pos + 2];
|
|
503
|
-
if (buffer?.[pos] && pos1 && pos2) {
|
|
504
|
-
return buffer[pos] << 16 | pos1 << 8 | pos2;
|
|
505
|
-
}
|
|
506
|
-
throw new Error("Invalid var size");
|
|
507
|
-
}
|
|
508
|
-
if (size === 4) return buffer.readUInt32BE(pos);
|
|
509
|
-
if (size === 8) return Number(buffer.readBigUInt64BE(pos));
|
|
510
|
-
throw new Error("Invalid var size");
|
|
511
|
-
}
|
|
512
|
-
findBox(buffer, type, start = 0, end = buffer.length) {
|
|
513
|
-
let pos = start;
|
|
514
|
-
while (pos < end) {
|
|
515
|
-
let boxSize = buffer.readUInt32BE(pos);
|
|
516
|
-
let boxType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
517
|
-
let hdrSize = 8;
|
|
518
|
-
if (boxSize === 1) {
|
|
519
|
-
boxSize = Number(buffer.readBigUInt64BE(pos + 8));
|
|
520
|
-
hdrSize = 16;
|
|
521
|
-
} else if (boxSize === 0) {
|
|
522
|
-
boxSize = end - pos;
|
|
523
|
-
}
|
|
524
|
-
if (boxType === type) {
|
|
525
|
-
return { pos: pos + hdrSize, size: boxSize - hdrSize };
|
|
526
|
-
}
|
|
527
|
-
pos += boxSize;
|
|
528
|
-
}
|
|
529
|
-
return null;
|
|
530
|
-
}
|
|
531
|
-
mapChrmToColorSpace(chrm, fallback) {
|
|
532
|
-
const tol = 100;
|
|
533
|
-
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 64e3) < tol && Math.abs(chrm.red_y - 33e3) < tol && Math.abs(chrm.green_x - 3e4) < tol && Math.abs(chrm.green_y - 6e4) < tol && Math.abs(chrm.blue_x - 15e3) < tol && Math.abs(chrm.blue_y - 6e3) < tol)
|
|
534
|
-
return "srgb";
|
|
535
|
-
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 64e3) < tol && Math.abs(chrm.red_y - 33e3) < tol && Math.abs(chrm.green_x - 21e3) < tol && Math.abs(chrm.green_y - 71e3) < tol && Math.abs(chrm.blue_x - 15e3) < tol && Math.abs(chrm.blue_y - 6e3) < tol)
|
|
536
|
-
return "adobe_rgb";
|
|
537
|
-
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 68e3) < tol && Math.abs(chrm.red_y - 32e3) < tol && Math.abs(chrm.green_x - 26500) < tol && Math.abs(chrm.green_y - 69e3) < tol && Math.abs(chrm.blue_x - 15e3) < tol && Math.abs(chrm.blue_y - 6e3) < tol)
|
|
538
|
-
return "display_p3";
|
|
539
|
-
if (Math.abs(chrm.white_x - 34590) < tol && Math.abs(chrm.white_y - 35850) < tol && // D50 white point
|
|
540
|
-
Math.abs(chrm.red_x - 73470) < tol && Math.abs(chrm.red_y - 26530) < tol && Math.abs(chrm.green_x - 15960) < tol && Math.abs(chrm.green_y - 85040) < tol && Math.abs(chrm.blue_x - 3600) < tol && Math.abs(chrm.blue_y - 1e3) < tol)
|
|
541
|
-
return "prophoto_rgb";
|
|
542
|
-
if (Math.abs(chrm.white_x - 31270) < tol && Math.abs(chrm.white_y - 32900) < tol && Math.abs(chrm.red_x - 70800) < tol && Math.abs(chrm.red_y - 29200) < tol && Math.abs(chrm.green_x - 17e3) < tol && Math.abs(chrm.green_y - 79700) < tol && Math.abs(chrm.blue_x - 13100) < tol && Math.abs(chrm.blue_y - 4600) < tol)
|
|
543
|
-
return "rec2020";
|
|
544
|
-
return fallback;
|
|
545
|
-
}
|
|
546
|
-
xmpToColorSpaceWorkup(space, target) {
|
|
547
|
-
return `<photoshop:${target}>${space}</photoshop:${target}>`;
|
|
548
|
-
}
|
|
549
|
-
mapXmpToColorSpace(xmpText, fallback) {
|
|
550
|
-
const lower = xmpText.toLowerCase();
|
|
551
|
-
if (lower.includes(this.xmpToColorSpaceWorkup("srgb", "iccprofile")))
|
|
552
|
-
return "srgb";
|
|
553
|
-
if (lower.includes(
|
|
554
|
-
this.xmpToColorSpaceWorkup("adobe rgb (1998)", "iccprofile")
|
|
555
|
-
))
|
|
556
|
-
return "adobe_rgb";
|
|
557
|
-
if (lower.includes(this.xmpToColorSpaceWorkup("display p3", "iccprofile")))
|
|
558
|
-
return "display_p3";
|
|
559
|
-
if (lower.includes(this.xmpToColorSpaceWorkup("prophoto rgb", "iccprofile")))
|
|
560
|
-
return "prophoto_rgb";
|
|
561
|
-
if (lower.includes(this.xmpToColorSpaceWorkup("rgb", "colormode")) && lower.includes("adobe rgb"))
|
|
562
|
-
return "adobe_rgb";
|
|
563
|
-
if (lower.includes(this.xmpToColorSpaceWorkup("grayscale", "colormode")))
|
|
564
|
-
return "gray";
|
|
565
|
-
return fallback;
|
|
566
|
-
}
|
|
567
|
-
mapProfileToColorSpace(profileName, fallback) {
|
|
568
|
-
if (!profileName || profileName === "embedded" || profileName.toLowerCase().includes("icc profile"))
|
|
569
|
-
return fallback;
|
|
570
|
-
const lower = profileName.toLowerCase();
|
|
571
|
-
if (lower.includes("srgb") || lower.includes("iec61966") || lower.includes("srgb2014"))
|
|
572
|
-
return "srgb";
|
|
573
|
-
if (lower.includes("display p3") || lower.includes("display-p3") || lower.includes("dci-p3") || lower.includes("p3"))
|
|
574
|
-
return "display_p3";
|
|
575
|
-
if (lower.includes("adobe rgb") || lower.includes("adobergb") || lower.includes("adobe rgb (1998)"))
|
|
576
|
-
return "adobe_rgb";
|
|
577
|
-
if (lower.includes("prophoto rgb") || lower.includes("prophoto") || lower.includes("romm rgb") || lower.includes("romm") || lower.includes("iso 22028"))
|
|
578
|
-
return "prophoto_rgb";
|
|
579
|
-
if (lower.includes("rec2020") || lower.includes("bt.2020") || lower.includes("bt2020") || lower.includes("bt.2100") || lower.includes("itur_2100"))
|
|
580
|
-
return "rec2020";
|
|
581
|
-
if (lower.includes("rec709") || lower.includes("bt.709") || lower.includes("bt709"))
|
|
582
|
-
return "rec709";
|
|
583
|
-
if (lower.includes("cmyk")) return "cmyk";
|
|
584
|
-
if (lower.includes("lab")) return "lab";
|
|
585
|
-
if (lower.includes("xyz")) return "xyz";
|
|
586
|
-
if (lower.includes("gray") || lower.includes("grey") || lower.includes("monochrome") || lower.includes("gamma") || lower.includes("gamma 2.2") || lower.includes("dot gain"))
|
|
587
|
-
return "gray";
|
|
588
|
-
return fallback;
|
|
589
|
-
}
|
|
590
319
|
async extractViaStream(stream, size = 4096) {
|
|
591
320
|
const headerSize = size;
|
|
592
321
|
const header = await this.readBytes(stream, headerSize);
|
|
@@ -598,34 +327,6 @@ var ImageService = class extends MimeService {
|
|
|
598
327
|
});
|
|
599
328
|
return await this.extractViaStream(stream, size);
|
|
600
329
|
}
|
|
601
|
-
// Magic bytes for image format detection
|
|
602
|
-
signatures = {
|
|
603
|
-
PNG: [137, 80, 78, 71, 13, 10, 26, 10],
|
|
604
|
-
JPEG: [255, 216, 255],
|
|
605
|
-
GIF87a: [71, 73, 70, 56, 55, 97],
|
|
606
|
-
GIF89a: [71, 73, 70, 56, 57, 97],
|
|
607
|
-
WEBP: [82, 73, 70, 70],
|
|
608
|
-
// RIFF
|
|
609
|
-
BMP: [66, 77],
|
|
610
|
-
TIFF_LE: [73, 73, 42, 0],
|
|
611
|
-
TIFF_BE: [77, 77, 0, 42],
|
|
612
|
-
AVIF: [
|
|
613
|
-
0,
|
|
614
|
-
0,
|
|
615
|
-
0,
|
|
616
|
-
32,
|
|
617
|
-
102,
|
|
618
|
-
116,
|
|
619
|
-
121,
|
|
620
|
-
112,
|
|
621
|
-
97,
|
|
622
|
-
118,
|
|
623
|
-
105,
|
|
624
|
-
102
|
|
625
|
-
],
|
|
626
|
-
HEIC: [102, 116, 121, 112]
|
|
627
|
-
// ftyp at offset 4
|
|
628
|
-
};
|
|
629
330
|
/**
|
|
630
331
|
* Probe image from various sources
|
|
631
332
|
* Handles local Buffer, remote URL, or S3 object
|
|
@@ -712,14 +413,14 @@ var ImageService = class extends MimeService {
|
|
|
712
413
|
clearTimeout(timeoutId);
|
|
713
414
|
}
|
|
714
415
|
}
|
|
715
|
-
getImageSpecsWorkup(rawbuffer, size = 4096) {
|
|
416
|
+
getImageSpecsWorkup(rawbuffer, size = 4096 * 6) {
|
|
716
417
|
const MAX_HEADER_SIZE = size;
|
|
717
418
|
const buffer = rawbuffer.subarray(
|
|
718
419
|
0,
|
|
719
420
|
Math.min(rawbuffer.length, MAX_HEADER_SIZE)
|
|
720
421
|
);
|
|
721
422
|
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) {
|
|
722
|
-
if (buffer.
|
|
423
|
+
if (buffer.readUInt32BE(8) !== 13 || buffer?.[12] !== 73 || buffer?.[13] !== 72 || buffer?.[14] !== 68 || buffer?.[15] !== 82) {
|
|
723
424
|
throw new Error("IHDR Chunk of png not found or incorrect.");
|
|
724
425
|
}
|
|
725
426
|
const colorType = buffer[25];
|
|
@@ -756,12 +457,12 @@ var ImageService = class extends MimeService {
|
|
|
756
457
|
let animated = false;
|
|
757
458
|
let iccProfile = null;
|
|
758
459
|
let exifDateTimeOriginal = null;
|
|
759
|
-
|
|
460
|
+
const orientation = null;
|
|
760
461
|
let pos = 33;
|
|
761
462
|
while (pos < buffer.length - 12) {
|
|
762
463
|
const chunkLen = buffer.readUInt32BE(pos);
|
|
763
|
-
|
|
764
|
-
|
|
464
|
+
const chunkType = buffer.toString("ascii", pos + 4, pos + 8);
|
|
465
|
+
const chunkData = pos + 8;
|
|
765
466
|
if (chunkType === "acTL") {
|
|
766
467
|
animated = true;
|
|
767
468
|
frames = buffer.readUInt32BE(chunkData);
|
|
@@ -817,9 +518,9 @@ var ImageService = class extends MimeService {
|
|
|
817
518
|
colorSpace = this.mapXmpToColorSpace(text, colorSpace);
|
|
818
519
|
}
|
|
819
520
|
} else if (chunkType === "tIME") {
|
|
820
|
-
|
|
521
|
+
const month = buffer?.[chunkData + 2], day = buffer?.[chunkData + 3], hour = buffer?.[chunkData + 4], minute = buffer?.[chunkData + 5], second = buffer?.[chunkData + 6];
|
|
821
522
|
if (typeof month !== "undefined" && typeof day !== "undefined" && typeof hour !== "undefined" && typeof minute !== "undefined" && typeof second !== "undefined") {
|
|
822
|
-
|
|
523
|
+
const year = buffer.readUInt16BE(chunkData);
|
|
823
524
|
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")}`;
|
|
824
525
|
}
|
|
825
526
|
} else if (chunkType === "IDAT") {
|
|
@@ -843,18 +544,52 @@ var ImageService = class extends MimeService {
|
|
|
843
544
|
};
|
|
844
545
|
}
|
|
845
546
|
if (buffer.length >= 10 && buffer[0] === 255 && buffer[1] === 216) {
|
|
846
|
-
|
|
547
|
+
const colorSpace = "unknown", hasAlpha = false;
|
|
548
|
+
let pos = 2, colorModel = "unknown", width = 0, height = 0, iccProfile = null, orientation = null, exifDateTimeOriginal = null;
|
|
847
549
|
while (pos < buffer.length - 10) {
|
|
848
550
|
if (buffer[pos] !== 255) {
|
|
849
|
-
|
|
551
|
+
const nextFF = buffer.indexOf(255, pos + 1);
|
|
552
|
+
if (nextFF === -1 || nextFF >= buffer.length - 4) break;
|
|
553
|
+
pos = nextFF;
|
|
554
|
+
continue;
|
|
850
555
|
}
|
|
851
556
|
const marker = buffer[pos + 1];
|
|
852
557
|
if (marker === 218) break;
|
|
558
|
+
if (pos + 4 > buffer.length) break;
|
|
853
559
|
const segmentSize = buffer.readUInt16BE(pos + 2);
|
|
854
|
-
if (
|
|
855
|
-
|
|
560
|
+
if (segmentSize < 2) {
|
|
561
|
+
pos += 2;
|
|
562
|
+
continue;
|
|
563
|
+
}
|
|
564
|
+
const segmentEnd = pos + 2 + segmentSize;
|
|
565
|
+
if (segmentEnd > buffer.length) {
|
|
566
|
+
if (marker && marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
567
|
+
if (pos + 9 <= buffer.length) {
|
|
568
|
+
const numComponents = buffer[pos + 4];
|
|
569
|
+
switch (numComponents) {
|
|
570
|
+
case 1:
|
|
571
|
+
colorModel = "grayscale";
|
|
572
|
+
break;
|
|
573
|
+
case 3:
|
|
574
|
+
colorModel = "ycbcr";
|
|
575
|
+
break;
|
|
576
|
+
case 4:
|
|
577
|
+
colorModel = "ycck";
|
|
578
|
+
break;
|
|
579
|
+
default:
|
|
580
|
+
colorModel = "unknown";
|
|
581
|
+
}
|
|
582
|
+
if (pos + 7 <= buffer.length - 2) {
|
|
583
|
+
width = buffer.readUInt16BE(pos + 7);
|
|
584
|
+
}
|
|
585
|
+
if (pos + 5 <= buffer.length - 2) {
|
|
586
|
+
height = buffer.readUInt16BE(pos + 5);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
break;
|
|
856
591
|
}
|
|
857
|
-
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
592
|
+
if (marker && marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
858
593
|
const numComponents = buffer[pos + 4];
|
|
859
594
|
switch (numComponents) {
|
|
860
595
|
case 1:
|
|
@@ -882,13 +617,21 @@ var ImageService = class extends MimeService {
|
|
|
882
617
|
orientation = ori;
|
|
883
618
|
exifDateTimeOriginal = dateTimeOriginal;
|
|
884
619
|
} else if (marker === 226) {
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
620
|
+
if (pos + 18 <= buffer.length) {
|
|
621
|
+
const iccHeader = buffer.toString("ascii", pos + 4, pos + 18);
|
|
622
|
+
if (iccHeader.startsWith("ICC_PROFILE")) {
|
|
623
|
+
iccProfile = "embedded";
|
|
624
|
+
}
|
|
888
625
|
}
|
|
626
|
+
} else if (marker === 255) {
|
|
627
|
+
pos++;
|
|
628
|
+
continue;
|
|
889
629
|
}
|
|
890
|
-
pos
|
|
891
|
-
while (pos < buffer.length
|
|
630
|
+
pos = segmentEnd;
|
|
631
|
+
while (pos < buffer.length && buffer[pos] === 0) {
|
|
632
|
+
pos++;
|
|
633
|
+
}
|
|
634
|
+
while (pos < buffer.length - 1 && buffer[pos] === 255 && buffer[pos + 1] === 255) {
|
|
892
635
|
pos++;
|
|
893
636
|
}
|
|
894
637
|
}
|
|
@@ -914,7 +657,7 @@ var ImageService = class extends MimeService {
|
|
|
914
657
|
const width = buffer.readUInt16LE(6), height = buffer.readUInt16LE(8);
|
|
915
658
|
let frames = 0;
|
|
916
659
|
let pos = 13;
|
|
917
|
-
|
|
660
|
+
const bufTen = buffer?.[10];
|
|
918
661
|
if (typeof bufTen === "undefined") {
|
|
919
662
|
pos = 13;
|
|
920
663
|
frames = 0;
|
|
@@ -931,7 +674,7 @@ var ImageService = class extends MimeService {
|
|
|
931
674
|
} else if (buffer?.[pos] && buffer?.[pos] === 44) {
|
|
932
675
|
frames++;
|
|
933
676
|
pos += 10;
|
|
934
|
-
|
|
677
|
+
const b = buffer?.[pos - 1];
|
|
935
678
|
if (typeof b !== "undefined" && b & 128)
|
|
936
679
|
pos += 3 << (b & 7) + 1;
|
|
937
680
|
pos++;
|
|
@@ -970,7 +713,7 @@ var ImageService = class extends MimeService {
|
|
|
970
713
|
const width = buffer.readInt32LE(18);
|
|
971
714
|
const height = Math.abs(buffer.readInt32LE(22));
|
|
972
715
|
const bitDepth = buffer.readUInt16LE(28);
|
|
973
|
-
|
|
716
|
+
const colorModel = bitDepth <= 8 ? "indexed" : "rgb";
|
|
974
717
|
return {
|
|
975
718
|
width,
|
|
976
719
|
height,
|
|
@@ -991,7 +734,8 @@ var ImageService = class extends MimeService {
|
|
|
991
734
|
}
|
|
992
735
|
if (buffer.length >= 30 && buffer.toString("ascii", 0, 4) === "RIFF" && buffer.toString("ascii", 8, 12) === "WEBP") {
|
|
993
736
|
const chunkType = buffer.toString("ascii", 12, 16);
|
|
994
|
-
|
|
737
|
+
const colorSpace = "unknown";
|
|
738
|
+
let colorModel = "unknown", hasAlpha = false, width = 0, height = 0, frames = 1, animated = false, iccProfile = null;
|
|
995
739
|
if (chunkType === "VP8X") {
|
|
996
740
|
const flags = buffer?.[20];
|
|
997
741
|
colorModel = flags ? flags & 2 ? "rgba" : "rgb" : "unknown";
|
|
@@ -1121,7 +865,7 @@ var ImageService = class extends MimeService {
|
|
|
1121
865
|
}
|
|
1122
866
|
}
|
|
1123
867
|
let frames = 1;
|
|
1124
|
-
|
|
868
|
+
const animated = isAvis;
|
|
1125
869
|
if (animated) {
|
|
1126
870
|
const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
|
|
1127
871
|
if (iloc) {
|
|
@@ -1149,6 +893,438 @@ var ImageService = class extends MimeService {
|
|
|
1149
893
|
// Can have 'XMP' box, but parse XMP for date if needed
|
|
1150
894
|
};
|
|
1151
895
|
}
|
|
896
|
+
if (buffer.length >= 32 && buffer.toString("ascii", 4, 8) === "ftyp") {
|
|
897
|
+
const ftyp = this.findBox(buffer, "ftyp");
|
|
898
|
+
if (!ftyp) throw new Error("Invalid HEIC: No ftyp");
|
|
899
|
+
const brands = buffer.toString("ascii", ftyp.pos, ftyp.pos + ftyp.size).toLowerCase();
|
|
900
|
+
const isHeic = brands.includes("heic") || brands.includes("heix") || brands.includes("heim") || brands.includes("heis") || brands.includes("hevc") || brands.includes("hevx") || brands.includes("mif1");
|
|
901
|
+
if (!isHeic) throw new Error("Not a HEIC file");
|
|
902
|
+
const meta = this.findBox(buffer, "meta");
|
|
903
|
+
if (!meta) throw new Error("Invalid HEIC: No meta");
|
|
904
|
+
const metaSubStart = meta.pos + 4;
|
|
905
|
+
const metaSubEnd = meta.pos + meta.size;
|
|
906
|
+
const iprp = this.findBox(buffer, "iprp", metaSubStart, metaSubEnd);
|
|
907
|
+
if (!iprp) throw new Error("Invalid HEIC: No iprp");
|
|
908
|
+
const ipco = this.findBox(buffer, "ipco", iprp.pos, iprp.pos + iprp.size);
|
|
909
|
+
if (!ipco) throw new Error("Invalid HEIC: No ipco");
|
|
910
|
+
const ispe = this.findBox(buffer, "ispe", ipco.pos, ipco.pos + ipco.size);
|
|
911
|
+
if (!ispe) throw new Error("Invalid HEIC: No ispe");
|
|
912
|
+
if (buffer[ispe.pos] !== 0) throw new Error("Invalid ispe version");
|
|
913
|
+
const width = buffer.readUInt32BE(ispe.pos + 4);
|
|
914
|
+
const height = buffer.readUInt32BE(ispe.pos + 8);
|
|
915
|
+
let colorSpace = "rgb", colorModel = "rgb", hasAlpha = false, iccProfile = null;
|
|
916
|
+
const colr = this.findBox(buffer, "colr", ipco.pos, ipco.pos + ipco.size);
|
|
917
|
+
if (colr) {
|
|
918
|
+
const colrType = buffer.toString("ascii", colr.pos, colr.pos + 4);
|
|
919
|
+
if (colrType === "nclx") {
|
|
920
|
+
const matrix = buffer.readUInt16BE(colr.pos + 6);
|
|
921
|
+
colorModel = matrix === 2 ? "rgb" : "ycbcr";
|
|
922
|
+
} else if (colrType === "rICC" || colrType === "prof") {
|
|
923
|
+
colorSpace = "unknown";
|
|
924
|
+
iccProfile = "embedded";
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
const auxC = this.findBox(buffer, "auxC", ipco.pos, ipco.pos + ipco.size);
|
|
928
|
+
if (auxC && buffer.toString("ascii", auxC.pos, auxC.pos + auxC.size).includes("alpha")) {
|
|
929
|
+
hasAlpha = true;
|
|
930
|
+
if (colorModel === "rgb") {
|
|
931
|
+
colorModel = "rgba";
|
|
932
|
+
} else if (colorModel === "ycbcr") {
|
|
933
|
+
colorModel = "ycck";
|
|
934
|
+
} else if (colorModel === "grayscale" || colorModel === "unknown") {
|
|
935
|
+
colorModel = "grayscale-alpha";
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
const xmpXml = this.parseXmpFromAvif(buffer, meta, ipco);
|
|
939
|
+
let exifDateTimeOriginal = null;
|
|
940
|
+
if (xmpXml) {
|
|
941
|
+
let dateStart = xmpXml.indexOf('xmp:CreateDate="');
|
|
942
|
+
if (dateStart === -1)
|
|
943
|
+
dateStart = xmpXml.indexOf('photoshop:DateCreated="');
|
|
944
|
+
if (dateStart !== -1) {
|
|
945
|
+
dateStart += 16;
|
|
946
|
+
const dateEnd = xmpXml.indexOf('"', dateStart);
|
|
947
|
+
if (dateEnd !== -1) {
|
|
948
|
+
exifDateTimeOriginal = xmpXml.substring(dateStart, dateEnd);
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
let frames = 1;
|
|
953
|
+
const animated = brands.includes("heim") || brands.includes("heis");
|
|
954
|
+
if (animated) {
|
|
955
|
+
const iloc = this.findBox(buffer, "iloc", metaSubStart, metaSubEnd);
|
|
956
|
+
if (iloc) {
|
|
957
|
+
const version = buffer?.[iloc.pos] ?? 0;
|
|
958
|
+
const itemCountPos = iloc.pos + (version < 2 ? 4 : 6);
|
|
959
|
+
let itemCount = buffer?.readUInt16BE(itemCountPos) ?? 0;
|
|
960
|
+
if (version >= 2) itemCount = buffer?.readUInt32BE(itemCountPos) ?? 0;
|
|
961
|
+
frames = Math.max(1, Math.floor(itemCount / (hasAlpha ? 2 : 1)));
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return {
|
|
965
|
+
width,
|
|
966
|
+
height,
|
|
967
|
+
format: "heic",
|
|
968
|
+
frames,
|
|
969
|
+
animated,
|
|
970
|
+
colorModel,
|
|
971
|
+
hasAlpha: hasAlpha ? true : null,
|
|
972
|
+
orientation: null,
|
|
973
|
+
// Can have 'irot' or 'imir' transforms, or EXIF; add parsing if needed
|
|
974
|
+
aspectRatio: width / height,
|
|
975
|
+
colorSpace,
|
|
976
|
+
iccProfile,
|
|
977
|
+
exifDateTimeOriginal
|
|
978
|
+
// Can have dedicated 'Exif' item; add parser similar to XMP if required
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
const headerText = buffer.toString("utf-8", 0, Math.min(1024, buffer.length)).trim();
|
|
982
|
+
if (headerText.startsWith("<svg") || headerText.startsWith("<?xml") && headerText.includes("<svg")) {
|
|
983
|
+
const widthMatch = headerText.match(
|
|
984
|
+
/width\s*=\s*["']?(\d+(?:\.\d+)?)(?:px)?["']?/i
|
|
985
|
+
);
|
|
986
|
+
const heightMatch = headerText.match(
|
|
987
|
+
/height\s*=\s*["']?(\d+(?:\.\d+)?)(?:px)?["']?/i
|
|
988
|
+
);
|
|
989
|
+
const viewBoxMatch = headerText.match(
|
|
990
|
+
/viewBox\s*=\s*["']?(\d+(?:\.\d+)?\s+\d+(?:\.\d+)?\s+\d+(?:\.\d+)?\s+\d+(?:\.\d+)?)["']?/i
|
|
991
|
+
);
|
|
992
|
+
let width = 0;
|
|
993
|
+
let height = 0;
|
|
994
|
+
if (widthMatch?.[1] && heightMatch?.[1]) {
|
|
995
|
+
width = parseFloat(widthMatch[1]);
|
|
996
|
+
height = parseFloat(heightMatch[1]);
|
|
997
|
+
} else if (viewBoxMatch) {
|
|
998
|
+
const [_x0, _y0, vbWidth, vbHeight] = viewBoxMatch?.[1]?.split(/\s+/).map((t) => Number.parseFloat(t));
|
|
999
|
+
width = vbWidth;
|
|
1000
|
+
height = vbHeight;
|
|
1001
|
+
} else {
|
|
1002
|
+
width = 0;
|
|
1003
|
+
height = 0;
|
|
1004
|
+
throw new Error("SVG has no defined width/height or viewBox");
|
|
1005
|
+
}
|
|
1006
|
+
const animated = headerText.includes("<animate") || headerText.includes("<animation") || headerText.includes("<motion.");
|
|
1007
|
+
const frames = 1;
|
|
1008
|
+
return {
|
|
1009
|
+
width,
|
|
1010
|
+
height,
|
|
1011
|
+
format: "svg",
|
|
1012
|
+
frames,
|
|
1013
|
+
animated,
|
|
1014
|
+
hasAlpha: true,
|
|
1015
|
+
// SVG supports transparency
|
|
1016
|
+
orientation: null,
|
|
1017
|
+
aspectRatio: width / height || 1,
|
|
1018
|
+
// Fallback if 0
|
|
1019
|
+
colorModel: "vector",
|
|
1020
|
+
colorSpace: "srgb",
|
|
1021
|
+
// Typically sRGB for web SVGs
|
|
1022
|
+
iccProfile: null,
|
|
1023
|
+
// Rare, but can embed ICC in <color-profile>
|
|
1024
|
+
exifDateTimeOriginal: null
|
|
1025
|
+
// No standard EXIF
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
if (buffer.length >= 6 && buffer[0] === 0 && buffer[1] === 0 && buffer[2] === 1 && buffer[3] === 0) {
|
|
1029
|
+
const frames = buffer.readUInt16LE(4);
|
|
1030
|
+
if (frames === 0) throw new Error("Invalid ICO: No images");
|
|
1031
|
+
let maxWidth = 0;
|
|
1032
|
+
let maxHeight = 0;
|
|
1033
|
+
let hasAlpha = null;
|
|
1034
|
+
let colorModel = "unknown";
|
|
1035
|
+
for (let i = 0; i < frames; i++) {
|
|
1036
|
+
const entryPos = 6 + i * 16;
|
|
1037
|
+
if (entryPos + 16 > buffer.length) break;
|
|
1038
|
+
let entryWidth = buffer[entryPos];
|
|
1039
|
+
let entryHeight = buffer[entryPos + 1];
|
|
1040
|
+
entryWidth = entryWidth === 0 ? 256 : entryWidth;
|
|
1041
|
+
entryHeight = entryHeight === 0 ? 256 : entryHeight;
|
|
1042
|
+
const bpp = buffer.readUInt16LE(entryPos + 6);
|
|
1043
|
+
const _size = buffer.readUInt32LE(entryPos + 8);
|
|
1044
|
+
const offset = buffer.readUInt32LE(entryPos + 12);
|
|
1045
|
+
if (bpp <= 8) colorModel = "indexed";
|
|
1046
|
+
else if (bpp === 24) colorModel = "rgb";
|
|
1047
|
+
else if (bpp === 32) {
|
|
1048
|
+
colorModel = "rgba";
|
|
1049
|
+
hasAlpha = true;
|
|
1050
|
+
}
|
|
1051
|
+
if (offset + 8 <= rawbuffer.length && // Use full buffer for data
|
|
1052
|
+
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) {
|
|
1053
|
+
hasAlpha = true;
|
|
1054
|
+
colorModel = "rgba";
|
|
1055
|
+
}
|
|
1056
|
+
if (entryWidth && entryWidth > maxWidth) maxWidth = entryWidth;
|
|
1057
|
+
if (entryHeight && entryHeight > maxHeight) maxHeight = entryHeight;
|
|
1058
|
+
}
|
|
1059
|
+
return {
|
|
1060
|
+
width: maxWidth,
|
|
1061
|
+
height: maxHeight,
|
|
1062
|
+
format: "ico",
|
|
1063
|
+
frames,
|
|
1064
|
+
animated: false,
|
|
1065
|
+
// ICO not animated
|
|
1066
|
+
hasAlpha,
|
|
1067
|
+
orientation: null,
|
|
1068
|
+
aspectRatio: maxWidth / maxHeight,
|
|
1069
|
+
colorModel,
|
|
1070
|
+
colorSpace: "srgb",
|
|
1071
|
+
// Typically
|
|
1072
|
+
iccProfile: null,
|
|
1073
|
+
exifDateTimeOriginal: null
|
|
1074
|
+
};
|
|
1075
|
+
}
|
|
1076
|
+
if (buffer.length >= 8 && // "II*\0" (little-endian classic TIFF)
|
|
1077
|
+
(buffer[0] === 73 && buffer[1] === 73 && buffer[2] === 42 && buffer[3] === 0 || // "MM\0*" (big-endian classic TIFF)
|
|
1078
|
+
buffer[0] === 77 && buffer[1] === 77 && buffer[2] === 0 && buffer[3] === 42 || // BigTIFF magic "II+\0" or "MM\0+"
|
|
1079
|
+
buffer[0] === 73 && buffer[1] === 73 && buffer[2] === 43 && buffer[3] === 0 || buffer[0] === 77 && buffer[1] === 77 && buffer[2] === 0 && buffer[3] === 43)) {
|
|
1080
|
+
let readIFDValue2 = function(entryOff, type, count) {
|
|
1081
|
+
const valueOrOff = readU32(entryOff + 8);
|
|
1082
|
+
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;
|
|
1083
|
+
let dataOff = valueOrOff;
|
|
1084
|
+
if (valueFitsInline) {
|
|
1085
|
+
dataOff = entryOff + 8;
|
|
1086
|
+
} else {
|
|
1087
|
+
dataOff = TIFF_START + valueOrOff;
|
|
1088
|
+
}
|
|
1089
|
+
if (dataOff < 0 || dataOff >= src.length) return null;
|
|
1090
|
+
const readShortN = /* @__PURE__ */ __name((o) => readU16(o), "readShortN");
|
|
1091
|
+
const readLongN = /* @__PURE__ */ __name((o) => readU32(o), "readLongN");
|
|
1092
|
+
switch (type) {
|
|
1093
|
+
case TYPE.ASCII: {
|
|
1094
|
+
const end = Math.min(
|
|
1095
|
+
src.indexOf(0, dataOff) === -1 ? dataOff + count : src.indexOf(0, dataOff),
|
|
1096
|
+
src.length
|
|
1097
|
+
);
|
|
1098
|
+
try {
|
|
1099
|
+
return src.toString("ascii", dataOff, end);
|
|
1100
|
+
} catch {
|
|
1101
|
+
return null;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
case TYPE.BYTE:
|
|
1105
|
+
case TYPE.SBYTE:
|
|
1106
|
+
case TYPE.UNDEFINED: {
|
|
1107
|
+
const out = [];
|
|
1108
|
+
for (let i = 0; i < count; i++) {
|
|
1109
|
+
const off = dataOff + i;
|
|
1110
|
+
if (off >= src.length || !src[off]) break;
|
|
1111
|
+
out.push(src[off]);
|
|
1112
|
+
}
|
|
1113
|
+
return count === 1 ? out[0] ?? null : out;
|
|
1114
|
+
}
|
|
1115
|
+
case TYPE.SHORT: {
|
|
1116
|
+
const out = [];
|
|
1117
|
+
for (let i = 0; i < count; i++) {
|
|
1118
|
+
const off = dataOff + i * 2;
|
|
1119
|
+
if (off + 2 > src.length) break;
|
|
1120
|
+
out.push(readShortN(off));
|
|
1121
|
+
}
|
|
1122
|
+
return count === 1 ? out[0] ?? null : out;
|
|
1123
|
+
}
|
|
1124
|
+
case TYPE.LONG: {
|
|
1125
|
+
const out = [];
|
|
1126
|
+
for (let i = 0; i < count; i++) {
|
|
1127
|
+
const off = dataOff + i * 4;
|
|
1128
|
+
if (off + 4 > src.length) break;
|
|
1129
|
+
out.push(readLongN(off));
|
|
1130
|
+
}
|
|
1131
|
+
return count === 1 ? out[0] ?? null : out;
|
|
1132
|
+
}
|
|
1133
|
+
case TYPE.RATIONAL: {
|
|
1134
|
+
const out = [];
|
|
1135
|
+
for (let i = 0; i < count; i++) {
|
|
1136
|
+
const off = dataOff + i * 8;
|
|
1137
|
+
if (off + 8 > src.length) break;
|
|
1138
|
+
const num = readLongN(off);
|
|
1139
|
+
const den = readLongN(off + 4);
|
|
1140
|
+
out.push(den ? num / den : 0);
|
|
1141
|
+
}
|
|
1142
|
+
return count === 1 ? out[0] ?? null : out;
|
|
1143
|
+
}
|
|
1144
|
+
default:
|
|
1145
|
+
return null;
|
|
1146
|
+
}
|
|
1147
|
+
};
|
|
1148
|
+
var readIFDValue = readIFDValue2;
|
|
1149
|
+
__name(readIFDValue2, "readIFDValue");
|
|
1150
|
+
const little = buffer[0] === 73;
|
|
1151
|
+
const isBigTiff = buffer[2] === 43 || buffer[3] === 43;
|
|
1152
|
+
if (isBigTiff) {
|
|
1153
|
+
throw new Error(
|
|
1154
|
+
"BigTIFF (0x2B) detected \u2014 not supported in lightweight header parser."
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
const src = rawbuffer;
|
|
1158
|
+
const readU16 = /* @__PURE__ */ __name((off) => little ? src.readUInt16LE(off) : src.readUInt16BE(off), "readU16");
|
|
1159
|
+
const readU32 = /* @__PURE__ */ __name((off) => little ? src.readUInt32LE(off) : src.readUInt32BE(off), "readU32");
|
|
1160
|
+
const TIFF_START = 0;
|
|
1161
|
+
const firstIFDOff = readU32(4);
|
|
1162
|
+
let ifdOff = TIFF_START + firstIFDOff;
|
|
1163
|
+
let frames = 0;
|
|
1164
|
+
let width = null;
|
|
1165
|
+
let height = null;
|
|
1166
|
+
let samplesPerPixel = null;
|
|
1167
|
+
let extraSamples = [];
|
|
1168
|
+
let photometric = null;
|
|
1169
|
+
let orientation = null;
|
|
1170
|
+
let iccPresent = false;
|
|
1171
|
+
let exifIFDOffset = null;
|
|
1172
|
+
const TAG = {
|
|
1173
|
+
ImageWidth: 256,
|
|
1174
|
+
ImageLength: 257,
|
|
1175
|
+
BitsPerSample: 258,
|
|
1176
|
+
// not strictly needed here
|
|
1177
|
+
Compression: 259,
|
|
1178
|
+
// not strictly needed here
|
|
1179
|
+
PhotometricInterpretation: 262,
|
|
1180
|
+
StripOffsets: 273,
|
|
1181
|
+
// not needed for metadata
|
|
1182
|
+
Orientation: 274,
|
|
1183
|
+
SamplesPerPixel: 277,
|
|
1184
|
+
RowsPerStrip: 278,
|
|
1185
|
+
// not needed for metadata
|
|
1186
|
+
PlanarConfiguration: 284,
|
|
1187
|
+
// not needed for metadata
|
|
1188
|
+
ExtraSamples: 338,
|
|
1189
|
+
ICCProfile: 34675,
|
|
1190
|
+
ExifIFDPointer: 34665,
|
|
1191
|
+
// EXIF IFD:
|
|
1192
|
+
DateTimeOriginal: 36867
|
|
1193
|
+
};
|
|
1194
|
+
const TYPE = {
|
|
1195
|
+
BYTE: 1,
|
|
1196
|
+
ASCII: 2,
|
|
1197
|
+
SHORT: 3,
|
|
1198
|
+
LONG: 4,
|
|
1199
|
+
RATIONAL: 5,
|
|
1200
|
+
SBYTE: 6,
|
|
1201
|
+
UNDEFINED: 7,
|
|
1202
|
+
SSHORT: 8,
|
|
1203
|
+
SLONG: 9,
|
|
1204
|
+
SRATIONAL: 10,
|
|
1205
|
+
FLOAT: 11,
|
|
1206
|
+
DOUBLE: 12
|
|
1207
|
+
};
|
|
1208
|
+
const MAX_IFDS = 32;
|
|
1209
|
+
while (ifdOff > 0 && ifdOff + 2 <= src.length && frames < MAX_IFDS) {
|
|
1210
|
+
const numEntries = readU16(ifdOff);
|
|
1211
|
+
const entriesBase = ifdOff + 2;
|
|
1212
|
+
for (let i = 0; i < numEntries; i++) {
|
|
1213
|
+
const entry = entriesBase + i * 12;
|
|
1214
|
+
if (entry + 12 > src.length) break;
|
|
1215
|
+
const tag = readU16(entry);
|
|
1216
|
+
const type = readU16(entry + 2);
|
|
1217
|
+
const count = readU32(entry + 4);
|
|
1218
|
+
if (tag === TAG.ImageWidth && width == null) {
|
|
1219
|
+
const v = readIFDValue2(entry, type, count);
|
|
1220
|
+
if (typeof v === "number") width = v;
|
|
1221
|
+
if (Array.isArray(v) && typeof v[0] === "number") width = v[0];
|
|
1222
|
+
} else if (tag === TAG.ImageLength && height == null) {
|
|
1223
|
+
const v = readIFDValue2(entry, type, count);
|
|
1224
|
+
if (typeof v === "number") height = v;
|
|
1225
|
+
if (Array.isArray(v) && typeof v[0] === "number") height = v[0];
|
|
1226
|
+
} else if (tag === TAG.SamplesPerPixel && samplesPerPixel == null) {
|
|
1227
|
+
const v = readIFDValue2(entry, type, count);
|
|
1228
|
+
if (typeof v === "number") samplesPerPixel = v;
|
|
1229
|
+
} else if (tag === TAG.ExtraSamples) {
|
|
1230
|
+
const v = readIFDValue2(entry, type, count);
|
|
1231
|
+
if (typeof v === "number") extraSamples = [v];
|
|
1232
|
+
else if (Array.isArray(v))
|
|
1233
|
+
extraSamples = v.filter((n) => typeof n === "number");
|
|
1234
|
+
} else if (tag === TAG.PhotometricInterpretation && photometric == null) {
|
|
1235
|
+
const v = readIFDValue2(entry, type, count);
|
|
1236
|
+
if (typeof v === "number") photometric = v;
|
|
1237
|
+
} else if (tag === TAG.Orientation && orientation == null) {
|
|
1238
|
+
const v = readIFDValue2(entry, type, count);
|
|
1239
|
+
if (typeof v === "number") orientation = v;
|
|
1240
|
+
} else if (tag === TAG.ICCProfile) {
|
|
1241
|
+
iccPresent = true;
|
|
1242
|
+
} else if (tag === TAG.ExifIFDPointer && exifIFDOffset == null) {
|
|
1243
|
+
const v = readIFDValue2(entry, type, count);
|
|
1244
|
+
if (typeof v === "number") exifIFDOffset = TIFF_START + v;
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
frames++;
|
|
1248
|
+
const nextPtrOff = entriesBase + numEntries * 12;
|
|
1249
|
+
if (nextPtrOff + 4 > src.length) break;
|
|
1250
|
+
const nextRel = readU32(nextPtrOff);
|
|
1251
|
+
ifdOff = nextRel ? TIFF_START + nextRel : 0;
|
|
1252
|
+
}
|
|
1253
|
+
let exifDateTimeOriginal = null;
|
|
1254
|
+
if (exifIFDOffset && exifIFDOffset + 2 <= src.length) {
|
|
1255
|
+
try {
|
|
1256
|
+
const num = readU16(exifIFDOffset);
|
|
1257
|
+
const base = exifIFDOffset + 2;
|
|
1258
|
+
for (let i = 0; i < num; i++) {
|
|
1259
|
+
const entry = base + i * 12;
|
|
1260
|
+
if (entry + 12 > src.length) break;
|
|
1261
|
+
const tag = readU16(entry);
|
|
1262
|
+
if (tag === TAG.DateTimeOriginal) {
|
|
1263
|
+
const type = readU16(entry + 2);
|
|
1264
|
+
const count = readU32(entry + 4);
|
|
1265
|
+
const val = readIFDValue2(entry, type, count);
|
|
1266
|
+
if (typeof val === "string" && /^\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}/.test(val)) {
|
|
1267
|
+
exifDateTimeOriginal = val;
|
|
1268
|
+
}
|
|
1269
|
+
break;
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
} catch {
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
if (!width || !height) {
|
|
1276
|
+
throw new Error(
|
|
1277
|
+
"Invalid TIFF: missing ImageWidth/ImageLength in first IFD (consider reading more than header)."
|
|
1278
|
+
);
|
|
1279
|
+
}
|
|
1280
|
+
let colorModel = "unknown";
|
|
1281
|
+
let colorSpace = "unknown";
|
|
1282
|
+
switch (photometric) {
|
|
1283
|
+
case 0:
|
|
1284
|
+
case 1:
|
|
1285
|
+
colorModel = samplesPerPixel === 2 || extraSamples.length ? "grayscale-alpha" : "grayscale";
|
|
1286
|
+
colorSpace = "gray";
|
|
1287
|
+
break;
|
|
1288
|
+
case 2:
|
|
1289
|
+
colorModel = samplesPerPixel === 4 || extraSamples.length ? "rgba" : "rgb";
|
|
1290
|
+
colorSpace = "srgb";
|
|
1291
|
+
break;
|
|
1292
|
+
case 3:
|
|
1293
|
+
colorModel = "indexed";
|
|
1294
|
+
colorSpace = "srgb";
|
|
1295
|
+
break;
|
|
1296
|
+
case 5:
|
|
1297
|
+
colorModel = "cmyk";
|
|
1298
|
+
colorSpace = "cmyk";
|
|
1299
|
+
break;
|
|
1300
|
+
case 6:
|
|
1301
|
+
colorModel = "ycbcr";
|
|
1302
|
+
colorSpace = "srgb";
|
|
1303
|
+
break;
|
|
1304
|
+
case 8:
|
|
1305
|
+
colorModel = "lab";
|
|
1306
|
+
colorSpace = "lab";
|
|
1307
|
+
break;
|
|
1308
|
+
default:
|
|
1309
|
+
break;
|
|
1310
|
+
}
|
|
1311
|
+
const hasAlpha = extraSamples.some((v) => v === 1 || v === 2) || // 1: associated alpha, 2: unassociated
|
|
1312
|
+
photometric === 2 && samplesPerPixel === 4 || (photometric === 0 || photometric === 1) && samplesPerPixel === 2;
|
|
1313
|
+
return {
|
|
1314
|
+
width,
|
|
1315
|
+
height,
|
|
1316
|
+
format: "tiff",
|
|
1317
|
+
frames,
|
|
1318
|
+
animated: false,
|
|
1319
|
+
hasAlpha,
|
|
1320
|
+
orientation: orientation ?? null,
|
|
1321
|
+
aspectRatio: width / height,
|
|
1322
|
+
colorModel,
|
|
1323
|
+
colorSpace,
|
|
1324
|
+
iccProfile: iccPresent ? "embedded" : null,
|
|
1325
|
+
exifDateTimeOriginal
|
|
1326
|
+
};
|
|
1327
|
+
}
|
|
1152
1328
|
throw new Error("Unsupported image format or invalid file");
|
|
1153
1329
|
}
|
|
1154
1330
|
};
|
|
@@ -1156,4 +1332,4 @@ var ImageService = class extends MimeService {
|
|
|
1156
1332
|
export {
|
|
1157
1333
|
ImageService
|
|
1158
1334
|
};
|
|
1159
|
-
//# sourceMappingURL=chunk-
|
|
1335
|
+
//# sourceMappingURL=chunk-TUY7Y4DL.js.map
|