/*************************************************************************** * E-Com Technology Ltd. * * ECOMPACS DICOM Network Transport Libraries * Version 0.1 Beta ***************************************************************************/ #ifndef _DICOM_VR #define _DICOM_VR class DString; class DICOMVRSet; class UID; #define VR_SHORT_DATA_LENGTH 64 ////////////////////////////////////////////////////////////////////// // class VR class DICOM_API VR { //public: protected: UINT16 Group; UINT16 Element; UINT32 Length; BYTE ShortData[VR_SHORT_DATA_LENGTH]; void * LongData; UINT16 TypeCode; ArrayOfPtr * SQVRSetArray; DICOMVRSet * Owner; protected: #ifdef _DEBUG char Description [255]; #endif public: UINT16 GetGroup (void) const { return Group; } UINT16 GetElement (void) const { return Element; } UINT32 GetLength (void) const { return Length; } void * GetData (void) const { if (LongData) return LongData; else return (void *) ShortData; } UINT32 GetTag (void) const { return MakeTagFrom (Group, Element) ; } #ifdef _DEBUG void SetTag (UINT16 g, UINT16 e); #else void SetTag (UINT16 g, UINT16 e) { Group = g; Element = e; } #endif UINT16 GetTypeCode (void) const { return TypeCode; } void SetTypeCode (UINT32 theTypeCode) { TypeCode = theTypeCode; } void Attach (void * data, UINT32 length) { if (LongData) delete LongData; LongData = data; Length = length; } void * Detach (void) { if (LongData) { void * p = LongData; LongData = NULL; Length = 0; return p; } else { void * p = new BYTE[Length]; memcpy (p, ShortData, Length); Length = 0; return p; } } void * GetDataSetLength (UINT32 length); ArrayOfPtr * GetSQArray (void) const { return SQVRSetArray; } ArrayOfPtr * DetachSQArray (void) { ArrayOfPtr * pSQArray = SQVRSetArray; SQVRSetArray = NULL; return pSQArray; } void DeleteSQArray (void) { if (SQVRSetArray) delete SQVRSetArray; SQVRSetArray = NULL; } ArrayOfPtr * AddSQArray (void) { if (! SQVRSetArray) SQVRSetArray = new ArrayOfPtr ; return SQVRSetArray; } DICOMVRSet * GetOwner (void) const { return Owner; } public: VR () { Group = 0; Element = 0; LongData = NULL; Length = 0; TypeCode = 0; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG Description[0] = 0; #endif } VR (UINT16 g, UINT16 e, const UID & Uid); VR (UINT16 g, UINT16 e, const char * Str); VR (UINT16 g, UINT16 e, const DString & string); VR (UINT16 g, UINT16 e, UINT16 us) { Group = g; Element = e; Length = sizeof(UINT16); LongData = NULL; UINT16 *pus = (UINT16*)ShortData; *pus = us; TypeCode = 'US'; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (UINT16 g, UINT16 e, UINT32 ul) { Group = g; Element = e; Length = sizeof(UINT32); LongData = NULL; UINT32 *pul = (UINT32*)ShortData; *pul = ul; TypeCode = 'UL'; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (UINT16 g, UINT16 e, INT16 ss) { Group = g; Element = e; Length = sizeof(INT16); LongData = NULL; INT16 *pss = (INT16*)ShortData; *pss = ss; TypeCode = 'SS'; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (UINT16 g, UINT16 e, INT32 sl) { Group = g; Element = e; Length = sizeof(INT32); LongData = NULL; INT32 *psl = (INT32*)ShortData; *psl = sl; TypeCode = 'SL'; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (UINT16 g, UINT16 e, float fl) { Group = g; Element = e; Length = sizeof(float); LongData = NULL; float * pfl = (float*) ShortData; *pfl = fl; TypeCode = 'FL'; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (UINT16 g, UINT16 e, double dl) { Group = g; Element = e; Length = sizeof(double); LongData = NULL; double * pdl = (double*) ShortData; *pdl = dl; TypeCode = 'FD'; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (UINT16 g, UINT16 e, UINT16 * pus, int Count); VR (UINT16 g, UINT16 e, UINT32 * pul, int Count); VR (UINT16 g, UINT16 e, INT16 * pss, int Count); VR (UINT16 g, UINT16 e, INT32 * psl, int Count); VR (UINT16 g, UINT16 e) { Group = g; Element = e; Length = 0; LongData = NULL; TypeCode = 0; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (UINT32 Tag) { Group = GroupOfTag32 (Tag); Element = ElementOfTag32 (Tag); Length = 0; LongData = NULL; TypeCode = 0; SQVRSetArray= NULL; Owner = NULL; #ifdef _DEBUG DicomDictionary::Lookup (Group, Element, Description, sizeof(Description)); #endif } VR (const VR &); virtual ~VR () { Reset (); }; // --------------- operator overload ----------- operator UINT16 (void) const { if (Length < sizeof(UINT16)) return 0; UINT16 * pus = (UINT16 *) ShortData; return (* pus); } operator UINT32 (void) const { if (Length <= sizeof(UINT16)) return (this->operator UINT16 ()); UINT32 * pul = (UINT32 *) ShortData; return (* pul); } operator INT16 (void) const { if (Length < sizeof(INT16)) return 0; INT16 * pss = (INT16 *) ShortData; return (* pss); } operator INT32 (void) const { if (Length <= sizeof(INT16)) return (this->operator INT16 ()); INT32 * psl = (INT32 *) ShortData; return (*psl); } operator float (void) const { if (Length < sizeof(float)) return 0; float * pfl = (float*) ShortData; return (*pfl); } operator double (void) const { if (Length < sizeof(double)) return 0; double * pdl = (double*) ShortData; return (*pdl); } operator DString (void) const { DString str; Extract (str); return str; } // --------------- Assignment overload ----------- VR & operator = (UINT16 us) { if (LongData) { delete LongData; LongData = NULL; } UINT16 * pus = (UINT16 *) ShortData; * pus = us; Length = sizeof(UINT16); return (*this); } VR & operator = (UINT32 ul) { if (LongData) { delete LongData; LongData = NULL; } UINT32 * pul = (UINT32 *) ShortData; * pul = ul; Length = sizeof(UINT32); return (*this); } VR & operator = (INT16 ss) { if (LongData) { delete LongData; LongData = NULL; } INT16 * pss = (INT16 *) ShortData; * pss = ss; Length = sizeof(INT16); return (*this); } VR & operator = (INT32 sl) { if (LongData) { delete LongData; LongData = NULL; } INT32 * psl = (INT32 *) ShortData; * psl = sl; Length = sizeof(INT32); return (*this); } VR & operator = (float f) { if (LongData) { delete LongData; LongData = NULL; } float * pf = (float *) ShortData; * pf = f; Length = sizeof (float); return (*this); } VR & operator = (double d) { if (LongData) { delete LongData; LongData = NULL; } double * pd = (double *) ShortData; * pd = d; Length = sizeof (double); return (*this); } VR & operator = (const char * str) { if (LongData) { delete LongData; LongData = NULL; } Length = strlen (str); if (! Length) { LongData = NULL; return (*this); } BYTE * Data; if (Length > sizeof (ShortData)) { LongData = Data = new BYTE [Length+1]; } else { Data = ShortData; LongData = NULL; } memcpy (Data, str, Length); if (Length & 0x01) { ((char *)Data)[Length] = ' '; Length ++; } return (*this); } VR & operator = (const DString & str) { if (LongData) { delete LongData; LongData = NULL; } Length = str.GetLength (); if (! Length) { LongData = NULL; return (*this); } BYTE * Data; if (Length > sizeof (ShortData)) { LongData = Data = new BYTE [Length+1]; } else { Data = ShortData; LongData = NULL; } memcpy (Data, str.constBuffer (), Length); if (Length & 0x01) { ((char *)Data)[Length] = ' '; Length ++; } return (*this); } VR & operator = (DICOMVRSet & fromVRS); // ------------------ Misc function -------------- UINT32 Extract (char * Str, UINT32 size) const ; BOOL Extract (DString & s) const; DString ToStringOfData (void) const; virtual DString ToString (void) const; void Reset (void) { if (LongData) delete LongData; if (SQVRSetArray) { delete SQVRSetArray; SQVRSetArray = NULL; } Owner = NULL; Length = 0; LongData = NULL; } protected: BOOL SetIf (VR *); friend class DICOMVRSet; friend class VRGroup; friend class ImageProcess; friend class DICOMVRSet; friend class DICOMFile; friend class DICOMImage; friend class Decoder; friend class Encoder; friend class FixDecoder; friend class FixEncoder; friend class BigEndianDecoder; friend class BigEndianEncoder; friend class VRSeqFixDecoder; friend class VRSeqFixEncoder; friend class TableSet; friend DICOM_API BOOL operator == (const VR & vr, UINT32 Tag) ; friend DICOM_API BOOL operator == (const VR & vrA, const VR & vrB); friend void operator >> (class InBuffer & ib, VR * vr); friend void operator << (class OutBuffer & ob, const VR * vr); }; // --------------- Comparision overload ----------- inline BOOL operator == (const VR & vr, UINT32 Tag) { return (vr.GetTag () == Tag); } inline BOOL operator != (const VR & vr, UINT32 Tag) { return ! (vr == Tag); }; inline BOOL operator > (const VR & vr, UINT32 Tag) { return (vr.GetTag () > Tag); } inline BOOL operator < (const VR & vr, UINT32 Tag) { return (vr.GetTag () < Tag); } DICOM_API BOOL operator == (const VR & vrA, const VR & vrB); DICOM_API inline BOOL operator != (const VR & vrA, const VR & vrB) { return ! (vrA == vrB); }; #endif