This repository was archived by the owner on Aug 12, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRLE.cpp
More file actions
112 lines (95 loc) · 2.28 KB
/
Copy pathRLE.cpp
File metadata and controls
112 lines (95 loc) · 2.28 KB
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
#include "RLE.h"
#include "stddfns.h"
void RLE::Encode(std::vector<unsigned char>& initial)
{
int i = 0, end = (int)initial.size();//pointer i in initial
unsigned char count_of_letter;
std::vector<bool>checked(end);
count_of_letter = 1;
while (i < end-1) {
if (initial[i] != initial[i + 1] || count_of_letter==255) {
m_encoded.push_back(count_of_letter);
m_encoded.push_back(initial[i]);
count_of_letter = 1;
}
else {
count_of_letter++;
}
checked[i] = true;
/*if (count_of_letter == 255) {
m_encoded.push_back(count_of_letter);
m_encoded.push_back(initial[i]);
count_of_letter = 1;
}*/
i++;
if (i == end - 1) {
if (initial[i] == initial[i - 1]) {
m_encoded.push_back(count_of_letter);
m_encoded.push_back(initial[i]);
}
else {
count_of_letter = 1;
m_encoded.push_back(count_of_letter);
m_encoded.push_back(initial[i]);
}
}
}
return;
//while (i < end) {
// count_of_letter = 1;
//
// //counting counts of equals letters (maximum:255)
// while (initial[i] == initial[i - 1]) {
// count_of_letter++;
// i++;
// if (count_of_letter == 255 || i == end) {
// break;
// }
//
// }
// /*if (count_of_letter == 1) {
// i+=1;
// }*/
// this->m_encoded.push_back(count_of_letter);
// this->m_encoded.push_back(initial[i-1]);
// i += 1;
//}
///*if (true) {
// m_encoded.push_back(unsigned char(1));
// m_encoded.push_back(initial.back());
//}*/
}
void RLE::Decode()
{
for (int i = 1; i < m_encoded.size(); i += 2) {
for (unsigned char j = m_encoded[i - 1]; j> 0; j--) {
m_decoded.push_back(m_encoded[i]);
}
}
}
RLE::RLE(std::vector<unsigned char> initialText)
{
std::cout << "RLE STARTING\n";
Encode(initialText);
Decode();
this->m_compressionCoefficient = (double)initialText.size() / (double)m_encoded.size();
}
std::vector<unsigned char> RLE::get_decode()
{
return m_decoded;
}
std::vector<unsigned char> RLE::get_encode()
{
return m_encoded;
}
void RLE::print_info()
{
std::cout << "\nRLE INFORMATION:\n";
std::cout << "Compression coefficient:\t" << m_compressionCoefficient << '\n';
if (MATRIX_OUTPUT) {
std::cout << "ENCODED SERIES:\n";
for (int i = 1; i < m_encoded.size(); i += 2) {
std::cout << std::dec << +m_encoded[i - 1] << ":\t" << std::hex << +m_encoded[i] << '\n';
}
}
}