swap.c

Go to the documentation of this file.
00001 /***********************************************************************
00002  * 
00003  * swap.c
00004  *
00005  * Copyright by:        Dr. Claudio Klein
00006  *                      X-ray Research GmbH, Hamburg
00007  *
00008  * Version:     2.0
00009  * Date:        08/07/1996
00010  *
00011  **********************************************************************/
00012 
00013 void swaplong();
00014 void swapshort();
00015 
00016 #if ( SGI || sgi || LINUX || linux )
00017 void swapvms();
00018 
00019 /******************************************************************
00020  * Function: swapvms = swaps bytes of floats in image header (SGI only)
00021  ******************************************************************/
00022 void
00023 swapvms( data, n )
00024 float *data;
00025 int n;
00026 {
00027         int i;
00028         float d;
00029 
00030         for (i=0; i<n; i++ ) {
00031                 swab( &data[i], &data[i], 4 );
00032 #if ( SGI || sgi || LINUX || linux )
00033                 data[i]/=4.;
00034 #else
00035                 sprintf( str, "%1.6f",data[i] );
00036                 d = atof( str );
00037                 data[i]=d/4.;
00038 #endif
00039         }
00040 }
00041 #endif
00042 
00043 #ifdef SWAP
00044 /******************************************************************
00045  * Function: swaplong = swaps bytes of 32 bit integers
00046  ******************************************************************/
00047 void
00048 swaplong(data, nbytes)
00049 register char *data;
00050 int nbytes;
00051 {
00052         register int i, t1, t2, t3, t4;
00053 
00054         for(i=nbytes/4;i--;) {
00055                 t1 = data[i*4+3];
00056                 t2 = data[i*4+2];
00057                 t3 = data[i*4+1];
00058                 t4 = data[i*4+0];
00059                 data[i*4+0] = t1;
00060                 data[i*4+1] = t2;
00061                 data[i*4+2] = t3;
00062                 data[i*4+3] = t4;
00063         }
00064 }
00065 
00066 /******************************************************************
00067  * Function: swapshort = swaps the two 8-bit halves of a 16-bit word
00068  ******************************************************************/
00069 void
00070 swapshort(data, n)
00071 register unsigned short *data;
00072 int n;
00073 {
00074         register int i;
00075         register unsigned short t;
00076 
00077         for(i=(n>>1)+1;--i;) {
00078                 /*t = (( (*data)&0xFF) << 8 ) | (( (*data)&0xFF00) >> 8);*/
00079                 t = (((*data) << 8) | ((*data) >> 8));
00080                 *data++ = t;
00081         }
00082 }
00083 #endif