nifti_test.c
来自「DTMK软件开发包,此为开源软件,是一款很好的医学图像开发资源.」· C语言 代码 · 共 691 行 · 第 1/2 页
C
691 行
"myimage.img.gz",
".nii",
".myhiddenimage",
".myhiddenimage.nii"
};
const char * KNOWN_FILE_BASENAMES[NUM_FILE_NAMES]={
"myimage",
"myimage.tif",
"myimage.tif.gz",
"myimage",
"myimage",
"",
".myhiddenimage",
".myhiddenimage"
};
const int KNOWN_nifti_validfilename[NUM_FILE_NAMES]={
1,
1,
1,
1,
1,
0,
1,
1
};
const int KNOWN_nifti_is_complete_filename[NUM_FILE_NAMES]={
0,
0,
0,
1,
1,
0,
0,
1
};
unsigned int fni;
for(fni=0;fni<NUM_FILE_NAMES;fni++)
{
printf("\nTesting \"%s\" filename\n",FILE_NAMES[fni]);
{
int KnownValid=nifti_validfilename(FILE_NAMES[fni]);
snprintf(TEMP_STR,256,"nifti_validfilename(\"%s\")=%d",FILE_NAMES[fni],KnownValid);
PrintTest(TEMP_STR,KnownValid != KNOWN_nifti_validfilename[fni],NIFTITEST_FALSE,&Errors);
}
{
int KnownValid=nifti_is_complete_filename(FILE_NAMES[fni]);
snprintf(TEMP_STR,256,"nifti_is_complete_filename(\"%s\")=%d",FILE_NAMES[fni],KnownValid);
PrintTest(TEMP_STR,KnownValid != KNOWN_nifti_is_complete_filename[fni],NIFTITEST_FALSE,&Errors);
}
{
char * basename=nifti_makebasename(FILE_NAMES[fni]);
snprintf(TEMP_STR,256,"nifti_makebasename(\"%s\")=\"%s\"",FILE_NAMES[fni],basename);
PrintTest(TEMP_STR,strcmp(basename,KNOWN_FILE_BASENAMES[fni]) != 0,NIFTITEST_FALSE,&Errors);
free(basename);
}
}
/*
* the following 2 calls aren't tested, because all they do is display
* compile-time information -- no way to fail unless writing to stdout fails.
*/
nifti_disp_lib_hist();
nifti_disp_lib_version();
/*
* the following exercises error path code in nifti_image_read_bricks
*/
PrintTest(
"nifti_image_read_bricks 1",
nifti_image_read_bricks((char *)0,-1,(const int *)0,(nifti_brick_list *)0) != 0,
NIFTITEST_FALSE,
&Errors);
PrintTest(
"nifti_image_read_bricks 1",
nifti_image_read_bricks("NOFILE.NOFILE",-1,(const int *)0,(nifti_brick_list *)0) != 0,
NIFTITEST_FALSE,
&Errors);
}
/*
* call nifti_datatype_string with all possible values
*/
#define nifti_datatype_test(constant,string) \
{ \
char buf[64]; \
sprintf(buf,"nifti_datatype_string %s",string); \
PrintTest( \
buf, \
strcmp(nifti_datatype_string(constant),string) != 0, \
NIFTITEST_FALSE, \
&Errors); \
}
nifti_datatype_test(DT_UNKNOWN,"UNKNOWN");
nifti_datatype_test(DT_BINARY, "BINARY");
nifti_datatype_test(DT_INT8, "INT8");
nifti_datatype_test(DT_UINT8, "UINT8");
nifti_datatype_test(DT_INT16, "INT16");
nifti_datatype_test(DT_UINT16, "UINT16");
nifti_datatype_test(DT_INT32, "INT32");
nifti_datatype_test(DT_UINT32, "UINT32");
nifti_datatype_test(DT_INT64, "INT64");
nifti_datatype_test(DT_UINT64, "UINT64");
nifti_datatype_test(DT_FLOAT32, "FLOAT32");
nifti_datatype_test(DT_FLOAT64, "FLOAT64");
nifti_datatype_test(DT_FLOAT128, "FLOAT128");
nifti_datatype_test(DT_COMPLEX64, "COMPLEX64");
nifti_datatype_test(DT_COMPLEX128, "COMPLEX128");
nifti_datatype_test(DT_COMPLEX256, "COMPLEX256");
nifti_datatype_test(DT_RGB24, "RGB24");
#define nifti_is_inttype_test(constant,rval) \
{ \
char buf[64]; \
sprintf(buf,"nifti_datatype_string %d",constant); \
PrintTest( \
buf, \
nifti_is_inttype(constant) != rval, \
NIFTITEST_FALSE, \
&Errors); \
}
nifti_is_inttype_test(DT_UNKNOWN,0);
nifti_is_inttype_test(DT_BINARY,0);
nifti_is_inttype_test(DT_INT8,1);
nifti_is_inttype_test(DT_UINT8,1);
nifti_is_inttype_test(DT_INT16,1);
nifti_is_inttype_test(DT_UINT16,1);
nifti_is_inttype_test(DT_INT32,1);
nifti_is_inttype_test(DT_UINT32,1);
nifti_is_inttype_test(DT_INT64,1);
nifti_is_inttype_test(DT_UINT64,1);
nifti_is_inttype_test(DT_FLOAT32,0);
nifti_is_inttype_test(DT_FLOAT64,0);
nifti_is_inttype_test(DT_FLOAT128,0);
nifti_is_inttype_test(DT_COMPLEX64,0);
nifti_is_inttype_test(DT_COMPLEX128,0);
nifti_is_inttype_test(DT_COMPLEX256,0);
nifti_is_inttype_test(DT_RGB24,1);
#define nifti_units_string_test(constant,string) \
{ \
char buf[64]; \
sprintf(buf,"nifti_units_string_test %s",string); \
PrintTest( \
buf, \
strcmp(nifti_units_string(constant),string) != 0, \
NIFTITEST_FALSE, \
&Errors); \
}
nifti_units_string_test(NIFTI_UNITS_METER,"m");
nifti_units_string_test(NIFTI_UNITS_MM,"mm");
nifti_units_string_test(NIFTI_UNITS_MICRON,"um");
nifti_units_string_test(NIFTI_UNITS_SEC,"s");
nifti_units_string_test(NIFTI_UNITS_MSEC,"ms");
nifti_units_string_test(NIFTI_UNITS_USEC,"us");
nifti_units_string_test(NIFTI_UNITS_HZ,"Hz");
nifti_units_string_test(NIFTI_UNITS_PPM,"ppm");
nifti_units_string_test(NIFTI_UNITS_RADS,"rad/s");
#define nifti_intent_string_test(constant,string) \
{ \
char buf[64]; \
sprintf(buf,"nifti_intent_string %s",string); \
PrintTest( \
buf, \
strcmp(nifti_intent_string(constant),string) != 0, \
NIFTITEST_FALSE, \
&Errors); \
}
nifti_intent_string_test(NIFTI_INTENT_CORREL,"Correlation statistic");
nifti_intent_string_test(NIFTI_INTENT_TTEST,"T-statistic");
nifti_intent_string_test(NIFTI_INTENT_FTEST,"F-statistic");
nifti_intent_string_test(NIFTI_INTENT_ZSCORE,"Z-score");
nifti_intent_string_test(NIFTI_INTENT_CHISQ,"Chi-squared distribution");
nifti_intent_string_test(NIFTI_INTENT_BETA,"Beta distribution");
nifti_intent_string_test(NIFTI_INTENT_BINOM,"Binomial distribution");
nifti_intent_string_test(NIFTI_INTENT_GAMMA,"Gamma distribution");
nifti_intent_string_test(NIFTI_INTENT_POISSON,"Poisson distribution");
nifti_intent_string_test(NIFTI_INTENT_NORMAL,"Normal distribution");
nifti_intent_string_test(NIFTI_INTENT_FTEST_NONC,"F-statistic noncentral");
nifti_intent_string_test(NIFTI_INTENT_CHISQ_NONC,"Chi-squared noncentral");
nifti_intent_string_test(NIFTI_INTENT_LOGISTIC,"Logistic distribution");
nifti_intent_string_test(NIFTI_INTENT_LAPLACE,"Laplace distribution");
nifti_intent_string_test(NIFTI_INTENT_UNIFORM,"Uniform distribition");
nifti_intent_string_test(NIFTI_INTENT_TTEST_NONC,"T-statistic noncentral");
nifti_intent_string_test(NIFTI_INTENT_WEIBULL,"Weibull distribution");
nifti_intent_string_test(NIFTI_INTENT_CHI,"Chi distribution");
nifti_intent_string_test(NIFTI_INTENT_INVGAUSS,"Inverse Gaussian distribution");
nifti_intent_string_test(NIFTI_INTENT_EXTVAL,"Extreme Value distribution");
nifti_intent_string_test(NIFTI_INTENT_PVAL,"P-value");
nifti_intent_string_test(NIFTI_INTENT_LOGPVAL,"Log P-value");
nifti_intent_string_test(NIFTI_INTENT_LOG10PVAL,"Log10 P-value");
nifti_intent_string_test(NIFTI_INTENT_ESTIMATE,"Estimate");
nifti_intent_string_test(NIFTI_INTENT_LABEL,"Label index");
nifti_intent_string_test(NIFTI_INTENT_NEURONAME,"NeuroNames index");
nifti_intent_string_test(NIFTI_INTENT_GENMATRIX,"General matrix");
nifti_intent_string_test(NIFTI_INTENT_SYMMATRIX,"Symmetric matrix");
nifti_intent_string_test(NIFTI_INTENT_DISPVECT,"Displacement vector");
nifti_intent_string_test(NIFTI_INTENT_VECTOR,"Vector");
nifti_intent_string_test(NIFTI_INTENT_POINTSET,"Pointset");
nifti_intent_string_test(NIFTI_INTENT_TRIANGLE,"Triangle");
nifti_intent_string_test(NIFTI_INTENT_QUATERNION,"Quaternion");
nifti_intent_string_test(NIFTI_INTENT_DIMLESS,"Dimensionless number");
nifti_intent_string_test(-200,"Unknown");
#define nifti_slice_string_test(constant,string) \
{ \
char buf[64]; \
sprintf(buf,"nifti_slice_string_test %s",string); \
PrintTest( \
buf, \
strcmp(nifti_slice_string(constant),string) != 0, \
NIFTITEST_FALSE, \
&Errors); \
}
nifti_slice_string_test(NIFTI_SLICE_SEQ_INC,"sequential_increasing");
nifti_slice_string_test(NIFTI_SLICE_SEQ_DEC,"sequential_decreasing");
nifti_slice_string_test(NIFTI_SLICE_ALT_INC,"alternating_increasing");
nifti_slice_string_test(NIFTI_SLICE_ALT_DEC,"alternating_decreasing");
nifti_slice_string_test(NIFTI_SLICE_ALT_INC2,"alternating_increasing_2");
nifti_slice_string_test(NIFTI_SLICE_ALT_DEC2,"alternating_decreasing_2");
#define nifti_orientation_string_test(constant,string) \
{ \
char buf[64]; \
sprintf(buf,"nifti_orientation_string_test %s",string); \
PrintTest( \
buf, \
strcmp(nifti_orientation_string(constant),string) != 0, \
NIFTITEST_FALSE, \
&Errors); \
}
nifti_orientation_string_test(NIFTI_L2R,"Left-to-Right");
nifti_orientation_string_test(NIFTI_R2L,"Right-to-Left");
nifti_orientation_string_test(NIFTI_P2A,"Posterior-to-Anterior");
nifti_orientation_string_test(NIFTI_A2P,"Anterior-to-Posterior");
nifti_orientation_string_test(NIFTI_I2S,"Inferior-to-Superior");
nifti_orientation_string_test(NIFTI_S2I,"Superior-to-Inferior");
#define nifti_datatype_sizes_test(constant,Nbyper,Swapsize) \
{ \
int nbyper; \
int swapsize; \
char buf[64]; \
sprintf(buf,"nifti_datatype_sizes_test %d",constant); \
nifti_datatype_sizes(constant,&nbyper,&swapsize); \
PrintTest( \
buf, \
nbyper != Nbyper || swapsize != Swapsize, \
NIFTITEST_FALSE, \
&Errors); \
}
nifti_datatype_sizes_test(DT_UINT8,1,0);
nifti_datatype_sizes_test(DT_UINT16,2,2);
nifti_datatype_sizes_test(DT_RGB24,3,0);
nifti_datatype_sizes_test(DT_FLOAT32,4,4);
nifti_datatype_sizes_test(DT_COMPLEX64,8,4);
nifti_datatype_sizes_test(DT_UINT64,8,8);
nifti_datatype_sizes_test(DT_FLOAT128,16,16);
nifti_datatype_sizes_test(DT_COMPLEX128,16,8);
nifti_datatype_sizes_test(DT_COMPLEX256,32,16);
{
mat44 R;
unsigned i,j;
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
R.m[i][j] = (i == j ? 1 : 0);
float qb;
float qc;
float qd;
float qx;
float qy;
float qz;
float dx;
float dy;
float dz;
float qfac;
nifti_mat44_to_quatern(R,&qb,&qc,&qd,&qx,&qy,&qz,&dx,&dy,&dz,&qfac);
PrintTest("nifti_mat44_to_quatern",
qb != 0.000000 || qc != 0.000000 || qd != 0.000000 ||
qx != 0.000000 || qy != 0.000000 || qd != 0.000000 ||
dx != 1.000000 || dy != 1.000000 || dz != 1.000000 ||
qfac != 1.000000,
NIFTITEST_FALSE,&Errors);
}
{
mat44 x = nifti_make_orthog_mat44(0.14,0.0,0.0,
0.0,0.9,0.0,
0.0,0.0,1.1);
PrintTest("nifti_make_orthog_mat44",
x.m[0][0] != 1.0 || x.m[1][1] != 1.0 ||
x.m[2][2] != 1.0 || x.m[3][3] != 1.0,
NIFTITEST_FALSE,&Errors);
}
{
static char x[16] = { 'a','b','c','d','e','f','g','h',
'H','G','F','E','D','C','B','A' };
nifti_swap_Nbytes(1,16,(void *)x);
PrintTest("nifti_swap_16bytes",
x[0] != 'A' || x[1] != 'B' || x[2] != 'C' || x[3] != 'D' ||
x[4] != 'E' || x[5] != 'F' || x[6] != 'G' || x[7] != 'H' ||
x[8] != 'h' || x[9] != 'g' || x[10] != 'f' || x[11] != 'e' ||
x[12] != 'd' || x[13] != 'c' || x[14] != 'b' || x[15] != 'a',
NIFTITEST_FALSE,&Errors);
}
{
static char x[8] = { 'a','b','c','d','D','C','B','A' };
nifti_swap_Nbytes(1,8,(void *)x);
PrintTest("nifti_swap_8bytes",
x[0] != 'A' || x[1] != 'B' || x[2] != 'C' || x[3] != 'D' ||
x[4] != 'd' || x[5] != 'c' || x[6] != 'b' || x[7] != 'a',
NIFTITEST_FALSE,&Errors);
}
{
/*
* test nifti_simple_init_nim
*/
nifti_image *nim = nifti_simple_init_nim();
PrintTest("nifti_simple_init_nim",
nim == 0,NIFTITEST_FALSE,&Errors);
nifti_image_free(nim); nim = 0;
/*
* test nifti_image_open
*/
znzFile f = nifti_image_open("ATestReferenceImageForReadingAndWriting.hdr","r",&nim);
PrintTest("nifti_image_open",
nim == 0 || f == 0,
NIFTITEST_FALSE,&Errors);
PrintTest("nifti_image_load",
nifti_image_load(nim) == -1,
NIFTITEST_FALSE,&Errors);
nifti_image_unload(nim);
PrintTest("nifti_image_unload",
nim->data != 0,
NIFTITEST_FALSE,&Errors);
znzclose(f);
nifti_image_free(nim);
}
/*
* call various functions from nifti_stats
*/
printf("\n\nTOTAL ERRORS=%d\n",Errors);
return Errors;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?