GDCM  2.2.0
gdcmVL.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program: GDCM (Grassroots DICOM). A DICOM library
00004 
00005   Copyright (c) 2006-2011 Mathieu Malaterre
00006   All rights reserved.
00007   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
00008 
00009      This software is distributed WITHOUT ANY WARRANTY; without even
00010      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00011      PURPOSE.  See the above copyright notice for more information.
00012 
00013 =========================================================================*/
00014 #ifndef GDCMVL_H
00015 #define GDCMVL_H
00016 
00017 #include "gdcmTypes.h"
00018 
00019 #include <iostream>
00020 
00021 namespace gdcm
00022 {
00023 
00029 class GDCM_EXPORT VL
00030 {
00031 public:
00032   typedef uint32_t Type;
00033   VL(uint32_t vl = 0) : ValueLength(vl) { }
00034 
00035   // FIXME: ugly
00036   static uint32_t GetVL32Max() { return 0xFFFFFFFF; }
00037   static uint16_t GetVL16Max() { return 0xFFFF; }
00038 
00039   bool IsUndefined() const {
00040     return ValueLength == 0xFFFFFFFF;
00041   }
00042   void SetToUndefined() {
00043     ValueLength = 0xFFFFFFFF;
00044   }
00045 
00047   bool IsOdd() const {
00048     return !IsUndefined() && ValueLength % 2;
00049   }
00050 
00052   VL& operator+=(VL const &vl) {
00053     ValueLength += vl.ValueLength;
00054     return *this;
00055   }
00056   VL& operator++() {
00057     ++ValueLength;
00058     return *this;
00059   }
00060   VL operator++(int) {
00061     uint32_t tmp(ValueLength);
00062     ++ValueLength;
00063     return tmp;
00064   }
00065 
00066   operator uint32_t () const { return ValueLength; }
00067 
00068   VL GetLength() const {
00069     // VL cannot know it's length...well in implicit yes...
00070     // TODO: need to check we cannot call this function from an Explicit element
00071     return 4;
00072   }
00073 
00074   friend std::ostream& operator<<(std::ostream& os, const VL& vl);
00075 
00076   // PURPOSELY not implemented (could not differenciate 16bits vs 32bits VL)
00077   //friend std::istream& operator>>(std::istream& is, VL& n);
00078 
00079   template <typename TSwap>
00080   std::istream &Read(std::istream &is)
00081     {
00082     is.read((char*)(&ValueLength), sizeof(uint32_t));
00083     TSwap::SwapArray(&ValueLength,1);
00084     return is;
00085     }
00086 
00087   template <typename TSwap>
00088   std::istream &Read16(std::istream &is)
00089     {
00090     uint16_t copy;
00091     is.read((char*)(&copy), sizeof(uint16_t));
00092     TSwap::SwapArray(&copy,1);
00093     ValueLength = copy;
00094     assert( ValueLength <=  65535 /*UINT16_MAX*/ ); // ?? doh !
00095     return is;
00096     }
00097 
00098   template <typename TSwap>
00099   const std::ostream &Write(std::ostream &os) const
00100     {
00101     uint32_t copy = ValueLength;
00102 #ifndef GDCM_WRITE_ODD_LENGTH
00103     if( IsOdd() )
00104       {
00105       ++copy;
00106       }
00107 #else
00108     assert( !IsOdd() );
00109 #endif
00110     TSwap::SwapArray(&copy,1);
00111     return os.write((char*)(&copy), sizeof(uint32_t));
00112     }
00113 
00114   template <typename TSwap>
00115   const std::ostream &Write16(std::ostream &os) const
00116     {
00117     assert( ValueLength <=   65535 /*UINT16_MAX*/ );
00118     uint16_t copy = ValueLength;
00119 #ifndef GDCM_WRITE_ODD_LENGTH
00120     if( IsOdd() )
00121       {
00122       ++copy;
00123       }
00124 #else
00125     assert( !IsOdd() );
00126 #endif
00127     TSwap::SwapArray(&copy,1);
00128     return os.write((char*)(&copy), sizeof(uint16_t));
00129     }
00130 
00131 private:
00132   uint32_t ValueLength;
00133 };
00134 //----------------------------------------------------------------------------
00135 inline std::ostream& operator<<(std::ostream& os, const VL& val)
00136 {
00137   os << /*std::hex <<*/ val.ValueLength;
00138   return os;
00139 }
00140 
00141 } // end namespace gdcm
00142 
00143 #endif //GDCMVL_H

Generated on Tue Mar 27 2012 13:33:41 for GDCM by doxygen 1.8.0
SourceForge.net Logo