Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
imcutup.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "mrilib.h"
00008
00009 #define XYNUM 0
00010 #define YXNUM 1
00011 #define XDOTYNUM 2
00012 #define YDOTXNUM 3
00013
00014 int main( int argc , char * argv[] )
00015 {
00016 MRI_IMARR * imar ;
00017 MRI_IMAGE * im ;
00018 char prefix[240] = "cutup." , fnam[256] ;
00019 int iarg , ii,jj , nx,ny , nxim,nyim ;
00020 int nmode = XYNUM ;
00021
00022 if( argc < 4 ){
00023 printf("Usage: imcutup [options] nx ny fname1\n"
00024 "Breaks up larger images into smaller image files of size\n"
00025 "nx by ny pixels. Intended as an aid to using image files\n"
00026 "which have been catenated to make one big 2D image.\n"
00027 "OPTIONS:\n"
00028 " -prefix ppp = Prefix the output files with string 'ppp'\n"
00029 " -xynum = Number the output images in x-first, then y [default]\n"
00030 " -yxnum = Number the output images in y-first, then x\n"
00031 " -x.ynum = 2D numbering, x.y format\n"
00032 " -y.xnum = 2D numbering, y.x format\n"
00033 "For example:\n"
00034 " imcutup -prefix Fred 64 64 3D:-1:0:256:128:1:zork.im\n"
00035 "will break up the big 256 by 128 image in file zork.im\n"
00036 "into 8 images, each 64 by 64. The output filenames would be\n"
00037 " -xynum => Fred.001 Fred.002 Fred.003 Fred.004\n"
00038 " Fred.005 Fred.006 Fred.007 Fred.008\n"
00039 "\n"
00040 " -yxnum => Fred.001 Fred.003 Fred.005 Fred.007\n"
00041 " Fred.002 Fred.004 Fred.006 Fred.008\n"
00042 "\n"
00043 " -x.ynum => Fred.001.001 Fred.002.001 Fred.003.001 Fred.004.001\n"
00044 " Fred.001.002 Fred.002.002 Fred.003.002 Fred.004.002\n"
00045 "\n"
00046 " -y.xnum => Fred.001.001 Fred.001.002 Fred.001.003 Fred.001.004\n"
00047 " Fred.002.001 Fred.002.002 Fred.002.003 Fred.002.004\n"
00048 "\n"
00049 "You may want to look at the input image file with\n"
00050 " afni -im fname [then open the Sagittal image window]\n"
00051 "before deciding on what to do with the image file.\n"
00052 "\n"
00053 "N.B.: the file specification 'fname' must result in a single\n"
00054 " input 2D image - multiple images can't be cut up in one\n"
00055 " call to this program.\n"
00056 ) ;
00057 exit(0) ;
00058 }
00059
00060 machdep() ;
00061
00062 iarg = 1 ;
00063
00064 while( iarg < argc && argv[iarg][0] == '-' ){
00065
00066 if( strcmp(argv[iarg],"-xynum") == 0 ){
00067 nmode = XYNUM ; iarg++ ; continue ;
00068 }
00069
00070 if( strcmp(argv[iarg],"-yxnum") == 0 ){
00071 nmode = YXNUM ; iarg++ ; continue ;
00072 }
00073
00074 if( strcmp(argv[iarg],"-x.ynum") == 0 ){
00075 nmode = XDOTYNUM ; iarg++ ; continue ;
00076 }
00077
00078 if( strcmp(argv[iarg],"-y.xnum") == 0 ){
00079 nmode = YDOTXNUM ; iarg++ ; continue ;
00080 }
00081
00082 if( strcmp(argv[iarg],"-prefix") == 0 ){
00083 MCW_strncpy( prefix , argv[++iarg] , 240 ) ;
00084 ii = strlen(prefix) ;
00085 if( prefix[ii-1] != '.' ) strcat(prefix,".") ;
00086 iarg++ ; continue ;
00087 }
00088
00089 fprintf(stderr,"*** ERROR: illegal option %s\n",argv[iarg]) ;
00090 exit(1) ;
00091 }
00092
00093 if( iarg+3 > argc ){
00094 fprintf(stderr,"*** ERROR: not enough arguments!\n"); exit(1);
00095 }
00096
00097 nx = (int) strtod( argv[iarg++] , NULL ) ;
00098 ny = (int) strtod( argv[iarg++] , NULL ) ;
00099 if( nx < 1 || ny < 1 ){
00100 fprintf(stderr,"*** ERROR: illegal values nx=%d ny=%d\n",nx,ny);
00101 exit(1) ;
00102 }
00103
00104 im = mri_read_just_one( argv[iarg] ) ;
00105 if( im == NULL ){
00106 fprintf(stderr,
00107 "*** ERROR: file %s doesn't have exactly 1 image!\n",
00108 argv[iarg]) ;
00109 exit(1) ;
00110 }
00111
00112 nxim = im->nx / nx ;
00113 nyim = im->ny / ny ;
00114 if( nxim < 1 || nyim < 1 ){
00115 fprintf(stderr,"*** ERROR: image is too small for nx=%d ny=%d\n",
00116 nx,ny) ;
00117 exit(1) ;
00118 }
00119
00120 imar = mri_uncat2D( nx,ny , im ) ;
00121 if( imar == NULL || IMARR_COUNT(imar) < nxim*nyim ){
00122 fprintf(stderr,"*** ERROR: unknown error in mri_uncat2D()\n");
00123 exit(1) ;
00124 }
00125
00126 mri_free(im) ;
00127
00128 for( jj=0 ; jj < nyim ; jj++ ){
00129 for( ii=0 ; ii < nxim ; ii++ ){
00130 im = IMARR_SUBIMAGE(imar,ii+jj*nxim) ;
00131 switch( nmode ){
00132 default:
00133 case XYNUM:
00134 sprintf(fnam,"%s%03d",prefix,ii+jj*nxim+1) ; break ;
00135
00136 case YXNUM:
00137 sprintf(fnam,"%s%03d",prefix,jj+ii*nyim+1) ; break ;
00138
00139 case XDOTYNUM:
00140 sprintf(fnam,"%s%03d.%03d",prefix,ii+1,jj+1) ; break ;
00141
00142 case YDOTXNUM:
00143 sprintf(fnam,"%s%03d.%03d",prefix,jj+1,ii+1) ; break ;
00144 }
00145 mri_write(fnam,im) ;
00146 }
00147 }
00148
00149 exit(0) ;
00150 }