Debian Codegraph @

src/SPERR/utilities/qccpack/example_qcc_speck3d.c

raw
1 /*
2 * This program reads a 3D binary file, utilizes SPECK encoder
3 * to compress to a target bitrate, and then reports the compression errors.
4 */
5
6 #define HAVE_SPECK
7
8 #include "libQccPack.h"
9 #include "sam_helper.h"
10 #include "assert.h"
11
12 #include "SpeckConfig.h"
13
14 #include <sys/time.h>
15
16
17 void array_to_image_cube( const float* array, QccIMGImageCube* imagecube )
18 {
19 float min = array[0];
20 float max = array[0];
21 size_t idx = 0, frame, row, col;
22 for( frame = 0; frame < imagecube -> num_frames; frame++ )
23 for( row = 0; row < imagecube -> num_rows; row++ )
24 for( col = 0; col < imagecube -> num_cols; col++ )
25 {
26 if( array[idx] < min ) min = array[idx];
27 if( array[idx] > max ) max = array[idx];
28 imagecube -> volume[frame][row][col] = array[idx];
29 idx++;
30 }
31 imagecube -> min_val = min;
32 imagecube -> max_val = max;
33 }
34
35
36 void image_cube_to_array( const QccIMGImageCube* imagecube, float* array )
37 {
38 size_t idx = 0, frame, row, col;
39 for( frame = 0; frame < imagecube -> num_frames; frame++ )
40 for( row = 0; row < imagecube -> num_rows; row++ )
41 for( col = 0; col < imagecube -> num_cols; col++ )
42 {
43 array[idx++] = imagecube -> volume[frame][row][col];
44 }
45 }
46
47
48 int calc_num_of_xforms( int len )
49 {
50 assert( len > 0 );
51 // I decide 8 is the minimal length to do one level of xform.
52 float ratio = (float)len / 8.0f;
53 float f = logf(ratio) / logf(2.0f);
54 int num_of_xforms = f < 0.0f ? 0 : (int)f + 1;
55
56 return num_of_xforms;
57 }
58
59
60 int main( int argc, char** argv )
61 {
62 if( argc != 6 )
63 {
64 fprintf( stderr, "Usage: ./a.out input_file dim_x dim_y dim_z cratio\n" );
65 return 1;
66 }
67 const char* input_name = argv[1];
68 int num_of_cols = atoi( argv[2] );
69 int num_of_rows = atoi( argv[3] );
70 int num_of_frames = atoi( argv[4] );
71 double cratio = atof( argv[5] );
72 const size_t num_of_vals = num_of_cols * num_of_rows * num_of_frames;
73 const size_t num_of_bytes = sizeof(float) * num_of_vals;
74
75 const char* output_name = "qcc.tmp";
76 const int header_size = 29;
77 const int total_bits = (int)(8.0f * num_of_bytes / cratio) + header_size * 8;
78
79 /*
80 * Stage 1: Encoding
81 */
82
83 /* Prepare wavelet */
84 QccString waveletFilename = QCCWAVWAVELET_DEFAULT_WAVELET;
85 QccString boundary = "symmetric";
86 QccWAVWavelet wavelet;
87 if( QccWAVWaveletInitialize(&wavelet) )
88 {
89 fprintf( stderr, "QccWAVWaveletInitialize failed.\n" );
90 return 1;
91 }
92 if (QccWAVWaveletCreate( &wavelet, waveletFilename, boundary ))
93 {
94 fprintf( stderr, "Error calling QccWAVWaveletCreate()\n" );
95 return 1;
96 }
97
98 /* Prepare output bit buffer */
99 QccBitBuffer output_buffer;
100 if( QccBitBufferInitialize(&output_buffer) )
101 {
102 fprintf( stderr, "QccBitBufferInitialize() failed\n" ); return 1;
103 }
104 QccStringCopy( output_buffer.filename, output_name );
105 output_buffer.type = QCCBITBUFFER_OUTPUT;
106 if (QccBitBufferStart(&output_buffer))
107 {
108 fprintf( stderr, "Error calling QccBitBufferStart()\n");
109 return 1;
110 }
111
112 /* Prepare image cube */
113 float* in_array = (float*)malloc( num_of_bytes );
114 if( sam_read_n_bytes( input_name, num_of_bytes, in_array ) )
115 {
116 fprintf( stderr, "file read error: %s\n", input_name );
117 return 1;
118 }
119 QccIMGImageCube imagecube;
120 if( QccIMGImageCubeInitialize( &imagecube ) )
121 {
122 fprintf( stderr, "QccIMGImageCubeInitialize() failed\n");
123 return 1;
124 }
125 imagecube.num_cols = num_of_cols;
126 imagecube.num_rows = num_of_rows;
127 imagecube.num_frames = num_of_frames;
128 if( QccIMGImageCubeAlloc( &imagecube ) )
129 {
130 fprintf( stderr, "QccIMGImageCubeAlloc() failed\n");
131 return 1;
132 }
133 array_to_image_cube( in_array, &imagecube );
134
135 /* Get ready for SPECK! */
136 int transform_type = QCCWAVSUBBANDPYRAMID3D_PACKET;
137 int num_of_levels_xy = calc_num_of_xforms( num_of_cols );
138 int num_of_levels_z = calc_num_of_xforms( num_of_frames );
139 struct timeval start, end;
140 gettimeofday( &start, NULL );
141 if( QccSPECK3DEncode( &imagecube, NULL, transform_type,
142 num_of_levels_z, num_of_levels_xy,
143 &wavelet, &output_buffer, total_bits ) )
144 {
145 fprintf( stderr, "QccSPECK3DEncode() failed.\n" );
146 return 1;
147 }
148
149 /* Write bit buffer to disk, and free resources */
150 if( QccBitBufferEnd( &output_buffer ) )
151 {
152 fprintf( stderr, "QccBitBufferEnd() failed.\n" );
153 return 1;
154 }
155
156
157 /*
158 * Now start the decode procedures.
159 */
160
161 /* Prepare an input buffer */
162 QccBitBuffer input_buffer;
163 QccBitBufferInitialize( &input_buffer );
164 QccStringCopy( input_buffer.filename, output_name );
165 input_buffer.type = QCCBITBUFFER_INPUT;
166 if (QccBitBufferStart(&input_buffer) )
167 {
168 fprintf( stderr, "Error calling QccBitBufferStart()\n" );
169 return 1;
170 }
171
172 /* Decode header */
173 double image_mean;
174 int max_coeff_bits;
175 if (QccSPECK3DDecodeHeader( &input_buffer, &transform_type, &num_of_levels_z,
176 &num_of_levels_xy, &num_of_frames, &num_of_rows,
177 &num_of_cols, &image_mean, &max_coeff_bits ))
178 {
179 fprintf( stderr, "Error calling QccSPECK3DDecodeHeader()\n");
180 return 1;
181 }
182
183 /* Do a SPECK decode. Note that we reuse the imagecube object */
184 int TargetBitCnt = QCCENT_ANYNUMBITS;
185 if (QccSPECK3DDecode( &input_buffer, &imagecube, NULL,
186 transform_type, num_of_levels_z, num_of_levels_xy,
187 &wavelet, image_mean, max_coeff_bits,
188 TargetBitCnt ))
189 {
190 QccErrorAddMessage("Error calling QccSPECK3DDecode()" );
191 QccErrorExit();
192 }
193 if (QccBitBufferEnd(&input_buffer) )
194 {
195 fprintf( stderr, "Error calling QccBitBufferEnd()\n" );
196 return 1;
197 }
198 gettimeofday( &end, NULL );
199 long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec;
200 printf("qcc takes milliseconds: %f\n", (float)elapsed / 1000.0f );
201
202 /* Collected the decoded array, and print out some statistics. */
203 float* out_array = (float*)malloc( num_of_bytes );
204 image_cube_to_array( &imagecube, out_array );
205 float rmse, lmax, min, max, psnr;
206 if( sam_get_statsf( in_array, out_array, num_of_vals, &rmse, &lmax, &psnr, &min, &max ) )
207 {
208 fprintf( stderr, "get_rmse_max failed.\n" );
209 free( in_array );
210 free( out_array );
211 return 1;
212 }
213 printf("rmse = %f, lmax = %f, psnr = %fdB, orig_min = %f, orig_max = %f\n",
214 rmse, lmax, psnr, min, max );
215
216 /* cleanup */
217 QccWAVWaveletFree( &wavelet );
218 QccIMGImageCubeFree( &imagecube );
219 free( in_array );
220 free( out_array );
221
222 return 0;
223 }
224
225
226
227
228