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_quantize.c File Reference

#include "mrilib.h"

Go to the source code of this file.


Defines

#define RGB_TO_INT(r, g, b)   ((((int)(r))<<16) | (((int)(g))<< 8) | ((int)(b)))
#define INT_TO_RRR(i)   (((i) & 0xff0000) >> 16)
#define INT_TO_GGG(i)   (((i) & 0xff00) >> 8)
#define INT_TO_BBB(i)   ( (i) & 0xff)
#define RGB_MASK   RGB_TO_INT( 0xff , 0xff , 0xff )
#define RRR_MASK   RGB_TO_INT( 0xff , 0x00 , 0x00 )
#define GGG_MASK   RGB_TO_INT( 0x00 , 0xff , 0x00 )
#define BBB_MASK   RGB_TO_INT( 0x00 , 0x00 , 0xff )
#define MAXCOL   32767
#define QS_CUTOFF   40
#define QS_STACK   4096
#define QS_SWAP(x, y)   (temp=(x), (x)=(y),(y)=temp)
#define ILESS(a, b)   ( ((a)&smask) < ((b)&smask) )
#define IMORE(a, b)   ( ((a)&smask) > ((b)&smask) )
#define IEQUAL(a, b)   ( ((a)&smask) == ((b)&smask) )

Functions

MRI_IMAGEmri_quantize (int newcolors, MRI_IMAGE *im)
int main (int argc, char *argv[])
void qsort_int (int, int *, int)
void isort_int (int n, int *ar, int smask)
void qsrec_int (int n, int *ar, int cutoff, int smask)

Variables

int stack [QS_STACK]

Define Documentation

#define BBB_MASK   RGB_TO_INT( 0x00 , 0x00 , 0xff )
 

Definition at line 27 of file mri_quantize.c.

#define GGG_MASK   RGB_TO_INT( 0x00 , 0xff , 0x00 )
 

Definition at line 26 of file mri_quantize.c.

#define IEQUAL a,
b       ( ((a)&smask) == ((b)&smask) )
 

Definition at line 120 of file mri_quantize.c.

#define ILESS a,
b       ( ((a)&smask) < ((b)&smask) )
 

Definition at line 118 of file mri_quantize.c.

Referenced by isort_int(), and qsrec_int().

#define IMORE a,
b       ( ((a)&smask) > ((b)&smask) )
 

Definition at line 119 of file mri_quantize.c.

Referenced by qsrec_int().

#define INT_TO_BBB i       ( (i) & 0xff)
 

Definition at line 22 of file mri_quantize.c.

#define INT_TO_GGG i       (((i) & 0xff00) >> 8)
 

Definition at line 21 of file mri_quantize.c.

#define INT_TO_RRR i       (((i) & 0xff0000) >> 16)
 

Definition at line 20 of file mri_quantize.c.

#define MAXCOL   32767
 

Definition at line 29 of file mri_quantize.c.

Referenced by mri_quantize().

#define QS_CUTOFF   40
 

make color histogram *

Definition at line 109 of file mri_quantize.c.

Referenced by qsort_int().

#define QS_STACK   4096
 

Definition at line 110 of file mri_quantize.c.

#define QS_SWAP x,
y       (temp=(x), (x)=(y),(y)=temp)
 

Definition at line 111 of file mri_quantize.c.

#define RGB_MASK   RGB_TO_INT( 0xff , 0xff , 0xff )
 

Definition at line 24 of file mri_quantize.c.

Referenced by mri_quantize().

#define RGB_TO_INT r,
g,
b       ((((int)(r))<<16) | (((int)(g))<< 8) | ((int)(b)))
 

Definition at line 19 of file mri_quantize.c.

Referenced by mri_quantize().

#define RRR_MASK   RGB_TO_INT( 0xff , 0x00 , 0x00 )
 

Definition at line 25 of file mri_quantize.c.


Function Documentation

void isort_int int    n,
int *    ar,
int    smask
 

Definition at line 122 of file mri_quantize.c.

References a, ILESS, and p.

Referenced by qsort_int().

