| 213 | } |
| 214 | |
| 215 | cnpy::NpyArray load_the_npz_array(FILE* fp, uint32_t compr_bytes, uint32_t uncompr_bytes) |
| 216 | { |
| 217 | std::vector<unsigned char> buffer_compr(compr_bytes); |
| 218 | std::vector<unsigned char> buffer_uncompr(uncompr_bytes); |
| 219 | size_t nread = fread(&buffer_compr[0], 1, compr_bytes, fp); |
| 220 | if (nread != compr_bytes) |
| 221 | throw std::runtime_error("load_the_npy_file: failed fread"); |
| 222 | |
| 223 | z_stream d_stream; |
| 224 | |
| 225 | d_stream.zalloc = Z_NULL; |
| 226 | d_stream.zfree = Z_NULL; |
| 227 | d_stream.opaque = Z_NULL; |
| 228 | d_stream.avail_in = 0; |
| 229 | d_stream.next_in = Z_NULL; |
| 230 | inflateInit2(&d_stream, -MAX_WBITS); |
| 231 | |
| 232 | d_stream.avail_in = compr_bytes; |
| 233 | d_stream.next_in = &buffer_compr[0]; |
| 234 | d_stream.avail_out = uncompr_bytes; |
| 235 | d_stream.next_out = &buffer_uncompr[0]; |
| 236 | |
| 237 | inflate(&d_stream, Z_FINISH); |
| 238 | inflateEnd(&d_stream); |
| 239 | |
| 240 | std::vector<size_t> shape; |
| 241 | size_t word_size; |
| 242 | bool fortran_order; |
| 243 | cnpy::parse_npy_header(&buffer_uncompr[0], word_size, shape, fortran_order); |
| 244 | |
| 245 | cnpy::NpyArray array(shape, word_size, fortran_order); |
| 246 | |
| 247 | size_t offset = uncompr_bytes - array.num_bytes(); |
| 248 | memcpy(array.data<unsigned char>(), &buffer_uncompr[0] + offset, array.num_bytes()); |
| 249 | |
| 250 | return array; |
| 251 | } |
| 252 | |
| 253 | cnpy::npz_t cnpy::npz_load(std::string fname) |
| 254 | { |