Skip to content

Commit 4d0691d

Browse files
committed
Minor Updates
2 parents 9dcf332 + e99a75e commit 4d0691d

8 files changed

+202
-76
lines changed

arithmetic_decode.cpp

-17
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,6 @@ const ll MAX = 65535;
99

1010
int lower[257];
1111

12-
#define err() cout<<"=================================="<<endl;
13-
#define errA(A) for(auto i:A) cout<<i<<" ";cout<<endl;
14-
#define err1(a) cout<<#a<<" "<<a<<endl
15-
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
16-
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl
17-
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl
18-
19-
string write_bits(bool bit, int bit_to_fall){
20-
string tmp;
21-
tmp += to_string(bit);
22-
while(bit_to_fall){
23-
tmp += to_string(!bit);
24-
bit_to_fall -= 1;
25-
}
26-
return tmp;
27-
}
28-
2912
int to_int(int _pos, string &encode){
3013
int n = 0;
3114
for (int i = _pos; i < sizeof(short) * 8 + _pos; i++)

arithmetic_decode_image.cpp

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

13-
int to_int(int _pos, string &encode){
14-
int n = 0;
13+
// <<<<<<< HEAD
14+
// =======
15+
int parseLine(char* line){
16+
// This assumes that a digit will be found and the line ends in " Kb".
17+
int i = strlen(line);
18+
const char* p = line;
19+
while (*p <'0' || *p > '9') p++;
20+
line[i-3] = '\0';
21+
i = atoi(p);
22+
return i;
23+
}
24+
25+
int getValue(){ //Note: this value is in KB!
26+
FILE* file = fopen("/proc/self/status", "r");
27+
int result = -1;
28+
char line[128];
29+
30+
while (fgets(line, 128, file) != NULL){
31+
if (strncmp(line, "VmRSS:", 6) == 0){
32+
result = parseLine(line);
33+
break;
34+
}
35+
}
36+
fclose(file);
37+
return result;
38+
}
39+
40+
// >>>>>>> e99a75e16414337047bd8dbd39bc133504179d4b
41+
ll to_int(int _pos, string &encode){
42+
ll n = 0;
1543
for (int i = _pos; i < 32 + _pos; i++)
1644
{
1745
n <<= 1;
@@ -37,7 +65,7 @@ ll add_bit(ll value, int count_taken, bool &flag, string &encode){
3765
a.reset(0);
3866
}
3967

40-
value = (ll)(a.to_ulong());
68+
value = (ll)(a.to_ullong());
4169
return value;
4270
}
4371

@@ -60,13 +88,11 @@ int main()
6088

6189

6290
in >> code;
63-
cout<<code.size()<<"\n";
91+
6492
int height, width, num_channel;
6593

6694
in>>height>>width>>num_channel;
6795

68-
cout<<height<<" "<<width<<" "<<num_channel<<"\n";
69-
7096
rgb_image = (uint8_t*) malloc(width*height*num_channel);
7197

7298
int itr = 0;
@@ -95,6 +121,8 @@ int main()
95121

96122
rgb_image[itr++] = symbol;
97123

124+
cout << "here: " << value << " [" << _low[i] << ", " << _high[i] << ")" << endl;
125+
98126
for(;;){
99127
if(_high[i] >= HALF){
100128
if(_low[i] >= HALF){
@@ -122,4 +150,9 @@ int main()
122150

123151
stbi_write_png("ac_image.png", width, height, num_channel, rgb_image, width*num_channel);
124152

153+
auto stop = chrono::high_resolution_clock::now();
154+
155+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
156+
157+
cout<<(duration.count()/1000000.0)<<","<<getValue()<<"\n";
125158
}

arithmetic_encode.cpp

-38
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@ const ll MAX = 65535;
99

1010
int lower[257];
1111

12-
#define err() cout<<"=================================="<<endl;
13-
#define errA(A) for(auto i:A) cout<<i<<" ";cout<<endl;
14-
#define err1(a) cout<<#a<<" "<<a<<endl
15-
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
16-
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl
17-
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl
18-
1912
string write_bits(bool bit, int bit_to_fall){
2013
string tmp;
2114
tmp += to_string(bit);
@@ -26,37 +19,6 @@ string write_bits(bool bit, int bit_to_fall){
2619
return tmp;
2720
}
2821

29-
int to_int(int _pos, string &encode){
30-
int n = 0;
31-
for (int i = _pos; i < sizeof(short) * 8 + _pos; i++)
32-
{
33-
n <<= 1;
34-
n |= encode[i] - '0';
35-
}
36-
return n;
37-
}
38-
39-
ll add_bit(ll value, int count_taken, bool &flag, string &encode){
40-
bitset<16> a(value);
41-
42-
if(flag == 1){
43-
a.reset(0);
44-
}
45-
else if(count_taken > encode.length()){
46-
a.set(0);
47-
flag = 1;
48-
}
49-
else if(encode[count_taken] == '1'){
50-
a.set(0);
51-
}
52-
else if(encode[count_taken] == '0'){
53-
a.reset(0);
54-
}
55-
56-
value = (ll)(a.to_ulong());
57-
return value;
58-
}
59-
6022
int main(){
6123

6224
const ll ONE_QTR = MAX / 4 + 1;

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);
@@ -24,6 +49,7 @@ ofstream out("arithmetic_encoded.txt");
2449

2550
string image_pixels = "";
2651
string code = "";
52+
uint8_t* rgb_image;
2753

2854
int main(int argc, char** argv)
2955
{
@@ -47,18 +73,18 @@ int main(int argc, char** argv)
4773
num_channel = 3;
4874
}
4975

50-
// string filename;
51-
// cin>>filename;
76+
string filename;
77+
cin>>filename;
5278

53-
uint8_t* rgb_image = stbi_load("./images/9.png", &width, &height, &bpp, num_channel);
79+
auto start = chrono::high_resolution_clock::now();
80+
81+
rgb_image = stbi_load(filename.c_str(), &width, &height, &bpp, num_channel);
5482

5583
const ll ONE_QTR = MAX / 4 + 1;
5684
const ll HALF = 2 * ONE_QTR;
5785
const ll THREE_QTR = 3 * ONE_QTR;
5886

5987
int len = height*width*num_channel + 1;
60-
cout<<height<<" "<<width<<" "<<num_channel<<"\n";
61-
cout<<len<<"\n";
6288

6389
low.resize(len+1);
6490
high.resize(len+1);
@@ -132,10 +158,18 @@ int main(int argc, char** argv)
132158
out << lower[i] << " ";
133159

134160
out << code << "\n";
135-
136-
cout<<code.size()<<"\n";
137161

138162
out << height << " " << width << " " << num_channel << "\n";
139163

164+
auto stop = chrono::high_resolution_clock::now();
165+
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
166+
167+
cout<<filename<<",";
168+
float NoBpp = ((float)code.size())/(height*width*num_channel);
169+
float cp = (1 - ((float)code.size())/(height*width*num_channel*8))*100;
170+
cout<<height*width*num_channel<<","<<cp<<",";
171+
cout<<NoBpp<<","<<(duration.count()/1000000.0)<<",";
172+
cout<<getValue()<<",";
173+
140174
return 0;
141175
}

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;

0 commit comments

Comments
 (0)