Debian Codegraph @

src/SPERR/examples/C_API/2d.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 < 6) {
32 printf("Usage: ./a.out filename dimx dimy 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 int mode = (int)atoi(argv[4]);
41 const double quality = atof(argv[5]);
42 int is_float = 1;
43 if (argc == 7)
44 is_float = 0;
45
46 /* Read in a file and put its content in `inbuf` */
47 void* inbuf = NULL; /* Will be free'd later */
48 size_t inlen = read_file(filename, &inbuf);
49 if (inlen == 0)
50 return 1;
51 if (is_float && inlen != sizeof(float) * dimx * dimy) {
52 free(inbuf);
53 return 1;
54 }
55 if (!is_float && inlen != sizeof(double) * dimx * dimy) {
56 free(inbuf);
57 return 1;
58 }
59
60 /* Compress `inbuf` and put the compressed bitstream in `bitstream` *
61 * Note that we specify to include a header in the output bitstream. */
62 void* bitstream = NULL; /* Will be free'd later */
63 size_t stream_len = 0;
64 int rtn = sperr_comp_2d(inbuf, is_float, dimx, dimy, mode, quality, 1, &bitstream, &stream_len);
65 if (rtn != 0) {
66 printf("Compression error with code %d\n", rtn);
67 return rtn;
68 }
69
70 /* Write the compressed bitstream to disk */
71 FILE* f = fopen("./output.stream", "w");
72 assert(f != NULL);
73 fwrite(bitstream, 1, stream_len, f);
74 fclose(f);
75 f = NULL;
76
77 /* Extract header information from the bitstream. */
78 size_t out_dimx = 0;
79 size_t out_dimy = 0;
80 size_t out_dimz = 0;
81 int out_is_float = 0;
82 sperr_parse_header(bitstream, &out_dimx, &out_dimy, &out_dimz, &out_is_float);
83 if (out_dimx != dimx || out_dimy != dimy || out_dimz != 1 || out_is_float != is_float ) {
84 printf("Parse header wrong!\n");
85 return 1;
86 }
87
88 /* Decompress `bitstream` and put the decompressed slice in `outbuf` */
89 /* Note that here we need to strip off the 10-byte header before passing the buffer *
90 * to sperr_decomp_2d(). */
91 const size_t header_len = 10;
92 const uint8_t* start = (const uint8_t*)(bitstream) + header_len;
93 void* outbuf = NULL; /* Will be free'd later */
94 rtn = sperr_decomp_2d(start, stream_len - header_len, is_float, out_dimx, out_dimy, &outbuf);
95 if (rtn != 0) {
96 printf("Decompression error with code %d\n", rtn);
97 return 1;
98 }
99
100 /* Write the decompressed buffer to disk */
101 f = fopen("./output.data", "w");
102 assert(f != NULL);
103 if (is_float)
104 fwrite(outbuf, sizeof(float), out_dimx * out_dimy, f);
105 else
106 fwrite(outbuf, sizeof(double), out_dimx * out_dimy, f);
107 fclose(f);
108
109 /* Clean up */
110 free(outbuf);
111 free(bitstream);
112 free(inbuf);
113 }