@@ -32,16 +32,19 @@ struct compare{
32
32
33
33
huffcode codes[256 ];
34
34
35
+ ofstream out (" improved_huffman_encoded.txt" );
36
+
35
37
void preoder (pixel* root, string s, int level, int parent){
36
38
if (root->label < 256 ){
37
39
codes[root->label ].parent = parent;
38
40
codes[root->label ].code = s;
39
41
cout << setfill (' ' ) << setw (3 ) << root->label
40
42
<< " -> " << 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
+
41
46
return ;
42
47
}
43
- // out << root->label << " -> " << root->left->label << " [ label = \"" << s+'0' << "\"];"<<endl;
44
- // out << root->label << " -> " << root->right->label << " [ label = \"" << s+'1' << "\"];" << endl;
45
48
46
49
if (level == 3 )
47
50
parent = root->label ;
@@ -54,25 +57,51 @@ void preoder(pixel* root, string s, int level, int parent){
54
57
55
58
56
59
57
- int main ()
60
+ int main (int argc, char ** argv )
58
61
{
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
+ }
60
79
61
- uint8_t * rgb_image = stbi_load (" colombia.jpg" , &width, &height, &bpp, 1 );
62
80
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];
64
84
int itr = 0 ;
65
85
for (int i = 0 ; i < height; ++i)
66
86
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++];
68
89
69
90
stbi_image_free (rgb_image);
70
91
71
92
int hist[256 ] = {0 };
72
93
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 " ;
76
105
77
106
int totpix = height*width;
78
107
@@ -81,8 +110,6 @@ int main()
81
110
for (int i = 0 ; i < 256 ; ++i)
82
111
freq[i] = ((float )hist[i])/totpix;
83
112
84
-
85
-
86
113
priority_queue<pixel*, vector<pixel*>, compare> pq;
87
114
88
115
for (int i = 0 ; i < 256 ; i++){
@@ -116,27 +143,36 @@ int main()
116
143
{
117
144
for (int j = 0 ; j < width; ++j)
118
145
{
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
+ }
136
171
}
137
172
}
138
173
}
139
-
174
+ out << " \n " ;
175
+ out << height << " " << width << " " << num_channel << " \n " ;
140
176
cout << compression_size << " " << 256 *256 *8 << endl;
141
177
142
178
0 commit comments