-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
182 lines (127 loc) · 4.53 KB
/
Copy pathapp.js
File metadata and controls
182 lines (127 loc) · 4.53 KB
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
const zip = new JSZip();
function convertToBinaryString(number) {
let binary = number.toString(2);
while (binary.length < 8) {
binary = "0" + binary;
}
return binary;
}
function compress(string) {
const remaining = string.length % 8;
let extraCount = 8 - remaining;
let extra = "";
let i = extraCount;
while (i--) extra += "0";
string = string + extra;
// //get 8 char long chunks of encoded data and convert to numbers
const encodedData = [];
for (let i = 0; i < string.length; i += 8) {
encodedData.push(parseInt(string.slice(i, i + 8), 2));
}
const data = {
compressed : encodedData,
extra : extraCount
}
return data;
}
function decompress(encodedData , extra) {
//get back the image from the array of numbers
let binaryString = "";
for (let i = 0; i < encodedData.length; i++) {
binaryString += convertToBinaryString(encodedData[i]);
}
binaryString = binaryString.slice(0, binaryString.length - extra);
return binaryString;
}
const image = document.getElementsByTagName("img")[0];
const canvas = document.getElementsByTagName("canvas")[0];
const ctx = canvas.getContext("2d");
const originalSize = document.getElementById("originalSize");
const compressedSize = document.getElementById("compressedSize");
const compressionRatio = document.getElementById("compressionRatio");
function getOriginalSize() {
const size = canvas.width * canvas.height * 4;
originalSize.innerHTML = size + " bytes";
}
function getCompressedSize(c) {
console.log(c)
const com_size = c.size;
compressedSize.innerHTML = com_size + " bytes";
}
function getCompressionRatio() {
const original = originalSize.innerHTML.split(" ")[0];
const compressed = compressedSize.innerHTML.split(" ")[0];
let ratio = ((original - compressed) / original) * 100;
//truncate the ratio to 2 decimal places
ratio = Math.trunc(ratio * 100) / 100;
compressionRatio.innerHTML = ratio + "%";
}
function copyToCanvas() {
const img = new Image();
img.src = image.src;
canvas.imageSmoothingEnabled = false;
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
console.log(imageData.data);
getOriginalSize();
}
const compress_button = document.getElementById("compress");
compress_button.addEventListener("click", async () => {
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
console.log(imageData);
// //appply huffman coding
const huffman = new HuffmanCoding([], "", imageData.data);
huffman.getFrequency();
huffman.getCodes();
// //encode image data
huffman.encode();
const encodedString = huffman.encoded;
//compress string
let {compressed , extra} = compress(encodedString);
compressed = new Uint8Array(compressed);
//store the compressed string in a bin file
const blob = new Blob([compressed], {
type: "application/octet-stream",
});
console.log("blob", blob);
// //store the frequencies in a json file
huffman.frequency[256] = extra;
const frequencies = JSON.stringify(huffman.frequency);
const codesBlob = new Blob([frequencies], {
type: "application/json",
});
// make a zip file of the binary file and the json file
zip.file("compressed.bin", blob);
zip.file("frequencies.json", codesBlob);
const content = await zip.generateAsync({ type: "blob" });
saveAs(content, "compressed.zip");
getCompressedSize(content);
getCompressionRatio();
});
const decompress_button = document.getElementById("decompress");
decompress_button.addEventListener("click", async () => {
const extra = jsonFileData[256];
jsonFileData = jsonFileData.slice(0, 256);
console.log("json file data : ", jsonFileData);
const decompressedString = decompress(binaryFileData , extra);
console.log("Decompressed string : ", decompressedString);
const h2 = new HuffmanCoding(jsonFileData, decompressedString, []);
h2.getCodes();
console.log(h2.codes)
const d = h2.decode();
console.log("Decoded data new ", d);
// add the data ot the canvas
const reconstructedImageData = new ImageData(
new Uint8ClampedArray(4 * canvas.width * canvas.height),
canvas.width,
canvas.height
);
for (let i = 0; i < d.length; i++) {
reconstructedImageData.data[i] = d[i];
}
console.log("Reconstucted Image data : ", reconstructedImageData);
const imageData = ctx.createImageData(canvas.width, canvas.height);
imageData.data.set(reconstructedImageData.data);
ctx.putImageData(imageData, 0, 0);
image.src = canvas.toDataURL("image/png");
});