Skip to content

Commit 2e81466

Browse files
committed
updates
1 parent 676c182 commit 2e81466

File tree

4 files changed

+54
-15
lines changed

4 files changed

+54
-15
lines changed

huffman-decode_image.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pixel* add(pixel* root, int i, string s, string label){
2222
}
2323

2424
if(i == s.length()){
25-
cout << s << " " << label << endl;
25+
// cout << s << " " << label << endl;
2626
root->label = stoi(label);
2727
return root;
2828
}
@@ -56,6 +56,8 @@ void preoder(pixel* root, string s){
5656

5757
int main()
5858
{
59+
auto start = chrono::high_resolution_clock::now();
60+
5961
ifstream in("huffman_encoded.txt");
6062
int n;
6163
in>>n;
@@ -67,7 +69,7 @@ int main()
6769
}
6870

6971
// for checking
70-
preoder(root, "");
72+
// preoder(root, "");
7173

7274
string w;
7375
in >> w;
@@ -94,7 +96,14 @@ int main()
9496
}
9597
}
9698

97-
stbi_write_png("image.png", width, height, num_channel, rgb_image, width*num_channel);
99+
stbi_write_png("image.bmp", width, height, num_channel, rgb_image, width*num_channel);
100+
101+
auto stop = chrono::high_resolution_clock::now();
102+
103+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
104+
105+
cout<<(duration.count()/1000000.0)<<", ";
106+
98107

99108
return 0;
100109
}

huffman_image.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ ofstream out("huffman_encoded.txt");
3131
void preoder(pixel* root, string s){
3232

3333
if(root->label != -1){
34-
cout << setfill(' ') << setw(3) << root->label
35-
<< " -> " << setw(15) << s << setw(20) << fixed << setprecision(8) << root->freq << endl;
34+
// cout << setfill(' ') << setw(3) << root->label
35+
// << " -> " << setw(15) << s << setw(20) << fixed << setprecision(8) << root->freq << endl;
3636
out << root->label << " -> " << s << "\n";
3737
code[root->label] = s;
3838
return;
@@ -49,6 +49,7 @@ int main(int argc, char** argv)
4949
{
5050
int width, height, bpp, num_channel = 1;
5151

52+
5253
if (argc>2)
5354
{
5455
cout<<"Error: Too many arguments\n";
@@ -65,7 +66,9 @@ int main(int argc, char** argv)
6566
num_channel = 3;
6667
}
6768

68-
uint8_t* rgb_image = stbi_load("colombia.jpg", &width, &height, &bpp, num_channel);
69+
auto start = chrono::high_resolution_clock::now();
70+
71+
uint8_t* rgb_image = stbi_load("7.bmp", &width, &height, &bpp, num_channel);
6972

7073
int image[height][width][num_channel];
7174
int itr = 0;
@@ -134,9 +137,17 @@ int main(int argc, char** argv)
134137
}
135138
}
136139
}
140+
cout<<compression_size<<"\n";
137141
out << "\n";
138142
out << height << " " << width << " " << num_channel << "\n";
139-
cout << compression_size << " " << 256*256*8*num_channel << endl;
143+
144+
auto stop = chrono::high_resolution_clock::now();
145+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
146+
147+
float NoBpp = ((float)compression_size)/(height*width*num_channel);
148+
float cp = (1 - ((float)compression_size)/(height*width*num_channel*8))*100;
149+
cout<<height<<"x"<<width<<"x"<<num_channel<<", "<<cp<<", ";
150+
cout<<NoBpp<<", "<<(duration.count()/1000000.0)<<", ";
140151

141152

142153
return 0;

improved-huffman-decode_image.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pixel* add(pixel* root, int i, string s, string label, int level = 0){
2626
}
2727

2828
if(i == s.length()){
29-
cout << s << " " << (stoi(label)) << " " << label << endl;
29+
// cout << s << " " << (stoi(label)) << " " << label << endl;
3030
root->label = stoi(label);
3131
if(level > 3){
3232
flag[root->label] = 1;
@@ -62,6 +62,9 @@ void preoder(pixel* root, string s){
6262
}
6363

6464
int main(){
65+
66+
auto start = chrono::high_resolution_clock::now();
67+
6568
ifstream in("improved_huffman_encoded.txt");
6669
int n;
6770
in>>n;
@@ -74,7 +77,7 @@ int main(){
7477
}
7578

7679
// for checking
77-
preoder(root, "");
80+
// preoder(root, "");
7881

7982
string w;
8083
in >> w;
@@ -99,7 +102,7 @@ int main(){
99102
}
100103
else{
101104
ptr = tmp;
102-
level = 3;
105+
level = 3;
103106
}
104107
f = 0;
105108
continue;
@@ -128,7 +131,13 @@ int main(){
128131
}
129132
}
130133

131-
stbi_write_png("improved_image.png", width, height, num_channel, rgb_image, width*num_channel);
134+
stbi_write_png("improved_image.bmp", width, height, num_channel, rgb_image, width*num_channel);
135+
136+
auto stop = chrono::high_resolution_clock::now();
137+
138+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
139+
140+
cout<<(duration.count()/1000000.0)<<"\n";
132141

133142

134143
return 0;

improved-huffman_image.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void preoder(pixel* root, string s, int level, int parent){
3838
if(root->label < 256){
3939
codes[root->label].parent = parent;
4040
codes[root->label].code = s;
41-
cout << setfill(' ') << setw(3) << root->label
42-
<< " -> " << setw(20) << s << setw(4) << codes[root->label].parent << setw(20)<< fixed << setprecision(10) << root->freq << endl;
41+
// cout << setfill(' ') << setw(3) << root->label
42+
// << " -> " << setw(20) << s << setw(4) << codes[root->label].parent << setw(20)<< fixed << setprecision(10) << root->freq << endl;
4343

4444
out << root->label << " -> " << s << "\n";
4545

@@ -77,8 +77,9 @@ int main(int argc, char** argv)
7777
num_channel = 3;
7878
}
7979

80+
auto start = chrono::high_resolution_clock::now();
8081

81-
uint8_t* rgb_image = stbi_load("colombia.jpg", &width, &height, &bpp, num_channel);
82+
uint8_t* rgb_image = stbi_load("7.bmp", &width, &height, &bpp, num_channel);
8283

8384
int image[height][width][num_channel];
8485
int itr = 0;
@@ -173,7 +174,16 @@ int main(int argc, char** argv)
173174
}
174175
out << "\n";
175176
out << height << " " << width << " " << num_channel << "\n";
176-
cout << compression_size << " " << 256*256*8 << endl;
177+
// cout << compression_size << " " << 256*256*8 << endl;
178+
179+
auto stop = chrono::high_resolution_clock::now();
180+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
181+
182+
float NoBpp = ((float)compression_size)/(height*width*num_channel);
183+
float cp = (1 - ((float)compression_size)/(height*width*num_channel*8))*100;
184+
cout<<cp<<", ";
185+
cout<<NoBpp<<", "<<(duration.count()/1000000.0)<<", ";
186+
177187

178188

179189
return 0;

0 commit comments

Comments
 (0)