00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifdef HAVE_CONFIG_H
00027 # include <config.h>
00028 #endif
00029
00030
00031
00037
00040 #include <xsh_data_pre.h>
00041 #include <xsh_data_image_3d.h>
00042 #include <xsh_error.h>
00043 #include <xsh_msg.h>
00044 #include <xsh_data_instrument.h>
00045 #include <xsh_dfs.h>
00046 #include <xsh_pfits.h>
00047 #include <tests.h>
00048
00049 #include <cpl.h>
00050 #include <math.h>
00051 #include <getopt.h>
00052
00053
00054
00055
00056 #define MODULE_ID "XSH_DATA_IMAGE_3D"
00057
00058 enum {
00059 DBG_OPT, HELP_OPT, NX_OPT, NY_OPT, NZ_OPT
00060 } ;
00061
00062 static const char *Options = "?" ;
00063 static struct option LongOptions[] = {
00064 {"nx", required_argument, 0, NX_OPT},
00065 {"ny", required_argument, 0, NY_OPT},
00066 {"nz", required_argument, 0, NZ_OPT},
00067 {"debug", required_argument, 0, DBG_OPT},
00068 {"help", 0, 0, HELP_OPT },
00069 {0, 0, 0, 0}
00070 } ;
00071
00072 static int dbg_level = XSH_DEBUG_LEVEL_MEDIUM ;
00073 static int Nx = 3, Ny =4, Nz = 3 ;
00074
00075 static int Help( void ) ;
00076
00077 static int Help( void )
00078 {
00079 printf( "Test 3D images handling\n" ) ;
00080 printf( "Options:\n" ) ;
00081 printf( " --nx=<size> : X size of images (default 3)" ) ;
00082 printf( " --ny=<size> : Y size of images (default 4)" ) ;
00083 printf( " --nz=<size> : X size of images (default 3)" ) ;
00084 printf( " --dbg=<level> : Set debug level. <level> is one of:\n" ) ;
00085 printf( " none, low, high, medium.\n" ) ;
00086 printf( " Default is medium.\n" ) ;
00087 return 0 ;
00088 }
00089
00090 static void HandleOptions( int argc, char **argv )
00091 {
00092 int opt ;
00093 int opt_idx = 0 ;
00094
00095 while (( opt = getopt_long( argc, argv, Options,
00096 LongOptions, &opt_idx )) != EOF )
00097 switch( opt ) {
00098 case NX_OPT:
00099 sscanf( optarg, "%d", &Nx ) ;
00100 break ;
00101 case NY_OPT:
00102 sscanf( optarg, "%d", &Ny ) ;
00103 break ;
00104 case NZ_OPT:
00105 sscanf( optarg, "%d", &Nz ) ;
00106 break ;
00107 case DBG_OPT:
00108 if ( strcmp( optarg, "none" ) == 0 ) dbg_level = XSH_DEBUG_LEVEL_NONE ;
00109 else if ( strcmp( optarg, "low" ) == 0 )
00110 dbg_level = XSH_DEBUG_LEVEL_LOW ;
00111 else if ( strcmp( optarg, "medium" ) == 0 )
00112 dbg_level = XSH_DEBUG_LEVEL_MEDIUM ;
00113 else if ( strcmp( optarg, "high" ) == 0 )
00114 dbg_level = XSH_DEBUG_LEVEL_HIGH ;
00115 break ;
00116 default:
00117 Help() ;
00118 exit( 0 ) ;
00119 }
00120 }
00121
00122 static void fill_image( cpl_image * img, int first )
00123 {
00124 int ix, iy ;
00125 int * pixels = NULL ;
00126
00127 XSH_ASSURE_NOT_NULL( img ) ;
00128
00129 check( pixels = cpl_image_get_data_int( img ) ) ;
00130 xsh_msg( "fill image with %d", first ) ;
00131
00132 for( iy = 0 ; iy < Ny ; iy++, first++ )
00133 for( ix = 0 ; ix < Nx ; ix++ ) {
00134 *(pixels + ix + iy*Nx) = first ;
00135 }
00136 cleanup:
00137 return ;
00138 }
00139
00140 static int compare_img_3d( xsh_image_3d * one, xsh_image_3d * two )
00141 {
00142 if ( one->nx != two->nx || one->ny != two->ny || one->nz != two->nz ) {
00143 xsh_msg( "Sizes not equal (nx: %d-%d, ny: %d-%d, nz: %d-%d",
00144 one->nx, two->nx, one->ny, two->ny, one->nz, two->nz ) ;
00145 return 1 ;
00146 }
00147 return 0 ;
00148 }
00149
00150 int main( int argc, char** argv)
00151 {
00152 int ret = 1 ;
00153 int iz ;
00154 cpl_image * img = NULL ;
00155 xsh_image_3d * img_3d = NULL ;
00156 xsh_image_3d * new_3d = NULL ;
00157 cpl_propertylist * header = NULL ;
00158 const char * img_name = "TEST_3D_IMAGE.fits" ;
00159
00160
00161 TESTS_INIT( MODULE_ID);
00162 cpl_msg_set_level( CPL_MSG_DEBUG);
00163 xsh_debug_level_set( XSH_DEBUG_LEVEL_MEDIUM) ;
00164
00165 HandleOptions( argc, argv ) ;
00166
00167
00168 check( img_3d = xsh_image_3d_new( Nx, Ny, Nz, CPL_TYPE_INT ) ) ;
00169 xsh_msg( "Img 3D: axis: %dx%dx%d, type: %d", img_3d->nx, img_3d->ny,
00170 img_3d->nz, img_3d->type ) ;
00171
00172
00173 for( iz = 0 ; iz < Nz ; iz++ ) {
00174 cpl_error_code err = CPL_ERROR_NONE ;
00175 char i_name[128] ;
00176
00177
00178 check( img = cpl_image_new( Nx, Ny, CPL_TYPE_INT ) ) ;
00179
00180 fill_image( img, iz*100 ) ;
00181 sprintf( i_name, "TEST_3D_IMG_%d.fits", iz ) ;
00182 cpl_image_save( img, i_name, CPL_BPP_32_SIGNED, NULL, CPL_IO_DEFAULT ) ;
00183
00184 err = xsh_image_3d_insert( img_3d, img, iz ) ;
00185 xsh_msg( " Insert Image err=%d", err ) ;
00186
00187 }
00188
00189
00190 check( xsh_image_3d_save(img_3d, img_name,
00191 header, CPL_IO_DEFAULT ) ) ;
00192 xsh_msg( "Image Saved" ) ;
00193
00194 ret = 0 ;
00195
00196
00197 new_3d = xsh_image_3d_load( img_name, CPL_TYPE_INT, 0 ) ;
00198 if( compare_img_3d( img_3d, new_3d ) != 0 ) {
00199 xsh_msg( "Finished BADLY" ) ;
00200 ret = 1 ;
00201 }
00202 else xsh_msg( "Finished OK" ) ;
00203
00204 cleanup:
00205 return ret ;
00206 }
00207