/**************************************************************** * DICOMVRSet.hpp ****************************************************************/ #ifndef _INCLUDE_DICOM_VRSET_ #define _INCLUDE_DICOM_VRSET_ class DICOMVRSet; class VRGroup; class DMarkup; class DICOMDataSet; class LinkedVR : public VR { public: inline LinkedVR (UINT16 g, UINT16 e, void * d, UINT32 l) { Group = g; Element = e; LongData = d; Length = l; SQVRSetArray= NULL; TypeCode = 0; } inline virtual ~LinkedVR () { LongData = NULL; } }; class VRItem { public: VR * vr; VRItem * PrevItem; VRItem * NextItem; inline VRItem (VRItem *p, VRItem *n) { PrevItem = p; NextItem = n; }; inline VRItem () { PrevItem = NULL; NextItem = NULL; }; }; class DICOM_API DICOMVRSet { public: UINT32 lParam; public: DICOMVRSet (); DICOMVRSet (const DICOMVRSet &); virtual ~DICOMVRSet () { Reset (); }; BOOL Push (VR * vr); BOOL Push (UINT16 Group, UINT16 Element, DICOMVRSet *); // Sequence Embedding inline void Add (UINT32 tag) { if (! IsExist (tag)) Push (new VR (tag)); } inline void Add (UINT16 g, UINT16 e) { if (! IsExist (g, e)) Push (new VR (g, e)); } inline void Add (UINT16 g, UINT16 e, const UID & v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, const char * v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, const DString & v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, UINT16 v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, UINT32 v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, INT16 v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, INT32 v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, float v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, double v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else Push (new VR (g, e, v)); } inline void Add (UINT16 g, UINT16 e, DICOMVRSet & v) { VR * vr = GetVR (g, e); if (vr) (*vr) = v; else { vr = new VR (g, e); (*vr) = v; Push (vr); } } void Add (UINT16 g, UINT16 e, UINT16 * pus, int Count); void Add (UINT16 g, UINT16 e, UINT32 * pul, int Count); void Add (UINT16 g, UINT16 e, INT16 * pss, int Count); void Add (UINT16 g, UINT16 e, INT32 * psl, int Count); VR * Pop (void); VR * TakeOut (UINT16 g, UINT16 e); BOOL CopyTo (DICOMVRSet &) const; BOOL CopyTo (DICOMVRSet &, UINT32 FromTag, UINT32 ToTag = 0xFFFFFFFF) const; BOOL Delete (UINT16 g, UINT16 e); BOOL Delete (const DICOMVRSet & DVRS); BOOL Filter (const DICOMVRSet & DVRS); BOOL DeleteGroup (UINT16 g); VR * GetVR (UINT16 g, UINT16 e) const; VR * GetFirstVR () const; VR * GetLastVR () const; DICOMVRSet * GetSequence (UINT16 g, UINT16 e, int nItem = 0) const { VR * vr = this->GetVR (g, e); if (! vr) return NULL; Array * pSQArray = vr->GetSQArray (); if (! pSQArray) return NULL; if (pSQArray->GetSize () <= nItem) return NULL; DICOMVRSet * VRS; if (nItem == 0) VRS = pSQArray->GetFirst (); else VRS = pSQArray->GetAt (nItem); return VRS; } inline LE_UINT16 GetUINT16 (UINT16 g, UINT16 e) const { VR * vr = this->GetVR (g, e); if (! vr) return (0); return (LE_UINT16) (*vr); } inline LE_UINT32 GetUINT32 (UINT16 g, UINT16 e) const { VR * vr = this->GetVR (g, e); if (! vr) return (0); return (LE_UINT32) (*vr); } inline DString GetDString (UINT16 g, UINT16 e) const { DString str; VR * vr = this->GetVR (g, e); if (vr) vr->Extract (str); return str; } inline float GetFloat (UINT16 g, UINT16 e) const { VR * vr = this->GetVR (g, e); if (! vr) return (0); return (float) (*vr); } inline double GetDouble (UINT16 g, UINT16 e) const { VR * vr = this->GetVR (g, e); if (! vr) return (0); return (double) (*vr); } inline int GetSize (void) const { return GroupCount; } inline int GetLength (void) const { return Length; } inline int GetVRCount (void) const; BOOL Package (void); BOOL Package (UINT16 g); inline BOOL IsEmpty (void) const { return (GroupCount == 0); } inline BOOL IsExist (UINT16 g, UINT16 e = 0) const { if (e) return (UINT32) GetVR (g,e); else return (UINT32) SeekFor (g); } inline BOOL IsExist (UINT32 tag) const { UINT16 g = GroupOfTag32 (tag); UINT16 e = ElementOfTag32 (tag); if (e) return (UINT32) GetVR (g,e); else return (UINT32) SeekFor (g); } virtual void Reset (void); DICOMVRSet & operator = (DICOMVRSet & DVRS); DICOMVRSet & TransferTo (DICOMVRSet & ToVRS); // Append all VR from FromVRS to me DICOMVRSet & AppendFrom (DICOMVRSet & FromVRS); virtual int Serialize (BYTE * pBuffer, int BufferSize) const; virtual int Deserialize (const BYTE * pBuffer, int BufferSize); // VRSetToXML virtual DString XmlSerialize (void) const; // XMLToVRSet virtual int XmlDeserialize (const char * XML); virtual int XmlDeserialize (const DString & XML); virtual DString ToString (void) const; virtual DString ToString (int nMinLength) const; protected: void Init (void) { lParam = 0; GroupCount = 0; FirstGroup = NULL; LastGroup = NULL; Length = 0; LastVR = NULL; LastVRG = NULL; CurrentGroup= NULL; } VRGroup * SeekFor (UINT16 g) const; BOOL PushGroup (VRGroup * VRGroupPtr); VRGroup * PopGroup (void); BOOL Push (DICOMVRSet *); // Sequence Embedding DString ToStringOfTabStop (int nbTab, int nMinLength = 0) const; int XmlSerialize (DMarkup * xml) const; int XmlDeserialize (DMarkup * xml); static BOOL XMLToVRSetArray (DMarkup * xml, Array * arVRS); static BOOL XMLToADDS (DMarkup * xml, Array * ADDS); private: VRGroup * FirstGroup; VRGroup * LastGroup; int GroupCount; VRGroup * CurrentGroup; mutable VRGroup * LastVRG; VR * LastVR; UINT32 Length; static VR ReservedVR; friend class VRSetIterator; friend class FixDecoder; friend class BigEndianDecoder; friend class Iterator; public: static BOOL VRSetArrayToXML (const Array * arVRS, DString * str); static BOOL VRSetArrayToXML (const Array * arVRS, DString * str); static BOOL XMLToVRSetArray (const DString * str, Array * arVRS); static BOOL ADDSToXML (const Array * ADDS, DString * str); static BOOL ADDSToXML (const Array * ADDS, DString * str); static BOOL XMLToADDS (const DString * str, Array * ADDS); public: class DICOM_API Iterator { private: const DICOMVRSet & DVS; int TotalVRCount; VRGroup * CurrentGroup; VRItem * CurrentItem; VR * CurrentVR; int m_Index; bool m_Continue; public: inline Iterator (const DICOMVRSet & theDVS) : DVS (theDVS) { Restart (); } inline void Restart () { TotalVRCount = DVS.GetVRCount (); CurrentGroup = DVS.FirstGroup; CurrentItem = NULL; CurrentVR = NULL; m_Index = -1; m_Continue = true; Next (); } inline operator bool () const { return m_Continue; } inline VR * Current (void) const { return CurrentVR; } inline int Index (void) const { return m_Index; } inline int GetNbOfVR (void) const { return TotalVRCount; } inline void operator ++ () // ++ Iter; { Next (); } inline void operator ++ (int) // Iter ++; { Next (); } inline VR * operator () (void) { return Current (); } inline VR * operator * (void) { return Current (); } inline operator INT16 () const { return (INT16) (*Current ()); } inline operator INT32 () const { return (INT32) (*Current ()); } inline operator UINT16 () const { return (UINT16) (*Current ()); } inline operator UINT32 () const { return (UINT32) (*Current ()); } inline operator DString () const { return (DString) (*Current ()); } void Next (); }; public: class DICOM_API Proxy { public: inline Proxy (DICOMVRSet & VRS, UINT16 g, UINT16 e) : m_VRSet (VRS), m_Group (g), m_Element (e) { } inline Proxy & operator = (const Proxy & vrp) { VR * vr = vrp.m_VRSet.GetVR (vrp.m_Group, vrp.m_Element); if (vr) { VR * nvr = new VR (*vr); nvr->SetTag (m_Group, m_Element); m_VRSet.Push (nvr); } return (*this); } inline Proxy & operator = (UINT16 v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (UINT32 v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (INT16 v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (INT32 v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (float v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (double v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (const char * v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (const UID & v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (const DString & v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline Proxy & operator = (DICOMVRSet & v) { m_VRSet.Add (m_Group, m_Element, v); return (*this); } inline operator DString () const { DString str = m_VRSet.GetDString (m_Group, m_Element); return str; } inline operator UINT32 () const { return m_VRSet.GetUINT32 (m_Group, m_Element); } inline operator UINT16 () const { return m_VRSet.GetUINT16 (m_Group, m_Element); } inline operator INT32 () const { return (INT32) (m_VRSet.GetUINT32 (m_Group, m_Element)); } inline operator INT16 () const { return (INT16) (m_VRSet.GetUINT16 (m_Group, m_Element)); } inline operator float () const { return m_VRSet.GetFloat (m_Group, m_Element); } inline operator double () const { return m_VRSet.GetDouble (m_Group, m_Element); } private: DICOMVRSet & m_VRSet; UINT16 m_Group; UINT16 m_Element; }; inline Proxy operator [] (UINT32 tag) { return Proxy (*this, GroupOfTag32 (tag), ElementOfTag32 (tag)); } inline Proxy operator () (UINT32 tag) { return Proxy (*this, GroupOfTag32 (tag), ElementOfTag32 (tag)); } inline Proxy operator () (UINT16 g, UINT16 e) { return Proxy (*this, g, e); } inline const Proxy operator [] (UINT32 tag) const { return Proxy (const_cast (*this), GroupOfTag32 (tag), ElementOfTag32 (tag)); } inline const Proxy operator () (UINT32 tag) const { return Proxy (const_cast (*this), GroupOfTag32 (tag), ElementOfTag32 (tag)); } inline const Proxy operator () (UINT16 g, UINT16 e) const { return Proxy (const_cast (*this), g, e); } friend class Proxy; }; class VRGroup { private: VRGroup * PrevGroup; VRGroup * NextGroup; VRItem * FirstItem; VRItem * LastItem; int ItemCount; private: UINT16 Group; UINT32 Length; LinkedVR *Element0; //public: protected: VRGroup (UINT16 G); ~VRGroup () { Reset () ; delete Element0; Element0 = NULL; } void Reset (void); BOOL Push (VR * vr); VR * Pop (void); VR * TakeOut (UINT16 e); VR * SeekFor (UINT16 e) const ; int GetVRCount (void) const { return (ItemCount+1) ; } // All VR plus the Element0 protected: BOOL Package (void); friend class DICOMVRSet; friend class DICOMVRSet::Iterator; friend class VRSetIterator; }; inline void DICOMVRSet::Iterator::Next () { if (! CurrentItem) { if (! CurrentGroup) { CurrentVR = NULL; m_Continue = false; m_Index = -1; return; } m_Index ++; CurrentItem = CurrentGroup->FirstItem; CurrentVR = CurrentGroup -> Element0; return; } m_Index ++; CurrentVR = CurrentItem -> vr; CurrentItem = CurrentItem->NextItem; if (! CurrentItem) CurrentGroup = CurrentGroup->NextGroup; } DICOM_API BOOL operator == (const DICOMVRSet & SetA, const DICOMVRSet & SetB) ; inline DICOM_API BOOL operator != (const DICOMVRSet & SetA, const DICOMVRSet & SetB) { return ! (SetA == SetB); } ////////////////////////////////////////////////////////////////////// // class VRSetIterator class DICOM_API VRSetIterator { private: const DICOMVRSet & DVS; int TotalVRCount; VRGroup * CurrentGroup; VRItem * CurrentItem; VR * LastVR; int VRCount; public: VRSetIterator (const DICOMVRSet & theDVS) : DVS (theDVS) { TotalVRCount = theDVS.GetVRCount (); CurrentGroup = theDVS.FirstGroup; CurrentItem = NULL; LastVR = NULL; VRCount = 0; } VR * operator () (void); }; #endif