/
darx.cpp
384 lines (357 loc) · 15 KB
/
darx.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "darx.h"
#include <iostream>
#include <assert.h>
// magic number for darx files
#define DARX_MAGIC "DARX"
#define DARX_MAGIC_LEN 4
// endianness test "LIVE" is big endian, "EVIL" is little endian
#define DARX_MAGIC_BE 0x4c495645
namespace darx{
int VERBOSE;
CustomElementTypeStruct UNKNOWN_TYPE(TYPE_CUSTOM, 1, 8, "unknown");
const char* errors[]={
"success", "unsupported element type", "unsuported compression type",
"invalid structure"
};
// structure used for mixed types
MixedElementTypeStruct::MixedElementTypeStruct(
ElementType _type, uint8_t _components, uint8_t _bit_width
): ElementTypeStruct(_type, _components, _bit_width){
subtypes = new ElementTypeStruct*[_components];
}
MixedElementTypeStruct::~MixedElementTypeStruct(){
if(subtypes){
delete[] subtypes;
subtypes = 0;
}
};
// structure used for custom types
CustomElementTypeStruct::CustomElementTypeStruct(
ElementType _type, uint8_t _components, uint8_t _bit_width, const char* _type_name
): ElementTypeStruct(_type,_components,_bit_width) {
int len=strlen(_type_name);
type_name=new char[len+1];
for(int i=0; i<len; i++){type_name[i] = _type_name[i];}
}
typedef struct {
bool swapEndian;
uint8_t int_size;
uint8_t long_size;
} data_type_info;
bool write_tensor_type(ElementTypeStruct* tensor_type, FILE* file){
if(!tensor_type){
return false;
}
uint8_t ttt = (uint8_t)tensor_type->type;
if(VERBOSE){ std::cout << "# element type : " << ((int)ttt) << std::endl; }
fwrite(&ttt, sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# comps : " << ((int)tensor_type->components) << std::endl; }
fwrite(&(tensor_type->components), sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# bitwidth : " << ((int)tensor_type->bit_width) << std::endl; }
fwrite(&(tensor_type->bit_width ), sizeof(uint8_t), 1, file);
switch(tensor_type->type){
case TYPE_INT: case TYPE_UINT: case TYPE_FLOAT: case TYPE_CHAR:
break;
case TYPE_MIXED:{
int components = tensor_type->components;
MixedElementTypeStruct* mixedtype = (MixedElementTypeStruct*)tensor_type;
for(int comp_idx=0; comp_idx < components; comp_idx++){
write_tensor_type(mixedtype->subtypes[comp_idx], file);
}
} break;
case TYPE_CUSTOM:{
CustomElementTypeStruct* customtype = (CustomElementTypeStruct*)tensor_type;
const char* type_name = customtype->type_name ? customtype->type_name : "Unknown";
uint8_t namelen = strlen(type_name);
fwrite(&namelen, sizeof(uint8_t), 1, file);
fwrite(type_name, sizeof(char), namelen, file);
} break;
default:
return UNSUPPORTED_ELEMENT_TYPE;
}
return true;
}
ElementTypeStruct* read_tensor_type(darx& darx, FILE* file, data_type_info& dtinfo){
uint8_t tensor_type_tag, components, bit_width;
fread(&tensor_type_tag, sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# element type : " << ((int)tensor_type_tag) << std::endl; }
fread(&(components), sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# comps : " << ((int)components) << std::endl; }
fread(&(bit_width ), sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# bitwidth : " << ((int)bit_width) << std::endl; }
switch(tensor_type_tag){
case TYPE_INT: case TYPE_UINT: case TYPE_FLOAT: case TYPE_CHAR:
return new ElementTypeStruct((ElementType)tensor_type_tag, components, bit_width);
break;
case TYPE_MIXED:{
MixedElementTypeStruct* mixedtype = new MixedElementTypeStruct((ElementType)tensor_type_tag, components, bit_width);
for(int comp_idx=0; comp_idx < components; comp_idx++){
mixedtype->subtypes[comp_idx] = read_tensor_type(darx, file, dtinfo);
}
return mixedtype;
} break;
case TYPE_CUSTOM:{
uint8_t namelen;
fread(&namelen, sizeof(uint8_t), 1, file);
char* type_name = new char[namelen+1];
fread(type_name, sizeof(char), namelen, file);
type_name[namelen] = 0;
CustomElementTypeStruct* customtype = new CustomElementTypeStruct(
(ElementType)tensor_type_tag, components, bit_width, type_name
);
return customtype;
} break;
default:
return 0;
}
}
bool compress_data(datatensor& tensor, uint8_t** cdata, unsigned int* cdata_len, bool* cdata_is_temp);
bool decompress_data(datatensor& tensor, uint8_t** cdata, unsigned int* cdata_len, bool* cdata_is_temp);
int write_tensor(datatensor& tensor, FILE* file){
// write tensor's name
uint8_t namelen = tensor.name ? strlen(tensor.name) : 0;
fwrite(&namelen, sizeof(uint8_t), 1, file);
if(tensor.name){
if(VERBOSE){ std::cout << "# name : " << tensor.name << std::endl; }
fwrite(tensor.name, sizeof(char), namelen, file);
}
// write the tensor's rank
if(VERBOSE){ std::cout << "# rank : " << ((int)tensor.rank) << std::endl; }
fwrite(&(tensor.rank), sizeof(uint8_t), 1, file);
// write the length of each dimmension
if(VERBOSE){ std::cout << "# lengths : "; for(int i=0; i < tensor.rank; i++){ std::cout << tensor.lengths[i] << " "; } std::cout << std::endl; }
fwrite(tensor.lengths, sizeof(unsigned int), tensor.rank, file);
// write the element type stuct
if(!write_tensor_type(tensor.type, file)){
return UNSUPPORTED_ELEMENT_TYPE;
}
// write the data (possibly compressed)
{
bool cdata_is_temp=false;
unsigned int cdata_length=0;
uint8_t* cdata=0;
if(!tensor.data){
return INVALID_STRUCT;
}
if(!compress_data(tensor, &cdata, &cdata_length, &cdata_is_temp)){
if(VERBOSE){ std::cout << "# cdata size: " << cdata_length << std::endl; }
return UNSUPPORTED_COMPRESS_TYPE;
}
uint8_t ctype = (uint8_t)tensor.compression;
fwrite(&ctype, sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# cdata size: " << cdata_length << std::endl; }
fwrite(&cdata_length, sizeof(unsigned int), 1, file);
if(VERBOSE){ std::cout << "# cdata : " << ((void*)cdata) << std::endl; }
fwrite(cdata, cdata_length, 1, file);
if(cdata_is_temp){
if(VERBOSE){ std::cout << "# deleting temp cdata... " << cdata << std::endl; }
delete[] cdata;
}
}
return SUCCESS;
}
bool compress_data(datatensor& tensor, uint8_t** cdata, unsigned int* cdata_len, bool* cdata_is_temp){
CompressionType compression = tensor.compression;
if(compression == UNCOMPRESSED){
if(VERBOSE){ std::cout << "# [no compression] " << std::endl; }
(*cdata) = (uint8_t*)tensor.data;
(*cdata_len) = tensor.data_size;
(*cdata_is_temp) = false;
return true;
}
return false;
}
bool decompress_data(datatensor& tensor, uint8_t** cdata, unsigned int* cdata_len, bool* cdata_is_temp){
CompressionType compression = tensor.compression;
if(compression == UNCOMPRESSED){
if(VERBOSE){ std::cout << "# [no compression] " << std::endl; }
tensor.data = (*cdata);
tensor.data_size = (*cdata_len);
(*cdata_is_temp) = false;
return true;
}
return false;
}
/**
* Indicates whether a given open file contains .darx data.
*
* @return true if the data pointed by the file pointer is a .darx file,
* false otherwise.
*/
bool read_tensor(datatensor& tensor, darx& darx, FILE* file, data_type_info& dtinfo){
if(VERBOSE){ std::cout << "# file pos: " << ftell(file) << std::endl; }
uint8_t namelen=0;
fread(&namelen, sizeof(uint8_t), 1, file);
if(namelen > 0){
char* tensor_name = new char[namelen+1];
tensor.name=tensor_name;
fread(tensor_name, sizeof(uint8_t), namelen, file);
tensor_name[namelen]=0;
if(VERBOSE){ std::cout << "# name : " << tensor.name << std::endl; }
} else {
if(VERBOSE){ std::cout << "# (unnamed)" << std::endl; }
}
// write the tensor's rank
fread(&(tensor.rank), sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# rank : " << ((int)tensor.rank) << std::endl; }
// write the length of each dimmension
tensor.lengths = new unsigned int[tensor.rank];
if(VERBOSE){ std::cout << "# lengths : "; }
for(int dim_idx=0; dim_idx < tensor.rank; dim_idx++){
tensor.lengths[dim_idx]=0;
fread(&(tensor.lengths[dim_idx]), dtinfo.int_size, 1, file);
if(VERBOSE){ std::cout << tensor.lengths[dim_idx] << " "; }
}
if(VERBOSE){ std::cout << std::endl; }
// write the element type stuct
tensor.type = read_tensor_type(darx, file, dtinfo);
if(!tensor.type){
return UNSUPPORTED_ELEMENT_TYPE;
}
// write the data (possibly compressed)
{
uint8_t ctype;
fread(&ctype, sizeof(uint8_t), 1, file);
tensor.compression = (CompressionType)ctype;
if(VERBOSE){ std::cout << "# compression : " << ((int)ctype) << std::endl; }
bool cdata_is_temp=false;
unsigned int cdata_length;
fread(&cdata_length, sizeof(unsigned int), 1, file);
if(VERBOSE){ std::cout << "# cdata size: " << cdata_length << std::endl; }
uint8_t* cdata = new uint8_t[cdata_length];
fread(cdata, cdata_length, 1, file);
if(VERBOSE){ std::cout << "# cdata : " << ((void*)cdata) << std::endl; }
if(!decompress_data(tensor, &cdata, &cdata_length, &cdata_is_temp)){
return UNSUPPORTED_COMPRESS_TYPE;
}
if(cdata_is_temp){
if(VERBOSE){ std::cout << "# deleting temp cdata... " << cdata << std::endl; }
delete[] cdata;
}
}
return SUCCESS;
}
bool is_darx(FILE* file){
char magic[DARX_MAGIC_LEN];
fread(magic, sizeof(char), DARX_MAGIC_LEN, file); // read magic number
fseek(file, 0, SEEK_SET); // reset the file read position
return !strncmp(magic, DARX_MAGIC, DARX_MAGIC_LEN);
}
/** Loads a darx data archive from a file into the structure.
*
* @param[in] darx - reference to the darx structure to fill
* @param[in] file - pointer to the file containing the darx data
*
* @returns an error code indicating success or reson of failure
* @note reads only uncompressed 8,16 and 32 bit-per-pixel bmp images.
*/
ErrorCode load_image_from(darx& darx, FILE* file){
data_type_info dtinfo;
char magic[DARX_MAGIC_LEN+1];
fread(magic, sizeof(char), DARX_MAGIC_LEN, file);
magic[DARX_MAGIC_LEN] = 0;
if(VERBOSE){ std::cout << "# read magic number : '" << magic << "' == " << DARX_MAGIC << std::endl; }
if(VERBOSE){ std::cout << "# " << magic[0] << magic[1] << magic[2] << magic[3] << std::endl; }
if(strncmp(magic, DARX_MAGIC, DARX_MAGIC_LEN)){
return INVALID_STRUCT;
}
char magic2[4];
fread(&magic2, sizeof(char), 4, file);
bool storedAsBE = ( magic2[3] == ((uint8_t)( DARX_MAGIC_BE&0xff)));
darx.isBigEndian = storedAsBE;
if(VERBOSE){ std::cout << "# file endianness ["<<magic2[3]<<" == " << ((uint8_t)(DARX_MAGIC_BE&0xff)) << "]: " << (storedAsBE ? "big" : "little") << std::endl; }
int endianness_i = 0x00010203;
bool systemIsBE = ( ((char*)&endianness_i)[0] == 0x00);
if(VERBOSE){ std::cout << "# system endianness : " << (systemIsBE ? "big" : "little") << std::endl; }
dtinfo.swapEndian = (systemIsBE == storedAsBE);
if(dtinfo.swapEndian){
if(VERBOSE){ std::cout << "# need to swap endianness.." << std::endl; }
}
fread(&dtinfo.int_size, sizeof(uint8_t), 1, file);
fread(&dtinfo.long_size, sizeof(uint8_t), 1, file);
if(VERBOSE){ std::cout << "# data sizes [int:" << ((int)dtinfo.int_size) << ", long:" << ((long)dtinfo.long_size) << "]" << std::endl; }
fread(&darx.number_of_tensors, sizeof(uint16_t), 1, file);
if(VERBOSE){ std::cout << "# tensors : " << darx.number_of_tensors << std::endl; }
long int *tensor_indices = new long int[darx.number_of_tensors];
assert(sizeof(long int) >= dtinfo.long_size);
if(VERBOSE){ std::cout << "# tensor file locations : ["; }
for(int i=0; i < darx.number_of_tensors; i++){
fread(&(tensor_indices[i]), dtinfo.long_size, 1, file);
if(VERBOSE){ std::cout << tensor_indices[i] << " "; }
}
if(VERBOSE){ std::cout << "]" << std::endl; }
fread(&darx.metadata_size, sizeof(uint16_t), 1, file);
if(darx.metadata_size > 0){
if(VERBOSE){ std::cout << "# metadata size:" << darx.metadata_size << std::endl; }
darx.metadata = new char[darx.metadata_size];
fread(&darx.metadata, 1, darx.metadata_size, file);
} else {
if(VERBOSE){ std::cout << "# no metadata (size:0)." << std::endl; }
darx.metadata = 0;
}
if(VERBOSE){ std::cout << "# readin tensors ("<< darx.number_of_tensors <<")." << std::endl; }
darx.tensors = new datatensor[darx.number_of_tensors];
for(int tensor_idx=0; tensor_idx < darx.number_of_tensors; tensor_idx++){
fseek(file, tensor_indices[tensor_idx], SEEK_SET);
read_tensor(darx.tensors[tensor_idx], darx, file, dtinfo);
}
if(VERBOSE){ std::cout << "# darx file read successfully." << std::endl; }
delete[] tensor_indices;
return SUCCESS;
}
/** Saves a darx data archive to a file.
*
* @param[in] darx - reference to the darx structure to store
* @param[in] file - pointer to the file where to store the darx data
*
* @returns true if the darx file could be saved
*/
bool save_image_to(darx& darx, FILE* file){
if(!darx.valid){
return false;
}
// store magic number
const char* magic = DARX_MAGIC;
if(VERBOSE){ std::cout << "# image magic number : " << magic << std::endl; }
fwrite(magic, sizeof(char), DARX_MAGIC_LEN, file);
// store an endianness test in the file:
uint32_t int_magic2 = DARX_MAGIC_BE; // "LIVE" in hex
if(VERBOSE){ std::cout << "# image magic number, endianed : " << int_magic2 << std::endl; }
fwrite(&int_magic2, sizeof(uint32_t), 1, file);
// write the size of an integer (in an 8-bit int)
uint8_t int_size = sizeof(unsigned int);
if(VERBOSE){ std::cout << "# int size : " << int_size << std::endl; }
fwrite(&int_size, sizeof(uint8_t), 1, file);
// write the size of a long int (in a 8-bit int)
uint8_t long_size = sizeof(long int);
if(VERBOSE){ std::cout << "# long size : " << long_size << std::endl; }
fwrite(&long_size, sizeof(uint8_t), 1, file);
// count how many tensors we were given, and store the number
if(VERBOSE){ std::cout << "# tensors : " << darx.number_of_tensors << std::endl; }
fwrite(&darx.number_of_tensors, sizeof(uint16_t), 1, file);
// record the file offset of the archive's tensor index.
long int tensors_index_pos = ftell(file);
// leave a space in the file for the tensors index
if(VERBOSE){ std::cout << "# tensors index filepos : " << tensors_index_pos << std::endl; }
fseek(file, darx.number_of_tensors * sizeof(long int), SEEK_CUR);
// write out any metadata that may be added to the file
if(VERBOSE){ std::cout << "# metadata : " << ((void*)darx.metadata) << "(size : " << darx.metadata_size << ")" << std::endl; }
fwrite(&darx.metadata_size, sizeof(uint16_t), 1, file);
fwrite(darx.metadata, 1, darx.metadata_size, file);
// write the tensors to the file, one by one
for(int tensor_idx=0; tensor_idx < darx.number_of_tensors; tensor_idx++){
long int tensor_pos = ftell(file);
if(VERBOSE){ std::cout << "# tensor["<<tensor_idx<<"] @ file pos : " << tensor_pos << std::endl; }
fseek(file, tensors_index_pos, SEEK_SET); // get file position of tensor
fwrite(&tensor_pos, sizeof(long int), 1, file); // write it in the index
tensors_index_pos += sizeof(long int);
fseek(file, tensor_pos, SEEK_SET); // seek back to the tensor's file position
// write the tensor
int write_result = write_tensor(darx.tensors[tensor_idx], file);
if(write_result != SUCCESS){
return write_result;
}
}
return SUCCESS;
}
};