1 /*=========================================================================
3 Program: Visualization Toolkit
4 Module: $RCSfile: vtkByteSwap.h,v $
6 Date: $Date: 2003/02/11 13:41:50 $
7 Version: $Revision: 1.52 $
9 Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
11 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notice for more information.
17 =========================================================================*/
18 // .NAME vtkByteSwap - perform machine dependent byte swapping
19 // .SECTION Description
20 // vtkByteSwap is used by other classes to perform machine dependent byte
21 // swapping. Byte swapping is often used when reading or writing binary
23 #ifndef __vtkByteSwap_h
24 #define __vtkByteSwap_h
26 #include "vtkObject.h"
28 class VTK_COMMON_EXPORT vtkByteSwap : public vtkObject
31 static vtkByteSwap *New();
32 vtkTypeRevisionMacro(vtkByteSwap,vtkObject);
35 // Swap 2 byte word to be LE.
36 static void Swap2LE(short *s);
37 static void Swap2LE(unsigned short *s);
40 // Swap four byte word to be LE.
41 static void Swap4LE(char *c);
42 static void Swap4LE(float *p) { vtkByteSwap::Swap4LE((char *)p);};
43 static void Swap4LE(int *i) { vtkByteSwap::Swap4LE((char *)i);};
44 static void Swap4LE(unsigned int *i) { vtkByteSwap::Swap4LE((char *)i);};
45 static void Swap4LE(long *i) { vtkByteSwap::Swap4LE((char *)i);};
46 static void Swap4LE(unsigned long *i) { vtkByteSwap::Swap4LE((char *)i);};
49 // Swap eight byte word to be LE. Currently implemented for doubles, but
50 // will be necessary for 64bit integers? 16bit chars?
51 static void Swap8LE(char *c);
52 static void Swap8LE(double *d) { vtkByteSwap::Swap8LE((char *)d);};
55 // Swap bunch of bytes to LE. Num is the number of two byte words to swap.
56 static void Swap2LERange(char *c,int num);
57 static void Swap2LERange(short *i,int num)
58 { vtkByteSwap::Swap2LERange((char *)i,num);};
61 // Swap bunch of bytes to be LE. Num is the number of four byte words to swap.
62 static void Swap4LERange(char *c,int num);
63 static void Swap4LERange(unsigned char *c,int num)
64 { vtkByteSwap::Swap4LERange((char *)c,num);};
65 static void Swap4LERange(float *p,int num)
66 { vtkByteSwap::Swap4LERange((char *)p,num);};
67 static void Swap4LERange(int *i,int num)
68 { vtkByteSwap::Swap4LERange((char *)i,num);};
69 static void Swap4LERange(unsigned int *i,int num)
70 { vtkByteSwap::Swap4LERange((char *)i,num);};
71 static void Swap4LERange(long *i,int num)
72 { vtkByteSwap::Swap4LERange((char *)i,num);};
73 static void Swap4LERange(unsigned long *i,int num)
74 { vtkByteSwap::Swap4LERange((char *)i,num);};
77 // Swap bunch of bytes to be LE. Num is the number of eight byte words to swap.
78 // Currently implemented for doubles...
79 static void Swap8LERange(char *c, int num);
80 static void Swap8LERange(double *d, int num)
81 { vtkByteSwap::Swap8LERange((char *)d, num);};
84 // Swap 2 byte word to BE.
85 static void Swap2BE(short *s);
86 static void Swap2BE(unsigned short *s);
89 // For writing, swap four byte word to be BE.
90 // For reading, swap four byte word from BE to machine's internal
92 static void Swap4BE(char *c);
93 static void Swap4BE(float *p) { vtkByteSwap::Swap4BE((char *)p);};
94 static void Swap4BE(int *i) { vtkByteSwap::Swap4BE((char *)i);};
95 static void Swap4BE(unsigned int *i) { vtkByteSwap::Swap4BE((char *)i);};
96 static void Swap4BE(long *i) { vtkByteSwap::Swap4BE((char *)i);};
97 static void Swap4BE(unsigned long *i) { vtkByteSwap::Swap4BE((char *)i);};
100 // For writing, swap eight byte word to be BE.
101 // For reading, swap eight byte word from BE to machine's internal
103 static void Swap8BE(char *c);
104 static void Swap8BE(double *d) { vtkByteSwap::Swap8BE((char *)d);};
107 // Swap bunch of bytes to BE. Num is the number of two byte words to swap.
108 static void Swap2BERange(char *c,int num);
109 static void Swap2BERange(short *i,int num)
110 { vtkByteSwap::Swap2BERange((char *)i,num);};
113 // Swap bunch of bytes to be BE. Num is the number of four byte words to swap.
114 static void Swap4BERange(char *c,int num);
115 static void Swap4BERange(float *p,int num)
116 { vtkByteSwap::Swap4BERange((char *)p,num); };
117 static void Swap4BERange(int *i,int num)
118 { vtkByteSwap::Swap4BERange((char *)i,num); };
119 static void Swap4BERange(unsigned int *i,int num)
120 { vtkByteSwap::Swap4BERange((char *)i,num); };
121 static void Swap4BERange(long *i,int num)
122 { vtkByteSwap::Swap4BERange((char *)i,num); };
123 static void Swap4BERange(unsigned long *i,int num)
124 { vtkByteSwap::Swap4BERange((char *)i,num); };
126 #ifdef VTK_USE_64BIT_IDS
127 static void Swap4BERange(vtkIdType *i,int num)
128 { vtkByteSwap::Swap4BERange((char *)i,num); };
132 // Swap bunch of bytes to be BE. Num is the number of eight byte words to swap.
133 static void Swap8BERange(char *c,int num);
134 static void Swap8BERange(double *d,int num)
135 { vtkByteSwap::Swap8BERange((char *)d,num); };
138 // Swap bunch of bytes to BE. Num is the number of two byte words to swap.
139 // The results are written out to file to prevent having to keep the swapped
141 static void SwapWrite2BERange(char *c,int num,FILE *fp);
142 static void SwapWrite2BERange(short *i,int num, FILE *fp)
143 {vtkByteSwap::SwapWrite2BERange((char *)i,num,fp);};
146 // Swap bunch of bytes to BE. Num is the number of four byte words to swap.
147 // The results are written out to file to prevent having to keep the swapped
149 static void SwapWrite4BERange(char *c,int num,FILE *fp);
150 static void SwapWrite4BERange(float *p,int num, FILE *fp)
151 { vtkByteSwap::SwapWrite4BERange((char *)p,num,fp);};
152 static void SwapWrite4BERange(int *i,int num,FILE *fp)
153 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
154 static void SwapWrite4BERange(unsigned int *i,int num,FILE *fp)
155 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
156 static void SwapWrite4BERange(unsigned long *i,int num, FILE *fp)
157 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
158 static void SwapWrite4BERange(long *i,int num, FILE *fp)
159 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
160 #ifdef VTK_USE_64BIT_IDS
161 static void SwapWrite4BERange(vtkIdType *i,int num, FILE *fp)
162 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
165 // Swap bunch of bytes to BE. Num is the number of eight byte words to swap.
166 // The results are written out to file to prevent having to keep the swapped
167 // copy in memory. Implemented for doubles for now.
168 static void SwapWrite8BERange(char *c,int num,FILE *fp);
169 static void SwapWrite8BERange(double *d,int num, FILE *fp)
170 { vtkByteSwap::SwapWrite8BERange((char *)d,num,fp);};
173 // Swap bunch of bytes to BE. Num is the number of two byte words to swap.
174 // The results are written out to stream to prevent having to keep the swapped
176 static void SwapWrite2BERange(char *c,int num, ostream *fp);
177 static void SwapWrite2BERange(short *i,int num, ostream *fp)
178 {vtkByteSwap::SwapWrite2BERange((char *)i,num,fp);};
181 // Swap bunch of bytes to BE. Num is the number of four byte words to swap.
182 // The results are written out to stream to prevent having to keep the swapped
184 static void SwapWrite4BERange(char *c,int num, ostream *fp);
185 static void SwapWrite4BERange(float *p,int num, ostream *fp)
186 { vtkByteSwap::SwapWrite4BERange((char *)p,num,fp);};
187 static void SwapWrite4BERange(int *i,int num, ostream *fp)
188 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
189 static void SwapWrite4BERange(unsigned int *i,int num, ostream *fp)
190 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
191 static void SwapWrite4BERange(unsigned long *i,int num, ostream *fp)
192 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
193 static void SwapWrite4BERange(long *i,int num, ostream *fp)
194 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
195 #ifdef VTK_USE_64BIT_IDS
196 static void SwapWrite4BERange(vtkIdType *i,int num, ostream *fp)
197 { vtkByteSwap::SwapWrite4BERange((char *)i,num,fp);};
200 // Swap bunch of bytes to BE. Num is the number of eight byte words to swap.
201 // The results are written out to stream to prevent having to keep the swapped
203 static void SwapWrite8BERange(char *c,int num, ostream *fp);
204 static void SwapWrite8BERange(double *d,int num, ostream *fp)
205 { vtkByteSwap::SwapWrite8BERange((char *)d,num,fp);};
208 // Swaps the bytes of a buffer. Uses an arbitrary word size, but
209 // assumes the word size is divisible by two.
210 static void SwapVoidRange(void *buffer, int numWords, int wordSize);
220 static void Swap2Bytes(char* &data);
222 static void Swap4Bytes(char* &data);
224 static void Swap8Bytes(char* &data);
227 vtkByteSwap(const vtkByteSwap&); // Not implemented.
228 void operator=(const vtkByteSwap&); // Not implemented.
233 vtkByteSwap::Swap2Bytes(char* &data)
236 one_byte = data[0]; data[0] = data[1]; data[1] = one_byte;
240 vtkByteSwap::Swap4Bytes(char* &data)
243 one_byte = data[0]; data[0] = data[3]; data[3] = one_byte;
244 one_byte = data[1]; data[1] = data[2]; data[2] = one_byte;
248 vtkByteSwap::Swap8Bytes(char* &data)
251 one_byte = data[0]; data[0] = data[7]; data[7] = one_byte;
252 one_byte = data[1]; data[1] = data[6]; data[6] = one_byte;
253 one_byte = data[2]; data[2] = data[5]; data[5] = one_byte;
254 one_byte = data[3]; data[3] = data[4]; data[4] = one_byte;