| 1 |
/* |
| 2 |
* This program reads a 2D 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 <sys/time.h> |
| 9 |
|
| 10 |
#include "libQccPack.h" |
| 11 |
#include "sam_helper.h" |
| 12 |
#include "assert.h" |
| 13 |
|
| 14 |
#include "SpeckConfig.h" |
| 15 |
|
| 16 |
|
| 17 |
int array_to_image( const float* array, QccIMGImageComponent* image ) |
| 18 |
{ |
| 19 |
float min = array[0], max = array[0]; |
| 20 |
size_t counter = 0, row, col; |
| 21 |
for( row = 0; row < image->num_rows; row++ ) |
| 22 |
for( col = 0; col < image->num_cols; col++ ) |
| 23 |
{ |
| 24 |
float val = array[ counter++ ]; |
| 25 |
image->image[row][col] = val; |
| 26 |
if( val < min ) min = val; |
| 27 |
if( val > max ) max = val; |
| 28 |
} |
| 29 |
|
| 30 |
image->min_val = min; |
| 31 |
image->max_val = max; |
| 32 |
|
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
int image_to_array( const QccIMGImageComponent* image, float* array ) |
| 37 |
{ |
| 38 |
size_t counter = 0, row, col; |
| 39 |
for( row = 0; row < image->num_rows; row++ ) |
| 40 |
for( col = 0; col < image->num_cols; col++ ) |
| 41 |
{ |
| 42 |
array[counter++] = image->image[row][col]; |
| 43 |
} |
| 44 |
|
| 45 |
return 0; |
| 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 != 5 ) |
| 63 |
{ |
| 64 |
fprintf( stderr, "Usage: ./a.out input_raw_file dim_X, dim_Y, comp_ratio\n" ); |
| 65 |
return 1; |
| 66 |
} |
| 67 |
const char* input_name = argv[1]; |
| 68 |
const char* output_name = "qcc.bitstream"; |
| 69 |
int num_of_cols = atoi( argv[2] ); |
| 70 |
int num_of_rows = atoi( argv[3] ); |
| 71 |
const size_t num_of_vals = num_of_cols * num_of_rows; |
| 72 |
const size_t num_of_bytes = sizeof(float) * num_of_vals; |
| 73 |
int num_of_levels = calc_num_of_xforms( num_of_cols ); |
| 74 |
|
| 75 |
const float cratio = atof( argv[4] ); |
| 76 |
const int header_size = 21; /* bytes */ |
| 77 |
/* Allocate bit budget for the header too */ |
| 78 |
const int total_bits = (int)(8.0f * num_of_bytes / cratio) + 8 * header_size; |
| 79 |
|
| 80 |
/* |
| 81 |
* Stage 1: Encoding |
| 82 |
*/ |
| 83 |
|
| 84 |
/* Prepare necessary Wavelets. */ |
| 85 |
QccString WaveletFilename = QCCWAVWAVELET_DEFAULT_WAVELET; |
| 86 |
QccString Boundary = "symmetric"; |
| 87 |
QccWAVWavelet Wavelet; |
| 88 |
if( QccWAVWaveletInitialize( &Wavelet ) ) |
| 89 |
{ |
| 90 |
fprintf( stderr, "QccWAVWaveletInitialize failed.\n" ); |
| 91 |
return 1; |
| 92 |
} |
| 93 |
if( QccWAVWaveletCreate( &Wavelet, WaveletFilename, Boundary ) ) |
| 94 |
{ |
| 95 |
fprintf( stderr, "QccWAVWaveletCreate failed.\n" ); |
| 96 |
return 1; |
| 97 |
} |
| 98 |
|
| 99 |
/* Prepare necessary output buffer. */ |
| 100 |
QccBitBuffer OutputBuffer; |
| 101 |
if( QccBitBufferInitialize( &OutputBuffer ) ) |
| 102 |
{ |
| 103 |
fprintf( stderr, "QccBitBufferInitialize failed.\n" ); |
| 104 |
return 1; |
| 105 |
} |
| 106 |
QccStringCopy( OutputBuffer.filename, output_name ); |
| 107 |
OutputBuffer.type = QCCBITBUFFER_OUTPUT; |
| 108 |
if (QccBitBufferStart(&OutputBuffer)) |
| 109 |
{ |
| 110 |
fprintf( stderr, "QccBitBufferStart failed.\n" ); |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
/* Prepare necessary image component. */ |
| 115 |
QccIMGImageComponent Image; |
| 116 |
if( QccIMGImageComponentInitialize( &Image ) ) |
| 117 |
{ |
| 118 |
fprintf( stderr, "QccIMGImageComponentInitialize failed.\n" ); |
| 119 |
return 1; |
| 120 |
} |
| 121 |
QccStringCopy( Image.filename, input_name ); |
| 122 |
Image.num_cols = num_of_cols; |
| 123 |
Image.num_rows = num_of_rows; |
| 124 |
if( QccIMGImageComponentAlloc( &Image ) ) |
| 125 |
{ |
| 126 |
fprintf( stderr, "QccIMGImageComponentAlloc failed.\n" ); |
| 127 |
return 1; |
| 128 |
} |
| 129 |
|
| 130 |
/* Get ready to read data. */ |
| 131 |
float* in_array = (float*)malloc( num_of_bytes ); |
| 132 |
if( sam_read_n_bytes( input_name, num_of_bytes, in_array ) != 0 ) |
| 133 |
return 1; |
| 134 |
if( array_to_image( in_array, &Image ) ) |
| 135 |
{ |
| 136 |
fprintf( stderr, "sam_read_data failed.\n" ); |
| 137 |
free( in_array ); |
| 138 |
return 1; |
| 139 |
} |
| 140 |
|
| 141 |
/* Encode to a bitstream, and write to the bit stream. */ |
| 142 |
struct timeval start, end; |
| 143 |
gettimeofday( &start, NULL ); |
| 144 |
if( QccSPECKEncode( &Image, NULL, num_of_levels, |
| 145 |
total_bits, &Wavelet, &OutputBuffer ) ) |
| 146 |
{ |
| 147 |
fprintf( stderr, "QccSPECKEncode failed.\n" ); |
| 148 |
free( in_array ); |
| 149 |
return 1; |
| 150 |
} |
| 151 |
if( QccBitBufferEnd( &OutputBuffer ) ) |
| 152 |
{ |
| 153 |
fprintf( stderr, "QccBitBufferEnd failed.\n" ); |
| 154 |
free( in_array ); |
| 155 |
return 1; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/* |
| 160 |
* Stage 2: Decoding |
| 161 |
*/ |
| 162 |
|
| 163 |
/* Prepare input buffer. */ |
| 164 |
QccBitBuffer InputBuffer; |
| 165 |
if( QccBitBufferInitialize( &InputBuffer ) ) |
| 166 |
{ |
| 167 |
fprintf( stderr, "QccBitBufferInitialize failed.\n" ); |
| 168 |
free( in_array ); |
| 169 |
return 1; |
| 170 |
} |
| 171 |
QccStringCopy( InputBuffer.filename, output_name ); |
| 172 |
InputBuffer.type = QCCBITBUFFER_INPUT; |
| 173 |
if (QccBitBufferStart(&InputBuffer)) |
| 174 |
{ |
| 175 |
fprintf( stderr, "QccBitBufferStart failed.\n" ); |
| 176 |
free( in_array ); |
| 177 |
return 1; |
| 178 |
} |
| 179 |
|
| 180 |
double image_mean; |
| 181 |
int max_coeff_bits; |
| 182 |
if( QccSPECKDecodeHeader( &InputBuffer, &num_of_levels, &num_of_rows, |
| 183 |
&num_of_cols, &image_mean, &max_coeff_bits ) ) |
| 184 |
{ |
| 185 |
fprintf( stderr, "QccSPECKDecodeHeader failed.\n" ); |
| 186 |
free( in_array ); |
| 187 |
return 1; |
| 188 |
} |
| 189 |
assert( num_of_vals == num_of_rows * num_of_cols ); |
| 190 |
|
| 191 |
/* Note that we re-use the ImageComponent that was used for encoding. */ |
| 192 |
|
| 193 |
if( QccSPECKDecode( &InputBuffer, &Image, NULL, num_of_levels, &Wavelet, |
| 194 |
image_mean, max_coeff_bits, total_bits ) ) |
| 195 |
{ |
| 196 |
fprintf( stderr, "QccSPECKDecode failed.\n" ); |
| 197 |
free( in_array ); |
| 198 |
return 1; |
| 199 |
} |
| 200 |
gettimeofday( &end, NULL ); |
| 201 |
long elapsed = (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec; |
| 202 |
printf("qcc takes milliseconds: %f\n", (float)elapsed / 1000.0f ); |
| 203 |
|
| 204 |
/* Evaluate the encoding and decoding process! */ |
| 205 |
float* out_array = (float*)malloc( num_of_bytes ); |
| 206 |
image_to_array( &Image, out_array ); |
| 207 |
float rmse, lmax, min, max, psnr; |
| 208 |
if( sam_get_statsf( in_array, out_array, num_of_vals, &rmse, &lmax, &psnr, &min, &max ) ) |
| 209 |
{ |
| 210 |
fprintf( stderr, "get_rmse_max failed.\n" ); |
| 211 |
free( in_array ); |
| 212 |
free( out_array ); |
| 213 |
return 1; |
| 214 |
} |
| 215 |
printf("rmse = %f, lmax = %f, psnr = %fdB, orig_min = %f, orig_max = %f\n", |
| 216 |
rmse, lmax, psnr, min, max ); |
| 217 |
|
| 218 |
|
| 219 |
/* Cleanup */ |
| 220 |
QccWAVWaveletFree(&Wavelet); |
| 221 |
QccIMGImageComponentFree( &Image ); |
| 222 |
free( in_array ); |
| 223 |
free( out_array ); |
| 224 |
|
| 225 |
return 0; |
| 226 |
} |