| 1 |
|
| 2 |
#define HAVE_SPECK |
| 3 |
|
| 4 |
#include "libQccPack.h" |
| 5 |
#include "sam_helper.h" |
| 6 |
|
| 7 |
#include <stdlib.h> |
| 8 |
|
| 9 |
|
| 10 |
int array_to_pyramid( const float* array, QccWAVSubbandPyramid* pyramid ) |
| 11 |
{ |
| 12 |
size_t counter = 0, row, col; |
| 13 |
for( row = 0; row < pyramid->num_rows; row++ ) |
| 14 |
for( col = 0; col < pyramid->num_cols; col++ ) |
| 15 |
{ |
| 16 |
pyramid->matrix[row][col] = array[ counter++ ]; |
| 17 |
} |
| 18 |
|
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
int pyramid_to_array( const QccWAVSubbandPyramid* pyramid, double* array ) |
| 23 |
{ |
| 24 |
size_t counter = 0, row, col; |
| 25 |
for( row = 0; row < pyramid->num_rows; row++ ) |
| 26 |
for( col = 0; col < pyramid->num_cols; col++ ) |
| 27 |
{ |
| 28 |
array[counter++] = pyramid->matrix[row][col]; |
| 29 |
} |
| 30 |
|
| 31 |
return 0; |
| 32 |
} |
| 33 |
|
| 34 |
int main( int argc, char* argv[] ) |
| 35 |
{ |
| 36 |
if( argc != 5 ) |
| 37 |
{ |
| 38 |
printf("Usage: ./a.out input_filename dim_x dim_y output_filename\n"); |
| 39 |
return 1; |
| 40 |
} |
| 41 |
|
| 42 |
const char* input_name = argv[1]; |
| 43 |
const size_t num_of_cols = atoi( argv[2] ); |
| 44 |
const size_t num_of_rows = atoi( argv[3] ); |
| 45 |
const char* output_name = argv[4]; |
| 46 |
const size_t num_of_vals = num_of_cols * num_of_rows; |
| 47 |
|
| 48 |
/* Read input data */ |
| 49 |
float* in_buf = (float*)malloc( sizeof(float) * num_of_vals ); |
| 50 |
if( sam_read_n_bytes( input_name, sizeof(float) * num_of_vals, in_buf ) != 0 ) |
| 51 |
{ |
| 52 |
printf("Error: read input file!\n"); |
| 53 |
return 1; |
| 54 |
} |
| 55 |
|
| 56 |
/* Prepare Qcc data structure: QccWAVSubbandPyramid */ |
| 57 |
QccWAVSubbandPyramid pyramid; |
| 58 |
QccWAVSubbandPyramidInitialize( &pyramid ); |
| 59 |
pyramid.num_levels = 0; |
| 60 |
pyramid.num_cols = num_of_cols; |
| 61 |
pyramid.num_rows = num_of_rows; |
| 62 |
if (QccWAVSubbandPyramidAlloc( &pyramid )) |
| 63 |
{ |
| 64 |
printf("(QccSPECKEncode): Error calling QccWAVSubbandPyramidAlloc()"); |
| 65 |
return 1; |
| 66 |
} |
| 67 |
array_to_pyramid( in_buf, &pyramid ); |
| 68 |
|
| 69 |
/* Prepare Qcc data structure: QccWAVWavelet */ |
| 70 |
QccString WaveletFilename = QCCWAVWAVELET_DEFAULT_WAVELET; |
| 71 |
QccString Boundary = "symmetric"; |
| 72 |
QccWAVWavelet Wavelet; |
| 73 |
if( QccWAVWaveletInitialize( &Wavelet ) ) |
| 74 |
{ |
| 75 |
fprintf( stderr, "QccWAVWaveletInitialize failed.\n" ); |
| 76 |
return 1; |
| 77 |
} |
| 78 |
if( QccWAVWaveletCreate( &Wavelet, WaveletFilename, Boundary ) ) |
| 79 |
{ |
| 80 |
fprintf( stderr, "QccWAVWaveletCreate failed.\n" ); |
| 81 |
return 1; |
| 82 |
} |
| 83 |
|
| 84 |
/* Apply dwt */ |
| 85 |
double image_mean; |
| 86 |
QccWAVSubbandPyramidSubtractMean( &pyramid, &image_mean, NULL ); |
| 87 |
float min_xy = (float)num_of_cols; |
| 88 |
if( num_of_rows < num_of_cols ) |
| 89 |
min_xy = (float)num_of_rows; |
| 90 |
float f = log2f( min_xy / 8.0f ); |
| 91 |
int num_level_xy = f < 0.0f ? 0 : (int)f + 1; |
| 92 |
QccWAVSubbandPyramidDWT( &pyramid, num_level_xy, &Wavelet ); |
| 93 |
|
| 94 |
/* Apply idwt */ |
| 95 |
QccWAVSubbandPyramidInverseDWT( &pyramid, &Wavelet ); |
| 96 |
QccWAVSubbandPyramidAddMean( &pyramid, image_mean ); |
| 97 |
|
| 98 |
/* write coefficients to a file */ |
| 99 |
printf("mean = %lf\n", image_mean ); |
| 100 |
double* out_buf = (double*)malloc( sizeof(double) * num_of_vals ); |
| 101 |
pyramid_to_array( &pyramid, out_buf ); |
| 102 |
if( sam_write_n_bytes( output_name, sizeof(double) * num_of_vals, out_buf ) ) |
| 103 |
{ |
| 104 |
printf("Output write error!\n"); |
| 105 |
return 1; |
| 106 |
} |
| 107 |
|
| 108 |
/* compare the results against the original in double precision */ |
| 109 |
double* in_bufd = (double*)malloc( sizeof(double) * num_of_vals ); |
| 110 |
for( size_t i = 0; i < num_of_vals; i++ ) |
| 111 |
in_bufd[i] = in_buf[i]; |
| 112 |
double rmse, lmax, psnr, arr1min, arr1max; |
| 113 |
sam_get_statsd( in_bufd, out_buf, num_of_vals, |
| 114 |
&rmse, &lmax, &psnr, &arr1min, &arr1max ); |
| 115 |
printf("Qcc: rmse = %f, lmax = %f, psnr = %fdB, orig_min = %f, orig_max = %f\n", |
| 116 |
rmse, lmax, psnr, arr1min, arr1max ); |
| 117 |
|
| 118 |
/* clean up */ |
| 119 |
free( in_bufd ); |
| 120 |
free( out_buf ); |
| 121 |
QccWAVSubbandPyramidFree( &pyramid ); |
| 122 |
free( in_buf ); |
| 123 |
} |