File size: 1,058 Bytes
d283c04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, it, expect } from 'vitest';
import { dctForward8x8, dctInverse8x8 } from '../core/dct.js';

describe('DCT', () => {
  it('should round-trip an 8x8 block', () => {
    const block = new Float64Array(64);
    for (let i = 0; i < 64; i++) {
      block[i] = Math.random() * 255;
    }
    const original = new Float64Array(block);

    dctForward8x8(block);
    dctInverse8x8(block);

    for (let i = 0; i < 64; i++) {
      expect(block[i]).toBeCloseTo(original[i], 6);
    }
  });

  it('should produce DC coefficient as mean of block', () => {
    const block = new Float64Array(64);
    let sum = 0;
    for (let i = 0; i < 64; i++) {
      block[i] = 100 + Math.random() * 10;
      sum += block[i];
    }
    const mean = sum / 64;

    dctForward8x8(block);

    // DC coefficient should be proportional to the mean
    // DC = alpha(0) * alpha(0) * sum = (1/8) * sum = mean * 8 * (1/8) = mean * sqrt(64)/...
    // Actually: DC = (1/sqrt(8)) * (1/sqrt(8)) * sum(block) = sum/8
    expect(block[0]).toBeCloseTo(sum / 8, 4);
  });
});