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  

thd_makemask.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 #include "mrilib.h"
00008 
00009 /*---------------------------------------------------------------------*/
00010 /*! Make a byte mask from mask dataset:
00011      miv = sub-brick of input
00012      if( mask_bot <= mask_top ) then
00013        only nonzero values in this range will be used
00014      else
00015        all nonzero values in the mask will be used
00016    The input dataset should be byte-, short-, or float-valued.
00017 
00018    The output is a byte array with 1s in "hit" locations and 0s in
00019    other locations.  The number of bytes is DSET_NVOX(mask_dset).
00020    This array should be free()-d someday.  If NULL is returned,
00021    some grotesque error transpired.
00022 -----------------------------------------------------------------------*/
00023 
00024 byte * THD_makemask( THD_3dim_dataset *mask_dset ,
00025                      int miv , float mask_bot , float mask_top )
00026 {
00027    byte *mmm = NULL ;
00028    int nvox , ii ;
00029 
00030    if( !ISVALID_DSET(mask_dset)    ||
00031        miv < 0                     ||
00032        miv >= DSET_NVALS(mask_dset)  ) return NULL ;
00033 
00034    nvox = DSET_NVOX(mask_dset) ;
00035 
00036    DSET_load(mask_dset) ; if( !DSET_LOADED(mask_dset) ) return NULL ;
00037 
00038    mmm = (byte *) calloc( sizeof(byte) * nvox , 1 ) ;
00039 
00040    switch( DSET_BRICK_TYPE(mask_dset,miv) ){
00041       default:
00042          free(mmm) ; DSET_unload(mask_dset) ; return NULL ;
00043 
00044       case MRI_short:{
00045          short mbot , mtop ;
00046          short *mar = (short *) DSET_ARRAY(mask_dset,miv) ;
00047          float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ;
00048          if( mfac == 0.0 ) mfac = 1.0 ;
00049          if( mask_bot <= mask_top ){
00050             mbot = SHORTIZE(mask_bot/mfac) ;
00051             mtop = SHORTIZE(mask_top/mfac) ;
00052          } else {
00053             mbot = (short) -MRI_TYPE_maxval[MRI_short] ;
00054             mtop = (short)  MRI_TYPE_maxval[MRI_short] ;
00055          }
00056          for( ii=0 ; ii < nvox ; ii++ )
00057             if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ) mmm[ii]=1;
00058       }
00059       break ;
00060 
00061       case MRI_byte:{
00062          byte mbot , mtop ;
00063          byte *mar = (byte *) DSET_ARRAY(mask_dset,miv) ;
00064          float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ;
00065          if( mfac == 0.0 ) mfac = 1.0 ;
00066          if( mask_bot <= mask_top && mask_top > 0.0 ){
00067             mbot = BYTEIZE(mask_bot/mfac) ;
00068             mtop = BYTEIZE(mask_top/mfac) ;
00069          } else {
00070             mbot = 0 ;
00071             mtop = (byte) MRI_TYPE_maxval[MRI_short] ;
00072          }
00073          for( ii=0 ; ii < nvox ; ii++ )
00074             if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ) mmm[ii]=1;
00075       }
00076       break ;
00077 
00078       case MRI_float:{
00079          float mbot , mtop ;
00080          float *mar = (float *) DSET_ARRAY(mask_dset,miv) ;
00081          float mfac = DSET_BRICK_FACTOR(mask_dset,miv) ;
00082          if( mfac == 0.0 ) mfac = 1.0 ;
00083          if( mask_bot <= mask_top ){
00084             mbot = (float) (mask_bot/mfac) ;
00085             mtop = (float) (mask_top/mfac) ;
00086          } else {
00087             mbot = -WAY_BIG ;
00088             mtop =  WAY_BIG ;
00089          }
00090          for( ii=0 ; ii < nvox ; ii++ )
00091             if( mar[ii] >= mbot && mar[ii] <= mtop && mar[ii] != 0 ) mmm[ii]=1;
00092       }
00093       break ;
00094    }
00095 
00096    return mmm ;
00097 }
00098 
00099 /*---------------------------------------------------------------------*/
00100 /*! Count the number of nonzero voxels in a mask.
00101 -----------------------------------------------------------------------*/
00102 
00103 int THD_countmask( int nvox , byte *mmm )
00104 {
00105    int ii,mc ;
00106 
00107    if( nvox <= 0 || mmm == NULL ) return 0 ;
00108 
00109    for( ii=mc=0 ; ii < nvox ; ii++ ) if( mmm[ii] ) mc++ ;
00110 
00111    return mc ;
00112 }
 

Powered by Plone

This site conforms to the following standards: