Debian Codegraph @

src/SPERR/examples/C_API/3d.c

raw
1 #include "SPERR_C_API.h"
2
3 #include <assert.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 /*
8 * Given a file name, this function reads in its content and allocates a buffer `dst` to store it.
9 * Upon success, it returns the size of the allocated buffer `dst`. Otherwise, it returns 0.
10 * The caller is responsible of free'ing buffer `dst` using free().
11 */
12 size_t read_file(const char* filename, void** dst)
13 {
14 FILE* f = fopen(filename, "r");
15 if (!f)
16 return 0;
17 fseek(f, 0, SEEK_END);
18 const size_t len = ftell(f);
19 fseek(f, 0, SEEK_SET);
20 uint8_t* buf = malloc(len);
21 int rtn = fread(buf, 1, len, f);
22 assert(rtn == len);
23 fclose(f);
24 *dst = buf;
25
26 return len;
27 }
28
29 int main(int argc, char** argv)
30 {
31 if (argc < 7) {
32 printf("Usage: ./a.out filename dimx dimy dimz mode quality [-d]\n");
33 printf(" Note: -d is optional to indicate that the input is in double format\n");
34 return 1;
35 }
36
37 const char* filename = argv[1];
38 const size_t dimx = (size_t)atol(argv[2]);
39 const size_t dimy = (size_t)atol(argv[3]);
40 const size_t dimz = (size_t)atol(argv[4]);
41 const int mode = (int)atoi(argv[5]);
42 const double quality = atof(argv[6]);
43 int is_float = 1;
44 if (argc == 8)
45 is_float = 0;
46
47 /* Hard code the preferred chunk size in this example. */
48 const size_t chunk_x = 256;
49 const size_t chunk_y = 256;
50 const size_t chunk_z = 256;
51
52 /* Read in a file and put its content in `inbuf` */
53 void* inbuf = NULL; /* Will be free'd later */
54 size_t inlen = read_file(filename, &inbuf);
55 if (inlen == 0)
56 return 1;
57 if (is_float && inlen != sizeof(float) * dimx * dimy * dimz) {
58 free(inbuf);
59 return 1;
60 }
61 if (!is_float && inlen != sizeof(double) * dimx * dimy * dimz) {
62 free(inbuf);
63 return 1;
64 }
65
66 /* Let's use all the OpenMP threads, or in case that SPERR isn't compiled with
67 OpenMP, then this value doesn't do anything. */
68 const int nthreads = 0;
69
70 /* Compress `inbuf` and put the compressed bitstream in `bitstream` */
71 void* bitstream = NULL; /* Will be free'd later */
72 size_t stream_len = 0;
73 int rtn = sperr_comp_3d(inbuf, is_float, dimx, dimy, dimz, chunk_x, chunk_y, chunk_z, mode,
74 quality, nthreads, &bitstream, &stream_len);
75 if (rtn != 0) {
76 printf("Compression error with code %d\n", rtn);
77 return rtn;
78 }
79
80 /* Write the compressed bitstream to disk */
81 FILE* f = fopen("./output.stream", "w");
82 assert(f != NULL);
83 fwrite(bitstream, 1, stream_len, f);
84 fclose(f);
85 f = NULL;
86
87 /* Also test the sperr_parse_header() function */
88 size_t out_dimx = 0;
89 size_t out_dimy = 0;
90 size_t out_dimz = 0;
91 int out_is_float = 0;
92 sperr_parse_header(bitstream, &out_dimx, &out_dimy, &out_dimz, &out_is_float);
93 if (out_dimx != dimx || out_dimy != dimy || out_dimz != dimz || out_is_float != is_float ) {
94 printf("Parse header wrong!\n");
95 return 1;
96 }
97
98 /* Decompress `bitstream` and put the decompressed volume in `outbuf` */
99 void* outbuf = NULL; /* Will be free'd later */
100 rtn = sperr_decomp_3d(bitstream, stream_len, is_float, nthreads, &out_dimx, &out_dimy, &out_dimz,
101 &outbuf);
102 if (rtn != 0) {
103 printf("Decompression error with code %d\n", rtn);
104 return rtn;
105 }
106
107 /* Write the decompressed buffer to disk */
108 f = fopen("./output.data", "w");
109 assert(f != NULL);
110 if (is_float)
111 fwrite(outbuf, sizeof(float), out_dimx * out_dimy * out_dimz, f);
112 else
113 fwrite(outbuf, sizeof(double), out_dimx * out_dimy * out_dimz, f);
114 fclose(f);
115 f = NULL;
116
117 /* Clean up */
118 free(outbuf);
119 free(bitstream);
120 free(inbuf);
121 }