Debian Codegraph @

src/SPERR/utilities/qccpack/raw2imagecube.c

raw
1 /* This program converts raw data values
2 * into a QccIMGImageCube object, then write this object
3 * to an ICB-format file, using the QccIMGImageCube function.
4 *
5 * Programmer: Samuel Li
6 * Date: 7/2/2015
7 *
8 * Modified: 8/5/2015
9 * Read double precision added.
10 */
11
12 #include "libQccPack.h"
13
14 #ifdef DOUBLE
15 #define FLOAT_SIZE 8
16 #else
17 #define FLOAT_SIZE 4
18 #endif
19
20 #define USG_STRING "%d:dim_x %d:dim_y %d:dim_z %s:input_name (raw) %s:output_name (icb)"
21
22
23 int ImageCubeReadData( QccString filename, QccIMGImageCube* image_cube )
24 {
25 FILE* infile = fopen( filename, "rb" );
26 if( infile == NULL ) {
27 printf( "Read file open error!\n" );
28 exit(1);
29 }
30
31 fseek( infile, 0, SEEK_END );
32 long size = ftell( infile );
33 if( size % FLOAT_SIZE == 0 ) {
34 size /= FLOAT_SIZE;
35 if( size != (image_cube->num_cols) * (image_cube->num_rows) *
36 (image_cube -> num_frames) ) {
37 printf( "Read file length error!\n" );
38 exit(1);
39 }
40 }
41 else{
42 printf( "Read file broken!\n" );
43 exit(1);
44 }
45
46 double min = MAXDOUBLE;
47 double max = -MAXDOUBLE;
48 int frame, row, col;
49 long planeSize = (image_cube -> num_cols) * (image_cube -> num_rows);
50 void* ptr = malloc( FLOAT_SIZE * planeSize );
51 #ifdef DOUBLE
52 double* buf = (double*) ptr;
53 #else
54 float* buf = (float*) ptr;
55 #endif
56
57 for( frame = 0; frame < image_cube -> num_frames; frame++ ) {
58 fseek( infile, FLOAT_SIZE * frame * planeSize, SEEK_SET );
59 long result = fread( buf, FLOAT_SIZE, planeSize, infile );
60 if( result != planeSize ) {
61 printf( "Input file read error!\n");
62 exit(1);
63 }
64 long i;
65 for( i = 0; i < planeSize; i++ ) {
66 if( buf[i] < min ) min = buf[i];
67 if( buf[i] > max ) max = buf[i];
68 }
69 i = 0;
70 for (row = 0; row < image_cube->num_rows; row++)
71 for (col = 0; col < image_cube->num_cols; col++)
72 image_cube -> volume[frame][row][col] = buf[ i++ ];
73 }
74
75 image_cube -> min_val = min;
76 image_cube -> max_val = max;
77 free( ptr );
78 return 0;
79 }
80
81 int main (int argc, char* argv[] )
82 {
83 QccIMGImageCube imagecube;
84 QccIMGImageCubeInitialize( &imagecube );
85 QccString input_name;
86
87 if( QccParseParameters( argc, argv, USG_STRING, &imagecube.num_cols, &imagecube.num_rows,
88 &imagecube.num_frames, input_name, imagecube.filename ) )
89 QccErrorExit();
90
91
92 if( QccIMGImageCubeAlloc( &imagecube ) )
93 QccErrorPrintMessages();
94
95
96 ImageCubeReadData( input_name, &imagecube );
97 QccIMGImageCubeWrite( &imagecube );
98
99 //QccIMGImageCubePrint( &imagecube );
100
101 QccIMGImageCubeFree( &imagecube );
102
103 return 0;
104 }