mirror of
https://github.com/Nexoniarz/YAPF.git
synced 2026-11-03 04:54:26 +00:00
Format (version 1, replaces the earlier experimental versions): - per-tile YCoCg-R or subtract-green colour transform, 8-bit modular - 12 row filters per 8-row band (Left, Gradient, Up-right, Up-average, Up 1-8 rows), none with more than a one-add serial dependency - residuals in groups of 8 at the group's width (w bits x 8 = w bytes), 4-bit control codes with repeat-previous-width runs chosen by DP - constant and stored tiles, independent 64x64 tiles, mips, GPU hints - all sizes validated before decoding; corrupt input is rejected Library: - SIMD on by default: SSSE3 (runtime CPUID check; GCC, Clang, MSVC, MinGW) and NEON on ARM64; -DYAPF_NO_SIMD for plain C - yapf_load_memory, yapf_load_mt, yapf_load_memory_mt - 6-10x faster decoding than PNG (stb_image) on one core; -O2 and -O3 builds perform the same - fixes for macOS (sysconf) and the Windows import library exports Tools and integrations: - `yapf` CLI: convert PNG/JPG/TGA/BMP/PSD/GIF <-> YAPF, info, bench, thumbnail, mips - JavaScript and Python bindings (byte-identical to the C encoder) - GIMP 3 plugin: exports the flattened image, optional mipmaps - Blender extension: import (also as plane), export, drag and drop - VS Code preview, Figma plugin, Photoshop scripts, Linux MIME type and thumbnailer - Makefile, updated build.sh / build.bat, tools/build_release.sh - tests/test_yapf.c (784 checks) and bindings/js/test.js Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
57 lines
2.5 KiB
JavaScript
57 lines
2.5 KiB
JavaScript
/* node bindings/js/test.js — round-trips, the sample file, and corrupt input. */
|
|
'use strict';
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const YAPF = require('./yapf.js');
|
|
|
|
let tests = 0, fails = 0;
|
|
const check = (ok, msg) => { tests++; if (!ok) { fails++; console.log('FAIL:', msg); } };
|
|
|
|
let seed = 12345;
|
|
const rnd = () => (seed = (Math.imul(seed, 1103515245) + 12345) >>> 0) >>> 8;
|
|
|
|
function fill(w, h, ch, kind) {
|
|
const p = new Uint8Array(w * h * ch);
|
|
for (let y = 0; y < h; y++) for (let x = 0; x < w; x++) for (let c = 0; c < ch; c++) {
|
|
const i = (y * w + x) * ch + c;
|
|
p[i] = kind === 0 ? rnd() : kind === 1 ? x * 3 + y * 5 + c * 40
|
|
: kind === 2 ? (((x / 7 | 0) + (y / 5 | 0) + c) & 1 ? 200 : 30)
|
|
: kind === 3 ? ((x * x + y * y) / 50 | 0) + rnd() % 5
|
|
: c === ch - 1 ? 255 : x ^ y;
|
|
}
|
|
return p;
|
|
}
|
|
|
|
for (const [w, h] of [[1, 1], [7, 1], [1, 9], [63, 65], [64, 64], [97, 61], [200, 130]])
|
|
for (let ch = 1; ch <= 4; ch++) for (let kind = 0; kind < 5; kind++) {
|
|
const pixels = fill(w, h, ch, kind);
|
|
const back = YAPF.decode(YAPF.encode({ width: w, height: h, channels: ch, pixels }));
|
|
check(back.width === w && back.height === h && back.channels === ch &&
|
|
Buffer.compare(Buffer.from(back.pixels), Buffer.from(pixels)) === 0,
|
|
`roundtrip ${w}x${h} ch${ch} kind${kind}`);
|
|
}
|
|
|
|
/* The bundled sample: decode to the reference pixels, re-encode to the same bytes. */
|
|
const samplePath = path.join(__dirname, '..', '..', 'other', 'YAPF.YAPF');
|
|
if (fs.existsSync(samplePath)) {
|
|
const file = fs.readFileSync(samplePath);
|
|
const img = YAPF.decode(file);
|
|
let h = 2166136261;
|
|
for (const v of img.pixels) h = Math.imul(h ^ v, 16777619) >>> 0;
|
|
check(h === 0x8891c2b4, 'sample decodes to the reference pixels');
|
|
check(Buffer.compare(Buffer.from(YAPF.encode(img)), file) === 0,
|
|
'sample re-encodes to byte-identical output (matches the C encoder)');
|
|
}
|
|
|
|
/* Corrupt input must throw, never hang or return garbage silently. */
|
|
const good = YAPF.encode({ width: 130, height: 70, channels: 4, pixels: fill(130, 70, 4, 3) });
|
|
let threwOnTruncation = true;
|
|
for (let i = 0; i < 2000; i++) {
|
|
const bad = good.slice(0, i % 3 === 1 ? rnd() % good.length : good.length);
|
|
if (i % 3 !== 1) bad[rnd() % bad.length] ^= 1 << (rnd() % 8);
|
|
try { YAPF.decode(bad); if (i % 3 === 1) threwOnTruncation = false; } catch (e) { /* expected */ }
|
|
}
|
|
check(threwOnTruncation, 'truncated files are rejected');
|
|
|
|
console.log(`${tests} tests, ${fails} failures`);
|
|
process.exit(fails ? 1 : 0);
|