Debian Codegraph @

src/H5Z-SPERR/utilities/example-2d.c

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <math.h>
5
6 #include "hdf5.h"
7
8 #include "h5z-sperr.h"
9
10 int main (int argc, char* argv[])
11 {
12 if (argc != 6) {
13 printf("Usage: example-2d 2D_double_file dimx dimy mode quality\n");
14 exit (1);
15 }
16 const char* filename = argv[1];
17 hsize_t dims[2] = {atoi(argv[2]), atoi(argv[3])}; /* dataset dimensions */
18 hsize_t chunks[2] = {dims[0], dims[1]}; /* dataset chunks */
19 hid_t file, dataset, datatype, dataspace; /* handles */
20 herr_t status;
21
22 /*
23 * Data and output buffer initialization.
24 */
25 size_t total_vals = dims[0] * dims[1];
26 double* data = (double*)malloc(total_vals * sizeof(double));
27 FILE* fp = fopen(filename, "rb");
28 if (!fp) {
29 printf("File open error: %s\n", filename);
30 exit (1);
31 }
32 size_t nread = fread(data, sizeof(double), total_vals, fp);
33 fclose(fp);
34 fp = NULL;
35 if (nread != total_vals) {
36 printf("File read error: %s\n", filename);
37 exit (1);
38 }
39
40 /*
41 * Create a new file using H5F_ACC_TRUNC access, default file creation
42 * properties, and default file access properties.
43 */
44 const char* h5name = "sperr-example.h5";
45 file = H5Fcreate(h5name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
46
47 /*
48 * Describe the size of the array and create the data space for fixed size dataset.
49 */
50 dataspace = H5Screate_simple(2, dims, NULL);
51
52 /*
53 * Setup SPERR filter using dataset creation properties
54 */
55 hid_t prop = H5Pcreate(H5P_DATASET_CREATE);
56 H5Pset_chunk(prop, 2, chunks);
57 htri_t filter_avail = H5Zfilter_avail(H5Z_FILTER_SPERR);
58 if (filter_avail > 0)
59 printf("Filter %d available!\n", H5Z_FILTER_SPERR);
60 else
61 printf("Filter %d unavailable!\n", H5Z_FILTER_SPERR);
62 /*
63 * mode == 1 --> fixed bit-per-pixel (BPP)
64 * mode == 2 --> fixed peak signal-to-noise ratio (PSNR)
65 * mode == 3 --> fixed point-wise error (PWE)
66 */
67 int mode = atoi(argv[4]);
68 double quality = atof(argv[5]);
69 unsigned int cd_values = H5Z_SPERR_make_cd_values(mode, quality, 0);
70 status = H5Pset_filter(prop, H5Z_FILTER_SPERR, H5Z_FLAG_MANDATORY, 1, &cd_values);
71
72 /*
73 * Create a dataset WITH compression, and write to it!
74 */
75 const char* dsname = "f64_sperr";
76 dataset = H5Dcreate(file, dsname, H5T_NATIVE_DOUBLE, dataspace, H5P_DEFAULT, prop, H5P_DEFAULT);
77 status = H5Dwrite(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data);
78
79 /*
80 * Clean up writing.
81 */
82 H5Dclose(dataset);
83 H5Pclose(prop);
84 H5Sclose(dataspace);
85 H5Fclose(file);
86
87 /*
88 * Open the file
89 */
90 file = H5Fopen(h5name, H5F_ACC_RDONLY, H5P_DEFAULT);
91 dataset = H5Dopen(file, dsname, H5P_DEFAULT);
92 dataspace = H5Dget_space(dataset);
93 hssize_t npoints = H5Sget_simple_extent_npoints(dataspace);
94
95 /*
96 * Read the compressed array
97 */
98 dataset = H5Dopen(file, dsname, H5P_DEFAULT);
99 double* comp = (double*)malloc(npoints * sizeof(double));
100 H5Dread(dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, comp);
101
102 /* Find the maximum difference */
103 double max = 0.0;
104 for (size_t i = 0; i < npoints; i++)
105 if (fabs(comp[i] - data[i]) > max)
106 max = fabs(comp[i] - data[i]);
107
108 printf("max diff = %g\n", max);
109
110 /* clean up reading */
111 H5Dclose(dataset);
112 H5Sclose(dataspace);
113 H5Fclose(file);
114 free(data);
115 free(comp);
116
117 return 0;
118 }