Skip to content

Commit e0c731b

Browse files
committed
upload
1 parent 1691075 commit e0c731b

File tree

3 files changed

+326
-31
lines changed

3 files changed

+326
-31
lines changed

huffman_image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int main(int argc, char** argv)
8888
if (hist[i] != 0)
8989
nodes += 1;
9090

91-
out<<nodes<<"\n";
91+
out << nodes << "\n";
9292

9393
int totpix = height*width;
9494

improved-huffman_image.cpp

Lines changed: 66 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,19 @@ struct compare{
3232

3333
huffcode codes[256];
3434

35+
ofstream out("improved_huffman_encoded.txt");
36+
3537
void preoder(pixel* root, string s, int level, int parent){
3638
if(root->label < 256){
3739
codes[root->label].parent = parent;
3840
codes[root->label].code = s;
3941
cout << setfill(' ') << setw(3) << root->label
4042
<< " -> " << setw(20) << s << setw(4) << codes[root->label].parent << setw(20)<< fixed << setprecision(10) << root->freq << endl;
43+
44+
out << root->label << " -> " << s << "\n";
45+
4146
return;
4247
}
43-
// out << root->label << " -> " << root->left->label << " [ label = \"" << s+'0' << "\"];"<<endl;
44-
// out << root->label << " -> " << root->right->label << " [ label = \"" << s+'1' << "\"];" << endl;
4548

4649
if(level == 3)
4750
parent = root->label;
@@ -54,25 +57,51 @@ void preoder(pixel* root, string s, int level, int parent){
5457

5558

5659

57-
int main()
60+
int main(int argc, char** argv)
5861
{
59-
int width, height, bpp;
62+
int width, height, bpp, num_channel = 1;
63+
64+
if (argc>2)
65+
{
66+
cout<<"Error: Too many arguments\n";
67+
return 0;
68+
}
69+
if (argc==2)
70+
{
71+
if (string(argv[1])!="grey" && string(argv[1])!="rgb")
72+
{
73+
cout<<"Error: Invalid Argument\n";
74+
return 0;
75+
}
76+
if (string(argv[1]) == "rgb")
77+
num_channel = 3;
78+
}
6079

61-
uint8_t* rgb_image = stbi_load("colombia.jpg", &width, &height, &bpp, 1);
6280

63-
int image[height][width];
81+
uint8_t* rgb_image = stbi_load("colombia.jpg", &width, &height, &bpp, num_channel);
82+
83+
int image[height][width][num_channel];
6484
int itr = 0;
6585
for (int i = 0; i < height; ++i)
6686
for (int j = 0; j < width; ++j)
67-
image[i][j] = rgb_image[itr++];
87+
for (int k = 0; k < num_channel; ++k)
88+
image[i][j][k] = rgb_image[itr++];
6889

6990
stbi_image_free(rgb_image);
7091

7192
int hist[256] = {0};
7293

73-
for (int i = 0; i < height; i++)
74-
for (int j = 0; j < width; j++)
75-
hist[image[i][j]] += 1;
94+
for (int i = 0; i < height; ++i)
95+
for (int j = 0; j < width; ++j)
96+
for (int k = 0; k < num_channel; ++k)
97+
hist[image[i][j][k]] += 1;
98+
99+
int nodes = 0;
100+
for (int i = 0; i < 256; i++)
101+
if (hist[i] != 0)
102+
nodes += 1;
103+
104+
out << nodes << "\n";
76105

77106
int totpix = height*width;
78107

@@ -81,8 +110,6 @@ int main()
81110
for (int i = 0; i < 256; ++i)
82111
freq[i] = ((float)hist[i])/totpix;
83112

84-
85-
86113
priority_queue<pixel*, vector<pixel*>, compare> pq;
87114

88115
for(int i = 0; i < 256; i++){
@@ -116,27 +143,36 @@ int main()
116143
{
117144
for (int j = 0; j < width; ++j)
118145
{
119-
if(f == 0){
120-
compression_size += codes[image[i][j]].code.size();
121-
}
122-
else if(codes[image[i][j]].parent == codes[last].parent){
123-
compression_size += 1;
124-
string code = codes[image[i][j]].code;
125-
compression_size += code.substr(3, code.length()).size();
126-
}
127-
else{
128-
compression_size += 1;
129-
compression_size += codes[image[i][j]].code.size();
130-
}
131-
if(codes[image[i][j]].parent == -1)
132-
f = 0;
133-
else{
134-
last = image[i][j];
135-
f = 1;
146+
for (int k = 0; k < num_channel; ++k)
147+
{
148+
if(f == 0){
149+
compression_size += codes[image[i][j][k]].code.size();
150+
out << codes[image[i][j][k]].code;
151+
}
152+
else if(codes[image[i][j][k]].parent == codes[last].parent){
153+
compression_size += 1;
154+
out << "1";
155+
string code = codes[image[i][j][k]].code;
156+
out << code.substr(3, code.length());
157+
compression_size += code.substr(3, code.length()).size();
158+
}
159+
else{
160+
compression_size += 1;
161+
out << "0";
162+
compression_size += codes[image[i][j][k]].code.size();
163+
out << codes[image[i][j][k]].code;
164+
}
165+
if(codes[image[i][j][k]].parent == -1)
166+
f = 0;
167+
else{
168+
last = image[i][j][k];
169+
f = 1;
170+
}
136171
}
137172
}
138173
}
139-
174+
out << "\n";
175+
out << height << " " << width << " " << num_channel << "\n";
140176
cout << compression_size << " " << 256*256*8 << endl;
141177

142178

0 commit comments

Comments
 (0)