| 1 |
/* |
| 2 |
Copyright (C) 2009 Andrew Collette |
| 3 |
http://h5py.alfven.org |
| 4 |
License: BSD (see LICENSE.txt) |
| 5 |
|
| 6 |
Example program demonstrating use of the LZF filter from C code. |
| 7 |
|
| 8 |
To compile this program: |
| 9 |
|
| 10 |
h5cc -DH5_USE_16_API lzf/*.c lzf_filter.c example.c -o example |
| 11 |
|
| 12 |
To run: |
| 13 |
|
| 14 |
$ ./example |
| 15 |
Success! |
| 16 |
$ h5ls -v test_lzf.hdf5 |
| 17 |
Opened "test_lzf.hdf5" with sec2 driver. |
| 18 |
dset Dataset {100/100, 100/100, 100/100} |
| 19 |
Location: 0:1:0:976 |
| 20 |
Links: 1 |
| 21 |
Modified: 2009-02-15 16:35:11 PST |
| 22 |
Chunks: {1, 100, 100} 40000 bytes |
| 23 |
Storage: 4000000 logical bytes, 174288 allocated bytes, 2295.05% utilization |
| 24 |
Filter-0: shuffle-2 OPT {4} |
| 25 |
Filter-1: lzf-32000 OPT {1, 261, 40000} |
| 26 |
Type: native float |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include "hdf5.h" |
| 31 |
#include "lzf_filter.h" |
| 32 |
|
| 33 |
#define SIZE 100*100*100 |
| 34 |
#define SHAPE {100,100,100} |
| 35 |
#define CHUNKSHAPE {1,100,100} |
| 36 |
|
| 37 |
int main(){ |
| 38 |
|
| 39 |
static float data[SIZE]; |
| 40 |
static float data_out[SIZE]; |
| 41 |
const hsize_t shape[] = SHAPE; |
| 42 |
const hsize_t chunkshape[] = CHUNKSHAPE; |
| 43 |
int r, i; |
| 44 |
int return_code = 1; |
| 45 |
|
| 46 |
hid_t fid, sid, dset, plist = 0; |
| 47 |
|
| 48 |
for(i=0; i<SIZE; i++){ |
| 49 |
data[i] = i; |
| 50 |
} |
| 51 |
|
| 52 |
/* Register the filter with the library */ |
| 53 |
r = register_lzf(); |
| 54 |
if(r<0) goto failed; |
| 55 |
|
| 56 |
sid = H5Screate_simple(3, shape, NULL); |
| 57 |
if(sid<0) goto failed; |
| 58 |
|
| 59 |
fid = H5Fcreate("test_lzf.hdf5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); |
| 60 |
if(fid<0) goto failed; |
| 61 |
|
| 62 |
plist = H5Pcreate(H5P_DATASET_CREATE); |
| 63 |
if(plist<0) goto failed; |
| 64 |
|
| 65 |
/* Chunked layout required for filters */ |
| 66 |
r = H5Pset_chunk(plist, 3, chunkshape); |
| 67 |
if(r<0) goto failed; |
| 68 |
|
| 69 |
/* Use of the shuffle filter VASTLY improves performance of this |
| 70 |
and other block-oriented compression filters. Be sure to add |
| 71 |
this before the compression filter! |
| 72 |
*/ |
| 73 |
r = H5Pset_shuffle(plist); |
| 74 |
if(r<0) goto failed; |
| 75 |
|
| 76 |
/* Note the "optional" flag is necessary, as with the DEFLATE filter */ |
| 77 |
r = H5Pset_filter(plist, H5PY_FILTER_LZF, H5Z_FLAG_OPTIONAL, 0, NULL); |
| 78 |
if(r<0) goto failed; |
| 79 |
|
| 80 |
dset = H5Dcreate(fid, "dset", H5T_NATIVE_FLOAT, sid, plist); |
| 81 |
if(dset<0) goto failed; |
| 82 |
|
| 83 |
r = H5Dwrite(dset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &data); |
| 84 |
if(r<0) goto failed; |
| 85 |
|
| 86 |
r = H5Dread(dset, H5T_NATIVE_FLOAT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &data_out); |
| 87 |
if(r<0) goto failed; |
| 88 |
|
| 89 |
for(i=0;i<SIZE;i++){ |
| 90 |
if(data[i] != data_out[i]) goto failed; |
| 91 |
} |
| 92 |
|
| 93 |
fprintf(stdout, "Success!\n"); |
| 94 |
|
| 95 |
return_code = 0; |
| 96 |
|
| 97 |
failed: |
| 98 |
|
| 99 |
if(dset>0) H5Dclose(dset); |
| 100 |
if(sid>0) H5Sclose(sid); |
| 101 |
if(plist>0) H5Pclose(plist); |
| 102 |
if(fid>0) H5Fclose(fid); |
| 103 |
|
| 104 |
return return_code; |
| 105 |
} |
| 106 |
|