Debian Codegraph @

src/SPERR/utilities/qccpack/example_qcc_dwt3d.c

raw
1
2 #include <assert.h>
3 #include <stdlib.h>
4 #include "libQccPack.h"
5
6 int main(int argc, char** argv)
7 {
8 if (argc != 6) {
9 printf("Usage: ./a.out input_filename dim_x dim_y dim_z output_filename\n");
10 return 1;
11 }
12
13 const char* input_name = argv[1];
14 const size_t num_of_cols = atoi(argv[2]);
15 const size_t num_of_rows = atoi(argv[3]);
16 const size_t num_of_frms = atoi(argv[4]);
17 const char* output_name = argv[5];
18 const size_t num_of_vals = num_of_cols * num_of_rows * num_of_frms;
19
20 /* allocate memory for a 1D array */
21 float* buf = (float*)malloc(num_of_vals * sizeof(float));
22
23 FILE* f = fopen(input_name, "r");
24 size_t rnt = fread(buf, sizeof(float), num_of_vals, f);
25 assert(rnt == num_of_vals);
26 fclose(f);
27
28 /* allocate data structure to take in this 1D array */
29 QccWAVSubbandPyramid3D coeff3d;
30 QccWAVSubbandPyramid3DInitialize(&coeff3d);
31 coeff3d.num_cols = num_of_cols;
32 coeff3d.num_rows = num_of_rows;
33 coeff3d.num_frames = num_of_frms;
34 QccWAVSubbandPyramid3DAlloc(&coeff3d);
35 size_t counter = 0;
36 for (size_t frame = 0; frame < num_of_frms; frame++)
37 for (size_t row = 0; row < num_of_rows; row++)
38 for (size_t col = 0; col < num_of_cols; col++)
39 coeff3d.volume[frame][row][col] = buf[counter++];
40
41 /* create a wavelet */
42 QccString WaveletFilename = "CohenDaubechiesFeauveau.9-7.lft";
43 QccString Boundary = "symmetric";
44 QccWAVWavelet cdf97;
45 if (QccWAVWaveletInitialize(&cdf97)) {
46 QccErrorAddMessage("%s: Error calling QccWAVWaveletInitialize()", argv[0]);
47 QccErrorExit();
48 }
49 if (QccWAVWaveletCreate(&cdf97, WaveletFilename, Boundary)) {
50 QccErrorAddMessage("%s: Error calling QccWAVWaveletCreate()", argv[0]);
51 QccErrorExit();
52 }
53
54 /* decide how many levels of transforms to perform */
55 float min_xyz = (float)num_of_cols;
56 if (num_of_rows < min_xyz)
57 min_xyz = (float)num_of_rows;
58 if (num_of_frms < min_xyz)
59 min_xyz = (float)num_of_frms;
60 float tmpf = log2f(min_xyz / 8.0f);
61 int num_levels = tmpf < 0.0f ? 0 : (int)tmpf + 1;
62
63 /* forward transform */
64 QccWAVSubbandPyramid3DDWT(&coeff3d, QCCWAVSUBBANDPYRAMID3D_PACKET, num_levels, num_levels,
65 &cdf97);
66
67 /* inverse transform */
68 QccWAVSubbandPyramid3DInverseDWT(&coeff3d, &cdf97);
69
70 /* copy result to an output buffer */
71 float* out_buf = (float*)malloc(num_of_vals * sizeof(float));
72 counter = 0;
73 for (size_t frame = 0; frame < num_of_frms; frame++)
74 for (size_t row = 0; row < num_of_rows; row++)
75 for (size_t col = 0; col < num_of_cols; col++)
76 out_buf[counter++] = coeff3d.volume[frame][row][col];
77
78 /* write result to qccpack_result.raw */
79 f = fopen(output_name, "w");
80 fwrite(out_buf, sizeof(float), num_of_vals, f);
81 fclose(f);
82
83 /* free memory */
84 free(out_buf);
85 free(buf);
86 QccWAVSubbandPyramid3DFree(&coeff3d);
87 }