ByteSwap.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development Team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the ASSIMP team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the ASSIMP Development Team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file Helper class tp perform various byte oder swappings
  34. (e.g. little to big endian) */
  35. #ifndef AI_BYTESWAP_H_INC
  36. #define AI_BYTESWAP_H_INC
  37. #include "../include/aiAssert.h"
  38. namespace Assimp
  39. {
  40. // ---------------------------------------------------------------------------
  41. /** \brief Defines some useful byte order swap routines.
  42. *
  43. * This can e.g. be used for the conversion from and to littel and big endian.
  44. * This is required by some loaders since some model formats
  45. */
  46. class ByteSwap
  47. {
  48. ByteSwap() {}
  49. public:
  50. //! Swap the byte oder of 2 byte of data
  51. //! \param szOut Buffer to be swapped
  52. static inline void Swap2(char* szOut)
  53. {
  54. ai_assert(NULL != szOut);
  55. std::swap(szOut[0],szOut[1]);
  56. }
  57. //! Swap the byte oder of 4 byte of data
  58. //! \param szOut Buffer to be swapped
  59. static inline void Swap4(char* szOut)
  60. {
  61. ai_assert(NULL != szOut);
  62. std::swap(szOut[0],szOut[3]);
  63. std::swap(szOut[1],szOut[2]);
  64. }
  65. //! Swap the byte oder of 8 byte of data
  66. //! \param szOut Buffer to be swapped
  67. static inline void Swap8(char* szOut)
  68. {
  69. ai_assert(NULL != szOut);
  70. std::swap(szOut[0],szOut[7]);
  71. std::swap(szOut[1],szOut[6]);
  72. std::swap(szOut[2],szOut[5]);
  73. std::swap(szOut[3],szOut[4]);
  74. }
  75. //! Swap a single precision float
  76. //! \param fOut Float value to be swapped
  77. static inline void Swap(float* fOut)
  78. {
  79. Swap4((char*)fOut);
  80. }
  81. //! Swap a double precision float
  82. //! \param fOut Double value to be swapped
  83. static inline void Swap(double* fOut)
  84. {
  85. Swap8((char*)fOut);
  86. }
  87. //! Swap a 16 bit integer
  88. //! \param fOut Integer to be swapped
  89. static inline void Swap(int16_t* fOut)
  90. {
  91. Swap2((char*)fOut);
  92. }
  93. //! Swap a 32 bit integer
  94. //! \param fOut Integer to be swapped
  95. static inline void Swap(int32_t* fOut)
  96. {
  97. Swap4((char*)fOut);
  98. }
  99. //! Swap a 64 bit integer
  100. //! \param fOut Integer to be swapped
  101. static inline void Swap(int64_t* fOut)
  102. {
  103. Swap8((char*)fOut);
  104. }
  105. };
  106. };
  107. #endif //!! AI_BYTESWAP_H_INC