SPB Git

Repository Public MIT

media.ts 4.7 KB 116 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Skills8 * File             : tests/media.ts9 * Module           : tests10 * Repository       : https://git.spboucher.ai/skills-spboucher-ai11 * Project Website  : https://skills.spboucher.ai12 * Created          : 2026-11-1013 * Last Modified    : 2026-11-1014 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: dev@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * SPDX Identifier  : MIT34 * License          : MIT License35 * License File     : LICENSE36 *37 * Do not embed credentials, private keys, access tokens,38 * certificates or confidential personal information.39 *40 * =============================================================================41 * END OF SP BOUCHER SOURCE HEADER42 * =============================================================================43 */4445import { deflateSync } from "node:zlib";4647/** Test-only image fixtures, built in memory. They are valid files that a browser can display. */48const CRC = (() => {49  const t = new Uint32Array(256);50  for (let n = 0; n < 256; n++) {51    let c = n;52    for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;53    t[n] = c >>> 0;54  }55  return t;56})();57function crc32(b: Buffer): number {58  let c = 0xffffffff;59  for (const x of b) c = CRC[(c ^ x) & 0xff]! ^ (c >>> 8);60  return (c ^ 0xffffffff) >>> 0;61}62function chunk(type: string, data: Buffer): Buffer {63  const len = Buffer.alloc(4);64  len.writeUInt32BE(data.length);65  const body = Buffer.concat([Buffer.from(type, "latin1"), data]);66  const crc = Buffer.alloc(4);67  crc.writeUInt32BE(crc32(body));68  return Buffer.concat([len, body, crc]);69}7071/** A real RGB PNG that looks like a small dark terminal: title bar, then light "text" bars. */72export function makePng(width = 640, height = 400, seed = 0): Buffer {73  const raw = Buffer.alloc((width * 3 + 1) * height);74  for (let y = 0; y < height; y++) {75    const row = y * (width * 3 + 1);76    raw[row] = 0;77    for (let x = 0; x < width; x++) {78      let r = 16, g = 20, b = 28;79      if (y < 28) { r = 38; g = 44; b = 58; }80      else {81        const line = Math.floor((y - 44) / 22);82        const inLine = y >= 44 && (y - 44) % 22 < 9 && line >= 0;83        const len = 80 + ((line * 97 + seed * 53) % (width - 180));84        if (inLine && x > 24 && x < 24 + len) { r = 190 - (line % 3) * 40; g = 200; b = 215 - (line % 2) * 60; }85      }86      const p = row + 1 + x * 3;87      raw[p] = r; raw[p + 1] = g; raw[p + 2] = b;88    }89  }90  const ihdr = Buffer.alloc(13);91  ihdr.writeUInt32BE(width, 0);92  ihdr.writeUInt32BE(height, 4);93  ihdr[8] = 8; ihdr[9] = 2; ihdr[10] = 0; ihdr[11] = 0; ihdr[12] = 0;94  return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(raw, { level: 9 })), chunk("IEND", Buffer.alloc(0))]);95}9697/** Header-only JPEG (SOI, APP0, SOF0, EOI): enough for format and size sniffing, not for display. */98export function jpegHeader(width = 320, height = 200): Buffer {99  const app0 = Buffer.from([0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00]);100  const sof = Buffer.from([0xff, 0xc0, 0x00, 0x0b, 0x08, height >> 8, height & 0xff, width >> 8, width & 0xff, 0x01, 0x01, 0x11, 0x00]);101  return Buffer.concat([Buffer.from([0xff, 0xd8]), app0, sof, Buffer.from([0xff, 0xd9])]);102}103104/** Header-only lossless WebP (RIFF/WEBP/VP8L) carrying a size. */105export function webpHeader(width = 320, height = 200): Buffer {106  const bits = ((width - 1) & 0x3fff) | (((height - 1) & 0x3fff) << 14);107  const vp8l = Buffer.alloc(5);108  vp8l[0] = 0x2f;109  vp8l.writeUInt32LE(bits >>> 0, 1);110  const pad = Buffer.alloc(8);111  const body = Buffer.concat([Buffer.from("WEBP", "latin1"), Buffer.from("VP8L", "latin1"), Buffer.from([vp8l.length + pad.length, 0, 0, 0]), vp8l, pad]);112  const size = Buffer.alloc(4);113  size.writeUInt32LE(body.length);114  return Buffer.concat([Buffer.from("RIFF", "latin1"), size, body]);115}116
Files main
Menu

Theme