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  

niml_byteorder.c

Go to the documentation of this file.
00001 #include "niml_private.h"
00002 
00003 /*************************************************************************/
00004 /************************ Byte ordering functions ************************/
00005 /*************************************************************************/
00006 
00007 /*---------------------------------------------------------------*/
00008 /*! Find the byte order on this system.
00009     Return is either NI_LSB_FIRST or NI_MSB_FIRST.
00010     We are assuming that there are only 2 possible order, which
00011     is of course false.
00012 -----------------------------------------------------------------*/
00013 
00014 int NI_byteorder(void)
00015 {
00016    union { unsigned char bb[2] ;
00017            short         ss    ; } fred ;
00018 
00019    fred.bb[0] = 1 ; fred.bb[1] = 0 ;
00020 
00021    return (fred.ss == 1) ? NI_LSB_FIRST : NI_MSB_FIRST ;
00022 }
00023 
00024 /*---------------------------------------------------------------*/
00025 /*! Struct defined for use in NI_swap2(). */
00026 
00027 typedef struct { unsigned char a,b ; } twobytes ;
00028 
00029 /*---------------------------------------------------------------*/
00030 /*! Swap arrays of 2 bytes (shorts).
00031 -----------------------------------------------------------------*/
00032 
00033 void NI_swap2( int n , void *ar )
00034 {
00035    register int ii ;
00036    register twobytes *tb = (twobytes *) ar ;
00037    register unsigned char tt ;
00038 
00039    for( ii=0 ; ii < n ; ii++ ){
00040       tt = tb[ii].a ; tb[ii].a = tb[ii].b ; tb[ii].b = tt ;
00041    }
00042    return ;
00043 }
00044 
00045 /*---------------------------------------------------------------*/
00046 /*! Struct defined for use in NI_swap4(). */
00047 
00048 typedef struct { unsigned char a,b,c,d ; } fourbytes ;
00049 
00050 /*---------------------------------------------------------------*/
00051 /*! Swap arrays of 4 bytes (ints or floats).
00052 -----------------------------------------------------------------*/
00053 
00054 void NI_swap4( int n , void *ar )
00055 {
00056    register int ii ;
00057    register fourbytes *tb = (fourbytes *) ar ;
00058    register unsigned char tt , uu ;
00059 
00060    for( ii=0 ; ii < n ; ii++ ){
00061       tt = tb[ii].a ; tb[ii].a = tb[ii].d ; tb[ii].d = tt ;
00062       uu = tb[ii].b ; tb[ii].b = tb[ii].c ; tb[ii].c = uu ;
00063    }
00064    return ;
00065 }
00066 
00067 /*---------------------------------------------------------------*/
00068 /*! Struct defined for use in NI_swap8(). */
00069 
00070 typedef struct { unsigned char a,b,c,d , e,f,g,h ; } eightbytes ;
00071 
00072 /*---------------------------------------------------------------*/
00073 /*! Swap arrays of 8 bytes (doubles or 64 bit ints).
00074 -----------------------------------------------------------------*/
00075 
00076 void NI_swap8( int n , void *ar )
00077 {
00078    register int ii ;
00079    register eightbytes *tb = (eightbytes *) ar ;
00080    register unsigned char tt , uu , vv , ww ;
00081 
00082    for( ii=0 ; ii < n ; ii++ ){
00083       tt = tb[ii].a ; tb[ii].a = tb[ii].h ; tb[ii].h = tt ;
00084       uu = tb[ii].b ; tb[ii].b = tb[ii].g ; tb[ii].g = uu ;
00085       vv = tb[ii].c ; tb[ii].c = tb[ii].f ; tb[ii].f = vv ;
00086       ww = tb[ii].d ; tb[ii].d = tb[ii].e ; tb[ii].e = ww ;
00087    }
00088    return ;
00089 }
 

Powered by Plone

This site conforms to the following standards: