This commit is contained in:
2026-02-21 17:50:49 +11:00
parent 6c741577ce
commit 8b0960d366
7 changed files with 133 additions and 155 deletions

View File

@@ -145,15 +145,11 @@ function isPaarrotXMP(segmentData: Uint8Array): boolean {
export function extractColorFromJPEG(imageData: ArrayBuffer | Uint8Array): string | undefined {
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
console.log('[extractColorFromJPEG] Input size:', data.length, 'bytes');
if (!isJPEG(data)) {
console.error('[extractColorFromJPEG] Not a JPEG file');
return undefined;
}
const segments = parseSegments(data);
console.log('[extractColorFromJPEG] Found', segments.length, 'segments');
for (const segment of segments) {
// APP1 marker (0xE1) is used for XMP
@@ -172,7 +168,6 @@ export function extractColorFromJPEG(imageData: ArrayBuffer | Uint8Array): strin
const xmpString = new TextDecoder().decode(segment.data.slice(XMP_NAMESPACE_BYTES.length));
const color = extractColorFromXMP(xmpString);
if (color) {
console.log('[extractColorFromJPEG] Found color:', color);
return color;
}
}
@@ -180,7 +175,6 @@ export function extractColorFromJPEG(imageData: ArrayBuffer | Uint8Array): strin
}
}
console.log('[extractColorFromJPEG] No color found');
return undefined;
}
@@ -190,15 +184,11 @@ export function extractColorFromJPEG(imageData: ArrayBuffer | Uint8Array): strin
export function embedColorInJPEG(imageData: ArrayBuffer | Uint8Array, color: string): Uint8Array | null {
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
console.log('[embedColorInJPEG] Input size:', data.length, 'bytes');
if (!isJPEG(data)) {
console.error('[embedColorInJPEG] Not a JPEG file');
return null;
}
const segments = parseSegments(data);
console.log('[embedColorInJPEG] Found', segments.length, 'segments');
// Find existing paarrot XMP segment
let existingSegment: { offset: number; length: number } | null = null;
@@ -225,14 +215,10 @@ export function embedColorInJPEG(imageData: ArrayBuffer | Uint8Array, color: str
app1Segment.set(writeUint16BE(segmentLength), 2);
app1Segment.set(segmentData, 4);
console.log('[embedColorInJPEG] Created APP1 segment, size:', app1Segment.length, 'bytes');
console.log('[embedColorInJPEG] Color being embedded:', color);
let newData: Uint8Array;
if (existingSegment) {
// Replace existing segment
console.log('[embedColorInJPEG] Replacing existing segment at offset', existingSegment.offset);
newData = new Uint8Array(data.length - existingSegment.length + app1Segment.length);
newData.set(data.slice(0, existingSegment.offset), 0);
newData.set(app1Segment, existingSegment.offset);
@@ -242,15 +228,12 @@ export function embedColorInJPEG(imageData: ArrayBuffer | Uint8Array, color: str
);
} else {
// Insert after SOI marker (at offset 2)
console.log('[embedColorInJPEG] Inserting new segment after SOI');
newData = new Uint8Array(data.length + app1Segment.length);
newData.set(JPEG_SOI, 0);
newData.set(app1Segment, 2);
newData.set(data.slice(2), 2 + app1Segment.length);
}
console.log('[embedColorInJPEG] Output size:', newData.length, 'bytes');
return newData;
}