From 5a422aba704c375a307a902bafe658342e209906 Mon Sep 17 00:00:00 2001 From: scuri Date: Fri, 17 Oct 2008 06:10:15 +0000 Subject: First commit - moving from LuaForge to SourceForge --- src/im_bin.cpp | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/im_bin.cpp (limited to 'src/im_bin.cpp') diff --git a/src/im_bin.cpp b/src/im_bin.cpp new file mode 100644 index 0000000..de279b5 --- /dev/null +++ b/src/im_bin.cpp @@ -0,0 +1,111 @@ +/** \file + * \brief Binary Data Utilities + * + * See Copyright Notice in im_lib.h + * $Id: im_bin.cpp,v 1.1 2008/10/17 06:10:16 scuri Exp $ + */ + +#include + +#include "im_util.h" + + +int imBinCPUByteOrder(void) +{ + static int CPUByteOrder = -1; + + if (CPUByteOrder == -1) + { + unsigned short w = 0x0001; + unsigned char* b = (unsigned char*)&w; + CPUByteOrder = (b[0] == 0x01)? IM_LITTLEENDIAN: IM_BIGENDIAN; + } + + return CPUByteOrder; +} + +void imBinSwapBytes(void *data, int count, int size) +{ + switch(size) + { + case 2: + imBinSwapBytes2(data, count); + break; + case 4: + imBinSwapBytes4(data, count); + break; + case 8: + imBinSwapBytes8(data, count); + break; + } +} + +void imBinSwapBytes2(void *data, int count) +{ + assert(data); + + unsigned char lTemp; + unsigned char *values = (unsigned char *)data; + + while (count-- != 0) + { + lTemp = values[1]; + values[1] = values[0]; + values[0] = lTemp; + + values += 2; + } +} + +void imBinSwapBytes4(void *data, int count) +{ + assert(data); + + unsigned char lTemp; + unsigned char *values = (unsigned char *)data; + + while (count-- != 0) + { + lTemp = values[3]; + values[3] = values[0]; + values[0] = lTemp; + + lTemp = values[2]; + values[2] = values[1]; + values[1] = lTemp; + + values += 4; + } +} + +void imBinSwapBytes8(void *data, int count) +{ + assert(data); + + unsigned char lTemp; + unsigned char *values = (unsigned char *)data; + + assert(values); + + while (count-- != 0) + { + lTemp = values[7]; + values[7] = values[0]; + values[0] = lTemp; + + lTemp = values[6]; + values[6] = values[1]; + values[1] = lTemp; + + lTemp = values[5]; + values[5] = values[2]; + values[2] = lTemp; + + lTemp = values[4]; + values[4] = values[3]; + values[3] = lTemp; + + values += 8; + } +} + -- cgit v1.2.3