const MtxDCT = (function(N) { var dct = new Array(N); for(var i=0; i<N; i++) { var Ci = Math.sqrt(i ? 2/N : 1/N); dct[i] = new Array(N); for(var j=0; j<N; j++) dct[i][j] = Ci*Math.cos(Math.PI*(j+0.5)*i/N); } return dct; })(8); const MtxIDCT = (function(dct) { var N = dct.length; var idct = new Array(N); for(var i=0; i<N; i++) { idct[i] = new Array(N); for(var j=0; j<N; j++) idct[i][j] = dct[j][i]; } return idct; })(MtxDCT); function Mul(left, right) { if (left.length !== right.length) throw new Error('left.length !== right.length'); var N = left.length; var Y = new Array(N); for(var i=0; i<N; i++) { Y[i] = new Array(N); for(var j=0; j<N; j++) { Y[i][j] = 0; for(var k=0; k<N; k++) Y[i][j] += left[i][k]*right[k][j]; } } return Y; } function INT(block) { var Y = new Array(block.length); for(var i=0; i<block.length; i++) { Y[i] = new Array(block[i].length); for(var j=0; j<block[i].length; j++) Y[i][j] = Math.round(block[i][j]); } return Y; } module.exports = { F: block => Mul(Mul(MtxDCT, block), MtxIDCT), f: block => Mul(Mul(MtxIDCT, block), MtxDCT), INT } ////////////////////// TEST //////////////////////////// const dct = require('./dct'); const D = [ [ 36, 242, 131, 90, 35, 19, 156, 26], [ 32, 176, 160, 189, 198, 45, 132, 86], [ 6, 253, 191, 99, 246, 223, 196, 28], [234, 61, 98, 159, 141, 134, 221, 90], [102, 221, 98, 253, 167, 72, 81, 78], [ 46, 8, 243, 124, 119, 170, 166, 212], [ 91, 187, 112, 186, 210, 228, 125, 114], [ 71, 117, 207, 188, 66, 192, 27, 112] ]; console.log(D); console.log(dct.INT(dct.f(dct.F(D))));