Skip to content

Commit be6d0c5

Browse files
committed
added huffman decode and rgb support
1 parent 6372448 commit be6d0c5

File tree

3 files changed

+145
-14
lines changed

3 files changed

+145
-14
lines changed

huffman-decode.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ int main(){
9090
return 0;
9191
}
9292

93-
blvbjeanvnkjvnjlearknjnakrnekndcjbhbllivuekrjhggefskvhfbvkjdbsrjgk
93+
// blvbjeanvnkjvnjlearknjnakrnekndcjbhbllivuekrjhggefskvhfbvkjdbsrjgk

huffman-decode_image.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
#define STB_IMAGE_WRITE_IMPLEMENTATION
5+
#include "stb_image_write.h"
6+
7+
struct pixel{
8+
int label;
9+
pixel* left, *right;
10+
11+
pixel(int x): label(x){
12+
left = NULL;
13+
right = NULL;
14+
}
15+
16+
};
17+
18+
pixel* add(pixel* root, int i, string s, string label){
19+
20+
if(root == NULL){
21+
root = new pixel(-1);
22+
}
23+
24+
if(i == s.length()){
25+
cout << s << " " << label << endl;
26+
root->label = stoi(label);
27+
return root;
28+
}
29+
30+
if(s[i] == '0'){
31+
root->left = add(root->left, i+1, s, label);
32+
}
33+
else{
34+
root->right = add(root->right, i+1, s, label);
35+
}
36+
37+
return root;
38+
}
39+
40+
void preoder(pixel* root, string s){
41+
42+
if(root == NULL)
43+
return;
44+
45+
if(root->label != -1){
46+
cout << setfill(' ') << setw(3) << root->label
47+
<< " -> " << setw(7) << s << endl;
48+
return;
49+
}
50+
51+
preoder(root->left, s + '0');
52+
preoder(root->right, s + '1');
53+
54+
return;
55+
}
56+
57+
int main()
58+
{
59+
ifstream in("huffman_encoded.txt");
60+
int n;
61+
in>>n;
62+
pixel* root = new pixel(-1);
63+
for(int i=0; i<n; i++){
64+
string a,b,c;
65+
in>>a>>b>>c;
66+
add(root, 0, c, a);
67+
}
68+
69+
// for checking
70+
preoder(root, "");
71+
72+
string w;
73+
in >> w;
74+
75+
pixel* ptr = root;
76+
77+
int height, width, num_channel;
78+
79+
in>>height>>width>>num_channel;
80+
81+
uint8_t* rgb_image;
82+
rgb_image = (uint8_t*) malloc(width*height*num_channel);
83+
84+
int itr = 0;
85+
86+
for(auto i:w){
87+
if(i=='0')
88+
ptr = ptr->left;
89+
else
90+
ptr = ptr->right;
91+
if(ptr->label != -1){
92+
rgb_image[itr++] = char(ptr->label);
93+
ptr = root;
94+
}
95+
}
96+
97+
stbi_write_png("image.png", width, height, num_channel, rgb_image, width*num_channel);
98+
99+
return 0;
100+
}

huffman_image.cpp

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ struct compare{
2626

2727
string code[256];
2828

29+
ofstream out("huffman_encoded.txt");
30+
2931
void preoder(pixel* root, string s){
3032

3133
if(root->label != -1){
3234
cout << setfill(' ') << setw(3) << root->label
3335
<< " -> " << setw(15) << s << setw(20) << fixed << setprecision(8) << root->freq << endl;
36+
out << root->label << " -> " << s << "\n";
3437
code[root->label] = s;
3538
return;
3639
}
@@ -42,25 +45,50 @@ void preoder(pixel* root, string s){
4245
}
4346

4447

45-
int main()
48+
int main(int argc, char** argv)
4649
{
47-
int width, height, bpp;
50+
int width, height, bpp, num_channel = 1;
4851

49-
uint8_t* rgb_image = stbi_load("colombia.jpg", &width, &height, &bpp, 1);
52+
if (argc>2)
53+
{
54+
cout<<"Error: Too many arguments\n";
55+
return 0;
56+
}
57+
if (argc==2)
58+
{
59+
if (string(argv[1])!="grey" && string(argv[1])!="rgb")
60+
{
61+
cout<<"Error: Invalid Argument\n";
62+
return 0;
63+
}
64+
if (string(argv[1]) == "rgb")
65+
num_channel = 3;
66+
}
5067

51-
int image[height][width];
68+
uint8_t* rgb_image = stbi_load("colombia.jpg", &width, &height, &bpp, num_channel);
69+
70+
int image[height][width][num_channel];
5271
int itr = 0;
5372
for (int i = 0; i < height; ++i)
5473
for (int j = 0; j < width; ++j)
55-
image[i][j] = rgb_image[itr++];
74+
for(int k = 0; k < num_channel; ++k)
75+
image[i][j][k] = rgb_image[itr++];
5676

5777
stbi_image_free(rgb_image);
5878

5979
int hist[256] = {0};
6080

61-
for (int i = 0; i < height; i++)
62-
for (int j = 0; j < width; j++)
63-
hist[image[i][j]] += 1;
81+
for (int i = 0; i < height; ++i)
82+
for (int j = 0; j < width; ++j)
83+
for (int k = 0; k < num_channel; ++k)
84+
hist[image[i][j][k]] += 1;
85+
86+
int nodes = 0;
87+
for (int i = 0; i < 256; i++)
88+
if (hist[i] != 0)
89+
nodes += 1;
90+
91+
out<<nodes<<"\n";
6492

6593
int totpix = height*width;
6694

@@ -69,8 +97,6 @@ int main()
6997
for (int i = 0; i < 256; ++i)
7098
freq[i] = ((float)hist[i])/totpix;
7199

72-
73-
74100
priority_queue<pixel*, vector<pixel*>, compare> pq;
75101

76102
for(int i = 0; i < 256; i++){
@@ -101,11 +127,16 @@ int main()
101127
{
102128
for (int j = 0; j < width; ++j)
103129
{
104-
compression_size += code[image[i][j]].size();
130+
for (int k = 0; k < num_channel; ++k)
131+
{
132+
compression_size += code[image[i][j][k]].size();
133+
out << code[image[i][j][k]];
134+
}
105135
}
106136
}
107-
108-
cout << compression_size << " " << 256*256*8 << endl;
137+
out << "\n";
138+
out << height << " " << width << " " << num_channel << "\n";
139+
cout << compression_size << " " << 256*256*8*num_channel << endl;
109140

110141

111142
return 0;

0 commit comments

Comments
 (0)