|
| 1 | +import { Buffer } from 'node:buffer'; |
| 2 | +// @ts-expect-error Missing upstream types for this library |
| 3 | +import TGA from 'tga'; |
| 4 | +// @ts-expect-error Missing upstream types for this library |
| 5 | +import imagePixels from 'image-pixels'; |
| 6 | +import { inflate, deflate } from 'pako'; |
| 7 | +import { PNG } from 'pngjs'; |
| 8 | +import bmp from 'bmp-js'; |
| 9 | +import sharp from 'sharp'; |
| 10 | +import { logger } from '@/logger'; |
| 11 | + |
| 12 | +/** Ingests a BMP-format painting and converts it to the usual TGA format. |
| 13 | + * @param painting base64-encoded bmp image |
| 14 | + * @returns base64-encoded zlib-compressed TGA |
| 15 | + */ |
| 16 | +export async function processBmpPainting(painting: string): Promise<string | null> { |
| 17 | + const paintingBuffer = Buffer.from(painting, 'base64'); |
| 18 | + const bitmap = bmp.decode(paintingBuffer); |
| 19 | + const tga = createTgaFromPixelData(bitmap.width, bitmap.height, bitmap.data, false); |
| 20 | + |
| 21 | + let output: Uint8Array; |
| 22 | + try { |
| 23 | + output = deflate(tga, { level: 6 }); |
| 24 | + } catch (err) { |
| 25 | + logger.error(err, 'Could not compress painting'); |
| 26 | + return null; |
| 27 | + } |
| 28 | + return Buffer.from(output.buffer).toString('base64'); |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Ingests a raw painting and converts it for CDN upload. |
| 33 | + * @param painting base64-encoded zlib-compressed TGA |
| 34 | + * @returns PNG in buffer |
| 35 | + */ |
| 36 | +export async function processPainting(painting: string): Promise<Buffer | null> { |
| 37 | + const paintingBuffer = Buffer.from(painting, 'base64'); |
| 38 | + let output: Uint8Array; |
| 39 | + try { |
| 40 | + output = inflate(paintingBuffer); |
| 41 | + } catch (err) { |
| 42 | + logger.error(err, 'Could not decompress painting'); |
| 43 | + return null; |
| 44 | + } |
| 45 | + let tga: TGA; |
| 46 | + try { |
| 47 | + tga = new TGA(Buffer.from(output.buffer)); |
| 48 | + } catch (e) { |
| 49 | + logger.error(e, 'Could not parse painting'); |
| 50 | + return null; |
| 51 | + } |
| 52 | + const png = new PNG({ |
| 53 | + width: tga.width, |
| 54 | + height: tga.height |
| 55 | + }); |
| 56 | + png.data = tga.pixels; |
| 57 | + return PNG.sync.write(png); |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Rezise an image. |
| 62 | + * @param data base64-encoded input image, most codecs OK (check Sharp docs) |
| 63 | + * @param width Target width in pixels |
| 64 | + * @param height Target height in pixels |
| 65 | + * @returns New image, same codec as the input (not base64'd!) |
| 66 | + */ |
| 67 | +export async function resizeImage(data: string, width: number, height: number): Promise<Buffer> { |
| 68 | + const image = Buffer.from(data, 'base64'); |
| 69 | + return sharp(image) |
| 70 | + .resize({ height, width }) |
| 71 | + .toBuffer() |
| 72 | + .catch((err) => { |
| 73 | + logger.error(err, 'Could not resize image!'); |
| 74 | + throw err; |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +export async function getTGAFromPNG(image: Buffer): Promise<string | null> { |
| 79 | + const pngData = await imagePixels(Buffer.from(image)); |
| 80 | + const tga = TGA.createTgaBuffer(pngData.width, pngData.height, pngData.data); |
| 81 | + let output: Uint8Array; |
| 82 | + try { |
| 83 | + output = deflate(tga, { level: 6 }); |
| 84 | + } catch (err) { |
| 85 | + logger.error(err, 'Could not decompress image'); |
| 86 | + return null; |
| 87 | + } |
| 88 | + return Buffer.from(output.buffer).toString('base64').trim(); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Makes a new TGA from BGRX pixel data |
| 93 | + * @param width Width of the data |
| 94 | + * @param height Height of the data |
| 95 | + * @param pixels [B1, G1, R1, X1, B2, R2, G2, X2...] |
| 96 | + * @param dontFlipY Invert the y-axis |
| 97 | + * @returns Buffer with TGA data |
| 98 | + * |
| 99 | + * Modified from https://github.com/steel1990/tga/blob/dcd2bff6536c1c75719ed4309389cd66f991d8d3/src/index.js#L16-L45 |
| 100 | + */ |
| 101 | +function createTgaFromPixelData(width: number, height: number, pixels: Buffer, dontFlipY: boolean): Buffer { |
| 102 | + const buffer = Buffer.alloc(18 + pixels.length); |
| 103 | + // write header |
| 104 | + buffer.writeInt8(0, 0); |
| 105 | + buffer.writeInt8(0, 1); |
| 106 | + buffer.writeInt8(2, 2); |
| 107 | + buffer.writeInt16LE(0, 3); |
| 108 | + buffer.writeInt16LE(0, 5); |
| 109 | + buffer.writeInt8(0, 7); |
| 110 | + buffer.writeInt16LE(0, 8); |
| 111 | + buffer.writeInt16LE(0, 10); |
| 112 | + buffer.writeInt16LE(width, 12); |
| 113 | + buffer.writeInt16LE(height, 14); |
| 114 | + buffer.writeInt8(32, 16); |
| 115 | + buffer.writeInt8(8, 17); |
| 116 | + |
| 117 | + let offset = 18; |
| 118 | + for (let i = 0; i < height; i++) { |
| 119 | + for (let j = 0; j < width; j++) { |
| 120 | + const idx = ((dontFlipY ? i : height - i - 1) * width + j) * 4; |
| 121 | + buffer.writeUInt8(pixels[idx + 1], offset++); // b |
| 122 | + buffer.writeUInt8(pixels[idx + 2], offset++); // g |
| 123 | + buffer.writeUInt8(pixels[idx + 3], offset++); // r |
| 124 | + buffer.writeUInt8(255, offset++); // a |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return buffer; |
| 129 | +} |
0 commit comments