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  

mri_zeropad.c

Go to the documentation of this file.
00001 #include "mrilib.h"
00002 
00003 /*-----------------------------------------------------------------
00004    Pad a 2D image by adding/subtracting of zeros:
00005         nxbot = # to add on -x side (can be negative, to remove planes)
00006         nxtop = # to add on +x side, etc.
00007        If all n?bot,n?top values are zero, just returns a copy
00008        of the input.  If too much is cut off, or something else
00009        bad happens, returns NULL.
00010 
00011        Adapted from edt_volpad.c -- 26 Feb 2001 -- RWCox
00012 -------------------------------------------------------------------*/
00013 
00014 MRI_IMAGE * mri_zeropad_2D( int nxbot , int nxtop ,
00015                             int nybot , int nytop , MRI_IMAGE * fim )
00016 {
00017    int nxold,nyold , nxnew,nynew , nx,ny ;
00018    int ii,jj , iibot,iitop , jjbot,jjtop ;
00019    MRI_IMAGE * vim ;
00020 
00021 ENTRY("mri_zeropad_2D") ;
00022 
00023    /*- check for user stupidity -*/
00024 
00025    if( fim == NULL ) RETURN(NULL) ;
00026 
00027    nx = fim->nx ; ny = fim->ny ;
00028 
00029    /*- special case: just copy input -*/
00030 
00031    if( nxbot == 0 && nybot == 0 &&
00032        nxtop == 0 && nytop == 0    ){
00033 
00034       vim = mri_copy( fim ) ;
00035       RETURN(vim) ;
00036    }
00037 
00038    nxold = nx ; nxnew = nxold + nxbot + nxtop ;  /* dimensions */
00039    nyold = ny ; nynew = nyold + nybot + nytop ;
00040 
00041    iibot = MAX(0,-nxbot) ; iitop = MIN(nxold,nxold+nxtop) ;  /* range of data  */
00042    jjbot = MAX(0,-nybot) ; jjtop = MIN(nyold,nyold+nytop) ;  /* in old dataset */
00043 
00044    if( nxnew < 1 || iibot >= iitop ||   /* check for reasonable sizes */
00045        nynew < 1 || jjbot >= jjtop   ){ /* and ranges of dataset     */
00046 
00047       fprintf(stderr,"*** mri_zeropad: can't cut image down to nothing!\n") ;
00048       RETURN(NULL) ;
00049    }
00050 
00051    vim = mri_new( nxnew , nynew , fim->kind ) ;
00052    MRI_COPY_AUX(vim,fim) ;
00053    memset( mri_data_pointer(vim) , 0 ,
00054            nxnew*nynew*mri_datum_size(vim->kind) ) ;
00055 
00056    /* macros for computing 1D subscripts from 2D indices */
00057 
00058 #undef  SNEW  /* in case was defined in some stupid .h file */
00059 #undef  SOLD
00060 #define SNEW(i,j) ((i+nxbot)+(j+nybot)*nxnew)
00061 #define SOLD(i,j) (i+j*nxold)
00062 
00063    switch( fim->kind ){  /* copy rows of old into new */
00064 
00065       default:
00066          fprintf(stderr,"*** mri_zeropad: unknown input datum=%d\n",fim->kind) ;
00067          mri_free(vim) ;
00068       RETURN(NULL) ;
00069 
00070       case MRI_byte:{
00071          byte * bnew = MRI_BYTE_PTR(vim), * bold = MRI_BYTE_PTR(fim) ;
00072          for( jj=jjbot ; jj < jjtop ; jj++ )
00073             for( ii=iibot ; ii < iitop ; ii++ )
00074                bnew[SNEW(ii,jj)] = bold[SOLD(ii,jj)] ;
00075       }
00076       break ;
00077 
00078       case MRI_rgb:{
00079          byte * bnew = MRI_RGB_PTR(vim), * bold = MRI_RGB_PTR(fim) ;
00080          for( jj=jjbot ; jj < jjtop ; jj++ )
00081             for( ii=iibot ; ii < iitop ; ii++ ){
00082                bnew[3*SNEW(ii,jj)  ] = bold[3*SOLD(ii,jj)  ] ;
00083                bnew[3*SNEW(ii,jj)+1] = bold[3*SOLD(ii,jj)+1] ;
00084                bnew[3*SNEW(ii,jj)+2] = bold[3*SOLD(ii,jj)+2] ;
00085             }
00086       }
00087       break ;
00088 
00089       case MRI_short:{
00090          short * bnew = MRI_SHORT_PTR(vim), * bold = MRI_SHORT_PTR(fim) ;
00091          for( jj=jjbot ; jj < jjtop ; jj++ )
00092             for( ii=iibot ; ii < iitop ; ii++ )
00093                bnew[SNEW(ii,jj)] = bold[SOLD(ii,jj)] ;
00094       }
00095       break ;
00096 
00097       case MRI_int:{
00098          int * bnew = MRI_INT_PTR(vim), * bold = MRI_INT_PTR(fim) ;
00099          for( jj=jjbot ; jj < jjtop ; jj++ )
00100             for( ii=iibot ; ii < iitop ; ii++ )
00101                bnew[SNEW(ii,jj)] = bold[SOLD(ii,jj)] ;
00102       }
00103       break ;
00104 
00105       case MRI_float:{
00106          float * bnew = MRI_FLOAT_PTR(vim), * bold = MRI_FLOAT_PTR(fim) ;
00107          for( jj=jjbot ; jj < jjtop ; jj++ )
00108             for( ii=iibot ; ii < iitop ; ii++ )
00109                bnew[SNEW(ii,jj)] = bold[SOLD(ii,jj)] ;
00110       }
00111       break ;
00112 
00113       case MRI_double:{
00114          double * bnew = MRI_DOUBLE_PTR(vim), * bold = MRI_DOUBLE_PTR(fim) ;
00115          for( jj=jjbot ; jj < jjtop ; jj++ )
00116             for( ii=iibot ; ii < iitop ; ii++ )
00117                bnew[SNEW(ii,jj)] = bold[SOLD(ii,jj)] ;
00118       }
00119       break ;
00120 
00121       case MRI_complex:{
00122          complex * bnew = MRI_COMPLEX_PTR(vim), * bold = MRI_COMPLEX_PTR(fim) ;
00123          for( jj=jjbot ; jj < jjtop ; jj++ )
00124             for( ii=iibot ; ii < iitop ; ii++ )
00125                bnew[SNEW(ii,jj)] = bold[SOLD(ii,jj)] ;
00126       }
00127       break ;
00128 
00129    } /* end of switch on datum type */
00130 
00131    RETURN(vim) ;
00132 }
 

Powered by Plone

This site conforms to the following standards: