ltmarx / tests /dwt.test.ts
harelcain's picture
Upload 19 files
d283c04 verified
import { describe, it, expect } from 'vitest';
import { createBuffer2D, dwtForward, dwtInverse, yPlaneToBuffer, bufferToYPlane } from '../core/dwt.js';
describe('DWT', () => {
it('should round-trip a simple buffer (1 level)', () => {
const width = 16;
const height = 16;
const buf = createBuffer2D(width, height);
for (let i = 0; i < buf.data.length; i++) {
buf.data[i] = Math.round(Math.random() * 255);
}
const original = new Float64Array(buf.data);
const { buf: transformed, dims } = dwtForward(buf, 1);
dwtInverse(transformed, dims);
for (let i = 0; i < original.length; i++) {
expect(transformed.data[i]).toBeCloseTo(original[i], 8);
}
});
it('should round-trip a buffer (2 levels)', () => {
const width = 64;
const height = 64;
const buf = createBuffer2D(width, height);
for (let i = 0; i < buf.data.length; i++) {
buf.data[i] = Math.round(Math.random() * 255);
}
const original = new Float64Array(buf.data);
const { buf: transformed, dims } = dwtForward(buf, 2);
dwtInverse(transformed, dims);
for (let i = 0; i < original.length; i++) {
expect(transformed.data[i]).toBeCloseTo(original[i], 8);
}
});
it('should convert Y plane to buffer and back', () => {
const width = 32;
const height = 32;
const yPlane = new Uint8Array(width * height);
for (let i = 0; i < yPlane.length; i++) {
yPlane[i] = Math.floor(Math.random() * 256);
}
const buf = yPlaneToBuffer(yPlane, width, height);
const result = bufferToYPlane(buf);
expect(result).toEqual(yPlane);
});
});