-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvpl.cpp
85 lines (67 loc) · 1.68 KB
/
vpl.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
#include "vpl.h"
vpl::vpl(const std::string& filename) :vpl()
{
load(filename);
}
bool vpl::load(const std::string& filename)
{
const size_t filesize = std::filesystem::file_size(filename);
if (filesize != sizeof _header + sizeof(color[256]) + (32 * sizeof _sections[0]))
{
return false;
}
auto data = read_whole_file(filename);
return load(data.get());
}
bool vpl::load(const void* data)
{
if (!data)
{
return false;
}
char* filedata = reinterpret_cast<char*>(const_cast<void*>(data));
char* filecur = filedata;
purge();
memcpy_s(&_header, sizeof _header, filecur, sizeof _header);
filecur += sizeof _header;
const size_t table_size = _header.section_count * 256;
_internal_pal.load(filecur);
filecur += sizeof(color[256]);
_sections.reset(new byte[_header.section_count][256]);
if (!_sections)
{
purge();
return false;
}
memcpy_s(_sections.get(), table_size, filecur, table_size);
return true;
}
bool vpl::is_loaded() const
{
return !!_sections;
}
void vpl::purge()
{
_header = vplheader();
_internal_pal.purge();
_sections.reset();
}
file_type vpl::type() const
{
return file_type::vpl;
}
bool vpl::save(const std::filesystem::path path)
{
if (!is_loaded())
return false;
std::ofstream output(path.string(), std::ios::binary);
output.write(reinterpret_cast<const char*>(&_header), sizeof _header);
output.write(reinterpret_cast<const char*>(_internal_pal.entry()), sizeof(color[256]));
output.write(reinterpret_cast<const char*>(_sections.get()), _header.section_count * 256);
output.close();
return true;
}
byte(*vpl::data() const)[256]
{
return _sections.get();
}