Apply the auto-fixable lint rules and the formatter, and drop the Vite build artifacts.
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
/**
|
||||
* Utilities for reading and writing GIF metadata (Application Extension blocks)
|
||||
* Used to embed user color in avatar images
|
||||
*
|
||||
*
|
||||
* GIF structure:
|
||||
* - Header: GIF87a or GIF89a (6 bytes)
|
||||
* - Logical Screen Descriptor (7 bytes)
|
||||
* - Optional Global Color Table
|
||||
* - Extension blocks and image data
|
||||
* - Trailer: 0x3B
|
||||
*
|
||||
*
|
||||
* Application Extension format:
|
||||
* - 0x21 (Extension Introducer)
|
||||
* - 0xFF (Application Extension Label)
|
||||
@@ -20,7 +20,7 @@
|
||||
|
||||
/* eslint-disable no-bitwise */
|
||||
/* eslint-disable no-plusplus */
|
||||
/* eslint-disable no-console */
|
||||
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
/* eslint-disable no-continue */
|
||||
|
||||
@@ -33,14 +33,14 @@ const PAARROT_APP_ID = 'PAARROT\0'; // 8 bytes, null-padded
|
||||
const PAARROT_APP_ID_BYTES = new TextEncoder().encode(PAARROT_APP_ID);
|
||||
|
||||
/** Application authentication code (3 bytes) */
|
||||
const PAARROT_AUTH_CODE = new Uint8Array([0x31, 0x2E, 0x30]); // "1.0"
|
||||
const PAARROT_AUTH_CODE = new Uint8Array([0x31, 0x2e, 0x30]); // "1.0"
|
||||
|
||||
/**
|
||||
* Check if data is a valid GIF file
|
||||
*/
|
||||
function isGIF(data: Uint8Array): boolean {
|
||||
if (data.length < 6) return false;
|
||||
|
||||
|
||||
// Check GIF87a
|
||||
let is87a = true;
|
||||
for (let i = 0; i < 6; i++) {
|
||||
@@ -50,7 +50,7 @@ function isGIF(data: Uint8Array): boolean {
|
||||
}
|
||||
}
|
||||
if (is87a) return true;
|
||||
|
||||
|
||||
// Check GIF89a
|
||||
let is89a = true;
|
||||
for (let i = 0; i < 6; i++) {
|
||||
@@ -71,15 +71,18 @@ function getGlobalColorTableSize(data: Uint8Array): number {
|
||||
const packedByte = data[10];
|
||||
const hasGCT = (packedByte & 0x80) !== 0;
|
||||
if (!hasGCT) return 0;
|
||||
|
||||
|
||||
const gctSize = packedByte & 0x07;
|
||||
return 3 * (2 ** (gctSize + 1));
|
||||
return 3 * 2 ** (gctSize + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all Application Extension blocks
|
||||
*/
|
||||
function findAppExtensions(data: Uint8Array, startOffset: number): Array<{
|
||||
function findAppExtensions(
|
||||
data: Uint8Array,
|
||||
startOffset: number,
|
||||
): Array<{
|
||||
offset: number;
|
||||
length: number;
|
||||
appId: string;
|
||||
@@ -93,37 +96,37 @@ function findAppExtensions(data: Uint8Array, startOffset: number): Array<{
|
||||
authCode: Uint8Array;
|
||||
dataBlocks: Uint8Array[];
|
||||
}> = [];
|
||||
|
||||
|
||||
let offset = startOffset;
|
||||
|
||||
|
||||
while (offset < data.length - 1) {
|
||||
// Look for Extension Introducer (0x21)
|
||||
if (data[offset] === 0x21 && data[offset + 1] === 0xFF) {
|
||||
if (data[offset] === 0x21 && data[offset + 1] === 0xff) {
|
||||
// Application Extension
|
||||
const blockSize = data[offset + 2];
|
||||
if (blockSize !== 0x0B) {
|
||||
if (blockSize !== 0x0b) {
|
||||
offset++;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
const appId = new TextDecoder().decode(data.slice(offset + 3, offset + 11));
|
||||
const authCode = data.slice(offset + 11, offset + 14);
|
||||
|
||||
|
||||
// Read data sub-blocks
|
||||
let subOffset = offset + 14;
|
||||
const dataBlocks: Uint8Array[] = [];
|
||||
let totalLength = 14; // header length
|
||||
|
||||
|
||||
while (subOffset < data.length && data[subOffset] !== 0x00) {
|
||||
const subBlockSize = data[subOffset];
|
||||
dataBlocks.push(data.slice(subOffset + 1, subOffset + 1 + subBlockSize));
|
||||
subOffset += 1 + subBlockSize;
|
||||
totalLength += 1 + subBlockSize;
|
||||
}
|
||||
|
||||
|
||||
// Add terminator
|
||||
totalLength += 1;
|
||||
|
||||
|
||||
extensions.push({
|
||||
offset,
|
||||
length: totalLength,
|
||||
@@ -131,16 +134,16 @@ function findAppExtensions(data: Uint8Array, startOffset: number): Array<{
|
||||
authCode,
|
||||
dataBlocks,
|
||||
});
|
||||
|
||||
|
||||
offset = subOffset + 1;
|
||||
} else if (data[offset] === 0x3B) {
|
||||
} else if (data[offset] === 0x3b) {
|
||||
// Trailer - end of GIF
|
||||
break;
|
||||
} else {
|
||||
offset++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return extensions;
|
||||
}
|
||||
|
||||
@@ -149,33 +152,33 @@ function findAppExtensions(data: Uint8Array, startOffset: number): Array<{
|
||||
*/
|
||||
function createPaarrotExtension(color: string): Uint8Array {
|
||||
const colorBytes = new TextEncoder().encode(color);
|
||||
|
||||
|
||||
// Calculate total size
|
||||
// 0x21 + 0xFF + 0x0B + appId(8) + authCode(3) + sub-block(1 + colorBytes.length) + 0x00
|
||||
const totalSize = 2 + 1 + 8 + 3 + 1 + colorBytes.length + 1;
|
||||
const extension = new Uint8Array(totalSize);
|
||||
|
||||
|
||||
let offset = 0;
|
||||
extension[offset++] = 0x21; // Extension Introducer
|
||||
extension[offset++] = 0xFF; // Application Extension Label
|
||||
extension[offset++] = 0x0B; // Block size (always 11)
|
||||
|
||||
extension[offset++] = 0xff; // Application Extension Label
|
||||
extension[offset++] = 0x0b; // Block size (always 11)
|
||||
|
||||
// Application Identifier (8 bytes)
|
||||
extension.set(PAARROT_APP_ID_BYTES, offset);
|
||||
offset += 8;
|
||||
|
||||
|
||||
// Application Authentication Code (3 bytes)
|
||||
extension.set(PAARROT_AUTH_CODE, offset);
|
||||
offset += 3;
|
||||
|
||||
|
||||
// Data sub-block
|
||||
extension[offset++] = colorBytes.length;
|
||||
extension.set(colorBytes, offset);
|
||||
offset += colorBytes.length;
|
||||
|
||||
|
||||
// Block terminator
|
||||
extension[offset] = 0x00;
|
||||
|
||||
|
||||
return extension;
|
||||
}
|
||||
|
||||
@@ -184,16 +187,16 @@ function createPaarrotExtension(color: string): Uint8Array {
|
||||
*/
|
||||
export function extractColorFromGIF(imageData: ArrayBuffer | Uint8Array): string | undefined {
|
||||
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
|
||||
|
||||
|
||||
if (!isGIF(data)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
|
||||
const gctSize = getGlobalColorTableSize(data);
|
||||
const startOffset = 13 + gctSize; // Header(6) + LSD(7) + GCT
|
||||
|
||||
|
||||
const extensions = findAppExtensions(data, startOffset);
|
||||
|
||||
|
||||
for (const ext of extensions) {
|
||||
if (ext.appId.startsWith('PAARROT')) {
|
||||
if (ext.dataBlocks.length > 0) {
|
||||
@@ -202,25 +205,28 @@ export function extractColorFromGIF(imageData: ArrayBuffer | Uint8Array): string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Embed paarrot color into GIF image data
|
||||
*/
|
||||
export function embedColorInGIF(imageData: ArrayBuffer | Uint8Array, color: string): Uint8Array | null {
|
||||
export function embedColorInGIF(
|
||||
imageData: ArrayBuffer | Uint8Array,
|
||||
color: string,
|
||||
): Uint8Array | null {
|
||||
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
|
||||
|
||||
|
||||
if (!isGIF(data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
const gctSize = getGlobalColorTableSize(data);
|
||||
const insertOffset = 13 + gctSize; // After Header + LSD + GCT
|
||||
|
||||
|
||||
const extensions = findAppExtensions(data, insertOffset);
|
||||
|
||||
|
||||
// Find existing PAARROT extension
|
||||
let existingExt: { offset: number; length: number } | null = null;
|
||||
for (const ext of extensions) {
|
||||
@@ -229,12 +235,12 @@ export function embedColorInGIF(imageData: ArrayBuffer | Uint8Array, color: stri
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Create new extension
|
||||
const newExtension = createPaarrotExtension(color);
|
||||
|
||||
|
||||
let newData: Uint8Array;
|
||||
|
||||
|
||||
if (existingExt) {
|
||||
// Replace existing extension
|
||||
newData = new Uint8Array(data.length - existingExt.length + newExtension.length);
|
||||
@@ -242,7 +248,7 @@ export function embedColorInGIF(imageData: ArrayBuffer | Uint8Array, color: stri
|
||||
newData.set(newExtension, existingExt.offset);
|
||||
newData.set(
|
||||
data.slice(existingExt.offset + existingExt.length),
|
||||
existingExt.offset + newExtension.length
|
||||
existingExt.offset + newExtension.length,
|
||||
);
|
||||
} else {
|
||||
// Insert after GCT (before any other blocks)
|
||||
@@ -251,7 +257,7 @@ export function embedColorInGIF(imageData: ArrayBuffer | Uint8Array, color: stri
|
||||
newData.set(newExtension, insertOffset);
|
||||
newData.set(data.slice(insertOffset), insertOffset + newExtension.length);
|
||||
}
|
||||
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
@@ -260,16 +266,16 @@ export function embedColorInGIF(imageData: ArrayBuffer | Uint8Array, color: stri
|
||||
*/
|
||||
export function removeColorFromGIF(imageData: ArrayBuffer | Uint8Array): Uint8Array | null {
|
||||
const data = imageData instanceof Uint8Array ? imageData : new Uint8Array(imageData);
|
||||
|
||||
|
||||
if (!isGIF(data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
const gctSize = getGlobalColorTableSize(data);
|
||||
const startOffset = 13 + gctSize;
|
||||
|
||||
|
||||
const extensions = findAppExtensions(data, startOffset);
|
||||
|
||||
|
||||
// Find existing PAARROT extension
|
||||
let existingExt: { offset: number; length: number } | null = null;
|
||||
for (const ext of extensions) {
|
||||
@@ -278,18 +284,15 @@ export function removeColorFromGIF(imageData: ArrayBuffer | Uint8Array): Uint8Ar
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!existingExt) {
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
// Remove the extension
|
||||
const newData = new Uint8Array(data.length - existingExt.length);
|
||||
newData.set(data.slice(0, existingExt.offset), 0);
|
||||
newData.set(
|
||||
data.slice(existingExt.offset + existingExt.length),
|
||||
existingExt.offset
|
||||
);
|
||||
|
||||
newData.set(data.slice(existingExt.offset + existingExt.length), existingExt.offset);
|
||||
|
||||
return newData;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user