-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.cpp
More file actions
85 lines (69 loc) · 2.17 KB
/
Packet.cpp
File metadata and controls
85 lines (69 loc) · 2.17 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
#include "Packet.hpp"
Packet::Packet() = default;
Packet::Packet(std::vector<unsigned char> bytes)
{
id = bytes[0];
crc = bytes[1];
for (size_t i = 0; i < data.size() && i < bytes.size() - 2; i++)
data[i] = bytes[i + 2];
}
std::vector<Packet> Packet::loadImage(const std::string& filename)
{
std::vector<unsigned char> image; //the raw pixels
std::vector<Packet> packets;
unsigned dim = Packet::IMAGE_DIM;
unsigned error = lodepng::decode(image, dim, dim, filename);
if (error) {
std::cerr << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
exit(1);
}
Packet current;
auto iter = image.begin();
for (size_t i = 0; i < Packet::PACKETS; i++) {
for (size_t n = 0; n < Packet::PACKET_SIZE; n++) {
current.data[n] = *iter;
iter += 4;
}
current.id = i;
current.calc_crc();
packets.push_back(current);
}
return packets;
}
void Packet::writeImage(const std::vector<Packet>& packets, const std::string& filename)
{
std::vector<unsigned char> imageData;
imageData.reserve(PIXELS * 4);
for (auto& packet : packets) {
for (uint8_t byte : packet.data) {
//RGB are all equal in greyscale
for (auto i = 0; i < 3; i++) {
imageData.push_back(static_cast<unsigned char>(byte));
}
//Alpha (RGBA encoding)
imageData.push_back(255);
}
}
auto error = lodepng::encode(filename, imageData, Packet::IMAGE_DIM, Packet::IMAGE_DIM);
if (error) {
std::cout << "encoder error " << error << ": " << lodepng_error_text(error) << std::endl;
exit(2);
}
}
bool Packet::check_crc() const
{
auto crc = crc32(0L, data.data(), Packet::PACKET_SIZE) & 0xFF;
return this->crc == crc;
}
void Packet::calc_crc()
{
crc = crc32(0L, data.data(), Packet::PACKET_SIZE) & 0xFF;
}
std::vector<unsigned char> Packet::get_data() const
{
const auto* raw_data = (const unsigned char*)this;
std::vector<unsigned char> bytes;
for (unsigned i = 0; i < sizeof(Packet); i++)
bytes.push_back(raw_data[i]);
return bytes;
}