예제 #1
0
파일: Xphace.c 프로젝트: Gilles86/afni
MRI_IMAGE * mri_scramble( MRI_IMAGE * ima , MRI_IMAGE * imb ,
                          float alpha     , float beta       )
{
   MRI_IMAGE * cxa, * cxb, * cxc ;
   int nx,ny , nxup,nyup ;

   if( ima == NULL        || imb == NULL        ||
       ima->nx != imb->nx || ima->ny != imb->ny ||
       alpha < 0.0        || alpha > 1.0        ||
       beta  < 0.0        || beta  > 1.0          ) return NULL ;

   cxa = mri_fft2D( ima , -1 ) ;
   cxb = mri_fft2D( imb , -1 ) ;
   cxc = cx_scramble( cxa,cxb,alpha,beta ) ;
   mri_free(cxa) ; mri_free(cxb) ;
   cxa = mri_fft2D( cxc , 1 ) ;
   mri_free(cxc) ;
   cxb = mri_to_mri( ima->kind , cxa ) ;
   mri_free(cxa) ;

   if( cxb->nx > ima->nx || cxb->ny > ima->ny ){
      cxc = mri_cut_2D( cxb , 0,ima->nx-1,0,ima->ny-1 ) ;
      mri_free(cxb) ; cxb = cxc ;
   }

   return cxb ;
}
예제 #2
0
파일: Xphace.c 프로젝트: Gilles86/afni
static void * PH_popup_image( void * handle , MRI_IMAGE * im )
{
   PLUGIN_impopper * imp = (PLUGIN_impopper *) handle ;

   /*-- input image is NULL ==> popdown, if applicable --*/

   if( im == NULL ){
      if( imp != NULL )
         drive_MCW_imseq( imp->seq , isqDR_destroy , NULL ) ;

      return ((void *) imp) ;
   }

   /*-- input = no popper handle ==> create one --*/

   if( imp == NULL ){
      imp      = XtNew(PLUGIN_impopper) ;
      imp->seq = NULL ; imp->im  = NULL ;
   }

   /*-- input = non-null image ==> replace image --*/

   mri_free( imp->im ) ;                   /* toss old copy */
   imp->im = mri_to_mri( im->kind , im ) ; /* make new copy */

   /*-- input = inactive popper handle ==> activate it --*/

   if( imp->seq == NULL ){
      imp->seq = open_MCW_imseq( MAIN_dc ,
                                 PLUGIN_imseq_getim , (XtPointer) imp ) ;

      drive_MCW_imseq( imp->seq , isqDR_realize, NULL ) ;
      drive_MCW_imseq( imp->seq , isqDR_onoffwid , (XtPointer) isqDR_offwid ) ;

      XtVaSetValues( imp->seq->wtop ,
                       XmNmwmDecorations , MWM_DECOR_BORDER | MWM_DECOR_TITLE | MWM_DECOR_MENU ,
                       XmNmwmFunctions   , MWM_FUNC_MOVE | MWM_FUNC_CLOSE ,
                       XmNtitle          , "Xphace Images" ,
                     NULL ) ;

   }

   drive_MCW_imseq( imp->seq , isqDR_clearstat , NULL ) ;
   drive_MCW_imseq( imp->seq , isqDR_reimage , (XtPointer) 0 ) ;

   return ((void *) imp) ;
}
예제 #3
0
파일: Xphace.c 프로젝트: Gilles86/afni
XtPointer PLUGIN_imseq_getim( int n , int type , XtPointer handle )
{
   PLUGIN_impopper * imp = (PLUGIN_impopper *) handle ;

   if( imp == NULL ) return(NULL) ;

   /*--- control info ---*/

   if( type == isqCR_getstatus ){
      MCW_imseq_status * stat = XtNew( MCW_imseq_status ) ;
      stat->num_total  = 1 ;
      stat->num_series = 1 ;
      stat->send_CB    = PLUGIN_seq_send_CB ;
      stat->parent     = (XtPointer) imp  ;
      stat->aux        = NULL ;

      stat->transforms0D = NULL ;
      stat->transforms2D = NULL ;
      stat->slice_proj   = NULL ;

      return((XtPointer) stat) ;
   }

   /*--- no overlay ---*/

   if( type == isqCR_getoverlay ) return(NULL) ;

   /*--- return a copy of the image
         (since the imseq will delete it when it is done) ---*/

   if( type == isqCR_getimage || type == isqCR_getqimage ){
      MRI_IMAGE * im = NULL ;
      if( imp->im != NULL ) im = mri_to_mri( imp->im->kind , imp->im ) ;
      return((XtPointer) im) ;
   }

   return(NULL) ;  /* should not occur, but who knows? */
}
예제 #4
0
파일: imstack.c 프로젝트: Gilles86/afni
int main( int argc , char * argv[] )
{
   char prefix[255]="obi-wan-kenobi" , fname[255] ;
   int datum = -1 ;
   int iarg = 1 ;
   int gnim , nx , ny , nz , kz ;
   char ** gname ;
   MRI_IMARR * arr ;
   MRI_IMAGE * im , * qim ;
   FILE * fp ;

   /***** help? *****/

   if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
      printf(
       "Usage: imstack [options] image_filenames ...\n"
       "Stacks up a set of 2D images into one big file (a la MGH).\n"
       "Options:\n"
       "  -datum type   Converts the output data file to be 'type',\n"
       "                  which is either 'short' or 'float'.\n"
       "                  The default type is the type of the first image.\n"
       "  -prefix name  Names the output files to be 'name'.b'type' and 'name'.hdr.\n"
       "                  The default name is 'obi-wan-kenobi'.\n"
      ) ;

      exit(0) ;
   }

   machdep() ;

   /***** scan for option *****/

   while( iarg < argc && argv[iarg][0] == '-' ){

      /*** -datum ***/

      if( strcmp(argv[iarg],"-datum") == 0 ){
         if( ++iarg >= argc ){
            fprintf(stderr,"-datum needs a type!\n") ; exit(1) ;
         }
         if( strcmp(argv[iarg],"short") == 0 ){
            datum = MRI_short ;
         } else if( strcmp(argv[iarg],"float") == 0 ){
            datum = MRI_float ;
         } else {
            fprintf(stderr,"-datum %s is illegal!\n",argv[iarg]) ; exit(1) ;
         }
         iarg++ ; continue ;
      }

      /*** -prefix ***/

      if( strcmp(argv[iarg],"-prefix") == 0 ){
         if( ++iarg >= argc ){
            fprintf(stderr,"-prefix needs a name!\n") ; exit(1) ;
         }
         strcpy(prefix,argv[iarg]) ;
         iarg++ ; continue ;
      }

      /*** ERROR ***/

      fprintf(stderr,"Unrecognized option: %s\n",argv[iarg]) ; exit(1) ;
   }

   /***** Check if any filenames left *****/

   if( iarg >= argc ){
      fprintf(stderr,"No input image filenames?!\n") ; exit(1) ;
   }

   /***** Perform filename expansion on the input list *****/

   MCW_warn_expand(1) ;
   MCW_file_expand( argc - iarg , argv + iarg , &gnim , &gname ) ;
   MCW_warn_expand(0) ;

   if( gnim < 1 ){
      fprintf(stderr,"Filename expansion fails on input filenames?!\n") ; exit(1) ;
   }

   /***** Read all files *****/

   arr = mri_read_many_files( gnim , gname ) ;
   if( arr == NULL || IMARR_COUNT(arr) <= 0 ){
      fprintf(stderr,"Can't read input files?!\n") ; exit(1) ;
   }
   MCW_free_expand( gnim , gname ) ;
   fprintf(stderr,"Read in %d 2D slices\n",IMARR_COUNT(arr)) ;

   /***** Set output datum, if not already fixed *****/

   if( datum < 0 ){
      datum = IMARR_SUBIMAGE(arr,0)->kind ;

      if( datum != MRI_short && datum != MRI_float ){
         fprintf(stderr,"Input image type is %s -- you must use -datum!\n",
                 MRI_TYPE_name[datum]) ;
         exit(1) ;
      }
   }

   /***** Check images for equal sizes *****/

   nx = IMARR_SUBIMAGE(arr,0)->nx ;
   ny = IMARR_SUBIMAGE(arr,0)->ny ;
   nz = IMARR_COUNT(arr) ;

   for( kz=1 ; kz < nz ; kz++ ){
      if( IMARR_SUBIMAGE(arr,kz)->nx != nx ||
          IMARR_SUBIMAGE(arr,kz)->ny != ny   ){

         fprintf(stderr,"All images must be the same size (%d x %d)\n",nx,ny) ;
         exit(1) ;
      }
   }

   /***** Write the output brick *****/

   sprintf(fname,"%s.b%s",prefix,MRI_TYPE_name[datum]) ;
   fp = fopen( fname , "w" ) ;
   if( fp == NULL ){
      fprintf(stderr,"Can't open output file %s\n",fname) ; exit(1) ;
   }

   for( kz=0 ; kz < nz ; kz++ ){

      im = IMARR_SUBIMAGE(arr,kz) ;
      if( im->kind != datum ) qim = mri_to_mri( datum , im ) ;
      else                    qim = im ;

      fwrite( mri_data_pointer(qim) , qim->pixel_size , qim->nx * qim->ny , fp ) ;

      if( qim != im ) mri_free(qim) ;
      mri_free(im);
   }
   fclose( fp ) ;
   fprintf(stderr,"Wrote output brick %s\n",fname) ;

   /***** Write the output header *****/

   sprintf(fname,"%s.hdr",prefix) ;
   fp = fopen( fname , "w" ) ;
   if( fp == NULL ){
      fprintf(stderr,"Can't open output file %s\n",fname) ; exit(1) ;
   }

   fprintf( fp , "%d %d %d 0\n" , nx,ny,nz ) ;
   fclose(fp) ;
   fprintf(stderr,"Wrote output header %s: %d %d %d\n",fname,nx,ny,nz) ;

   exit(0) ;
}
예제 #5
0
MRI_IMAGE * mri_dup2D( int nup , MRI_IMAGE *imin )
{
   MRI_IMAGE *flim , *newim ;
   float     *flar , *newar , *cold , *cnew ;
   int nx,ny , nxup,nyup , ii,jj,kk, NNmode = 0 ;

ENTRY("mri_dup2D") ;
   /*-- sanity checks --*/

   if( nup < 1 || imin == NULL ) RETURN( NULL );

   if( nup == 1 ){ newim = mri_to_mri( imin->kind, imin ); RETURN(newim); }

   /* does the user want neighbor interpolation?     22 Feb 2004 [rickr] */
   if ( AFNI_yesenv("AFNI_IMAGE_ZOOM_NN") ) {
      mri_dup2D_mode(0);
      NNmode = 1;
   }

   /*-- complex-valued images: do each part separately --*/

   if( imin->kind == MRI_complex ){
      MRI_IMARR *impair ; MRI_IMAGE *rim, *iim, *tim ;

      impair = mri_complex_to_pair( imin ) ;
      if( impair == NULL ){
         fprintf(stderr,"*** mri_complex_to_pair fails in mri_dup2D!\n"); EXIT(1);
      }
      rim = IMAGE_IN_IMARR(impair,0) ;
      iim = IMAGE_IN_IMARR(impair,1) ;  FREE_IMARR(impair) ;
      tim = mri_dup2D( nup, rim ); mri_free( rim ); rim = tim ;
      tim = mri_dup2D( nup, iim ); mri_free( iim ); iim = tim ;
      newim = mri_pair_to_complex( rim , iim ) ;
      mri_free( rim ) ; mri_free( iim ) ;
      MRI_COPY_AUX(newim,imin) ;
      RETURN(newim) ;
   }

   /*-- 14 Mar 2002: RGB image up by 2..4, all colors at once --*/

   if( imin->kind == MRI_rgb ){
     MRI_IMAGE *qqim=NULL ;
     if ( NNmode )
         qqim = mri_dup2D_rgb_NN(imin, nup);  /* 22 Feb 2004 [rickr] */
     else {
       switch(nup){
         case 4: qqim = mri_dup2D_rgb4(imin); break; /* special purpose fast codes */
         case 3: qqim = mri_dup2D_rgb3(imin); break; /* using fixed pt arithmetic  */
         case 2: qqim = mri_dup2D_rgb2(imin); break;

        /*-- other factors: do each color separately as a byte image --*/
        default:{
           MRI_IMARR *imtriple ; MRI_IMAGE *rim, *gim, *bim, *tim ;

           imtriple = mri_rgb_to_3byte( imin ) ;
           if( imtriple == NULL ){
             fprintf(stderr,"*** mri_rgb_to_3byte fails in mri_dup2D!\n"); RETURN(NULL);
           }
           rim = IMAGE_IN_IMARR(imtriple,0) ;
           gim = IMAGE_IN_IMARR(imtriple,1) ;
           bim = IMAGE_IN_IMARR(imtriple,2) ; FREE_IMARR(imtriple) ;
           tim = mri_dup2D( nup, rim ); mri_free(rim); rim = tim;
           tim = mri_dup2D( nup, gim ); mri_free(gim); gim = tim;
           tim = mri_dup2D( nup, bim ); mri_free(bim); bim = tim;
           newim = mri_3to_rgb( rim, gim, bim ) ;
           mri_free(rim) ; mri_free(gim) ; mri_free(bim) ;
           MRI_COPY_AUX(newim,imin) ;
           qqim = newim ;
         }
         break ;
       }
     }

     RETURN(qqim) ;
   }

   /*-- Special case: byte-valued image upsampled by 2/3/4 [13 Mar 2002] --*/

   if( imin->kind == MRI_byte && nup <= 4 ){
     void (*usbyte)(int,byte *,byte *) = NULL ;
     byte *bar=MRI_BYTE_PTR(imin) , *bnew , *cold, *cnew ;
     nx = imin->nx; ny = imin->ny; nxup = nx*nup; nyup = ny*nup ;
     newim = mri_new( nxup,nyup , MRI_byte ); bnew = MRI_BYTE_PTR(newim);
     switch( nup ){
       case 2: usbyte = upsample_1by2 ; break ;  /* special fast codes */
       case 3: usbyte = upsample_1by3 ; break ;
       case 4: usbyte = upsample_1by4 ; break ;
     }
     for( jj=0 ; jj < ny ; jj++ )                      /* upsample rows */
       usbyte( nx , bar+jj*nx , bnew+jj*nxup ) ;
     cold = (byte *) malloc( sizeof(byte) * ny   ) ;
     cnew = (byte *) malloc( sizeof(byte) * nyup ) ;
     for( ii=0 ; ii < nxup ; ii++ ){                   /* upsample cols */
       for( jj=0 ; jj < ny ; jj++ ) cold[jj] = bnew[ii+jj*nxup] ;
       usbyte( ny , cold , cnew ) ;
       for( jj=0 ; jj < nyup ; jj++ ) bnew[ii+jj*nxup] = cnew[jj] ;
     }
     free(cold); free(cnew); MRI_COPY_AUX(newim,imin); RETURN(newim);
   }

   /*-- otherwise, make sure we operate on a float image --*/

   if( imin->kind == MRI_float ) flim = imin ;
   else                          flim = mri_to_float( imin ) ;

   flar = MRI_FLOAT_PTR(flim) ;

   nx = flim->nx ; ny = flim->ny ; nxup = nx*nup ; nyup = ny*nup ;
   newim = mri_new( nxup , nyup , MRI_float ) ;
   newar = MRI_FLOAT_PTR(newim) ;

   /*-- upsample rows --*/

   for( jj=0 ; jj < ny ; jj++ )
      usammer( nup , nx , flar + jj*nx , newar + jj*nxup ) ;

   if( flim != imin ) mri_free(flim) ;

   /*-- upsample columns --*/

   cold = (float *) malloc( sizeof(float) * ny ) ;
   cnew = (float *) malloc( sizeof(float) * nyup ) ;
   if( cold == NULL || cnew == NULL ){
      fprintf(stderr,"*** mri_dup2D malloc failure!\n"); EXIT(1);
   }

   for( ii=0 ; ii < nxup ; ii++ ){
      for( jj=0 ; jj < ny ; jj++ ) cold[jj] = newar[ii + jj*nxup] ;
      usammer( nup , ny , cold , cnew ) ;
      for( jj=0 ; jj < nyup ; jj++ ) newar[ii+jj*nxup] = cnew[jj] ;
   }

   free(cold) ; free(cnew) ;

   /*-- type convert output, if necessary --*/

   switch( imin->kind ){

      default: break ;

      case MRI_byte:{
         byte * bar ; MRI_IMAGE * bim ; float fmin , fmax ;

         bim = mri_new( nxup,nyup , MRI_byte ) ; bar = MRI_BYTE_PTR(bim) ;
         fmin = mri_min(imin) ; fmax = mri_max(imin) ;
         for( ii=0 ; ii < newim->nvox ; ii++ )
            bar[ii] =  (newar[ii] < fmin) ? fmin
                     : (newar[ii] > fmax) ? fmax : newar[ii] ;
         mri_free(newim) ; newim = bim ;
      }
      break ;

      case MRI_short:{
         short * sar ; MRI_IMAGE * sim ; float fmin , fmax ;

         sim = mri_new( nxup,nyup , MRI_short ) ; sar = MRI_SHORT_PTR(sim) ;
         fmin = mri_min(imin) ; fmax = mri_max(imin) ;
         for( ii=0 ; ii < newim->nvox ; ii++ )
            sar[ii] =  (newar[ii] < fmin) ? fmin
                     : (newar[ii] > fmax) ? fmax : newar[ii] ;
         mri_free(newim) ; newim = sim ;
      }
      break ;

      case MRI_float:{
         float fmin , fmax ;

         fmin = mri_min(imin) ; fmax = mri_max(imin) ;
         for( ii=0 ; ii < newim->nvox ; ii++ )
                 if( newar[ii] < fmin ) newar[ii] = fmin ;
            else if( newar[ii] > fmax ) newar[ii] = fmax ;
      }
   }

   /*-- finito --*/

   MRI_COPY_AUX(newim,imin) ;
   RETURN( newim );
}
예제 #6
0
MRI_IMAGE * mri_cat2D(  int mx , int my , int gap , 
                        void *gapval , MRI_IMARR *imar )
{
   int nx , ny , ii , jj , kind , ij , nxout , nyout , ijoff , jout,iout ;
   MRI_IMAGE *imout , *imin=NULL ;
   void *vout ;

ENTRY("mri_cat2D") ;

   /*--- sanity checks ---*/

   if(   mx < 1 || my < 1 || imar == NULL || 
         (!OK_wrap && imar->num < mx*my) ) 
      RETURN( NULL );   
   if( gap < 0 || (gap > 0 && gapval == NULL) )                RETURN( NULL );

   for( ij=0 ; ij < mx*my ; ij++ ){     /* find first non-empty image */
      imin = IMARR_SUBIMAGE(imar,ij%imar->num) ;
      if( imin != NULL ) break ;
   }
   if( ij == mx*my ) RETURN( NULL );      /* all are empty! */

   kind = imin->kind ;
   nx   = imin->nx ;
   ny   = imin->ny ;


   if( mx==1 && my==1 ){                    /* 1x1 case (shouldn't happen) */
      imout = mri_to_mri( kind , imin ) ;   /* Just copy input to output   */
      RETURN( imout );
   }


   for( ij=0 ; ij < mx*my ; ij++ ){     /* check for consistency */
      imin = IMARR_SUBIMAGE(imar,ij%imar->num) ;
      if( imin != NULL &&
          (imin->kind != kind || imin->nx != nx || imin->ny != ny) )
         RETURN( NULL );
   }

   nxout = mx * nx + (mx-1) * gap ;
   nyout = my * ny + (my-1) * gap ;
   imout = mri_new( nxout , nyout , kind ) ;
   vout  = mri_data_pointer( imout ) ;


   ij = 0 ;
   for( jj=0 ; jj < my ; jj++ ){            /* loop over rows */
      for( ii=0 ; ii < mx ; ii++ , ij++ ){  /* loop over columns */

         if (WrapZero && ij >= imar->num) imin = NULL;
         else imin  = IMARR_SUBIMAGE(imar,ij%imar->num) ;
         ijoff = ii * (nx+gap) + jj * (ny+gap) * nxout ;

         /*--- NULL image ==> fill this spot with zeroes ---*/

         if( imin == NULL || mri_data_pointer(imin) == NULL ){
            switch( kind ){
               case MRI_byte:{
                  byte *pout = ((byte *) vout);
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     (void) memset( pout+ijoff , 0 , sizeof(byte)*nx ) ;
               } break ;

               case MRI_rgb:{                       /* 11 Feb 1999 */
                  byte *pout = ((byte *) vout);
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     (void) memset( pout+(3*ijoff) , 0 , sizeof(byte)*(3*nx) ) ;
               } break ;

               case MRI_short:{
                  short *pout = ((short *) vout);
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     (void) memset( pout+ijoff , 0 , sizeof(short)*nx ) ;
               } break ;

               case MRI_int:{
                  int *pout = ((int *) vout);
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     (void) memset( pout+ijoff , 0 , sizeof(int)*nx ) ;
               } break ;

               case MRI_float:{
                  float *pout = ((float *) vout);
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     for( iout=0 ; iout < nx ; iout++ )
                        pout[iout+ijoff] = 0 ;
               } break ;

               case MRI_double:{
                  double *pout = ((double *) vout);
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     for( iout=0 ; iout < nx ; iout++ )
                        pout[iout+ijoff] = 0 ;
               } break ;

               case MRI_complex:{
                  complex *pout = ((complex *) vout);
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     for( iout=0 ; iout < nx ; iout++ )
                        pout[iout+ijoff].r = pout[iout+ijoff].i = 0 ;
               } break ;
            }

         /*--- Copy input image data into place ---*/

         } else {
            switch( kind ){
               case MRI_byte:{
                  byte *pout = ((byte *) vout) ,
                       *pin  =  (byte *) MRI_BYTE_PTR(imin) ;
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     memcpy( pout+ijoff , pin , sizeof(byte)*nx ) , pin += nx ;
               } break ;

               case MRI_rgb:{                               /* 11 Feb 1999 */
                  byte *pout = ((byte *) vout) ,
                       *pin  =  (byte *) MRI_RGB_PTR(imin) ;
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     memcpy( pout+(3*ijoff) , pin , sizeof(byte)*(3*nx) ) , pin += 3*nx ;
               } break ;

               case MRI_short:{
                  short *pout = ((short *) vout) ,
                        *pin  =  (short *) MRI_SHORT_PTR(imin) ;
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     memcpy( pout+ijoff , pin , sizeof(short)*nx ) , pin += nx ;
               } break ;

               case MRI_int:{
                  int *pout = ((int *) vout) ,
                      *pin  =  (int *) MRI_INT_PTR(imin) ;
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     memcpy( pout+ijoff , pin , sizeof(int)*nx ) , pin += nx ;
               } break ;

               case MRI_float:{
                  float *pout = ((float *) vout) ,
                        *pin  =  (float *) MRI_FLOAT_PTR(imin) ;
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     memcpy( pout+ijoff , pin , sizeof(float)*nx ) , pin += nx ;
               } break ;

               case MRI_double:{
                  double *pout = ((double *) vout) ,
                         *pin  =  (double *) MRI_DOUBLE_PTR(imin) ;
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     memcpy( pout+ijoff , pin , sizeof(double)*nx ) , pin += nx ;
               } break ;

               case MRI_complex:{
                  complex *pout = ((complex *) vout) ,
                          *pin  =  (complex *) MRI_COMPLEX_PTR(imin) ;
                  for( jout=0 ; jout < ny ; jout++ , ijoff+=nxout )
                     memcpy( pout+ijoff , pin , sizeof(complex)*nx ) , pin += nx ;
               } break ;
            }
         }
      }
   }

   /*******************  Deal with the gaps  *******************/

   if( gap > 0 ){

      /**** put value into gap after each row ****/

      ii = nxout * gap ;
      for( jj=0 ; jj < my-1 ; jj++ ){
         ijoff = (ny + jj * (ny+gap)) * nxout ;
         switch( kind ){
            case MRI_byte:{
               byte gval = *((byte *)gapval) , *pout = ((byte *) vout) ;
               for( ij=0 ; ij < ii ; ij++ ) pout[ij+ijoff] = gval ;
            } break ;

            case MRI_rgb:{       /* 11 Feb 1999 */
               byte rval = *(((byte *)gapval)  ) ,
                    gval = *(((byte *)gapval)+1) ,
                    bval = *(((byte *)gapval)+2) , *pout = ((byte *) vout) ;

               for( ij=0 ; ij < ii ; ij++ ){
                  pout[3*(ij+ijoff)  ] = rval ;
                  pout[3*(ij+ijoff)+1] = gval ;
                  pout[3*(ij+ijoff)+2] = bval ;
               }
            } break ;

            case MRI_short:{
               short gval = *((short *)gapval) , *pout = ((short *) vout) ;
               for( ij=0 ; ij < ii ; ij++ ) pout[ij+ijoff] = gval ;
            } break ;

            case MRI_float:{
               float gval = *((float *)gapval) , *pout = ((float *) vout) ;
               for( ij=0 ; ij < ii ; ij++ ) pout[ij+ijoff] = gval ;
            } break ;

            case MRI_int:{
               int gval = *((int *)gapval) , *pout = ((int *) vout) ;
               for( ij=0 ; ij < ii ; ij++ ) pout[ij+ijoff] = gval ;
            } break ;

            case MRI_double:{
               double gval = *((double *)gapval) , *pout = ((double *) vout) ;
               for( ij=0 ; ij < ii ; ij++ ) pout[ij+ijoff] = gval ;
            } break ;

            case MRI_complex:{
               complex gval = *((complex *)gapval) , *pout = ((complex *) vout) ;
               for( ij=0 ; ij < ii ; ij++ ) pout[ij+ijoff] = gval ;
            } break ;
         }
      }

      /**** put value into gap after each column ****/

      for( ii=0 ; ii < mx-1 ; ii++ ){
         ijoff = nx + ii*(nx+gap) ;
         switch( kind ){
            case MRI_byte:{
               byte gval = *((byte *)gapval) , *pout = ((byte *) vout) ;
               for( ij=0 ; ij < gap ; ij++ , ijoff++ )
                  for( jj=0 ; jj < nyout ; jj++ ) pout[jj*nxout+ijoff] = gval ;
            } break ;

            case MRI_rgb:{              /* 11 Feb 1999 */
               byte rval = *(((byte *)gapval)  ) ,
                    gval = *(((byte *)gapval)+1) ,
                    bval = *(((byte *)gapval)+2) , *pout = ((byte *) vout) ;

               for( ij=0 ; ij < gap ; ij++ , ijoff++ )
                  for( jj=0 ; jj < nyout ; jj++ ){
                     pout[3*(jj*nxout+ijoff)  ] = rval ;
                     pout[3*(jj*nxout+ijoff)+1] = gval ;
                     pout[3*(jj*nxout+ijoff)+2] = bval ;
                  }
            } break ;

            case MRI_short:{
               short gval = *((short *)gapval) , *pout = ((short *) vout) ;
               for( ij=0 ; ij < gap ; ij++ , ijoff++ )
                  for( jj=0 ; jj < nyout ; jj++ ) pout[jj*nxout+ijoff] = gval ;
            } break ;

            case MRI_float:{
               float gval = *((float *)gapval) , *pout = ((float *) vout) ;
               for( ij=0 ; ij < gap ; ij++ , ijoff++ )
                  for( jj=0 ; jj < nyout ; jj++ ) pout[jj*nxout+ijoff] = gval ;
            } break ;

            case MRI_int:{
               int gval = *((int *)gapval) , *pout = ((int *) vout) ;
               for( ij=0 ; ij < gap ; ij++ , ijoff++ )
                  for( jj=0 ; jj < nyout ; jj++ ) pout[jj*nxout+ijoff] = gval ;
            } break ;

            case MRI_double:{
               double gval = *((double *)gapval) , *pout = ((double *) vout) ;
               for( ij=0 ; ij < gap ; ij++ , ijoff++ )
                  for( jj=0 ; jj < nyout ; jj++ ) pout[jj*nxout+ijoff] = gval ;
            } break ;

            case MRI_complex:{
               complex gval = *((complex *)gapval) , *pout = ((complex *) vout) ;
               for( ij=0 ; ij < gap ; ij++ , ijoff++ )
                  for( jj=0 ; jj < nyout ; jj++ ) pout[jj*nxout+ijoff] = gval ;
            } break ;
         }
      }
   }

   RETURN( imout );
}
예제 #7
0
파일: plug_imreg.c 프로젝트: ccraddock/afni
char * IMREG_main( PLUGIN_interface * plint )
{
    MCW_idcode * idc ;                          /* input dataset idcode */
    THD_3dim_dataset * old_dset , * new_dset ;  /* input and output datasets */
    char * new_prefix , * str ;                 /* strings from user */
    int base , ntime , datum , nx,ny,nz , ii,kk , npix ;
    float                      dx,dy,dz ;
    MRI_IMARR * ims_in , * ims_out ;
    MRI_IMAGE * im , * imbase ;

    byte   ** bptr = NULL , ** bout = NULL ;
    short  ** sptr = NULL , ** sout = NULL ;
    float  ** fptr = NULL , ** fout = NULL ;

    float * dxar = NULL , * dyar = NULL , * phiar = NULL ;

    /*--------------------------------------------------------------------*/
    /*----- Check inputs from AFNI to see if they are reasonable-ish -----*/

    /*--------- go to first input line ---------*/

    PLUTO_next_option(plint) ;

    idc      = PLUTO_get_idcode(plint) ;   /* get dataset item */
    old_dset = PLUTO_find_dset(idc) ;      /* get ptr to dataset */
    if( old_dset == NULL )
        return "*************************\n"
               "Cannot find Input Dataset\n"
               "*************************"  ;

    ntime = DSET_NUM_TIMES(old_dset) ;
    if( ntime < 2 )
        return "*****************************\n"
               "Dataset has only 1 time point\n"
               "*****************************"  ;

    ii = DSET_NVALS_PER_TIME(old_dset) ;
    if( ii > 1 )
        return "************************************\n"
               "Dataset has > 1 value per time point\n"
               "************************************"  ;

    nx = old_dset->daxes->nxx ;
    dx = old_dset->daxes->xxdel ;
    ny = old_dset->daxes->nyy ;
    dy = old_dset->daxes->yydel ;
    npix = nx*ny ;
    nz = old_dset->daxes->nzz ;
    dz = old_dset->daxes->zzdel ;

    if( nx != ny || fabs(dx) != fabs(dy) ) {

#ifdef IMREG_DEBUG
        fprintf(stderr,"\nIMREG: nx=%d ny=%d nz=%d  dx=%f dy=%f dz=%f\n",
                nx,ny,nz,dx,dy,dz ) ;
#endif

        return "***********************************\n"
               "Dataset does not have square slices\n"
               "***********************************"  ;
    }

    new_prefix = PLUTO_get_string(plint) ;   /* get string item (the output prefix) */
    if( ! PLUTO_prefix_ok(new_prefix) )      /* check if it is OK */
        return "************************\n"
               "Output Prefix is illegal\n"
               "************************"  ;

    /*--------- go to next input line ---------*/

    PLUTO_next_option(plint) ;

    base = PLUTO_get_number(plint) ;
    if( base >= ntime )
        return "********************\n"
               "Base value too large\n"
               "********************"  ;

    /*--------- see if the 3rd option line is present --------*/

    str = PLUTO_get_optiontag( plint ) ;
    if( str != NULL ) {
        float fsig , fdxy , fdph ;
        fsig = PLUTO_get_number(plint) * 0.42466090 ;
        fdxy = PLUTO_get_number(plint) ;
        fdph = PLUTO_get_number(plint) ;
        mri_align_params( 0 , 0.0,0.0,0.0 , fsig,fdxy,fdph ) ;
        /* fprintf(stderr,"Set fine params = %f %f %f\n",fsig,fdxy,fdph) ; */
    }

    /*------------- ready to compute new dataset -----------*/

#ifdef IMREG_DEBUG
    fprintf(stderr,"IMREG: loading dataset\n") ;
#endif

    DSET_load( old_dset ) ;

    /*** 1) Copy the dataset in toto ***/

#ifdef IMREG_DEBUG
    fprintf(stderr,"IMREG: Copying dataset\n") ;
#endif

    new_dset = PLUTO_copy_dset( old_dset , new_prefix ) ;
    if( new_dset == NULL )
        return "****************************\n"
               "Failed to copy input dataset\n"
               "****************************"  ;

    /*** 2) Make an array of empty images ***/

#ifdef IMREG_DEBUG
    fprintf(stderr,"IMREG: making empty images\n") ;
#endif

    datum = DSET_BRICK_TYPE(new_dset,0) ;

    INIT_IMARR(ims_in) ;
    for( ii=0 ; ii < ntime ; ii++ ) {
        im = mri_new_vol_empty( nx , ny , 1 , datum ) ;
        ADDTO_IMARR(ims_in,im) ;
    }

    imbase = mri_new_vol_empty( nx , ny , 1 , datum ) ;

    dxar  = (float *) malloc( sizeof(float) * ntime ) ;
    dyar  = (float *) malloc( sizeof(float) * ntime ) ;
    phiar = (float *) malloc( sizeof(float) * ntime ) ;

    /*** 3) Get pointers to sub-bricks in old and new datasets ***/

#ifdef IMREG_DEBUG
    fprintf(stderr,"IMREG: getting input brick pointers\n") ;
#endif

    switch( datum ) { /* pointer type depends on input datum type */
    case MRI_byte:
        bptr = (byte **) malloc( sizeof(byte *) * ntime ) ;
        bout = (byte **) malloc( sizeof(byte *) * ntime ) ;
        for( ii=0 ; ii < ntime ; ii++ ) {
            bptr[ii] = (byte *) DSET_ARRAY(old_dset,ii) ;
            bout[ii] = (byte *) DSET_ARRAY(new_dset,ii) ;
        }
        break ;

    case MRI_short:
        sptr = (short **) malloc( sizeof(short *) * ntime ) ;
        sout = (short **) malloc( sizeof(short *) * ntime ) ;
        for( ii=0 ; ii < ntime ; ii++ ) {
            sptr[ii] = (short *) DSET_ARRAY(old_dset,ii) ;
            sout[ii] = (short *) DSET_ARRAY(new_dset,ii) ;
        }

#ifdef IMREG_DEBUG
        fprintf(stderr,"IMREG: sptr[0] = %p  sout[0] = %p\n",sptr[0],sout[0]) ;
#endif

        break ;

    case MRI_float:
        fptr = (float **) malloc( sizeof(float *) * ntime ) ;
        fout = (float **) malloc( sizeof(float *) * ntime ) ;
        for( ii=0 ; ii < ntime ; ii++ ) {
            fptr[ii] = (float *) DSET_ARRAY(old_dset,ii) ;
            fout[ii] = (float *) DSET_ARRAY(new_dset,ii) ;
        }
        break ;
    }

    /*** 4) Loop over slices ***/

    PLUTO_popup_meter(plint) ;

    for( kk=0 ; kk < nz ; kk++ ) {

        /*** 4a) Setup ims_in images to point to input slices ***/

#ifdef IMREG_DEBUG
        fprintf(stderr,"IMREG: slice %d -- setup input images\n",kk) ;
#endif

        for( ii=0 ; ii < ntime ; ii++ ) {
            im = IMARR_SUBIMAGE(ims_in,ii) ;
            switch( datum ) {
            case MRI_byte:
                mri_fix_data_pointer( bptr[ii] + kk*npix, im ) ;
                break ;
            case MRI_short:
                mri_fix_data_pointer( sptr[ii] + kk*npix, im ) ;
                break ;
            case MRI_float:
                mri_fix_data_pointer( fptr[ii] + kk*npix, im ) ;
                break ;
            }
        }

        /*** 4b) Setup im to point to base image ***/

#ifdef IMREG_DEBUG
        fprintf(stderr,"IMREG: slice %d -- setup base image\n",kk) ;
#endif

        switch( datum ) {
        case MRI_byte:
            mri_fix_data_pointer( bptr[base] + kk*npix, imbase ) ;
            break ;
        case MRI_short:
            mri_fix_data_pointer( sptr[base] + kk*npix, imbase ) ;
            break ;
        case MRI_float:
            mri_fix_data_pointer( fptr[base] + kk*npix, imbase ) ;
            break ;
        }

        /*** 4c) Register this slice at all times ***/

#ifdef IMREG_DEBUG
        fprintf(stderr,"IMREG: slice %d -- register\n",kk) ;
#endif

        ims_out = mri_align_dfspace( imbase , NULL , ims_in ,
                                     ALIGN_REGISTER_CODE , dxar,dyar,phiar ) ;

        if( ims_out == NULL )
            fprintf(stderr,"IMREG: mri_align_dfspace return NULL\n") ;

        /*** 4d) Put the output back in on top of the input;
                 note that the output is always in MRI_float format ***/

#ifdef IMREG_DEBUG
        fprintf(stderr,"IMREG: slice %d -- put output back into dataset\n",kk) ;
#endif

        for( ii=0 ; ii < ntime ; ii++ ) {
            switch( datum ) {
            case MRI_byte:
                im = mri_to_mri( MRI_byte , IMARR_SUBIMAGE(ims_out,ii) ) ;
                memcpy( bout[ii] + kk*npix , MRI_BYTE_PTR(im) , sizeof(byte)*npix ) ;
                mri_free(im) ;
                break ;

            case MRI_short:
#ifdef IMREG_DEBUG
                if( ii==0 )fprintf(stderr,"IMREG: conversion to short at ii=%d\n",ii) ;
#endif

                im = mri_to_mri( MRI_short , IMARR_SUBIMAGE(ims_out,ii) ) ;

#ifdef IMREG_DEBUG
                if( ii==0 )fprintf(stderr,"IMREG: copying to %p from %p\n",sout[ii] + kk*npix,MRI_SHORT_PTR(im)) ;
#endif

                memcpy( sout[ii] + kk*npix , MRI_SHORT_PTR(im) , sizeof(short)*npix ) ;

#ifdef IMREG_DEBUG
                if( ii==0 )fprintf(stderr,"IMREG: freeing\n") ;
#endif

                mri_free(im) ;
                break ;

            case MRI_float:
                im = IMARR_SUBIMAGE(ims_out,ii) ;
                memcpy( fout[ii] + kk*npix , MRI_FLOAT_PTR(im) , sizeof(float)*npix ) ;
                break ;
            }
        }

        PLUTO_set_meter(plint, (100*(kk+1))/nz ) ;

        /*** 4e) Destroy the output images ***/

#ifdef IMREG_DEBUG
        fprintf(stderr,"IMREG: destroying aligned output\n") ;
#endif

        DESTROY_IMARR( ims_out ) ;
    }

    /*** 5) Destroy the empty images and other workspaces ***/

#ifdef IMREG_DEBUG
    fprintf(stderr,"IMREG: destroy workspaces\n") ;
#endif

    mri_clear_data_pointer(imbase) ;
    mri_free(imbase) ;
    for( ii=0 ; ii < ntime ; ii++ ) {
        im = IMARR_SUBIMAGE(ims_in,ii) ;
        mri_clear_data_pointer(im) ;
    }
    DESTROY_IMARR(ims_in) ;
    FREE_WORKSPACE ;

    /*------------- let AFNI know about the new dataset ------------*/

#ifdef IMREG_DEBUG
    fprintf(stderr,"IMREG: send result to AFNI\n") ;
#endif

    PLUTO_add_dset( plint , new_dset , DSET_ACTION_MAKE_CURRENT ) ;

    return NULL ;  /* null string returned means all was OK */
}