Skip to content

Commit e99a75e

Browse files
committed
changes
1 parent 2dac3de commit e99a75e

6 files changed

+195
-18
lines changed

arithmetic_decode_image.cpp

+32-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ const ll MAX = 4294967295;
1010
ll lower[257];
1111
vector <ll> _low, _high;
1212

13+
int parseLine(char* line){
14+
// This assumes that a digit will be found and the line ends in " Kb".
15+
int i = strlen(line);
16+
const char* p = line;
17+
while (*p <'0' || *p > '9') p++;
18+
line[i-3] = '\0';
19+
i = atoi(p);
20+
return i;
21+
}
22+
23+
int getValue(){ //Note: this value is in KB!
24+
FILE* file = fopen("/proc/self/status", "r");
25+
int result = -1;
26+
char line[128];
27+
28+
while (fgets(line, 128, file) != NULL){
29+
if (strncmp(line, "VmRSS:", 6) == 0){
30+
result = parseLine(line);
31+
break;
32+
}
33+
}
34+
fclose(file);
35+
return result;
36+
}
37+
1338
string write_bits(bool bit, int bit_to_fall){
1439
string tmp;
1540
tmp += to_string(bit);
@@ -70,13 +95,11 @@ int main()
7095

7196

7297
in >> code;
73-
cout<<code.size()<<"\n";
98+
7499
int height, width, num_channel;
75100

76101
in>>height>>width>>num_channel;
77102

78-
cout<<height<<" "<<width<<" "<<num_channel<<"\n";
79-
80103
rgb_image = (uint8_t*) malloc(width*height*num_channel);
81104

82105
int itr = 0;
@@ -127,9 +150,15 @@ int main()
127150
_high[i] = 2 * _high[i] + 1;
128151
value = add_bit(2 * value, count_taken, flag, code);
129152
count_taken++;
153+
cout<<("here");
130154
}
131155
}
132156

133157
stbi_write_png("ac_image.png", width, height, num_channel, rgb_image, width*num_channel);
134158

159+
auto stop = chrono::high_resolution_clock::now();
160+
161+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
162+
163+
cout<<(duration.count()/1000000.0)<<","<<getValue()<<"\n";
135164
}

arithmetic_encode_image.cpp

+41-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ const ll MAX = 4294967295;
1010
ll lower[257];
1111
vector <ll> low, high;
1212

13+
int parseLine(char* line){
14+
// This assumes that a digit will be found and the line ends in " Kb".
15+
int i = strlen(line);
16+
const char* p = line;
17+
while (*p <'0' || *p > '9') p++;
18+
line[i-3] = '\0';
19+
i = atoi(p);
20+
return i;
21+
}
22+
23+
int getValue(){ //Note: this value is in KB!
24+
FILE* file = fopen("/proc/self/status", "r");
25+
int result = -1;
26+
char line[128];
27+
28+
while (fgets(line, 128, file) != NULL){
29+
if (strncmp(line, "VmRSS:", 6) == 0){
30+
result = parseLine(line);
31+
break;
32+
}
33+
}
34+
fclose(file);
35+
return result;
36+
}
37+
1338
string write_bits(bool bit, int bit_to_fall){
1439
string tmp;
1540
tmp += to_string(bit);
@@ -55,6 +80,7 @@ ofstream out("arithmetic_encoded.txt");
5580

5681
string image_pixels = "";
5782
string code = "";
83+
uint8_t* rgb_image;
5884

5985
int main(int argc, char** argv)
6086
{
@@ -78,18 +104,18 @@ int main(int argc, char** argv)
78104
num_channel = 3;
79105
}
80106

81-
// string filename;
82-
// cin>>filename;
107+
string filename;
108+
cin>>filename;
83109

84-
uint8_t* rgb_image = stbi_load("./images/9.png", &width, &height, &bpp, num_channel);
110+
auto start = chrono::high_resolution_clock::now();
111+
112+
rgb_image = stbi_load(filename.c_str(), &width, &height, &bpp, num_channel);
85113

86114
const ll ONE_QTR = MAX / 4 + 1;
87115
const ll HALF = 2 * ONE_QTR;
88116
const ll THREE_QTR = 3 * ONE_QTR;
89117

90118
int len = height*width*num_channel + 1;
91-
cout<<height<<" "<<width<<" "<<num_channel<<"\n";
92-
cout<<len<<"\n";
93119

94120
low.resize(len+1);
95121
high.resize(len+1);
@@ -163,10 +189,18 @@ int main(int argc, char** argv)
163189
out << lower[i] << " ";
164190

165191
out << code << "\n";
166-
167-
cout<<code.size()<<"\n";
168192

169193
out << height << " " << width << " " << num_channel << "\n";
170194

195+
auto stop = chrono::high_resolution_clock::now();
196+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
197+
198+
cout<<filename<<",";
199+
float NoBpp = ((float)code.size())/(height*width*num_channel);
200+
float cp = (1 - ((float)code.size())/(height*width*num_channel*8))*100;
201+
cout<<height*width*num_channel<<","<<cp<<",";
202+
cout<<NoBpp<<","<<(duration.count()/1000000.0)<<",";
203+
cout<<getValue()<<",";
204+
171205
return 0;
172206
}

