Skip to content

Instantly share code, notes, and snippets.

@richard-to
Created March 13, 2014 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richard-to/9524373 to your computer and use it in GitHub Desktop.
Save richard-to/9524373 to your computer and use it in GitHub Desktop.
Very slow dct (n^4) implementation on a single 8x8 macroblock. Example data based on example in Wikipedia article on JPEG
var midpoint = 128;
var data = [
[52, 55, 61, 66, 70, 61, 64, 73],
[63, 59, 55, 90, 109, 85, 69, 72],
[62, 59, 68, 113, 144, 104, 66, 73],
[63, 58, 71, 122, 154, 106, 70, 69],
[67, 61, 68, 104, 126, 88, 68, 70],
[79, 65, 60, 70, 77, 68, 58, 75],
[85, 71, 64, 59, 55, 61, 65, 83],
[87, 79, 69, 68, 65, 76, 78, 94]
];
var shifted_data = [];
for (var x = 0; x < data.length; ++x) {
shifted_data.push([]);
for (var y = 0; y < data[x].length; ++y) {
shifted_data[x].push(data[x][y] - midpoint);
}
}
var dct = [];
for (var u = 0; u < shifted_data.length; ++u) {
dct.push([]);
var au = (u == 0) ? (1/Math.sqrt(2)) : 1;
for (var v = 0; v < shifted_data[u].length; ++v) {
var value = 0;
var av = (v == 0) ? (1/Math.sqrt(2)) : 1;
for (var x = 0; x < shifted_data.length; ++x) {
for (var y = 0; y < shifted_data[x].length; ++y) {
value += shifted_data[x][y] *
Math.cos( ((2 * x + 1) * u * Math.PI)/16 ) *
Math.cos( ((2 * y + 1) * v * Math.PI)/16 )
}
}
value = 1/4 * av * au * value;
dct[u].push(value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment