-
Notifications
You must be signed in to change notification settings - Fork 0
/
PAZ_IO
181 lines (160 loc) · 7.25 KB
/
PAZ_IO
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ifndef PAZ_IO
#define PAZ_IO
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <array>
#include <stdexcept>
namespace paz
{
class Bytes : public std::vector<unsigned char>
{
using std::vector<unsigned char>::vector;
public:
Bytes(const char* str);
Bytes(const std::string& str);
std::string str() const;
};
class Archive
{
std::unordered_map<std::string, Bytes> _blocks;
public:
Archive() = default;
Archive(const Bytes& src);
void add(const std::string& name, const Bytes& data);
Bytes get(const std::string& name) const;
void read(const std::string& path);
void write(const std::string& path) const;
void clear();
std::unordered_set<std::string> contents() const;
};
#ifndef PAZ_GRAPHICS
enum class ImageFormat
{
R8UInt, R8SInt, R8UNorm, R8SNorm, R16UInt, R16SInt, R16UNorm, R16SNorm,
R16Float, R32UInt, R32SInt, R32Float, RG8UInt, RG8SInt, RG8UNorm,
RG8SNorm, RG16UInt, RG16SInt, RG16UNorm, RG16SNorm, RG16Float, RG32UInt,
RG32SInt, RG32Float, RGBA8UInt, RGBA8SInt, RGBA8UNorm, RGBA8UNorm_sRGB,
RGBA8SNorm, RGBA16UInt, RGBA16SInt, RGBA16UNorm, RGBA16SNorm,
RGBA16Float, RGBA32UInt, RGBA32SInt, RGBA32Float
};
class Image
{
std::vector<unsigned char> _data;
ImageFormat _format;
int _rows = 0;
int _cols = 0;
public:
Image() = default;
Image(ImageFormat format, int width, int height) : _format(format),
_rows(height), _cols(width)
{
int c, b;
switch(_format)
{
case ImageFormat::R8UInt: c = 1; b = 1; break;
case ImageFormat::R8SInt: c = 1; b = 1; break;
case ImageFormat::R8UNorm: c = 1; b = 1; break;
case ImageFormat::R8SNorm: c = 1; b = 1; break;
case ImageFormat::R16UInt: c = 1; b = 2; break;
case ImageFormat::R16SInt: c = 1; b = 2; break;
case ImageFormat::R16UNorm: c = 1; b = 2; break;
case ImageFormat::R16SNorm: c = 1; b = 2; break;
case ImageFormat::R16Float: c = 1; b = 2; break;
case ImageFormat::R32UInt: c = 1; b = 4; break;
case ImageFormat::R32SInt: c = 1; b = 4; break;
case ImageFormat::R32Float: c = 1; b = 4; break;
case ImageFormat::RG8UInt: c = 2; b = 1; break;
case ImageFormat::RG8SInt: c = 2; b = 1; break;
case ImageFormat::RG8UNorm: c = 2; b = 1; break;
case ImageFormat::RG8SNorm: c = 2; b = 1; break;
case ImageFormat::RG16UInt: c = 2; b = 2; break;
case ImageFormat::RG16SInt: c = 2; b = 2; break;
case ImageFormat::RG16UNorm: c = 2; b = 2; break;
case ImageFormat::RG16SNorm: c = 2; b = 2; break;
case ImageFormat::RG16Float: c = 2; b = 2; break;
case ImageFormat::RG32UInt: c = 2; b = 4; break;
case ImageFormat::RG32SInt: c = 2; b = 4; break;
case ImageFormat::RG32Float: c = 2; b = 4; break;
case ImageFormat::RGBA8UInt: c = 4; b = 1; break;
case ImageFormat::RGBA8SInt: c = 4; b = 1; break;
case ImageFormat::RGBA8UNorm: c = 4; b = 1; break;
case ImageFormat::RGBA8UNorm_sRGB: c = 4; b = 1; break;
case ImageFormat::RGBA8SNorm: c = 4; b = 1; break;
case ImageFormat::RGBA16UInt: c = 4; b = 2; break;
case ImageFormat::RGBA16SInt: c = 4; b = 2; break;
case ImageFormat::RGBA16UNorm: c = 4; b = 2; break;
case ImageFormat::RGBA16SNorm: c = 4; b = 2; break;
case ImageFormat::RGBA16Float: c = 4; b = 2; break;
case ImageFormat::RGBA32UInt: c = 4; b = 4; break;
case ImageFormat::RGBA32SInt: c = 4; b = 4; break;
case ImageFormat::RGBA32Float: c = 4; b = 4; break;
default: throw std::logic_error("Unrecognized image format.");
}
_data.resize(static_cast<std::size_t>(width)*height*c*b);
}
Image(ImageFormat format, int width, int height, const void* data) :
Image(format, width, height)
{
std::copy(reinterpret_cast<const unsigned char*>(data),
reinterpret_cast<const unsigned char*>(data) + _data.size(),
_data.begin());
}
std::vector<unsigned char>& bytes()
{
return _data;
}
const std::vector<unsigned char>& bytes() const
{
return _data;
}
int width() const
{
return _cols;
}
int height() const
{
return _rows;
}
ImageFormat format() const
{
return _format;
}
};
#endif
std::array<std::string, 3> split_path(const std::string& path);
Image parse_pbm(const Bytes& content);
Bytes to_pbm(const Image& img);
Image parse_bmp(const Bytes& content);
Bytes to_bmp(const Image& img);
// Positions and normals are padded for graphics compatibility ([vx, vy, vz,
// 1] and [nx, ny, nz, 0]) and materials are one-indexed (0: unassigned, 1:
// `materialNames[0]`, etc.).
void parse_obj(const Bytes& content, std::vector<std::string>& names, std::
vector<std::vector<float>>& positions, std::vector<std::vector<float>>&
uvs, std::vector<std::vector<float>>& normals, std::vector<std::vector<
unsigned int>>& materials, std::vector<std::string>& materialNames,
std::vector<std::string>& materialLibs);
void parse_obj(const Bytes& content, std::vector<std::string>& names, std::
vector<std::vector<float>>& positions, std::vector<std::vector<float>>&
uvs, std::vector<std::vector<float>>& normals, std::vector<std::vector<
unsigned int>>& materials, std::vector<std::string>& materialNames,
std::vector<std::string>& materialLibs, std::vector<std::vector<unsigned
int>>& indices);
void parse_model(const Bytes& content, std::vector<float>& positions, std::
vector<float>& uvs, std::vector<float>& normals, std::vector<unsigned
int>& materials, std::vector<std::string>& materialNames, std::vector<
std::string>& materialLibs, std::vector<unsigned int>& indices);
Bytes to_model(const std::vector<float>& positions, const std::vector<
float>& uvs, const std::vector<float>& normals, const std::vector<
unsigned int>& materials, const std::vector<std::string>& materialNames,
const std::vector<std::string>& materialLibs, const std::vector<unsigned
int>& indices);
Bytes compress(const Bytes& src);
Bytes uncompress(const Bytes& src);
Bytes read_bytes(const std::string& path);
void write_bytes(const std::string& path, const Bytes& data);
void remove(const std::string& path);
}
#endif