huffman_decode_image.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,32 @@ using namespace std;
44
#define STB_IMAGE_WRITE_IMPLEMENTATION
55
#include "stb_image_write.h"
66

7+
int parseLine(char* line){
8+
// This assumes that a digit will be found and the line ends in " Kb".
9+
int i = strlen(line);
10+
const char* p = line;
11+
while (*p <'0' || *p > '9') p++;
12+
line[i-3] = '\0';
13+
i = atoi(p);
14+
return i;
15+
}
16+
17+
int getValue(){ //Note: this value is in KB!
18+
FILE* file = fopen("/proc/self/status", "r");
19+
int result = -1;
20+
char line[128];
21+
22+
while (fgets(line, 128, file) != NULL){
23+
if (strncmp(line, "VmRSS:", 6) == 0){
24+
result = parseLine(line);
25+
break;
26+
}
27+
}
28+
fclose(file);
29+
return result;
30+
}
31+
32+
733
struct pixel{
834
int label;
935
pixel* left, *right;
@@ -102,7 +128,7 @@ int main()
102128

103129
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
104130

105-
cout<<(duration.count()/1000000.0)<<", ";
131+
cout<<(duration.count()/1000000.0)<<","<<getValue()<<"\n";
106132

107133

108134
return 0;

huffman_encode_image.cpp

+29-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ using namespace std;
33
#define STB_IMAGE_IMPLEMENTATION
44
#include "stb_image.h"
55

6+
int parseLine(char* line){
7+
// This assumes that a digit will be found and the line ends in " Kb".
8+
int i = strlen(line);
9+
const char* p = line;
10+
while (*p <'0' || *p > '9') p++;
11+
line[i-3] = '\0';
12+
i = atoi(p);
13+
return i;
14+
}
15+
16+
int getValue(){ //Note: this value is in KB!
17+
FILE* file = fopen("/proc/self/status", "r");
18+
int result = -1;
19+
char line[128];
20+
21+
while (fgets(line, 128, file) != NULL){
22+
if (strncmp(line, "VmRSS:", 6) == 0){
23+
result = parseLine(line);
24+
break;
25+
}
26+
}
27+
fclose(file);
28+
return result;
29+
}
30+
631
struct pixel{
732
float freq;
833
int label;
@@ -148,10 +173,12 @@ int main(int argc, char** argv)
148173
auto stop = chrono::high_resolution_clock::now();
149174
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
150175

176+
cout<<filename<<",";
151177
float NoBpp = ((float)compression_size)/(height*width*num_channel);
152178
float cp = (1 - ((float)compression_size)/(height*width*num_channel*8))*100;
153-
cout<<height<<"x"<<width<<"x"<<num_channel<<", "<<cp<<", ";
154-
cout<<NoBpp<<", "<<(duration.count()/1000000.0)<<", ";
179+
cout<<height*width*num_channel<<","<<cp<<",";
180+
cout<<NoBpp<<","<<(duration.count()/1000000.0)<<",";
181+
cout<<getValue()<<",";
155182

156183

157184
return 0;

improved_huffman_decode_image.cpp

+35-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ using namespace std;
55
#define STB_IMAGE_WRITE_IMPLEMENTATION
66
#include "stb_image_write.h"
77

8+
int parseLine(char* line){
9+
// This assumes that a digit will be found and the line ends in " Kb".
10+
int i = strlen(line);
11+
const char* p = line;
12+
while (*p <'0' || *p > '9') p++;
13+
line[i-3] = '\0';
14+
i = atoi(p);
15+
return i;
16+
}
17+
18+
int getValue(){ //Note: this value is in KB!
19+
FILE* file = fopen("/proc/self/status", "r");
20+
int result = -1;
21+
char line[128];
22+
23+
while (fgets(line, 128, file) != NULL){
24+
if (strncmp(line, "VmRSS:", 6) == 0){
25+
result = parseLine(line);
26+
break;
27+
}
28+
}
29+
fclose(file);
30+
return result;
31+
}
32+
33+
834
struct pixel{
935
int label;
1036
pixel* left, *right;
@@ -138,8 +164,14 @@ int main(){
138164
for (int j = 0; j < width; ++j)
139165
{
140166
rgb_image_restore[itr++] = rgb_image[i*width + j];
141-
rgb_image_restore[itr++] = rgb_image[i*width + j + width*height];
142-
rgb_image_restore[itr++] = rgb_image[i*width + j + 2*width*height];
167+
if (num_channel == 2 || num_channel == 3)
168+
{
169+
rgb_image_restore[itr++] = rgb_image[i*width + j + width*height];
170+
}
171+
if (num_channel == 3)
172+
{
173+
rgb_image_restore[itr++] = rgb_image[i*width + j + 2*width*height];
174+
}
143175
}
144176
}
145177

@@ -149,7 +181,7 @@ int main(){
149181

150182
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
151183

152-
cout<<(duration.count()/1000000.0)<<"\n";
184+
cout<<(duration.count()/1000000.0)<<","<<getValue()<<"\n";
153185

154186

155187
return 0;

improved_huffman_encode_image.cpp

+31-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,31 @@ using namespace std;
33
#define STB_IMAGE_IMPLEMENTATION
44
#include "stb_image.h"
55

6+
int parseLine(char* line){
7+
// This assumes that a digit will be found and the line ends in " Kb".
8+
int i = strlen(line);
9+
const char* p = line;
10+
while (*p <'0' || *p > '9') p++;
11+
line[i-3] = '\0';
12+
i = atoi(p);
13+
return i;
14+
}
15+
16+
int getValue(){ //Note: this value is in KB!
17+
FILE* file = fopen("/proc/self/status", "r");
18+
int result = -1;
19+
char line[128];
20+
21+
while (fgets(line, 128, file) != NULL){
22+
if (strncmp(line, "VmRSS:", 6) == 0){
23+
result = parseLine(line);
24+
break;
25+
}
26+
}
27+
fclose(file);
28+
return result;
29+
}
30+
631
struct pixel{
732
float freq;
833
int label;
@@ -179,13 +204,17 @@ int main(int argc, char** argv)
179204
out << height << " " << width << " " << num_channel << "\n";
180205
// cout << compression_size << " " << 256*256*8 << endl;
181206

207+
208+
cout<<filename<<",";
209+
182210
auto stop = chrono::high_resolution_clock::now();
183211
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
184212

185213
float NoBpp = ((float)compression_size)/(height*width*num_channel);
186214
float cp = (1 - ((float)compression_size)/(height*width*num_channel*8))*100;
187-
cout<<height<<"x"<<width<<"x"<<num_channel<<", "<<cp<<", ";
188-
cout<<NoBpp<<", "<<(duration.count()/1000000.0)<<", ";
215+
cout<<height*width*num_channel<<","<<cp<<",";
216+
cout<<NoBpp<<","<<(duration.count()/1000000.0)<<",";
217+
cout<<getValue()<<",";
189218

190219

191220

0 commit comments

Comments
 (0)