Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | 17x 14x 3x 1x 1x 3x 3x 1x 1x 1x 14x 14x 13x 12x 11x 11x 10x 10x 9x 9x 14x 14x 1x 8x 8x 5x 3x 3x 5x 5x 1x 5x 3x 8x 5x 5x 5x 7x 6x 6x 6x 6x 6x 2x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 4x 3x 4x 3x 3x 2x 13x 4x 4x 4x 4x 4x 4x 9x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 7x 13x 13x 13x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Minimal TrueVision TGA decoder for LR2 font sprite sheets.
*
* LR2 fonts use TGA images at 24-bit RGB or 32-bit BGRA, type 2 (uncompressed) or type 10 (RLE). Other variants —
* color-mapped, grayscale, 16-bit BGR(A)5551 — aren't seen in shipped LR2 themes so they're not supported here.
*
* Output is `ImageData` so the caller can hand it directly to `createImageBitmap` for Pixi texture upload, or paint it
* onto an offscreen canvas.
*/
interface TGAHeader {
idLength: number;
colorMapType: number;
imageType: number;
width: number;
height: number;
pixelDepth: number;
imageDescriptor: number;
}
function readHeader(bytes: Uint8Array): TGAHeader | undefined {
if (bytes.length < 18) return undefined;
return {
idLength: bytes[0]!,
colorMapType: bytes[1]!,
imageType: bytes[2]!,
// bytes 3-7 = ColorMap spec (skipped — we don't support indexed) bytes 8-9 = X origin (LR2 uses 0) bytes 10-11 = Y
// origin
width: bytes[12]! | (bytes[13]! << 8),
height: bytes[14]! | (bytes[15]! << 8),
pixelDepth: bytes[16]!,
imageDescriptor: bytes[17]!,
};
}
/**
* Returns whether the supplied bytes look like a TGA we can decode. Detects the (somewhat ambiguous) TGA-V2 footer
* "TRUEVISION-XFILE.\0" or otherwise sanity-checks the header.
*/
export function isTGAImage(bytes: Uint8Array): boolean {
if (bytes.length >= 26) {
const tail = bytes.subarray(bytes.length - 18);
Iif (
tail[0] === 0x54 && // T
tail[1] === 0x52 && // R
tail[2] === 0x55 && // U
tail[3] === 0x45 // E
) {
// "TRUEVISION-XFILE.\0" footer present — definitely TGA-V2.
return true;
}
}
const header = readHeader(bytes);
if (!header) return false;
Iif (header.imageType === 0) return false; // No data
// Common types we accept
Eif ([2, 3, 10, 11].includes(header.imageType)) {
Eif ([8, 16, 24, 32].includes(header.pixelDepth)) return true;
}
return false;
}
/**
* Decodes TGA bytes into an `ImageData`-shaped object (raw RGBA pixel buffer + width / height). Returns `undefined` on
* unsupported / malformed input.
*
* Note: the returned `data` is a plain `Uint8ClampedArray`, not a DOM `ImageData` instance — the caller wraps it via
* `new ImageData(data, width, height)` or passes it to `ctx.putImageData` after constructing a fresh one. This keeps
* the decoder usable in Node tests where `ImageData` isn't a global.
*/
export function decodeTGA(bytes: Uint8Array): { width: number; height: number; data: Uint8ClampedArray } | undefined {
const header = readHeader(bytes);
if (!header) return undefined;
if (header.colorMapType !== 0) return undefined; // No paletted support
if (header.width <= 0 || header.height <= 0) return undefined;
// Image data starts after the 18-byte header + ID field + color map (skipped — we rejected colorMapType != 0).
const pixelDataStart = 18 + header.idLength;
if (pixelDataStart > bytes.length) return undefined;
const bytesPerPixel = header.pixelDepth >>> 3;
if (![1, 2, 3, 4].includes(bytesPerPixel)) return undefined;
const totalPixels = header.width * header.height;
// Image type values: 2 = uncompressed TrueColor RGB / RGBA 3 = uncompressed grayscale 10 = RLE TrueColor RGB / RGBA
// 11 = RLE grayscale
const isRLE = header.imageType === 10 || header.imageType === 11;
const isGrayscale = header.imageType === 3 || header.imageType === 11;
if (!isRLE && header.imageType !== 2 && header.imageType !== 3) {
return undefined;
}
const pixels = new Uint8ClampedArray(totalPixels * 4);
if (isRLE) {
if (!decodeRLE(bytes.subarray(pixelDataStart), bytesPerPixel, pixels, totalPixels, isGrayscale)) {
return undefined;
}
} else {
decodeUncompressed(bytes.subarray(pixelDataStart), bytesPerPixel, pixels, totalPixels, isGrayscale);
}
// TGA origin: bit 5 of imageDescriptor (`0x20`) flags top-left origin. When clear, the image is stored bottom-up and
// we flip rows so the resulting canvas-friendly buffer is top-down.
const topDown = (header.imageDescriptor & 0x20) !== 0;
if (!topDown) {
flipRows(pixels, header.width, header.height);
}
return { width: header.width, height: header.height, data: pixels };
}
function decodeUncompressed(
src: Uint8Array,
bytesPerPixel: number,
dest: Uint8ClampedArray,
totalPixels: number,
isGrayscale: boolean,
): void {
for (let i = 0; i < totalPixels; i += 1) {
writePixel(src, i * bytesPerPixel, bytesPerPixel, dest, i * 4, isGrayscale);
}
}
/**
* Decodes TGA RLE packets. Each packet starts with a header byte:
*
* - `bit 7 = 1` (`0x80..0xFF`) — Run-length packet: repeat the following pixel `(header & 0x7F) + 1` times.
* - `bit 7 = 0` (`0x00..0x7F`) — Raw packet: `(header & 0x7F) + 1` pixels stored verbatim.
*
* Returns `false` on truncation.
*/
function decodeRLE(
src: Uint8Array,
bytesPerPixel: number,
dest: Uint8ClampedArray,
totalPixels: number,
isGrayscale: boolean,
): boolean {
let srcIdx = 0;
let pixelIdx = 0;
while (pixelIdx < totalPixels) {
if (srcIdx >= src.length) return false;
const headerByte = src[srcIdx]!;
srcIdx += 1;
const isRun = (headerByte & 0x80) !== 0;
const count = (headerByte & 0x7f) + 1;
if (isRun) {
if (srcIdx + bytesPerPixel > src.length) return false;
// Read once, replicate.
const tmp = new Uint8ClampedArray(4);
writePixel(src, srcIdx, bytesPerPixel, tmp, 0, isGrayscale);
srcIdx += bytesPerPixel;
for (let i = 0; i < count && pixelIdx + i < totalPixels; i += 1) {
const destIdx = (pixelIdx + i) * 4;
dest[destIdx] = tmp[0]!;
dest[destIdx + 1] = tmp[1]!;
dest[destIdx + 2] = tmp[2]!;
dest[destIdx + 3] = tmp[3]!;
}
pixelIdx += count;
} else {
if (srcIdx + count * bytesPerPixel > src.length) return false;
for (let i = 0; i < count && pixelIdx + i < totalPixels; i += 1) {
writePixel(src, srcIdx + i * bytesPerPixel, bytesPerPixel, dest, (pixelIdx + i) * 4, isGrayscale);
}
srcIdx += count * bytesPerPixel;
pixelIdx += count;
}
}
return true;
}
function writePixel(
src: Uint8Array,
srcIdx: number,
bytesPerPixel: number,
dest: Uint8ClampedArray,
destIdx: number,
isGrayscale: boolean,
): void {
if (isGrayscale) {
const value = src[srcIdx] ?? 0;
dest[destIdx] = value;
dest[destIdx + 1] = value;
dest[destIdx + 2] = value;
dest[destIdx + 3] = bytesPerPixel === 2 ? (src[srcIdx + 1] ?? 255) : 255;
return;
}
if (bytesPerPixel === 2) {
// 16-bit BGR(A)5551 — high bit = alpha. Most LR2 fonts don't use this, but we still support it for completeness.
const lo = src[srcIdx] ?? 0;
const hi = src[srcIdx + 1] ?? 0;
const bgr = lo | (hi << 8);
const r = (((bgr >>> 10) & 0x1f) * 255) / 31;
const g = (((bgr >>> 5) & 0x1f) * 255) / 31;
const b = ((bgr & 0x1f) * 255) / 31;
const a = (bgr & 0x8000) !== 0 ? 255 : 0;
dest[destIdx] = r;
dest[destIdx + 1] = g;
dest[destIdx + 2] = b;
dest[destIdx + 3] = a;
return;
}
// TGA's truecolor pixels are stored BGR (24-bit) or BGRA (32-bit).
dest[destIdx] = src[srcIdx + 2] ?? 0; // R = TGA byte 2
dest[destIdx + 1] = src[srcIdx + 1] ?? 0; // G = TGA byte 1
dest[destIdx + 2] = src[srcIdx] ?? 0; // B = TGA byte 0
dest[destIdx + 3] = bytesPerPixel === 4 ? (src[srcIdx + 3] ?? 255) : 255;
}
function flipRows(pixels: Uint8ClampedArray, width: number, height: number): void {
const rowBytes = width * 4;
const tmp = new Uint8ClampedArray(rowBytes);
for (let y = 0; y < height >>> 1; y += 1) {
const top = y * rowBytes;
const bot = (height - 1 - y) * rowBytes;
tmp.set(pixels.subarray(top, top + rowBytes));
pixels.copyWithin(top, bot, bot + rowBytes);
pixels.set(tmp, bot);
}
}
|