]> Creatis software - gdcm.git/blobdiff - src/gdcmUtil.cxx
* Activate Debug mode
[gdcm.git] / src / gdcmUtil.cxx
index 8ff06805e1d29ddf98b94ff4ea847c0986bea01b..8825cb97d9f3cab91337203be6c3e79d7eaa2d12 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmUtil.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/02/02 18:13:57 $
-  Version:   $Revision: 1.131 $
+  Date:      $Date: 2005/02/11 20:48:49 $
+  Version:   $Revision: 1.139 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -351,7 +351,7 @@ unsigned int Util::GetCurrentProcessID()
  */
 bool Util::IsCurrentProcessorBigEndian()
 {
-#ifdef GDCM_WORDS_BIGENDIAN
+#if defined(GDCM_WORDS_BIGENDIAN)
    return true;
 #else
    return false;
@@ -437,7 +437,7 @@ bool Util::DicomStringEqual(const std::string &s1, const char *s2)
            OUT AsnObjectIdentifier * supportedView);
 #endif //_WIN32
 
-
+/// \brief gets current M.A.C adress (for internal use only)
 int GetMacAddrSys ( unsigned char *addr )
 {
 #ifdef _WIN32
@@ -450,8 +450,8 @@ int GetMacAddrSys ( unsigned char *addr )
 
    HANDLE PollForTrapEvent;
    AsnObjectIdentifier SupportedView;
-   UINT OID_ifEntryType[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 3 };
-   UINT OID_ifEntryNum[] = { 1, 3, 6, 1, 2, 1, 2, 1 };
+   UINT OID_ifEntryType[]  = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 3 };
+   UINT OID_ifEntryNum[]   = { 1, 3, 6, 1, 2, 1, 2, 1 };
    UINT OID_ipMACEntAddr[] = { 1, 3, 6, 1, 2, 1, 2, 2, 1, 6 };
    AsnObjectIdentifier MIB_ifMACEntAddr = {
        sizeof(OID_ipMACEntAddr) / sizeof(UINT), OID_ipMACEntAddr };
@@ -699,9 +699,9 @@ int GetMacAddrSys ( unsigned char *addr )
 }
 
 /**
- * Mini function to return the last digit from a number express in base 256
- * pre condition data contain an array of 6 unsigned char
- * post condition carry contain the last digit
+ * \brief Mini function to return the last digit from a number express in base 256
+ *        pre condition data contain an array of 6 unsigned char
+ *        post condition carry contain the last digit
  */
 inline int getlastdigit(unsigned char *data)
 {
@@ -748,7 +748,7 @@ std::string Util::GetMACAddress()
    }
    else
    {
-      gdcmVerboseMacro("Problem in finding the MAC Address");
+      gdcmWarningMacro("Problem in finding the MAC Address");
       return "";
    }
 }
@@ -809,17 +809,6 @@ const std::string &Util::GetRootUID()
 }
 
 //-------------------------------------------------------------------------
-/**
- * \brief
- * @param os ostream to write to
- * @param val val
- */ 
-template <class T>
-std::ostream &binary_write(std::ostream &os, const T &val)
-{
-   return os.write(reinterpret_cast<const char*>(&val), sizeof val);
-}
-
 /**
  * \brief binary_write binary_write
  * @param os ostream to write to 
@@ -827,7 +816,7 @@ std::ostream &binary_write(std::ostream &os, const T &val)
  */ 
 std::ostream &binary_write(std::ostream &os, const uint16_t &val)
 {
-#ifdef GDCM_WORDS_BIGENDIAN
+#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
    uint16_t swap;
    swap = ((( val << 8 ) & 0xff00 ) | (( val >> 8 ) & 0x00ff ) );
    return os.write(reinterpret_cast<const char*>(&swap), 2);
@@ -843,7 +832,7 @@ std::ostream &binary_write(std::ostream &os, const uint16_t &val)
  */ 
 std::ostream &binary_write(std::ostream &os, const uint32_t &val)
 {
-#ifdef GDCM_WORDS_BIGENDIAN
+#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
    uint32_t swap;
    swap = ( ((val<<24) & 0xff000000) | ((val<<8)  & 0x00ff0000) | 
             ((val>>8)  & 0x0000ff00) | ((val>>24) & 0x000000ff) );
@@ -873,6 +862,64 @@ std::ostream &binary_write(std::ostream &os, std::string const &val)
    return os.write(val.c_str(), val.size());
 }
 
+/**
+ * \brief  binary_write binary_write
+ * @param os ostream to write to
+ * @param val val
+ */ 
+std::ostream &binary_write(std::ostream &os, const uint8_t *val, size_t len)
+{
+   // We are writting sizeof(char) thus no need to swap bytes
+   return os.write(reinterpret_cast<const char*>(val), len);
+}
+
+/**
+ * \brief  binary_write binary_write
+ * @param os ostream to write to
+ * @param val val
+ */ 
+std::ostream &binary_write(std::ostream &os, const uint16_t *val, size_t len)
+{
+// This is tricky since we are writting two bytes buffer. Be carefull with little endian
+// vs big endian. Also this other trick is to allocate a small (efficient) buffer that store
+// intermidiate result before writting it.
+#if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
+   const int BUFFER_SIZE = 4096;
+   uint16_t *buffer = new uint16_t[BUFFER_SIZE/2];
+   uint16_t *binArea16 = (uint16_t*)val;
+   // how many BUFFER_SIZE long pieces in binArea ?
+   int nbPieces = len/BUFFER_SIZE; //(16 bits = 2 Bytes)
+   int remainingSize = len%BUFFER_SIZE;
+
+   for (int j=0;j<nbPieces;j++)
+   {
+      for (int i = 0; i < BUFFER_SIZE/2; i++)
+      {
+         //buffer[i] =  (binArea16[i] >> 8) | (binArea16[i] << 8);
+         uint16_t val16 = binArea16[i];
+         buffer[i] = ((( val16 << 8 ) & 0xff00 ) | (( val16 >> 8 ) & 0x00ff ) );
+      }
+      os.write ( (char*)buffer, BUFFER_SIZE );
+      binArea16 += BUFFER_SIZE/2;
+   }
+   if ( remainingSize > 0)
+   {
+      for (int i = 0; i < remainingSize/2; i++)
+      {
+         //buffer[i] =  (binArea16[i] >> 8) | (binArea16[i] << 8);
+         uint16_t val16 = binArea16[i];
+         buffer[i] = ((( val16 << 8 ) & 0xff00 ) | (( val16 >> 8 ) & 0x00ff ) );
+      }
+      os.write ( (char*)buffer, remainingSize );
+   }
+   delete[] buffer;
+   return os;
+#else
+   return os.write(reinterpret_cast<const char*>(val), len);
+#endif
+}
+
 //-------------------------------------------------------------------------
 // Protected