-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
433 lines (408 loc) · 12.8 KB
/
script.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
class MinHeap {
constructor() {
this.heap_array = [];
}
size() {
return this.heap_array.length;
}
empty() {
return (this.size() === 0);
}
push(value) {
this.heap_array.push(value);
this.up_heapify();
}
up_heapify() {
var current_index = this.size() - 1;
while (current_index > 0) {
var current_element = this.heap_array[current_index];
var parent_index = Math.trunc((current_index - 1) / 2);
var parent_element = this.heap_array[parent_index];
if (parent_element[0] < current_element[0]) {
break;
}
else {
this.heap_array[parent_index] = current_element;
this.heap_array[current_index] = parent_element;
current_index = parent_index;
}
}
}
top() {
return this.heap_array[0];
}
pop() {
if (this.empty() == false) {
var last_index = this.size() - 1;
this.heap_array[0] = this.heap_array[last_index];
this.heap_array.pop();
this.down_heapify();
}
}
down_heapify() {
var current_index = 0;
var current_element = this.heap_array[0];
while (current_index < this.size()) {
var child_index1 = (current_index * 2) + 1;
var child_index2 = (current_index * 2) + 2;
if (child_index1 >= this.size() && child_index2 >= this.size()) {
break;
}
else if (child_index2 >= this.size()) {
let child_element1 = this.heap_array[child_index1];
if (current_element[0] < child_element1[0]) {
break;
}
else {
this.heap_array[child_index1] = current_element;
this.heap_array[current_index] = child_element1;
current_index = child_index1;
}
}
else {
var child_element1 = this.heap_array[child_index1];
var child_element2 = this.heap_array[child_index2];
if (current_element[0] < child_element1[0] && current_element[0] < child_element2[0]) {
break;
}
else {
if (child_element1[0] < child_element2[0]) {
this.heap_array[child_index1] = current_element;
this.heap_array[current_index] = child_element1;
current_index = child_index1;
}
else {
this.heap_array[child_index2] = current_element;
this.heap_array[current_index] = child_element2;
current_index = child_index2;
}
}
}
}
}
}
class Codec {
getCodes(node, curr_code) {
if (typeof (node[1]) === "string") {
this.codes[node[1]] = curr_code;
return;
}
this.getCodes(node[1][0], curr_code + '0');
this.getCodes(node[1][1], curr_code + '1');
}
make_string(node) {
if (typeof (node[1]) === "string") {
return "'" + node[1];
}
return '0' + this.make_string(node[1][0]) + '1' + this.make_string(node[1][1]);
}
make_tree(tree_string) {
let node = [];
if (tree_string[this.index] === "'") {
this.index++;
node.push(tree_string[this.index]);
this.index++;
return node;
}
this.index++;
node.push(this.make_tree(tree_string));
this.index++;
node.push(this.make_tree(tree_string));
return node;
}
encode(data) {
this.heap = new MinHeap();
var mp = new Map();
for (let i = 0; i < data.length; i++) {
if (mp.has(data[i])) {
let foo = mp.get(data[i]);
mp.set(data[i], foo + 1);
}
else {
mp.set(data[i], 1);
}
}
if (mp.size === 0) {
let final_string = "zer#";
let output_message = "Compression complete and file will be downloaded automatically." + '\n' + "Compression Ratio : " + (data.length / final_string.length).toPrecision(6);
return [final_string, output_message];
}
if (mp.size === 1) {
let key, value;
for (let [k, v] of mp) {
key = k;
value = v;
}
let final_string = "one" + '#' + key + '#' + value.toString();
let output_message = "Compression complete and file will be downloaded automatically." + '\n' + "Compression Ratio : " + (data.length / final_string.length).toPrecision(6);
return [final_string, output_message];
}
for (let [key, value] of mp) {
this.heap.push([value, key]);
}
while (this.heap.size() >= 2) {
let min_node1 = this.heap.top();
this.heap.pop();
let min_node2 = this.heap.top();
this.heap.pop();
this.heap.push([min_node1[0] + min_node2[0], [min_node1, min_node2]]);
}
var huffman_tree = this.heap.top();
this.heap.pop();
this.codes = {};
this.getCodes(huffman_tree, "");
/// convert data into coded data
let binary_string = "";
for (let i = 0; i < data.length; i++) {
binary_string += this.codes[data[i]];
}
let padding_length = (8 - (binary_string.length % 8)) % 8;
for (let i = 0; i < padding_length; i++) {
binary_string += '0';
}
let encoded_data = "";
for (let i = 0; i < binary_string.length;) {
let curr_num = 0;
for (let j = 0; j < 8; j++, i++) {
curr_num *= 2;
curr_num += binary_string[i] - '0';
}
encoded_data += String.fromCharCode(curr_num);
}
let tree_string = this.make_string(huffman_tree);
let ts_length = tree_string.length;
let final_string = ts_length.toString() + '#' + padding_length.toString() + '#' + tree_string + encoded_data;
let output_message = "Compression complete and file will be downloaded automatically." + '\n' + "Compression Ratio : " + (data.length / final_string.length).toPrecision(6);
return [final_string, output_message];
}
decode(data) {
let k = 0;
let temp = "";
while (k < data.length && data[k] != '#') {
temp += data[k];
k++;
}
if (k == data.length){
alert("Invalid File! Please submit a valid De-Compressed file");
location.reload();
return;
}
if (temp === "zer") {
let decoded_data = "";
let output_message = "De-Compression complete and file will be downloaded automatically.";
return [decoded_data, output_message];
}
if (temp === "one") {
data = data.slice(k + 1);
k = 0;
temp = "";
while (data[k] != '#') {
temp += data[k];
k++;
}
let one_char = temp;
data = data.slice(k + 1);
let str_len = parseInt(data);
let decoded_data = "";
for (let i = 0; i < str_len; i++) {
decoded_data += one_char;
}
let output_message = "De-Compression complete and file will be downloaded automatically.";
return [decoded_data, output_message];
}
data = data.slice(k + 1);
let ts_length = parseInt(temp);
k = 0;
temp = "";
while (data[k] != '#') {
temp += data[k];
k++;
}
data = data.slice(k + 1);
let padding_length = parseInt(temp);
temp = "";
for (k = 0; k < ts_length; k++) {
temp += data[k];
}
data = data.slice(k);
let tree_string = temp;
temp = "";
for (k = 0; k < data.length; k++) {
temp += data[k];
}
let encoded_data = temp;
this.index = 0;
var huffman_tree = this.make_tree(tree_string);
let binary_string = "";
for (let i = 0; i < encoded_data.length; i++) {
let curr_num = encoded_data.charCodeAt(i);
let curr_binary = "";
for (let j = 7; j >= 0; j--) {
let foo = curr_num >> j;
curr_binary = curr_binary + (foo & 1);
}
binary_string += curr_binary;
}
binary_string = binary_string.slice(0, -padding_length);
let decoded_data = "";
let node = huffman_tree;
for (let i = 0; i < binary_string.length; i++) {
if (binary_string[i] === '1') {
node = node[1];
}
else {
node = node[0];
}
if (typeof (node[0]) === "string") {
decoded_data += node[0];
node = huffman_tree;
}
}
let output_message = "De-Compression complete and file will be downloaded automatically.";
return [decoded_data, output_message];
}
}
window.onload = function () {
decodeBtn = document.getElementById("decode");
encodeBtn = document.getElementById("encode");
uploadFile = document.getElementById("uploadfile")
submitBtn = document.getElementById("submitbtn");
step1 = document.getElementById("step1");
step2 = document.getElementById("step2");
step3 = document.getElementById("step3");
codecObj = new Codec();
submitBtn.onclick = function () {
var uploadedFile = uploadFile.files[0];
if (uploadedFile === undefined) {
alert("No file uploaded.\nPlease upload a file and try again");
return;
}
let nameSplit = uploadedFile.name.split('.');
var extension = nameSplit[nameSplit.length - 1].toLowerCase();
if (extension != "txt") {
alert("Invalid file type (." + extension + ") \nPlease upload a valid .txt file and try again");
return;
}
document.getElementById("step1").style.display = "none";
document.getElementById("step2").style.display = "inline-flex";
document.getElementById("startagain").style.visibility = "visible";
}
encodeBtn.onclick = function () {
var uploadedFile = uploadFile.files[0];
if (uploadedFile === undefined) {
alert("No file uploaded.\nPlease upload a file and try again");
return;
}
console.log(uploadedFile.size);
if(uploadedFile.size === 0){
alert("You have uploaded an empty file!\nThe compressed file might be larger in size than the uncompressed file (compression ratio might be smaller than one).\nBetter compression ratios are achieved for larger file sizes!");
}
else if(uploadedFile.size <= 350){
alert("The uploaded file is very small in size (" + uploadedFile.size +" bytes) !\nThe compressed file might be larger in size than the uncompressed file (compression ratio might be smaller than one).\nBetter compression ratios are achieved for larger file sizes!");
}
else if(uploadedFile.size < 1000){
alert("The uploaded file is small in size (" + uploadedFile.size +" bytes) !\nThe compressed file's size might be larger than expected (compression ratio might be small).\nBetter compression ratios are achieved for larger file sizes!");
}
onclickChanges2("Compressing your file ...\n", "Compressed");
var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
let text = fileLoadedEvent.target.result;
let [encodedString, outputMsg] = codecObj.encode(text);
myDownloadFile(uploadedFile.name.split('.')[0] + "_compressed.txt", encodedString);
ondownloadChanges(outputMsg);
}
fileReader.readAsText(uploadedFile, "UTF-8");
document.getElementById("step2").style.display = "none";
document.getElementById("step3").style.display = "inline-flex";
}
decodeBtn.onclick = function () {
console.log("decode onclick");
var uploadedFile = uploadFile.files[0];
if (uploadedFile === undefined) {
alert("No file uploaded.\nPlease upload a file and try again!");
return;
}
onclickChanges2("De-compressing your file ...\n", "De-Compressed");
var fileReader = new FileReader();
fileReader.onload = function (fileLoadedEvent) {
let text = fileLoadedEvent.target.result;
let [decodedString, outputMsg] = codecObj.decode(text);
myDownloadFile(uploadedFile.name.split('.')[0] + "_decompressed.txt", decodedString);
ondownloadChanges(outputMsg);
}
fileReader.readAsText(uploadedFile, "UTF-8");
document.getElementById("step2").style.display = "none";
document.getElementById("step3").style.display = "inline-flex";
}
}
function botVoice1(message) {
const speech = new SpeechSynthesisUtterance();
if (message==="uplaod"){
speech.text = "Please select an action to compress or decompress the file";
}
speech.volume = 1;
speech.rate = 0.75;
speech.pitch = 1;
window.speechSynthesis.speak(speech);
}
document.getElementById("submitbtn").addEventListener("click",function(){
botVoice1("uplaod");
});
function botVoice2(message) {
const speech = new SpeechSynthesisUtterance();
if (message==="uplaod"){
speech.text = "De-Compression complete and file will be downloaded automatically.";
}
speech.volume = 1;
speech.rate = 0.75;
speech.pitch = 1;
window.speechSynthesis.speak(speech);
}
document.getElementById("decode").addEventListener("click",function(){
botVoice2("uplaod");
});
function botVoice3(message) {
const speech = new SpeechSynthesisUtterance();
if (message==="uplaod"){
speech.text = "Succesfully compressed the file and file will be downloaded automatically. ";
}
speech.volume = 1;
speech.rate = 0.75;
speech.pitch = 1;
window.speechSynthesis.speak(speech);
}
document.getElementById("encode").addEventListener("click",function(){
botVoice3("uplaod");
});
function onclickChanges2(secMsg, word) {
decodeBtn.disabled = true;
encodeBtn.disabled = true;
step3.innerHTML = "";
let msg2 = document.createElement("span");
msg2.className = "text2";
msg2.innerHTML = secMsg;
step3.appendChild(msg2);
let msg3 = document.createElement("span");
msg3.className = "text2";
msg3.innerHTML = word + " file will be downloaded automatically!";
step3.appendChild(msg3);
}
function myDownloadFile(fileName, text) {
let a = document.createElement('a');
a.href = "data:application/octet-stream," + encodeURIComponent(text);
a.download = fileName;
a.click();
}
function ondownloadChanges(outputMsg) {
step3.innerHTML = "";
let img = document.createElement("img");
img.src = "done.jpg";
img.id = "doneImg";
step3.appendChild(img);
var br = document.createElement("br");
step3.appendChild(br);
let msg3 = document.createElement("span");
msg3.className = "text2";
msg3.innerHTML = outputMsg;
step3.appendChild(msg3);
}