Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

bitlist.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- 00002 @COPYRIGHT : 00003 Copyright 1993,1994,1995 David MacDonald, 00004 McConnell Brain Imaging Centre, 00005 Montreal Neurological Institute, McGill University. 00006 Permission to use, copy, modify, and distribute this 00007 software and its documentation for any purpose and without 00008 fee is hereby granted, provided that the above copyright 00009 notice appear in all copies. The author and McGill University 00010 make no representations about the suitability of this 00011 software for any purpose. It is provided "as is" without 00012 express or implied warranty. 00013 ---------------------------------------------------------------------------- */ 00014 00015 #include <volume_io/internal_volume_io.h> 00016 #include <bicpl/data_structures.h> 00017 00018 #ifndef lint 00019 static char rcsid[] = "$Header: /software/source//libraries/bicpl/Data_structures/bitlist.c,v 1.12 2000/02/06 15:30:10 stever Exp $"; 00020 #endif 00021 00022 /* ----------------------------- MNI Header ----------------------------------- 00023 @NAME : create_bitlist 00024 @INPUT : n_bits 00025 @OUTPUT : bitlist 00026 @RETURNS : Status 00027 @DESCRIPTION: Creates a bitlist containing the required number of bits. 00028 @METHOD : 00029 @GLOBALS : 00030 @CALLS : 00031 @CREATED : David MacDonald 00032 @MODIFIED : 00033 ---------------------------------------------------------------------------- */ 00034 00035 public void create_bitlist( 00036 int n_bits, 00037 bitlist_struct *bitlist ) 00038 { 00039 bitlist->n_words = (n_bits + BITS_PER_BITLIST_WORD - 1) / 00040 (BITS_PER_BITLIST_WORD); 00041 00042 if( bitlist->n_words > 0 ) 00043 ALLOC( bitlist->bits, bitlist->n_words ); 00044 00045 zero_bitlist( bitlist ); 00046 } 00047 00048 /* ----------------------------- MNI Header ----------------------------------- 00049 @NAME : zero_bitlist 00050 @INPUT : bitlist 00051 @OUTPUT : 00052 @RETURNS : 00053 @DESCRIPTION: Sets all bits to zero (FALSE). 00054 @METHOD : 00055 @GLOBALS : 00056 @CALLS : 00057 @CREATED : David MacDonald 00058 @MODIFIED : 00059 ---------------------------------------------------------------------------- */ 00060 00061 public void zero_bitlist( 00062 bitlist_struct *bitlist ) 00063 { 00064 int i; 00065 00066 for_less( i, 0, bitlist->n_words ) 00067 bitlist->bits[i] = (bitlist_type) 0; 00068 } 00069 00070 /* ----------------------------- MNI Header ----------------------------------- 00071 @NAME : fill_bitlist 00072 @INPUT : bitlist 00073 @OUTPUT : 00074 @RETURNS : 00075 @DESCRIPTION: Sets all bits to 1 (TRUE). 00076 @METHOD : 00077 @GLOBALS : 00078 @CALLS : 00079 @CREATED : David MacDonald 00080 @MODIFIED : 00081 ---------------------------------------------------------------------------- */ 00082 00083 public void fill_bitlist( 00084 bitlist_struct *bitlist ) 00085 { 00086 int i; 00087 00088 for_less( i, 0, bitlist->n_words ) 00089 bitlist->bits[i] = (bitlist_type) -1; 00090 } 00091 00092 /* ----------------------------- MNI Header ----------------------------------- 00093 @NAME : set_bitlist_bit 00094 @INPUT : bitlist 00095 : bit_index 00096 : value 00097 @OUTPUT : 00098 @RETURNS : 00099 @DESCRIPTION: Sets the bit_index'th bit to the given value. 00100 @METHOD : 00101 @GLOBALS : 00102 @CALLS : 00103 @CREATED : David MacDonald 00104 @MODIFIED : 00105 ---------------------------------------------------------------------------- */ 00106 00107 public void set_bitlist_bit( 00108 bitlist_struct *bitlist, 00109 int bit_index, 00110 BOOLEAN value ) 00111 { 00112 int word_index, bit_sub_index; 00113 bitlist_type bit; 00114 00115 word_index = bit_index >> LOG_BITS_PER_BITLIST_WORD; 00116 bit_sub_index = bit_index & BITS_PER_BITLIST_WORD_MINUS_1; 00117 00118 bit = (bitlist_type) 1 << (bitlist_type) bit_sub_index; 00119 00120 if( value ) /* setting bit to 1 */ 00121 bitlist->bits[word_index] |= bit; 00122 else if( bitlist->bits[word_index] & bit ) /* setting bit to 0 */ 00123 bitlist->bits[word_index] ^= bit; 00124 } 00125 00126 /* ----------------------------- MNI Header ----------------------------------- 00127 @NAME : get_bitlist_bit 00128 @INPUT : bitlist 00129 : bit_index 00130 @OUTPUT : 00131 @RETURNS : TRUE or FALSE 00132 @DESCRIPTION: Returns the bit_index'th bit. 00133 @METHOD : 00134 @GLOBALS : 00135 @CALLS : 00136 @CREATED : David MacDonald 00137 @MODIFIED : 00138 ---------------------------------------------------------------------------- */ 00139 00140 public BOOLEAN get_bitlist_bit( 00141 bitlist_struct *bitlist, 00142 int bit_index ) 00143 { 00144 int word_index, bit_sub_index; 00145 bitlist_type word_bits; 00146 BOOLEAN value; 00147 00148 word_index = bit_index >> LOG_BITS_PER_BITLIST_WORD; 00149 bit_sub_index = bit_index & BITS_PER_BITLIST_WORD_MINUS_1; 00150 00151 word_bits = bitlist->bits[word_index]; 00152 00153 value = ((word_bits >> (bitlist_type) bit_sub_index) & 1) != 0; 00154 00155 return( value ); 00156 } 00157 00158 /* ----------------------------- MNI Header ----------------------------------- 00159 @NAME : delete_bitlist 00160 @INPUT : bitlist 00161 @OUTPUT : 00162 @RETURNS : Status 00163 @DESCRIPTION: Deletes the given bitlist. 00164 @METHOD : 00165 @GLOBALS : 00166 @CALLS : 00167 @CREATED : David MacDonald 00168 @MODIFIED : 00169 ---------------------------------------------------------------------------- */ 00170 00171 public void delete_bitlist( 00172 bitlist_struct *bitlist ) 00173 { 00174 if( bitlist->n_words > 0 ) 00175 { 00176 FREE( bitlist->bits ); 00177 } 00178 } 00179 00180 /* ############################### bitlist 3d ############################### */ 00181 00182 /* ----------------------------- MNI Header ----------------------------------- 00183 @NAME : create_bitlist_3d 00184 @INPUT : nx 00185 : ny 00186 : nz 00187 @OUTPUT : bitlist 00188 @RETURNS : Status 00189 @DESCRIPTION: Creates a 3D bitlist. 00190 @METHOD : 00191 @GLOBALS : 00192 @CALLS : 00193 @CREATED : David MacDonald 00194 @MODIFIED : 00195 ---------------------------------------------------------------------------- */ 00196 00197 public void create_bitlist_3d( 00198 int nx, 00199 int ny, 00200 int nz, 00201 bitlist_3d_struct *bitlist ) 00202 { 00203 bitlist->nx = nx; 00204 bitlist->ny = ny; 00205 bitlist->nz = nz; 00206 bitlist->n_z_words = (nz + BITS_PER_BITLIST_WORD - 1) / 00207 BITS_PER_BITLIST_WORD; 00208 00209 if( nx > 0 && ny > 0 && nz > 0 ) 00210 ALLOC3D( bitlist->bits, nx, ny, bitlist->n_z_words ); 00211 00212 zero_bitlist_3d( bitlist ); 00213 } 00214 00215 /* ----------------------------- MNI Header ----------------------------------- 00216 @NAME : zero_bitlist_3d 00217 @INPUT : bitlist 00218 @OUTPUT : 00219 @RETURNS : 00220 @DESCRIPTION: Sets all bits to 0 (FALSE). 00221 @METHOD : 00222 @GLOBALS : 00223 @CALLS : 00224 @CREATED : David MacDonald 00225 @MODIFIED : 00226 ---------------------------------------------------------------------------- */ 00227 00228 public void zero_bitlist_3d( 00229 bitlist_3d_struct *bitlist ) 00230 { 00231 int x, y, z; 00232 00233 for_less( x, 0, bitlist->nx ) 00234 { 00235 for_less( y, 0, bitlist->ny ) 00236 { 00237 for_less( z, 0, bitlist->n_z_words ) 00238 { 00239 bitlist->bits[x][y][z] = (bitlist_type) 0; 00240 } 00241 } 00242 } 00243 } 00244 00245 public void get_bitlist_3d_sizes( 00246 bitlist_3d_struct *bitlist, 00247 int *nx, 00248 int *ny, 00249 int *nz ) 00250 { 00251 *nx = bitlist->nx; 00252 *ny = bitlist->ny; 00253 *nz = bitlist->nz; 00254 } 00255 00256 /* ----------------------------- MNI Header ----------------------------------- 00257 @NAME : fill_bitlist_3d 00258 @INPUT : bitlist 00259 @OUTPUT : 00260 @RETURNS : 00261 @DESCRIPTION: Sets all bits to 1 (TRUE). 00262 @METHOD : 00263 @GLOBALS : 00264 @CALLS : 00265 @CREATED : David MacDonald 00266 @MODIFIED : 00267 ---------------------------------------------------------------------------- */ 00268 00269 public void fill_bitlist_3d( 00270 bitlist_3d_struct *bitlist ) 00271 { 00272 int x, y, z; 00273 00274 for_less( x, 0, bitlist->nx ) 00275 { 00276 for_less( y, 0, bitlist->ny ) 00277 { 00278 for_less( z, 0, bitlist->n_z_words ) 00279 { 00280 bitlist->bits[x][y][z] = (bitlist_type) -1; 00281 } 00282 } 00283 } 00284 } 00285 00286 /* ----------------------------- MNI Header ----------------------------------- 00287 @NAME : set_bitlist_bit_3d 00288 @INPUT : bitlist 00289 : x 00290 : y 00291 : z 00292 : value 00293 @OUTPUT : 00294 @RETURNS : 00295 @DESCRIPTION: Sets the bit indicated by x y z to the given value. 00296 @METHOD : 00297 @GLOBALS : 00298 @CALLS : 00299 @CREATED : David MacDonald 00300 @MODIFIED : 00301 ---------------------------------------------------------------------------- */ 00302 00303 public void set_bitlist_bit_3d( 00304 bitlist_3d_struct *bitlist, 00305 int x, 00306 int y, 00307 int z, 00308 BOOLEAN value ) 00309 { 00310 int word_index, bit_sub_index; 00311 bitlist_type bit; 00312 00313 word_index = z >> LOG_BITS_PER_BITLIST_WORD; 00314 bit_sub_index = z & BITS_PER_BITLIST_WORD_MINUS_1; 00315 00316 bit = (bitlist_type) 1 << (bitlist_type) bit_sub_index; 00317 00318 if( value ) 00319 { 00320 bitlist->bits[x][y][word_index] |= bit; 00321 } 00322 else 00323 { 00324 if( bitlist->bits[x][y][word_index] & bit ) 00325 { 00326 bitlist->bits[x][y][word_index] ^= bit; 00327 } 00328 } 00329 } 00330 00331 /* ----------------------------- MNI Header ----------------------------------- 00332 @NAME : get_bitlist_bit_3d 00333 @INPUT : bitlist 00334 : x 00335 : y 00336 : z 00337 @OUTPUT : 00338 @RETURNS : TRUE or FALSE 00339 @DESCRIPTION: Returns the bit indicated by x, y, z. 00340 @METHOD : 00341 @GLOBALS : 00342 @CALLS : 00343 @CREATED : David MacDonald 00344 @MODIFIED : 00345 ---------------------------------------------------------------------------- */ 00346 00347 public BOOLEAN get_bitlist_bit_3d( 00348 bitlist_3d_struct *bitlist, 00349 int x, 00350 int y, 00351 int z ) 00352 { 00353 int word_index, bit_sub_index; 00354 bitlist_type word; 00355 00356 bit_sub_index = z & BITS_PER_BITLIST_WORD_MINUS_1; 00357 00358 word_index = z >> LOG_BITS_PER_BITLIST_WORD; 00359 00360 word = bitlist->bits[x][y][word_index]; 00361 00362 if( word == 0 ) 00363 return( FALSE ); 00364 else 00365 return( ((word >> (bitlist_type) bit_sub_index) & 1) != 0 ); 00366 } 00367 00368 /* ----------------------------- MNI Header ----------------------------------- 00369 @NAME : delete_bitlist_3d 00370 @INPUT : bitlist 00371 @OUTPUT : 00372 @RETURNS : Status 00373 @DESCRIPTION: Deletes the 3D bitlist. 00374 @METHOD : 00375 @GLOBALS : 00376 @CALLS : 00377 @CREATED : David MacDonald 00378 @MODIFIED : 00379 ---------------------------------------------------------------------------- */ 00380 00381 public void delete_bitlist_3d( 00382 bitlist_3d_struct *bitlist ) 00383 {; 00384 if( bitlist->nx > 0 && bitlist->ny > 0 && bitlist->nz > 0 ) 00385 { 00386 FREE3D( bitlist->bits ); 00387 } 00388 } 00389 00390 /* ----------------------------- MNI Header ----------------------------------- 00391 @NAME : io_bitlist_3d 00392 @INPUT : file 00393 : io_type READ_FILE or WRITE_FILE 00394 : bitlist 00395 @OUTPUT : 00396 @RETURNS : 00397 @DESCRIPTION: Inputs or outputs the 3D bitlist. 00398 @METHOD : 00399 @GLOBALS : 00400 @CALLS : 00401 @CREATED : David MacDonald 00402 @MODIFIED : 00403 ---------------------------------------------------------------------------- */ 00404 00405 public Status io_bitlist_3d( 00406 FILE *file, 00407 IO_types io_type, 00408 bitlist_3d_struct *bitlist ) 00409 { 00410 Status status; 00411 int x, y; 00412 00413 status = OK; 00414 00415 for_less( x, 0, bitlist->nx ) 00416 { 00417 for_less( y, 0, bitlist->ny ) 00418 { 00419 status = io_binary_data( file, io_type, 00420 (void *) (bitlist->bits[x][y]), 00421 sizeof( bitlist->bits[x][y] ), 00422 bitlist->n_z_words ); 00423 00424 if( status != OK ) 00425 break; 00426 } 00427 if( status != OK ) 00428 break; 00429 } 00430 00431 return( status ); 00432 }

Generated on Wed Jul 28 09:10:56 2004 for BICPL by doxygen 1.3.7