00123 {
00124    register int  j , p ;  /* array indices */
00125    register int temp ;  /* a[j] holding place */
00126    register int * a = ar ;
00127 
00128    if( n < 2 || ar == NULL ) return ;
00129 
00130    for( j=1 ; j < n ; j++ ){
00131 
00132      if( ILESS(a[j],a[j-1]) ){  /* out of order */
00133         p    = j ;
00134         temp = a[j] ;
00135         do{
00136           a[p] = a[p-1] ;       /* at this point, a[p-1] > temp, so move it up */
00137           p-- ;
00138         } while( p > 0 && ILESS(temp,a[p-1]) ) ;
00139         a[p] = temp ;           /* finally, put temp in its place */
00140      }
00141    }
00142    return ;
00143 }

int main int    argc,
char *    argv[]
 

\** File : SUMA.c

Author:
: Ziad Saad Date : Thu Dec 27 16:21:01 EST 2001
Purpose :

Input paramters :

Parameters:
param  Usage : SUMA ( )
Returns :
Returns:
Support :
See also:
OpenGL prog. Guide 3rd edition , varray.c from book's sample code
Side effects :

Definition at line 11 of file mri_quantize.c.

References argc, mri_quantize(), and mri_read_ppm().

00012 {
00013    MRI_IMAGE * fred ;
00014    fred = mri_read_ppm( "fred.pnm" ) ;
00015    mri_quantize( 100 , fred ) ;
00016    exit(0) ;
00017 }

MRI_IMAGE * mri_quantize int    newcolors,
MRI_IMAGE   im
 

Definition at line 33 of file mri_quantize.c.

References color, MRI_IMAGE::kind, malloc, MAXCOL, mri_free(), MRI_INT_PTR, mri_new_conforming, MRI_RGB_PTR, ncol, MRI_IMAGE::nvox, qsort_int(), RGB_MASK, and RGB_TO_INT.

Referenced by main().

00034 {
00035    MRI_IMAGE * intim , * outim ;
00036    int * intar ;
00037    byte * inar , * outar ;
00038    byte mask ;
00039    int ii , ncol , imask , smask ;
00040    int * color , * count ;
00041 
00042    /** sanity checks **/
00043 
00044    if( im == NULL             || newcolors < 2      ||   
00045       MRI_RGB_PTR(im) == NULL || im->kind != MRI_rgb  ) return NULL ;
00046 
00047    /** make copy of input image as ints **/
00048 
00049    inar  = MRI_RGB_PTR(im) ;
00050    intim = mri_new_conforming( im , MRI_int ) ;
00051    intar = MRI_INT_PTR(intim) ;
00052 
00053    for( ii=0 ; ii < im->nvox ; ii++ )
00054       intar[ii] = RGB_TO_INT( inar[3*ii] , inar[3*ii+1] , inar[3*ii+2] ) ;
00055 
00056    /** sort copy, count unique colors **/
00057 
00058    qsort_int( im->nvox , intar , RGB_MASK ) ;
00059    ncol = 1 ;
00060    for( ii=1 ; ii < im->nvox ; ii++ )
00061       if( intar[ii] != intar[ii-1] ) ncol++ ;
00062 
00063    /** if too many colors, get rid of some by masking out low order bits **/
00064 
00065 fprintf(stderr,"%d colors in input image\n",ncol) ;
00066 
00067    imask = 0 ;
00068    while( ncol > MAXCOL ){
00069       imask++ ; mask = (byte)( 256 - (1<<imask) ) ;
00070       smask = RGB_TO_INT( mask , mask , mask ) ;
00071 
00072       for( ii=0 ; ii < im->nvox ; ii++ )
00073 #if 0
00074          intar[ii] &= smask ;
00075 #else
00076          intar[ii] = RGB_TO_INT( inar[3*ii]   & mask ,
00077                                  inar[3*ii+1] & mask , inar[3*ii+2] & mask ) ;
00078 #endif
00079 
00080       qsort_int( im->nvox , intar , RGB_MASK ) ;
00081       ncol = 1 ;
00082       for( ii=1 ; ii < im->nvox ; ii++ )
00083          if( intar[ii] != intar[ii-1] ) ncol++ ;
00084 
00085 fprintf(stderr,"%d colors in input image with mask=%d\n",ncol,(int)mask) ;
00086    }
00087 
00088    /** make color histogram **/
00089 
00090    color = (int *) malloc( sizeof(int) * ncol ) ;
00091    count = (int *) malloc( sizeof(int) * ncol ) ;
00092 
00093    color[0] = intar[0] ; count[0] = 1 ; ncol = 0 ;
00094    for( ii=1 ; ii < im->nvox ; ii++ ){
00095       if( intar[ii] != intar[ii-1] ){
00096          ncol++ ;
00097          color[ncol] = intar[ii] ;
00098          count[ncol] = 1 ;
00099       } else {
00100          count[ncol]++ ;
00101       }
00102    }
00103    ncol++ ;
00104    mri_free( intim ) ;
00105 }

void qsort_int int   ,
int *   ,
int   
 

Definition at line 212 of file mri_quantize.c.

References a, isort_int(), QS_CUTOFF, and qsrec_int().

00213 {
00214    qsrec_int( n , a , QS_CUTOFF , smask ) ;
00215    isort_int( n , a , smask ) ;
00216    return ;
00217 }

void qsrec_int int    n,
int *    ar,
int    cutoff,
int    smask
 

Definition at line 147 of file mri_quantize.c.

References a, i, ILESS, IMORE, left, QS_SWAP, right, and stack.

Referenced by qsort_int().

00148 {
00149    register int i , j ;         /* scanning indices */
00150    register int temp , pivot ;  /* holding places */
00151    register int * a = ar ;
00152 
00153    int left , right , mst ;
00154 
00155    /* return if too short (insertion sort will clean up) */
00156 
00157    if( cutoff < 3 ) cutoff = 3 ;
00158    if( n < cutoff || ar == NULL ) return ;
00159 
00160    /* initialize stack to start with whole array */
00161 
00162    stack[0] = 0   ;
00163    stack[1] = n-1 ;
00164    mst      = 2   ;
00165 
00166    /* loop while the stack is nonempty */
00167 
00168    while( mst > 0 ){
00169       right = stack[--mst] ;  /* work on subarray from left -> right */
00170       left  = stack[--mst] ;
00171 
00172       i = ( left + right ) / 2 ;           /* middle of subarray */
00173 
00174       /*----- sort the left, middle, and right a[]'s -----*/
00175 
00176       if( IMORE(a[left],a[i]    ) ) QS_SWAP( a[left]  , a[i]     ) ;
00177       if( IMORE(a[left],a[right]) ) QS_SWAP( a[left]  , a[right] ) ;
00178       if( IMORE(a[i]   ,a[right]) ) QS_SWAP( a[right] , a[i]     ) ;
00179 
00180       pivot = a[i] ;                       /* a[i] is the median-of-3 pivot! */
00181       a[i]  = a[right] ;
00182 
00183       i = left ; j = right ;               /* initialize scanning */
00184 
00185       /*----- partition:  move elements bigger than pivot up and elements
00186                           smaller than pivot down, scanning in from ends -----*/
00187 
00188       do{
00189         for( ; ILESS(a[++i],pivot) ; ) ;  /* scan i up,   until a[i] >= pivot */
00190         for( ; IMORE(a[--j],pivot) ; ) ;  /* scan j down, until a[j] <= pivot */
00191 
00192         if( j <= i ) break ;         /* if j meets i, quit */
00193 
00194         QS_SWAP( a[i] , a[j] ) ;
00195       } while( 1 ) ;
00196 
00197       /*----- at this point, the array is partitioned -----*/
00198 
00199       a[right] = a[i] ; a[i] = pivot ;  /* restore the pivot */
00200 
00201       /*----- signal the subarrays that need further work -----*/
00202 
00203       if( (i-left)  > cutoff ){ stack[mst++] = left ; stack[mst++] = i-1   ; }
00204       if( (right-i) > cutoff ){ stack[mst++] = i+1  ; stack[mst++] = right ; }
00205 
00206    }  /* end of while stack is non-empty */
00207    return ;
00208 }

Variable Documentation

int stack[QS_STACK] [static]
 

Definition at line 113 of file mri_quantize.c.

Referenced by qsrec_int().

 

Powered by Plone

This site conforms to the following standards: