| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkByteSwapper.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:33 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
|
=========================================================================*/ |
| 20 |
|
#ifndef __itkByteSwapper_h |
| 21 |
|
#define __itkByteSwapper_h |
| 22 |
|
|
| 23 |
|
#include "itkObject.h" |
| 24 |
|
#include "itkObjectFactory.h" |
| 25 |
|
|
| 26 |
|
namespace itk |
| 27 |
|
{ |
| 28 |
|
|
| 29 |
|
/** \class ByteSwapper |
| 30 |
|
* \brief Perform machine dependent byte swapping. |
| 31 |
|
* |
| 32 |
|
* ByteSwapper is used by I/O classes to perform machine dependent byte |
| 33 |
|
* swapping. Byte swapping is often used when reading or writing binary |
| 34 |
|
* files. Files can either be Big Endian (BE) or Little Endian (LE). |
| 35 |
|
* |
| 36 |
|
* \ingroup IOFilters |
| 37 |
|
* \ingroup OSSystemObjects |
| 38 |
|
*/ |
| 39 |
|
|
| 40 |
|
template <class T> |
| 41 |
|
class ITK_EXPORT ByteSwapper : public Object |
| 42 |
|
{ |
| 43 |
|
public: |
| 44 |
|
/** Standard class typedefs. */ |
| 45 |
|
typedef ByteSwapper Self; |
| 46 |
TDA |
typedef Object Superclass; |
| 47 |
TDA |
typedef SmartPointer<Self> Pointer; |
| 48 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 49 |
|
|
| 50 |
|
/** Work around MSVC bug (including ByteSwapper.h in a templated class). */ |
| 51 |
|
typedef std::ostream OStreamType; |
| 52 |
|
|
| 53 |
|
/** Run-time type information (and related methods). */ |
| 54 |
|
itkTypeMacro(ByteSwapper,Object); |
| 55 |
|
|
| 56 |
|
/** Query the machine Endian-ness. */ |
| 57 |
|
static bool SystemIsBigEndian (); |
| 58 |
|
static bool SystemIsBE () { return SystemIsBigEndian(); } |
| 59 |
|
static bool SystemIsLittleEndian (); |
| 60 |
|
static bool SystemIsLE () { return SystemIsLittleEndian(); } |
| 61 |
|
|
| 62 |
|
/** Generic swap method handles type T. The swapping is |
| 63 |
|
* done in-place. Either 2-byte or 4-byte swapping |
| 64 |
|
* can be handled. Single byte types are not swapped; |
| 65 |
|
* others raise an exception. The method is used to |
| 66 |
|
* swap to and from Big Endian. */ |
| 67 |
|
static void SwapFromSystemToBigEndian(T *p); |
| 68 |
|
|
| 69 |
|
/** Generic swap method handles type T. The swapping is |
| 70 |
|
* done in-place. Either 2-byte or 4-byte swapping |
| 71 |
|
* can be handled. Single byte types are not swapped; |
| 72 |
|
* others raise an exception. The method is used to |
| 73 |
|
* swap to and from Big Endian. */ |
| 74 |
|
static void SwapRangeFromSystemToBigEndian(T *p, unsigned long num); |
| 75 |
|
|
| 76 |
|
/** Generic swap method handles type T. The data is |
| 77 |
|
* swapped and written (in binary) to the ostream |
| 78 |
|
* given. A total of num values of type T are written |
| 79 |
|
* and swapped. Either 2-byte or 4-byte swapping |
| 80 |
|
* can be handled. Single byte types are not swapped; |
| 81 |
|
* others raise an exception. The method is used to |
| 82 |
|
* swap to and from Big Endian. */ |
| 83 |
LEN |
static void SwapWriteRangeFromSystemToBigEndian(T *p, int num, OStreamType *fp); |
| 84 |
|
|
| 85 |
|
/** Generic swap method handles type T. The swapping is |
| 86 |
|
* done in-place. Either 2-byte or 4-byte swapping |
| 87 |
|
* can be handled. Single byte types are not swapped; |
| 88 |
|
* others raise an exception. The method is used to |
| 89 |
|
* swap to and from Little Endian. */ |
| 90 |
|
static void SwapFromSystemToLittleEndian(T *p); |
| 91 |
|
|
| 92 |
|
/** Generic swap method handles type T. The swapping is |
| 93 |
|
* done in-place. Either 2-byte or 4-byte swapping |
| 94 |
|
* can be handled. Single byte types are not swapped; |
| 95 |
|
* others raise an exception. The method is used to |
| 96 |
|
* swap to and from Little Endian. */ |
| 97 |
|
static void SwapRangeFromSystemToLittleEndian(T *p, unsigned long num); |
| 98 |
|
|
| 99 |
|
/** Generic swap method handles type T. The data is |
| 100 |
|
* swapped and written (in binary) to the ostream |
| 101 |
|
* given. A total of num values of type T are written |
| 102 |
|
* and swapped. Either 2-byte or 4-byte swapping |
| 103 |
|
* can be handled. Single byte types are not swapped; |
| 104 |
|
* others raise an exception. The method is used to |
| 105 |
|
* swap to and from Little Endian. */ |
| 106 |
LEN |
static void SwapWriteRangeFromSystemToLittleEndian(T *p, int num, OStreamType *fp); |
| 107 |
|
|
| 108 |
|
protected: |
| 109 |
|
ByteSwapper() {} |
| 110 |
|
~ByteSwapper() {} |
| 111 |
|
|
| 112 |
|
/** Swap 2 bytes. */ |
| 113 |
|
static void Swap2(void *p); |
| 114 |
|
|
| 115 |
|
/** Swap a range of two-byte words. Num is the number of two-byte |
| 116 |
|
* words to swap. */ |
| 117 |
|
static void Swap2Range(void *p, unsigned long num); |
| 118 |
|
|
| 119 |
|
/** Swap and write a range of two-byte words. Num is the number of two-byte |
| 120 |
|
* words to swap and write. */ |
| 121 |
|
static void SwapWrite2Range(void *p, unsigned long num, OStreamType *fp); |
| 122 |
|
|
| 123 |
|
/** Swap four bytes. */ |
| 124 |
|
static void Swap4(void *p); |
| 125 |
|
|
| 126 |
|
/** Swap a range of four-byte words. Num is the number of four-byte words |
| 127 |
|
* to swap. */ |
| 128 |
|
static void Swap4Range(void *p, unsigned long num); |
| 129 |
|
|
| 130 |
|
/** Swap and write a range of four-byte words. Num is the number of four-byte |
| 131 |
|
* words to swap and write. */ |
| 132 |
|
static void SwapWrite4Range(void *p, unsigned long num, OStreamType *fp); |
| 133 |
|
|
| 134 |
|
/** Swap 8 bytes. */ |
| 135 |
|
static void Swap8(void *p); |
| 136 |
|
|
| 137 |
|
/** Swap a range of four-byte words. Num is the number of four-byte words |
| 138 |
|
* to swap. */ |
| 139 |
|
static void Swap8Range(void *p, unsigned long num); |
| 140 |
|
|
| 141 |
|
/** Swap and write a range of 8-byte words. Num is the number of four-byte |
| 142 |
|
* words to swap and write. */ |
| 143 |
|
static void SwapWrite8Range(void *p, unsigned long num, OStreamType *fp); |
| 144 |
|
|
| 145 |
|
private: |
| 146 |
|
ByteSwapper(const ByteSwapper&); //purposely not implemented |
| 147 |
|
void operator=(const ByteSwapper&); //purposely not implemented |
| 148 |
|
|
| 149 |
|
}; |
| 150 |
|
|
| 151 |
|
} // end namespace itk |
| 152 |
|
|
| 153 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 154 |
|
#include "itkByteSwapper.txx" |
| 155 |
|
#endif |
| 156 |
|
|
| 157 |
|
#endif |
| 158 |
|
|