Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scriptjunkie/7dc65a7ee9b3430f439da55f33c60c14 to your computer and use it in GitHub Desktop.
Save scriptjunkie/7dc65a7ee9b3430f439da55f33c60c14 to your computer and use it in GitHub Desktop.
DCT encoding improvement
const NEW_CHARMAP = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20!\"#$%&'{([])}*+-.\\/0123456789:;,<=>?@EeAaUuOoIiFfGgHhJjLl|WwMmNnBbDdTtPpQqRrKkCcSsZzVvXxYy^_`~";
function get_new_char_code(old_char_code){
return NEW_CHARMAP.indexOf(String.fromCharCode(old_char_code));
}
function get_old_char_code(new_char_code){
return NEW_CHARMAP.charCodeAt(new_char_code);
}
function recalculate() {
var in_text = document.getElementById('in').value,
in_len = in_text.length;
/* Convert text to an array of ASCII values. */
var in_buf = [];
for (var i = 0; i < in_len; i++) in_buf.push(get_new_char_code(in_text.charCodeAt(i)));
/* Run DCT. */
var dct_result = dct(in_buf, in_len);
/* Update text under slider. */
var q_div = document.getElementById('div').value / 10;
document.getElementById('slider_pos').innerText = q_div;
/* Reduce precision. */
dct_result = quantize(dct_result, in_len, q_div);
/* Run inverse DCT. */
var idct_result = idct(dct_result, in_len);
/* Substitute unprintables. */
for (var i = 0; i < in_len; i++){
idct_result[i] = get_old_char_code(idct_result[i]);
if (idct_result[i] < 32 && idct_result[i] != 10) idct_result[i] = 63 /* ? */;
}
/* Write result, converting ASCII values to a string. */
var out_text = String.fromCharCode.apply(null, idct_result);
document.getElementById('out').value = out_text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment