OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I686LINUX / util / I686LINUX / include / vtk / vtkByteSwap.h
1 /*=========================================================================
2
3   Program:   Visualization Toolkit
4   Module:    $RCSfile: vtkByteSwap.h,v $
5   Language:  C++
6   Date:      $Date: 2003/02/11 13:41:50 $
7   Version:   $Revision: 1.52 $
8
9   Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen 
10   All rights reserved.
11   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
12
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.
16
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 
22 // files.
23 #ifndef __vtkByteSwap_h
24 #define __vtkByteSwap_h
25
26 #include "vtkObject.h"
27
28 class VTK_COMMON_EXPORT vtkByteSwap : public vtkObject
29 {
30 public:
31   static vtkByteSwap *New();
32   vtkTypeRevisionMacro(vtkByteSwap,vtkObject);
33
34   // Description:
35   // Swap 2 byte word to be LE.
36   static void Swap2LE(short *s);
37   static void Swap2LE(unsigned short *s);
38
39   // Description:
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);};
47
48   // Description:
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);};
53
54   // Description:
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);};
59
60   // Description:
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);};
75
76   // Description:
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);};
82
83   // Description:
84   // Swap 2 byte word to BE.
85   static void Swap2BE(short *s);
86   static void Swap2BE(unsigned short *s);
87
88   // Description:
89   // For writing, swap four byte word to be BE.
90   // For reading, swap four byte word from BE to machine's internal
91   // representation.
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);};
98
99   // Description:
100   // For writing, swap eight byte word to be BE.
101   // For reading, swap eight byte word from BE to machine's internal
102   // representation.
103   static void Swap8BE(char *c);
104   static void Swap8BE(double *d) { vtkByteSwap::Swap8BE((char *)d);};
105
106   // Description:
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);};
111
112     // Description:
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); };
125
126 #ifdef VTK_USE_64BIT_IDS
127   static void Swap4BERange(vtkIdType *i,int num) 
128   { vtkByteSwap::Swap4BERange((char *)i,num); };
129 #endif
130
131   // Description:
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); };
136
137   // Description:
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
140   // copy in memory.
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);};
144
145   // Description:
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
148   // copy in memory.
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);};
163 #endif
164   // Description:
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);};
171
172   // Description:
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
175   // copy in memory.
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);};
179
180   // Description:
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
183   // copy in memory.
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);};
198 #endif
199   // Description:
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
202   // copy in memory.
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);};
206
207   // Description:
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);
211
212
213 protected:
214   vtkByteSwap() {};
215   ~vtkByteSwap() {};
216
217 private:
218   // Description:
219   // Swaps bytes. 
220   static void Swap2Bytes(char* &data);
221
222   static void Swap4Bytes(char* &data);
223  
224   static void Swap8Bytes(char* &data);
225
226 private:
227   vtkByteSwap(const vtkByteSwap&);  // Not implemented.
228   void operator=(const vtkByteSwap&);  // Not implemented.
229 };
230
231
232 inline void 
233 vtkByteSwap::Swap2Bytes(char* &data)
234
235   char one_byte;  
236   one_byte = data[0]; data[0] = data[1]; data[1] = one_byte;
237 }
238
239 inline void 
240 vtkByteSwap::Swap4Bytes(char* &data)
241
242   char one_byte; 
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; 
245 }
246
247 inline void 
248 vtkByteSwap::Swap8Bytes(char* &data)
249
250   char one_byte;
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; 
255 }
256
257 #endif