42 #include <visp3/core/vpConfig.h>
51 #include <visp3/core/vpColor.h>
58 #if VISP_CXX_STANDARD > VISP_CXX_STANDARD_98
62 #ifndef DOXYGEN_SHOULD_SKIP_THIS
64 static inline unsigned long vp_mz_crc32(
unsigned long crc,
const unsigned char *ptr,
size_t buf_len)
66 static const unsigned int s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
67 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
68 unsigned int crcu32 =
static_cast<unsigned int>(crc);
72 unsigned char b = *ptr++;
73 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)];
74 crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)];
80 #ifdef VISP_HAVE_MINIZ
88 NpyArray(
const std::vector<size_t> &_shape,
size_t _word_size,
bool _fortran_order) :
102 return reinterpret_cast<T *
>(&(*data_holder)[0]);
108 return reinterpret_cast<T *
>(&(*data_holder)[0]);
114 const T *p = data<T>();
115 return std::vector<T>(p, p+
num_vals);
130 using npz_t = std::map<std::string, NpyArray>;
133 VISP_EXPORT
char map_type(
const std::type_info &t);
134 template<
typename T> std::vector<char>
create_npy_header(
const std::vector<size_t> &shape);
135 VISP_EXPORT
void parse_npy_header(FILE *fp,
size_t &word_size, std::vector<size_t> &shape,
bool &fortran_order);
136 VISP_EXPORT
void parse_npy_header(
unsigned char *buffer,
size_t &word_size, std::vector<size_t> &shape,
bool &fortran_order);
137 VISP_EXPORT
void parse_zip_footer(FILE *fp, uint16_t &nrecs,
size_t &global_header_size,
size_t &global_header_offset);
141 template<
typename T> std::vector<char> &
operator+=(std::vector<char> &lhs,
const T rhs)
144 for (
size_t byte = 0;
byte <
sizeof(T); ++byte) {
145 char val = *((
char *)&rhs+
byte);
151 template<>
inline std::vector<char> &
operator+=(std::vector<char> &lhs,
const std::string rhs)
153 lhs.insert(lhs.end(), rhs.begin(), rhs.end());
157 template<>
inline std::vector<char> &
operator+=(std::vector<char> &lhs,
const char *rhs)
160 size_t len = strlen(rhs);
162 for (
size_t byte = 0;
byte < len; ++byte) {
163 lhs.push_back(rhs[
byte]);
178 template<
typename T>
void npy_save(std::string fname,
const T *data,
const std::vector<size_t> shape, std::string mode =
"w")
181 std::vector<size_t> true_data_shape;
183 if (mode ==
"a") fp = fopen(fname.c_str(),
"r+b");
190 assert(!fortran_order);
192 if (word_size !=
sizeof(T)) {
193 std::cout<<
"libnpy error: "<<fname<<
" has word size "<<word_size<<
" but npy_save appending data sized "<<
sizeof(T)<<
"\n";
194 assert(word_size ==
sizeof(T));
196 if (true_data_shape.size() != shape.size()) {
197 std::cout<<
"libnpy error: npy_save attempting to append misdimensioned data to "<<fname<<
"\n";
198 assert(true_data_shape.size() != shape.size());
201 for (
size_t i = 1; i < shape.size(); ++i) {
202 if (shape[i] != true_data_shape[i]) {
203 std::cout<<
"libnpy error: npy_save attempting to append misshaped data to "<<fname<<
"\n";
204 assert(shape[i] == true_data_shape[i]);
207 true_data_shape[0] += shape[0];
210 fp = fopen(fname.c_str(),
"wb");
211 true_data_shape = shape;
214 std::vector<char> header = create_npy_header<T>(true_data_shape);
215 size_t nels = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<size_t>());
217 fseek(fp, 0, SEEK_SET);
218 fwrite(&header[0],
sizeof(
char), header.size(), fp);
219 fseek(fp, 0, SEEK_END);
220 fwrite(data,
sizeof(T), nels, fp);
235 template<
typename T>
void npz_save(std::string zipname, std::string fname,
const T *data,
const std::vector<size_t> &shape, std::string mode =
"w")
243 size_t global_header_offset = 0;
244 std::vector<char> global_header;
246 if (mode ==
"a") fp = fopen(zipname.c_str(),
"r+b");
253 size_t global_header_size;
255 fseek(fp,
static_cast<long>(global_header_offset), SEEK_SET);
256 global_header.resize(global_header_size);
257 size_t res = fread(&global_header[0],
sizeof(
char), global_header_size, fp);
258 if (res != global_header_size) {
259 throw std::runtime_error(
"npz_save: header read error while adding to existing zip");
261 fseek(fp,
static_cast<long>(global_header_offset), SEEK_SET);
264 fp = fopen(zipname.c_str(),
"wb");
267 std::vector<char> npy_header = create_npy_header<T>(shape);
269 size_t nels = std::accumulate(shape.begin(), shape.end(), 1, std::multiplies<size_t>());
270 size_t nbytes = nels*
sizeof(T) + npy_header.size();
273 uint32_t crc = vp_mz_crc32(0L, (uint8_t *)&npy_header[0], npy_header.size());
274 crc = vp_mz_crc32(crc, (uint8_t *)data, nels*
sizeof(T));
277 std::vector<char> local_header;
278 local_header +=
"PK";
279 local_header +=
static_cast<uint16_t
>(0x0403);
280 local_header +=
static_cast<uint16_t
>(20);
281 local_header +=
static_cast<uint16_t
>(0);
282 local_header +=
static_cast<uint16_t
>(0);
283 local_header +=
static_cast<uint16_t
>(0);
284 local_header +=
static_cast<uint16_t
>(0);
285 local_header +=
static_cast<uint32_t
>(crc);
286 local_header +=
static_cast<uint32_t
>(nbytes);
287 local_header +=
static_cast<uint32_t
>(nbytes);
288 local_header +=
static_cast<uint16_t
>(fname.size());
289 local_header +=
static_cast<uint16_t
>(0);
290 local_header += fname;
293 global_header +=
"PK";
294 global_header +=
static_cast<uint16_t
>(0x0201);
295 global_header +=
static_cast<uint16_t
>(20);
296 global_header.insert(global_header.end(), local_header.begin()+4, local_header.begin()+30);
297 global_header +=
static_cast<uint16_t
>(0);
298 global_header +=
static_cast<uint16_t
>(0);
299 global_header +=
static_cast<uint16_t
>(0);
300 global_header +=
static_cast<uint32_t
>(0);
301 global_header +=
static_cast<uint32_t
>(global_header_offset);
302 global_header += fname;
305 std::vector<char> footer;
307 footer +=
static_cast<uint16_t
>(0x0605);
308 footer +=
static_cast<uint16_t
>(0);
309 footer +=
static_cast<uint16_t
>(0);
310 footer +=
static_cast<uint16_t
>(nrecs+1);
311 footer +=
static_cast<uint16_t
>(nrecs+1);
312 footer +=
static_cast<uint32_t
>(global_header.size());
313 footer +=
static_cast<uint32_t
>(global_header_offset + nbytes + local_header.size());
314 footer +=
static_cast<uint16_t
>(0);
317 fwrite(&local_header[0],
sizeof(
char), local_header.size(), fp);
318 fwrite(&npy_header[0],
sizeof(
char), npy_header.size(), fp);
319 fwrite(data,
sizeof(T), nels, fp);
320 fwrite(&global_header[0],
sizeof(
char), global_header.size(), fp);
321 fwrite(&footer[0],
sizeof(
char), footer.size(), fp);
334 template<
typename T>
void npy_save(std::string fname,
const std::vector<T> data, std::string mode =
"w")
336 std::vector<size_t> shape;
337 shape.push_back(data.size());
338 npy_save(fname, &data[0], shape, mode);
351 template<
typename T>
void npz_save(std::string zipname, std::string fname,
const std::vector<T> data, std::string mode =
"w")
353 std::vector<size_t> shape;
354 shape.push_back(data.size());
355 npz_save(zipname, fname, &data[0], shape, mode);
360 std::vector<char> dict;
361 dict +=
"{'descr': '";
364 dict += std::to_string(
sizeof(T));
365 dict +=
"', 'fortran_order': False, 'shape': (";
366 dict += std::to_string(shape[0]);
367 for (
size_t i = 1; i < shape.size(); ++i) {
369 dict += std::to_string(shape[i]);
371 if (shape.size() == 1) dict +=
",";
374 int remainder = 16 - (10 + dict.size()) % 16;
375 dict.insert(dict.end(), remainder,
' ');
378 std::vector<char> header;
379 header +=
static_cast<char>(0x93);
381 header +=
static_cast<char>(0x01);
382 header +=
static_cast<char>(0x00);
383 header +=
static_cast<uint16_t
>(dict.size());
384 header.insert(header.end(), dict.begin(), dict.end());
506 static const std::string &getBuildInformation();
507 static std::string getTempPath();
508 static void getUserName(std::string &username);
509 static std::string getUserName();
510 static std::string getenv(
const std::string &env);
511 static std::string getViSPImagesDataPath();
512 static void getVersion(
const std::string &version,
unsigned int &major,
unsigned int &minor,
unsigned int &patch);
513 static bool checkDirectory(
const std::string &dirname);
514 static bool checkFifo(
const std::string &filename);
515 static bool checkFilename(
const std::string &filename);
516 static bool copy(
const std::string &src,
const std::string &dst);
518 static void makeDirectory(
const std::string &dirname);
519 static void makeFifo(
const std::string &dirname);
520 static std::string makeTempDirectory(
const std::string &dirname);
521 static std::string path(
const std::string &pathname);
523 static bool remove(
const std::string &filename);
524 static bool rename(
const std::string &oldfilename,
const std::string &newfilename);
532 static std::string toUpperCase(
const std::string &input);
533 static std::string toLowerCase(
const std::string &input);
534 static std::string getAbsolutePathname(
const std::string &pathname);
535 static std::string getFileExtension(
const std::string &pathname,
bool checkFile =
false);
536 static long getIndex(
const std::string &filename,
const std::string &format);
537 static std::string getName(
const std::string &pathname);
538 static std::string getNameWE(
const std::string &pathname);
539 static std::string getParent(
const std::string &pathname);
540 static std::string createFilePath(
const std::string &parent,
const std::string &child);
541 static bool isAbsolutePathname(
const std::string &pathname);
542 static bool isSamePathname(
const std::string &pathname1,
const std::string &pathname2);
543 static std::pair<std::string, std::string> splitDrive(
const std::string &pathname);
544 static std::vector<std::string> splitChain(
const std::string &chain,
const std::string &sep);
545 static std::vector<std::string> getDirFiles(
const std::string &dirname);
552 static bool loadConfigFile(
const std::string &confFile);
553 static bool readConfigVar(
const std::string &var,
float &value);
554 static bool readConfigVar(
const std::string &var,
double &value);
555 static bool readConfigVar(
const std::string &var,
int &value);
556 static bool readConfigVar(
const std::string &var,
unsigned int &value);
557 static bool readConfigVar(
const std::string &var,
bool &value);
558 static bool readConfigVar(
const std::string &var, std::string &value);
559 static bool readConfigVar(
const std::string &var,
vpColor &value);
560 static bool readConfigVar(
const std::string &var,
vpArray2D<double> &value,
const unsigned int &nCols = 0,
561 const unsigned int &nRows = 0);
564 static void setBaseName(
const std::string &s);
565 static void setBaseDir(
const std::string &dir);
566 static void addNameElement(
const std::string &strTrue,
const bool &cond =
true,
const std::string &strFalse =
"");
567 static void addNameElement(
const std::string &strTrue,
const double &val);
568 static std::string getBaseName();
569 static std::string getFullName();
572 static void saveConfigFile(
const bool &actuallySave =
true);
573 static void createBaseNamePath(
const bool &empty =
false);
576 static void readBinaryValueLE(std::ifstream &file, int16_t &short_value);
577 static void readBinaryValueLE(std::ifstream &file, uint16_t &ushort_value);
578 static void readBinaryValueLE(std::ifstream &file, int32_t &int_value);
579 static void readBinaryValueLE(std::ifstream &file, uint32_t &int_value);
580 static void readBinaryValueLE(std::ifstream &file,
float &float_value);
581 static void readBinaryValueLE(std::ifstream &file,
double &double_value);
583 static void writeBinaryValueLE(std::ofstream &file,
const int16_t short_value);
584 static void writeBinaryValueLE(std::ofstream &file,
const uint16_t ushort_value);
585 static void writeBinaryValueLE(std::ofstream &file,
const int32_t int_value);
586 static void writeBinaryValueLE(std::ofstream &file,
const uint32_t int_value);
587 static void writeBinaryValueLE(std::ofstream &file,
float float_value);
588 static void writeBinaryValueLE(std::ofstream &file,
double double_value);
590 static bool parseBoolean(std::string input);
591 static std::string trim(std::string s);
600 #ifndef DOXYGEN_SHOULD_SKIP_THIS
601 static int mkdir_p(
const std::string &path,
int mode);
Class to define RGB colors available for display functionalities.
VISP_EXPORT char map_type(const std::type_info &t)
std::vector< char > create_npy_header(const std::vector< size_t > &shape)
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")
VISP_EXPORT npz_t npz_load(std::string fname)
VISP_EXPORT void parse_zip_footer(FILE *fp, uint16_t &nrecs, size_t &global_header_size, size_t &global_header_offset)
void npy_save(std::string fname, const T *data, const std::vector< size_t > shape, std::string mode="w")
VISP_EXPORT void parse_npy_header(FILE *fp, size_t &word_size, std::vector< size_t > &shape, bool &fortran_order)
std::vector< char > & operator+=(std::vector< char > &lhs, const T rhs)
std::map< std::string, NpyArray > npz_t
VISP_EXPORT NpyArray npy_load(std::string fname)
VISP_EXPORT char BigEndianTest()
std::shared_ptr< std::vector< char > > data_holder
std::vector< size_t > shape
std::vector< T > as_vec() const
NpyArray(const std::vector< size_t > &_shape, size_t _word_size, bool _fortran_order)