-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIOStream.cpp
158 lines (138 loc) · 4.31 KB
/
IOStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "IOStream.hpp"
void IOStream::setTargetName(std::string filename)
{
targetName = filename;
}
void IOStream::pgmRead(std::string target, std::string &filetype, int &rows, int &cols,
int &greyScaleMax, int &size, std::vector<uint8_t> &input)
{
std::ifstream read(target, std::ios::in | std::ios::binary);
if (read.fail())
{
std::cerr << "Couldn't Open \"" << target << "\", Make sure that file exists";
return;
}
read >> filetype >> rows >> cols >> greyScaleMax;
size = rows * cols;
std::cout << "filetype: " << filetype << "\n";
std::cout << "rows: " << rows << "\n";
std::cout << "cols: " << cols << "\n";
std::cout << "greylevels: " << greyScaleMax << "\n";
std::cout << "size: " << size << "\n";
for (int i = 0; i < size; i++)
{
uint8_t pixel = read.get();
int t_data = static_cast<int>(pixel);
input.push_back(t_data);
}
read.close();
}
void IOStream::pgmWrite(std::string filetype, int rows, int cols,
int greyScaleMax, std::vector<uint8_t> &decoded)
{
std::cout << "filetype: " << filetype << "\n";
std::cout << "rows: " << rows << "\n";
std::cout << "cols: " << cols << "\n";
std::cout << "greylevels: " << greyScaleMax << "\n";
std::cout << "size: " << rows * cols << "\n";
std::string target = "./decoded/" + targetName + ".pgm";
std::ofstream write(target, std::ios::out | std::ios::binary);
if (write.fail())
{
std::cerr << "Couldn't Write to directory \"" << target << "\" Make sure it exists";
return;
}
write << filetype << "\n"
<< rows << " " << cols << "\n"
<< greyScaleMax << "\n";
int line = 1;
if (filetype.compare("P5") == 0)
{
for (auto value : decoded)
{
write << value;
}
}
else
{
for (auto value : decoded)
{
write << (int)value << " ";
if (line % 50 == 0)
write << "\n";
line++;
}
}
}
void IOStream::hufRead(std::string target, std::string &strDecoded)
{
std::ifstream read(target, std::ios::in | std::ios::binary);
if (read.fail())
{
std::cerr << "Couldn't Open the encoded file. Make sure that \"" << target << "\" exists";
return;
}
strDecoded = "";
int8_t packed;
while (!read.eof())
{
packed = read.get();
int t_data = static_cast<int>(packed);
strDecoded += std::bitset<8>(t_data).to_string();
}
read.close();
}
void IOStream::hufWrite(std::vector<int8_t> &data)
{
std::string target = "./encoded/" + targetName + ".huf";
std::ofstream write(target, std::ios::out | std::ios::binary);
if (write.fail())
{
std::cerr << "Couldn't Write to directory \"" << target << "\" Make sure it exists";
return;
}
for (auto value : data)
{
write << value;
}
write.close();
}
void IOStream::freqRead(std::string target, std::string &filetype, int &rows, int &cols,
int &greyScaleMax, int &size, int &encodedLength, std::map<int, int> &freq)
{
std::ifstream read(target, std::ios::in | std::ios::binary);
if (read.fail())
{
std::cerr << "Couldn't Open the Frequency Table. Make sure that \"" << target << "\" exists";
return;
}
read >> filetype >> rows >> cols >> greyScaleMax >> encodedLength;
size = rows * cols;
int greyValue;
int frequency;
while (true)
{
read >> greyValue >> frequency;
if (read.eof())
break;
}
read.close();
}
void IOStream::freqWrite(std::string &filetype, int &rows, int &cols,
int &greyScaleMax, int encodedLength, std::map<int, int> freq)
{
std::string target = "./encoded/" + targetName + ".frq";
std::ofstream write(target, std::ios::out | std::ios::binary);
if (write.fail())
{
std::cerr << "Couldn't Write to directory " << target << "\n"
<< "Make Sure the Directory exists";
return;
}
write << filetype << " " << rows << " " << cols << " " << greyScaleMax << " " << encodedLength << "\n";
for (auto value : freq)
{
write << (int)(value.first) << " " << (int)(value.second) << "\n";
}
write.close();
}