1 /*****************************************************************************
2    Major portions of this software are copyrighted by the Medical College
3    of Wisconsin, 1994-2000, and are released under the Gnu General Public
4    License, Version 2.  See the file README.Copyright for details.
5 ******************************************************************************/
6 
7 #include "mrilib.h"
8 
9 /*** 7D SAFE ***/
10 
11 #define SWAB16(x) ( ( ((x)&0x00ffU)<<8 ) | ( ((x)&0xff00U)>>8 ) )
12 
mri_swapbytes(MRI_IMAGE * im)13 void mri_swapbytes( MRI_IMAGE *im )
14 {
15    register int ii , npix ;
16    register short *iar ;
17 
18 ENTRY("mri_swapbytes") ;
19 
20    if( im == NULL || im->kind != MRI_short ){
21      fprintf( stderr , "mri_swapbytes called with non-short image kind\n" ) ;
22      EXRETURN ;
23    }
24 
25    npix = im->nvox ; iar = MRI_SHORT_PTR(im) ;
26 
27    for( ii=0 ; ii < npix ; ii++ ) iar[ii] = SWAB16( iar[ii] ) ;
28 
29    EXRETURN ;
30 }
31 
32 /*---------------------------------------------------------------------
33    Routines to swap byte arrays in pairs and tetrads -- 14 Sep 1998
34 -----------------------------------------------------------------------*/
35 
36 typedef struct { unsigned char a,b ; } twobytes ;
37 
swap_twobytes(int n,void * ar)38 void swap_twobytes( int n , void * ar )
39 {
40    register int ii ;
41    register twobytes * tb = (twobytes *) ar ;
42    register unsigned char tt ;
43 
44    for( ii=0 ; ii < n ; ii++ ){
45       tt       = tb[ii].a ;
46       tb[ii].a = tb[ii].b ;
47       tb[ii].b = tt ;
48    }
49 }
50 
51 typedef struct { unsigned char a,b,c,d ; } fourbytes ;
52 
swap_fourbytes(int n,void * ar)53 void swap_fourbytes( int n , void * ar )
54 {
55    register int ii ;
56    register fourbytes * tb = (fourbytes *) ar ;
57    register unsigned char tt ;
58 
59    for( ii=0 ; ii < n ; ii++ ){
60       tt       = tb[ii].a ;
61       tb[ii].a = tb[ii].d ;
62       tb[ii].d = tt ;
63       tt       = tb[ii].b ;
64       tb[ii].b = tb[ii].c ;
65       tb[ii].c = tt ;
66    }
67 }
68 
69 typedef struct { unsigned char a,b,c,d , D,C,B,A ; } eightbytes ;
70 
swap_eightbytes(int n,void * ar)71 void swap_eightbytes( int n , void * ar )
72 {
73    register int ii ;
74    register eightbytes * tb = (eightbytes *) ar ;
75    register unsigned char tt ;
76 
77    for( ii=0 ; ii < n ; ii++ ){
78       tt = tb[ii].a ; tb[ii].a = tb[ii].A ; tb[ii].A = tt ;
79       tt = tb[ii].b ; tb[ii].b = tb[ii].B ; tb[ii].B = tt ;
80       tt = tb[ii].c ; tb[ii].c = tb[ii].C ; tb[ii].C = tt ;
81       tt = tb[ii].d ; tb[ii].d = tb[ii].D ; tb[ii].D = tt ;
82    }
83 }
84