Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

from3d.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002    Major portions of this software are copyrighted by the Medical College
00003    of Wisconsin, 1994-2000, and are released under the Gnu General Public
00004    License, Version 2.  See the file README.Copyright for details.
00005 ******************************************************************************/
00006 
00007 /*---------------------------------------------------------------------------*/
00008 /*
00009   This program extracts 2D image files from an AFNI 3D dataset.
00010 
00011   File:    from3d.c
00012   Author:  B. Douglas Ward
00013   Date:    30 August 1996
00014 
00015   Mod:     Added call to AFNI_logger.
00016   Date:    15 August 2001
00017 
00018 */
00019 /*---------------------------------------------------------------------------*/
00020 
00021 #define PROGRAM_NAME "from3d"                        /* name of this program */
00022 #define PROGRAM_AUTHOR "B. Douglas Ward"                   /* program author */
00023 #define PROGRAM_INITIAL "30 August 1996"  /* date of initial program release */
00024 #define PROGRAM_LATEST "15 August 2001"     /* date of last program revision */
00025 
00026 /*---------------------------------------------------------------------------*/
00027 
00028 #include "mrilib.h"
00029 
00030 #define FatalError(str) \
00031    ( fprintf(stderr,"\nError: %s\n\r try 'from3d -help'\n",(str)) , exit(1) )
00032 
00033 /*---------------------------------------------------------------------------*/
00034 
00035 void Syntax(void)
00036 {
00037    printf(
00038     "Usage:   from3d [options] -input fname -prefix rname\n"
00039     "Purpose: Extract 2D image files from a 3D AFNI dataset.\n"
00040     "Options:\n"
00041     "-v             Print out verbose information during the run.\n"
00042     "-nsize         Adjust size of 2D data file to be NxN, by padding\n"
00043     "                 with zeros, where N is a power of 2.\n"
00044     "-raw           Write images in 'raw' format (just the data bytes)\n"
00045     "                 N.B.: there will be no header information saying\n"
00046     "                       what the image dimensions are - you'll have\n"
00047     "                       to get that information from the x and y\n"
00048     "                       axis information output by 3dinfo.\n"
00049     "-float         Write images as floats, no matter what they are in\n"
00050     "                 the dataset itself.\n"
00051     "-zfirst num    Set 'num' = number of first z slice to be extracted.\n"
00052     "                 (default = 1)\n"
00053     "-zlast num     Set 'num' = number of last z slice to be extracted.\n"
00054     "                 (default = largest)\n"
00055     "-tfirst num    Set 'num' = number of first time slice to be extracted.\n"
00056     "                 (default = 1)\n"
00057     "-tlast num     Set 'num' = number of last time slice to be extracted.\n"
00058     "                 (default = largest)\n"
00059     "-input fname   Read 3D dataset from file 'fname'.\n"
00060     "                 'fname' may include a sub-brick selector list.\n"
00061     "-prefix rname  Write 2D images using prefix 'rname'.\n"
00062     "\n"
00063     "               (-input and -prefix are non-optional options: they)\n"
00064     "               (must be present or the program will not execute. )\n"
00065     "\n"
00066     "N.B.: * Image data is extracted directly from the dataset bricks.\n"
00067     "         If a brick has a floating point scaling factor, it will NOT\n"
00068     "         be applied.\n"
00069     "      * Images are extracted parallel to the xy-plane of the dataset\n"
00070     "         orientation (which can be determined by program 3dinfo).\n"
00071     "         This is the order in which the images were input to the\n"
00072     "         dataset originally, via to3d.\n"
00073     "      * If either of these conditions is unacceptable, you can also\n"
00074     "         try to use the Save:bkg function from an AFNI image window.\n"
00075    ) ;
00076    exit(0) ;
00077 }
00078 
00079 /*---------------------------------------------------------------------------*/
00080 
00081 void F3D_initialize_user_data ( int Argc, char * Argv[],
00082    Boolean * verbose, Boolean * nsize, Boolean * raw, Boolean * do_floats ,
00083    int * zfirst, int * zlast, int * tfirst, int * tlast,
00084    char * input_filename, char * prefix_filename )
00085 {
00086    const int BIGNUMBER = 100000;
00087    int nopt;
00088    float ftemp;
00089 
00090    /*----- Does user request help menu? -----*/
00091    if (Argc < 2 || strncmp(Argv[1],"-help",4) == 0) Syntax();
00092 
00093    /*----- Add to program log -----*/
00094    machdep(); AFNI_logger (PROGRAM_NAME,Argc,Argv); 
00095 
00096    /* --- set default values --- */
00097    *verbose = FALSE;
00098    *nsize = FALSE;
00099    *raw   = FALSE;      /* 05 Jan 2000 */
00100    *do_floats = FALSE ; /* 05 Jan 2000 */
00101    *zfirst = 1;
00102    *zlast = BIGNUMBER;
00103    *tfirst = 1;
00104    *tlast = BIGNUMBER;
00105    strcpy(input_filename, "");
00106    strcpy(prefix_filename, "");
00107 
00108 
00109    /* --- scan options --- */
00110    nopt = 1 ;
00111    while ( nopt < Argc && Argv[nopt][0] == '-' )
00112    {
00113 
00114       /* --- verbose option --- */
00115       if ( strncmp(Argv[nopt],"-v",2) == 0 )
00116       {
00117          *verbose = TRUE;
00118          nopt++ ;
00119          continue;
00120       }
00121 
00122       /* --- nsize option --- */
00123       if ( strncmp(Argv[nopt],"-nsize",4) == 0 )
00124       {
00125          *nsize = TRUE;
00126          nopt++ ;
00127          continue;
00128       }
00129 
00130       /* --- raw option [05 Jan 2000]--- */
00131       if ( strncmp(Argv[nopt],"-raw",4) == 0 )
00132       {
00133          *raw = TRUE;
00134          nopt++ ;
00135          continue;
00136       }
00137 
00138       /* --- float option [05 Jan 2000]--- */
00139       if ( strncmp(Argv[nopt],"-float",4) == 0 )
00140       {
00141          *do_floats = TRUE;
00142          nopt++ ;
00143          continue;
00144       }
00145 
00146       /* --- zfirst option --- */
00147       if ( strncmp(Argv[nopt],"-zfirst",4) == 0 )
00148       {
00149          if( ++nopt >= Argc ) FatalError("-zfirst needs an argument") ;
00150          ftemp = strtod( Argv[nopt] , NULL ) ;
00151          *zfirst = (int) ftemp ;
00152          nopt++ ;
00153          continue ;
00154       }
00155 
00156       /* --- zlast option --- */
00157       if ( strncmp(Argv[nopt],"-zlast",4) == 0 )
00158       {
00159          if( ++nopt >= Argc ) FatalError("-zlast needs an argument") ;
00160          ftemp = strtod( Argv[nopt] , NULL ) ;
00161          *zlast = (int) ftemp ;
00162          nopt++ ;
00163          continue ;
00164       }
00165 
00166       /* --- tfirst option --- */
00167       if ( strncmp(Argv[nopt],"-tfirst",4) == 0 )
00168       {
00169          if( ++nopt >= Argc ) FatalError("-tfirst needs an argument") ;
00170          ftemp = strtod( Argv[nopt] , NULL ) ;
00171          *tfirst = (int) ftemp ;
00172          nopt++ ;
00173          continue ;
00174       }
00175 
00176       /* --- tlast option --- */
00177       if ( strncmp(Argv[nopt],"-tlast",4) == 0 )
00178       {
00179          if( ++nopt >= Argc ) FatalError("-tlast needs an argument") ;
00180          ftemp = strtod( Argv[nopt] , NULL ) ;
00181          *tlast = (int) ftemp ;
00182          nopt++ ;
00183          continue ;
00184       }
00185 
00186       /* --- input file name --- */
00187       if ( strncmp(Argv[nopt],"-input",4) == 0 )
00188       {
00189          if ( ++nopt >= Argc ) FatalError("-input needs a name") ;
00190          strcpy ( input_filename , Argv[nopt] ) ;
00191          nopt++ ; continue ;
00192       }
00193 
00194       /* --- prefix name --- */
00195       if ( strncmp(Argv[nopt],"-prefix",4) == 0 )
00196       {
00197          if ( ++nopt >= Argc ) FatalError("-prefix needs a name") ;
00198          strcpy ( prefix_filename , Argv[nopt] ) ;
00199          nopt++ ; continue ;
00200       }
00201 
00202       /* --- exception --- */
00203       FatalError ("Illegal input");
00204 
00205    }  /* nopt */
00206 
00207    /* --- check for valid inputs --- */
00208    if (*zfirst > *zlast)
00209       FatalError ("Cannot have zfirst > zlast");
00210    if (*tfirst > *tlast)
00211       FatalError ("Cannot have tfirst > tlast");
00212    if (!strcmp(input_filename,""))
00213       FatalError ("Must specify input file name. ");
00214    if (!strcmp(prefix_filename,""))
00215       FatalError ("Must specify prefix file name.");
00216 
00217    return;
00218 }
00219 
00220 /*---------------------------------------------------------------------------*/
00221 
00222 int main( int argc , char * argv[] )
00223 {
00224    /* --- variable declarations --- */
00225    THD_3dim_dataset * dset ;
00226    THD_diskptr * dskptr;
00227    int nx, ny, nz, nv;
00228    Boolean verbose, nsize, raw, do_floats;
00229    int ok;
00230    MRI_IMAGE * im, * im2d, * tim2d , * fim2d ;
00231    MRI_TYPE kind;
00232    int ibr, iz, count;
00233    int zfirst, zlast, tfirst, tlast;
00234    char input_filename[THD_MAX_NAME],
00235         prefix_filename[THD_MAX_NAME],
00236         output_filename[THD_MAX_NAME],
00237         str[THD_MAX_NAME];
00238 
00239   
00240   /*----- Identify software -----*/
00241   printf ("\n\n");
00242   printf ("Program:          %s \n", PROGRAM_NAME);
00243   printf ("Author:           %s \n", PROGRAM_AUTHOR); 
00244   printf ("Initial Release:  %s \n", PROGRAM_INITIAL);
00245   printf ("Latest Revision:  %s \n", PROGRAM_LATEST);
00246   printf ("\n");
00247 
00248    /* --- get user command line inputs --- */
00249    F3D_initialize_user_data (argc, argv,
00250       &verbose, &nsize,&raw,&do_floats,
00251       &zfirst, &zlast, &tfirst, &tlast,
00252       input_filename, prefix_filename );
00253 
00254    /* --- open 3D dataset --- */
00255    dset = THD_open_dataset( input_filename ) ;
00256    if( dset == NULL )  FatalError ("Unable to open input file") ;
00257    if ( verbose )  printf("\n" "3D Dataset File:    %s\n" , input_filename ) ;
00258 
00259    /* --- load data block --- */
00260    ok = THD_load_datablock( dset->dblk );
00261    if ( !ok )  FatalError ("Unable to load data block") ;
00262 
00263    /* --- get data dimensions --- */
00264    dskptr = dset->dblk->diskptr;
00265    nx = dskptr->dimsizes[0];
00266    ny = dskptr->dimsizes[1];
00267    nz = dskptr->dimsizes[2];
00268    nv = dskptr->nvals;
00269    if ( verbose )
00270       printf ("nx = %d  ny = %d  nz = %d  nv = %d\n",   nx, ny, nz, nv);
00271 
00272    /* --- check for valid user inputs --- */
00273    if (zfirst < 1) zfirst = 1;
00274    if (zlast > nz) zlast = nz;
00275    if (tfirst < 1) tfirst = 1;
00276    if (tlast > nv) tlast = nv;
00277    if (zfirst > nz)  FatalError ("No data selected -- zfirst too large.");
00278    if (zlast < 1)    FatalError ("No data selected -- zlast too small.");
00279    if (tfirst > nv)  FatalError ("No data selected -- tfirst too large.");
00280    if (tlast < 1)    FatalError ("No data selected -- tlast too small.");
00281 
00282    /* --- get data type --- */
00283    kind = IMAGE_IN_IMARR ( dset->dblk->brick, 0 ) -> kind;
00284    if ( verbose ){
00285       printf ("Input data type : %s\n", MRI_TYPE_name[kind]);
00286       if( do_floats && kind != MRI_float )
00287          printf ("Output data type: float\n") ;
00288    }
00289 
00290    /* --- create 2D data pointer --- */
00291    im2d = mri_new_vol_empty ( nx, ny, 1, kind );
00292 
00293    count = 0;
00294    for ( ibr = tfirst-1 ; ibr < tlast ; ibr++ )
00295    {
00296       for ( iz = zfirst-1 ; iz < zlast ; iz++ )
00297       {
00298          /* --- set 2D data pointer into 3D dataset --- */
00299          im = IMAGE_IN_IMARR ( dset->dblk->brick, ibr );
00300          switch ( kind )
00301          {
00302             case MRI_byte :
00303                im2d->im.byte_data = im->im.byte_data + iz * nx * ny ;
00304             break;
00305             case MRI_short :
00306                im2d->im.short_data = im->im.short_data + iz * nx * ny ;
00307             break;
00308             case MRI_int :
00309                im2d->im.int_data = im->im.int_data + iz * nx * ny ;
00310             break;
00311             case MRI_float :
00312                im2d->im.float_data = im->im.float_data + iz * nx * ny ;
00313             break;
00314             case MRI_double :
00315                im2d->im.double_data = im->im.double_data + iz * nx * ny ;
00316             break;
00317             case MRI_complex :
00318                im2d->im.complex_data = im->im.complex_data + iz * nx * ny ;
00319             break;
00320             case MRI_rgb :
00321                im2d->im.rgb_data = im->im.rgb_data + iz * nx * ny ;
00322             break;
00323             default :
00324                FatalError ("Illegal data type encountered.");
00325          }
00326 
00327          /* --- create 2D data file name --- */
00328          strcpy ( output_filename, prefix_filename );
00329          if ( nv > 1 )
00330             sprintf ( str, "%02d.%04d", iz+1, ibr+1 );
00331          else
00332             if ( nz > 999 )
00333                sprintf ( str, ".%04d", iz+1 );
00334             else
00335                sprintf ( str, ".%03d", iz+1 );
00336          strcat ( output_filename, str );
00337 
00338          /* --- write 2D data file --- */
00339          if ( verbose )
00340             printf ( "Writing%s2D image: %s\n",
00341                       (raw) ? " raw " : " " , output_filename );
00342 
00343          fim2d = (nsize) ? mri_nsize(im2d) : im2d ;
00344 
00345          if( do_floats ){
00346             tim2d = mri_to_float(fim2d) ;
00347             if( fim2d != im2d ) mri_free(fim2d) ;
00348             fim2d = tim2d ;
00349          }
00350 
00351          ok = (raw)? mri_write_raw( output_filename, fim2d )
00352                    : mri_write    ( output_filename, fim2d ) ;
00353 
00354          if( fim2d != im2d ) mri_free(fim2d) ;
00355 
00356          count += ok ;
00357 
00358       }  /* --- iz --- */
00359    }  /* --- ibr --- */
00360 
00361    if ( verbose )  printf ("Created %d 2D image files.\n", count);
00362 
00363    /* --- clean up --- */
00364    free ( im2d );
00365    THD_delete_3dim_dataset( dset , False ) ;
00366 
00367    exit(0) ;
00368 }
 

Powered by Plone

This site conforms to the following standards: