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
-17
View File
@@ -123,28 +123,22 @@ function parseChunks(data: Uint8Array): Array<{ id: string; data: Uint8Array; of
export function extractColorFromWebP(imageData: ArrayBuffer | Uint8Array): string | undefined {
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
console.log('[extractColorFromWebP] Input size:', data.length, 'bytes');
if (!isWebP(data)) {
console.error('[extractColorFromWebP] Not a WebP file');
return undefined;
}
const chunks = parseChunks(data);
console.log('[extractColorFromWebP] Found', chunks.length, 'chunks:', chunks.map(c => c.id).join(', '));
for (const chunk of chunks) {
if (chunk.id === 'XMP ') {
const xmpString = new TextDecoder().decode(chunk.data);
const color = extractColorFromXMP(xmpString);
if (color) {
console.log('[extractColorFromWebP] Found color:', color);
return color;
}
}
}
console.log('[extractColorFromWebP] No color found');
return undefined;
}
@@ -157,15 +151,11 @@ export function extractColorFromWebP(imageData: ArrayBuffer | Uint8Array): strin
export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: string): Uint8Array | null {
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
console.log('[embedColorInWebP] Input size:', data.length, 'bytes');
if (!isWebP(data)) {
console.error('[embedColorInWebP] Not a WebP file');
return null;
}
const chunks = parseChunks(data);
console.log('[embedColorInWebP] Found', chunks.length, 'chunks:', chunks.map(c => c.id).join(', '));
// Find existing XMP chunk with our color
let existingXMPChunk: { offset: number; length: number } | null = null;
@@ -192,14 +182,10 @@ export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: str
xmpChunk.set(xmpBytes, 8);
// Padding byte is already 0 from Uint8Array initialization
console.log('[embedColorInWebP] Created XMP chunk, size:', xmpChunk.length, 'bytes');
console.log('[embedColorInWebP] Color being embedded:', color);
let newData: Uint8Array;
if (existingXMPChunk) {
// Replace existing chunk
console.log('[embedColorInWebP] Replacing existing XMP chunk at offset', existingXMPChunk.offset);
newData = new Uint8Array(data.length - existingXMPChunk.length + xmpChunk.length);
newData.set(data.slice(0, existingXMPChunk.offset), 0);
newData.set(xmpChunk, existingXMPChunk.offset);
@@ -209,7 +195,6 @@ export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: str
);
} else {
// Insert new chunk at end (before any trailing data)
console.log('[embedColorInWebP] Inserting new XMP chunk at end');
newData = new Uint8Array(data.length + xmpChunk.length);
newData.set(data, 0);
newData.set(xmpChunk, data.length);
@@ -219,8 +204,6 @@ export function embedColorInWebP(imageData: ArrayBuffer | Uint8Array, color: str
const newFileSize = newData.length - 8; // RIFF size excludes first 8 bytes
newData.set(writeUint32LE(newFileSize), 4);
console.log('[embedColorInWebP] Output size:', newData.length, 'bytes');
return newData;
}