DicomVRSet.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /****************************************************************
  2. * DICOMVRSet.hpp
  3. ****************************************************************/
  4. #ifndef _INCLUDE_DICOM_VRSET_
  5. #define _INCLUDE_DICOM_VRSET_
  6. class DICOMVRSet;
  7. class VRGroup;
  8. class DMarkup;
  9. class DICOMDataSet;
  10. class LinkedVR : public VR
  11. {
  12. public:
  13. inline LinkedVR (UINT16 g, UINT16 e, void * d, UINT32 l)
  14. {
  15. Group = g;
  16. Element = e;
  17. LongData = d;
  18. Length = l;
  19. SQVRSetArray= NULL;
  20. TypeCode = 0;
  21. }
  22. inline virtual ~LinkedVR ()
  23. {
  24. LongData = NULL;
  25. }
  26. };
  27. class VRItem
  28. {
  29. public:
  30. VR * vr;
  31. VRItem * PrevItem;
  32. VRItem * NextItem;
  33. inline VRItem (VRItem *p, VRItem *n)
  34. {
  35. PrevItem = p;
  36. NextItem = n;
  37. };
  38. inline VRItem ()
  39. {
  40. PrevItem = NULL;
  41. NextItem = NULL;
  42. };
  43. };
  44. class DICOM_API DICOMVRSet
  45. {
  46. public:
  47. UINT32 lParam;
  48. public:
  49. DICOMVRSet ();
  50. DICOMVRSet (const DICOMVRSet &);
  51. virtual ~DICOMVRSet () { Reset (); };
  52. BOOL Push (VR * vr);
  53. BOOL Push (UINT16 Group, UINT16 Element, DICOMVRSet *); // Sequence Embedding
  54. inline void Add (UINT32 tag)
  55. {
  56. if (! IsExist (tag))
  57. Push (new VR (tag));
  58. }
  59. inline void Add (UINT16 g, UINT16 e)
  60. {
  61. if (! IsExist (g, e))
  62. Push (new VR (g, e));
  63. }
  64. inline void Add (UINT16 g, UINT16 e, const UID & v)
  65. {
  66. VR * vr = GetVR (g, e);
  67. if (vr)
  68. (*vr) = v;
  69. else
  70. Push (new VR (g, e, v));
  71. }
  72. inline void Add (UINT16 g, UINT16 e, const char * v)
  73. {
  74. VR * vr = GetVR (g, e);
  75. if (vr)
  76. (*vr) = v;
  77. else
  78. Push (new VR (g, e, v));
  79. }
  80. inline void Add (UINT16 g, UINT16 e, const DString & v)
  81. {
  82. VR * vr = GetVR (g, e);
  83. if (vr)
  84. (*vr) = v;
  85. else
  86. Push (new VR (g, e, v));
  87. }
  88. inline void Add (UINT16 g, UINT16 e, UINT16 v)
  89. {
  90. VR * vr = GetVR (g, e);
  91. if (vr)
  92. (*vr) = v;
  93. else
  94. Push (new VR (g, e, v));
  95. }
  96. inline void Add (UINT16 g, UINT16 e, UINT32 v)
  97. {
  98. VR * vr = GetVR (g, e);
  99. if (vr)
  100. (*vr) = v;
  101. else
  102. Push (new VR (g, e, v));
  103. }
  104. inline void Add (UINT16 g, UINT16 e, INT16 v)
  105. {
  106. VR * vr = GetVR (g, e);
  107. if (vr)
  108. (*vr) = v;
  109. else
  110. Push (new VR (g, e, v));
  111. }
  112. inline void Add (UINT16 g, UINT16 e, INT32 v)
  113. {
  114. VR * vr = GetVR (g, e);
  115. if (vr)
  116. (*vr) = v;
  117. else
  118. Push (new VR (g, e, v));
  119. }
  120. inline void Add (UINT16 g, UINT16 e, float v)
  121. {
  122. VR * vr = GetVR (g, e);
  123. if (vr)
  124. (*vr) = v;
  125. else
  126. Push (new VR (g, e, v));
  127. }
  128. inline void Add (UINT16 g, UINT16 e, double v)
  129. {
  130. VR * vr = GetVR (g, e);
  131. if (vr)
  132. (*vr) = v;
  133. else
  134. Push (new VR (g, e, v));
  135. }
  136. inline void Add (UINT16 g, UINT16 e, DICOMVRSet & v)
  137. {
  138. VR * vr = GetVR (g, e);
  139. if (vr)
  140. (*vr) = v;
  141. else
  142. {
  143. vr = new VR (g, e);
  144. (*vr) = v;
  145. Push (vr);
  146. }
  147. }
  148. void Add (UINT16 g, UINT16 e, UINT16 * pus, int Count);
  149. void Add (UINT16 g, UINT16 e, UINT32 * pul, int Count);
  150. void Add (UINT16 g, UINT16 e, INT16 * pss, int Count);
  151. void Add (UINT16 g, UINT16 e, INT32 * psl, int Count);
  152. VR * Pop (void);
  153. VR * TakeOut (UINT16 g, UINT16 e);
  154. BOOL CopyTo (DICOMVRSet &) const;
  155. BOOL CopyTo (DICOMVRSet &, UINT32 FromTag, UINT32 ToTag = 0xFFFFFFFF) const;
  156. BOOL Delete (UINT16 g, UINT16 e);
  157. BOOL Delete (const DICOMVRSet & DVRS);
  158. BOOL Filter (const DICOMVRSet & DVRS);
  159. BOOL DeleteGroup (UINT16 g);
  160. VR * GetVR (UINT16 g, UINT16 e) const;
  161. VR * GetFirstVR () const;
  162. VR * GetLastVR () const;
  163. DICOMVRSet * GetSequence (UINT16 g, UINT16 e, int nItem = 0) const
  164. {
  165. VR * vr = this->GetVR (g, e);
  166. if (! vr) return NULL;
  167. Array <DICOMVRSet *> * pSQArray = vr->GetSQArray ();
  168. if (! pSQArray) return NULL;
  169. if (pSQArray->GetSize () <= nItem) return NULL;
  170. DICOMVRSet * VRS;
  171. if (nItem == 0)
  172. VRS = pSQArray->GetFirst ();
  173. else
  174. VRS = pSQArray->GetAt (nItem);
  175. return VRS;
  176. }
  177. inline LE_UINT16 GetUINT16 (UINT16 g, UINT16 e) const
  178. {
  179. VR * vr = this->GetVR (g, e);
  180. if (! vr) return (0);
  181. return (LE_UINT16) (*vr);
  182. }
  183. inline LE_UINT32 GetUINT32 (UINT16 g, UINT16 e) const
  184. {
  185. VR * vr = this->GetVR (g, e);
  186. if (! vr) return (0);
  187. return (LE_UINT32) (*vr);
  188. }
  189. inline DString GetDString (UINT16 g, UINT16 e) const
  190. {
  191. DString str;
  192. VR * vr = this->GetVR (g, e);
  193. if (vr) vr->Extract (str);
  194. return str;
  195. }
  196. inline float GetFloat (UINT16 g, UINT16 e) const
  197. {
  198. VR * vr = this->GetVR (g, e);
  199. if (! vr) return (0);
  200. return (float) (*vr);
  201. }
  202. inline double GetDouble (UINT16 g, UINT16 e) const
  203. {
  204. VR * vr = this->GetVR (g, e);
  205. if (! vr) return (0);
  206. return (double) (*vr);
  207. }
  208. inline int GetSize (void) const { return GroupCount; }
  209. inline int GetLength (void) const { return Length; }
  210. inline int GetVRCount (void) const;
  211. BOOL Package (void);
  212. BOOL Package (UINT16 g);
  213. inline BOOL IsEmpty (void) const { return (GroupCount == 0); }
  214. inline BOOL IsExist (UINT16 g, UINT16 e = 0) const
  215. {
  216. if (e)
  217. return (UINT32) GetVR (g,e);
  218. else
  219. return (UINT32) SeekFor (g);
  220. }
  221. inline BOOL IsExist (UINT32 tag) const
  222. {
  223. UINT16 g = GroupOfTag32 (tag);
  224. UINT16 e = ElementOfTag32 (tag);
  225. if (e)
  226. return (UINT32) GetVR (g,e);
  227. else
  228. return (UINT32) SeekFor (g);
  229. }
  230. virtual void Reset (void);
  231. DICOMVRSet & operator = (DICOMVRSet & DVRS);
  232. DICOMVRSet & TransferTo (DICOMVRSet & ToVRS);
  233. // Append all VR from FromVRS to me
  234. DICOMVRSet & AppendFrom (DICOMVRSet & FromVRS);
  235. virtual int Serialize (BYTE * pBuffer, int BufferSize) const;
  236. virtual int Deserialize (const BYTE * pBuffer, int BufferSize);
  237. // VRSetToXML
  238. virtual DString XmlSerialize (void) const;
  239. // XMLToVRSet
  240. virtual int XmlDeserialize (const char * XML);
  241. virtual int XmlDeserialize (const DString & XML);
  242. virtual DString ToString (void) const;
  243. virtual DString ToString (int nMinLength) const;
  244. protected:
  245. void Init (void)
  246. {
  247. lParam = 0;
  248. GroupCount = 0;
  249. FirstGroup = NULL;
  250. LastGroup = NULL;
  251. Length = 0;
  252. LastVR = NULL;
  253. LastVRG = NULL;
  254. CurrentGroup= NULL;
  255. }
  256. VRGroup * SeekFor (UINT16 g) const;
  257. BOOL PushGroup (VRGroup * VRGroupPtr);
  258. VRGroup * PopGroup (void);
  259. BOOL Push (DICOMVRSet *); // Sequence Embedding
  260. DString ToStringOfTabStop (int nbTab, int nMinLength = 0) const;
  261. int XmlSerialize (DMarkup * xml) const;
  262. int XmlDeserialize (DMarkup * xml);
  263. static BOOL XMLToVRSetArray (DMarkup * xml, Array <DICOMVRSet *> * arVRS);
  264. static BOOL XMLToADDS (DMarkup * xml, Array <DICOMDataSet*> * ADDS);
  265. private:
  266. VRGroup * FirstGroup;
  267. VRGroup * LastGroup;
  268. int GroupCount;
  269. VRGroup * CurrentGroup;
  270. mutable VRGroup * LastVRG;
  271. VR * LastVR;
  272. UINT32 Length;
  273. static VR ReservedVR;
  274. friend class VRSetIterator;
  275. friend class FixDecoder;
  276. friend class BigEndianDecoder;
  277. friend class Iterator;
  278. public:
  279. static BOOL VRSetArrayToXML (const Array <const DICOMVRSet *> * arVRS, DString * str);
  280. static BOOL VRSetArrayToXML (const Array <DICOMVRSet *> * arVRS, DString * str);
  281. static BOOL XMLToVRSetArray (const DString * str, Array <DICOMVRSet *> * arVRS);
  282. static BOOL ADDSToXML (const Array <const DICOMDataSet *> * ADDS, DString * str);
  283. static BOOL ADDSToXML (const Array <DICOMDataSet *> * ADDS, DString * str);
  284. static BOOL XMLToADDS (const DString * str, Array <DICOMDataSet *> * ADDS);
  285. public:
  286. class DICOM_API Iterator
  287. {
  288. private:
  289. const DICOMVRSet & DVS;
  290. int TotalVRCount;
  291. VRGroup * CurrentGroup;
  292. VRItem * CurrentItem;
  293. VR * CurrentVR;
  294. int m_Index;
  295. bool m_Continue;
  296. public:
  297. inline Iterator (const DICOMVRSet & theDVS) : DVS (theDVS)
  298. {
  299. Restart ();
  300. }
  301. inline void Restart ()
  302. {
  303. TotalVRCount = DVS.GetVRCount ();
  304. CurrentGroup = DVS.FirstGroup;
  305. CurrentItem = NULL;
  306. CurrentVR = NULL;
  307. m_Index = -1;
  308. m_Continue = true;
  309. Next ();
  310. }
  311. inline operator bool () const { return m_Continue; }
  312. inline VR * Current (void) const { return CurrentVR; }
  313. inline int Index (void) const { return m_Index; }
  314. inline int GetNbOfVR (void) const { return TotalVRCount; }
  315. inline void operator ++ () // ++ Iter;
  316. {
  317. Next ();
  318. }
  319. inline void operator ++ (int) // Iter ++;
  320. {
  321. Next ();
  322. }
  323. inline VR * operator () (void) { return Current (); }
  324. inline VR * operator * (void) { return Current (); }
  325. inline operator INT16 () const { return (INT16) (*Current ()); }
  326. inline operator INT32 () const { return (INT32) (*Current ()); }
  327. inline operator UINT16 () const { return (UINT16) (*Current ()); }
  328. inline operator UINT32 () const { return (UINT32) (*Current ()); }
  329. inline operator DString () const { return (DString) (*Current ()); }
  330. void Next ();
  331. };
  332. public:
  333. class DICOM_API Proxy
  334. {
  335. public:
  336. inline Proxy (DICOMVRSet & VRS, UINT16 g, UINT16 e) :
  337. m_VRSet (VRS),
  338. m_Group (g), m_Element (e)
  339. {
  340. }
  341. inline Proxy & operator = (const Proxy & vrp)
  342. {
  343. VR * vr = vrp.m_VRSet.GetVR (vrp.m_Group, vrp.m_Element);
  344. if (vr)
  345. {
  346. VR * nvr = new VR (*vr);
  347. nvr->SetTag (m_Group, m_Element);
  348. m_VRSet.Push (nvr);
  349. }
  350. return (*this);
  351. }
  352. inline Proxy & operator = (UINT16 v)
  353. {
  354. m_VRSet.Add (m_Group, m_Element, v);
  355. return (*this);
  356. }
  357. inline Proxy & operator = (UINT32 v)
  358. {
  359. m_VRSet.Add (m_Group, m_Element, v);
  360. return (*this);
  361. }
  362. inline Proxy & operator = (INT16 v)
  363. {
  364. m_VRSet.Add (m_Group, m_Element, v);
  365. return (*this);
  366. }
  367. inline Proxy & operator = (INT32 v)
  368. {
  369. m_VRSet.Add (m_Group, m_Element, v);
  370. return (*this);
  371. }
  372. inline Proxy & operator = (float v)
  373. {
  374. m_VRSet.Add (m_Group, m_Element, v);
  375. return (*this);
  376. }
  377. inline Proxy & operator = (double v)
  378. {
  379. m_VRSet.Add (m_Group, m_Element, v);
  380. return (*this);
  381. }
  382. inline Proxy & operator = (const char * v)
  383. {
  384. m_VRSet.Add (m_Group, m_Element, v);
  385. return (*this);
  386. }
  387. inline Proxy & operator = (const UID & v)
  388. {
  389. m_VRSet.Add (m_Group, m_Element, v);
  390. return (*this);
  391. }
  392. inline Proxy & operator = (const DString & v)
  393. {
  394. m_VRSet.Add (m_Group, m_Element, v);
  395. return (*this);
  396. }
  397. inline Proxy & operator = (DICOMVRSet & v)
  398. {
  399. m_VRSet.Add (m_Group, m_Element, v);
  400. return (*this);
  401. }
  402. inline operator DString () const
  403. {
  404. DString str = m_VRSet.GetDString (m_Group, m_Element);
  405. return str;
  406. }
  407. inline operator UINT32 () const
  408. {
  409. return m_VRSet.GetUINT32 (m_Group, m_Element);
  410. }
  411. inline operator UINT16 () const
  412. {
  413. return m_VRSet.GetUINT16 (m_Group, m_Element);
  414. }
  415. inline operator INT32 () const
  416. {
  417. return (INT32) (m_VRSet.GetUINT32 (m_Group, m_Element));
  418. }
  419. inline operator INT16 () const
  420. {
  421. return (INT16) (m_VRSet.GetUINT16 (m_Group, m_Element));
  422. }
  423. inline operator float () const
  424. {
  425. return m_VRSet.GetFloat (m_Group, m_Element);
  426. }
  427. inline operator double () const
  428. {
  429. return m_VRSet.GetDouble (m_Group, m_Element);
  430. }
  431. private:
  432. DICOMVRSet & m_VRSet;
  433. UINT16 m_Group;
  434. UINT16 m_Element;
  435. };
  436. inline Proxy operator [] (UINT32 tag)
  437. {
  438. return Proxy (*this, GroupOfTag32 (tag), ElementOfTag32 (tag));
  439. }
  440. inline Proxy operator () (UINT32 tag)
  441. {
  442. return Proxy (*this, GroupOfTag32 (tag), ElementOfTag32 (tag));
  443. }
  444. inline Proxy operator () (UINT16 g, UINT16 e)
  445. {
  446. return Proxy (*this, g, e);
  447. }
  448. inline const Proxy operator [] (UINT32 tag) const
  449. {
  450. return Proxy (const_cast <DICOMVRSet &> (*this), GroupOfTag32 (tag), ElementOfTag32 (tag));
  451. }
  452. inline const Proxy operator () (UINT32 tag) const
  453. {
  454. return Proxy (const_cast <DICOMVRSet &> (*this), GroupOfTag32 (tag), ElementOfTag32 (tag));
  455. }
  456. inline const Proxy operator () (UINT16 g, UINT16 e) const
  457. {
  458. return Proxy (const_cast <DICOMVRSet &> (*this), g, e);
  459. }
  460. friend class Proxy;
  461. };
  462. class VRGroup
  463. {
  464. private:
  465. VRGroup * PrevGroup;
  466. VRGroup * NextGroup;
  467. VRItem * FirstItem;
  468. VRItem * LastItem;
  469. int ItemCount;
  470. private:
  471. UINT16 Group;
  472. UINT32 Length;
  473. LinkedVR *Element0;
  474. //public:
  475. protected:
  476. VRGroup (UINT16 G);
  477. ~VRGroup ()
  478. {
  479. Reset () ;
  480. delete Element0;
  481. Element0 = NULL;
  482. }
  483. void Reset (void);
  484. BOOL Push (VR * vr);
  485. VR * Pop (void);
  486. VR * TakeOut (UINT16 e);
  487. VR * SeekFor (UINT16 e) const ;
  488. int GetVRCount (void) const { return (ItemCount+1) ; } // All VR plus the Element0
  489. protected:
  490. BOOL Package (void);
  491. friend class DICOMVRSet;
  492. friend class DICOMVRSet::Iterator;
  493. friend class VRSetIterator;
  494. };
  495. inline void DICOMVRSet::Iterator::Next ()
  496. {
  497. if (! CurrentItem)
  498. {
  499. if (! CurrentGroup)
  500. {
  501. CurrentVR = NULL;
  502. m_Continue = false;
  503. m_Index = -1;
  504. return;
  505. }
  506. m_Index ++;
  507. CurrentItem = CurrentGroup->FirstItem;
  508. CurrentVR = CurrentGroup -> Element0;
  509. return;
  510. }
  511. m_Index ++;
  512. CurrentVR = CurrentItem -> vr;
  513. CurrentItem = CurrentItem->NextItem;
  514. if (! CurrentItem)
  515. CurrentGroup = CurrentGroup->NextGroup;
  516. }
  517. DICOM_API BOOL operator == (const DICOMVRSet & SetA, const DICOMVRSet & SetB) ;
  518. inline DICOM_API BOOL operator != (const DICOMVRSet & SetA, const DICOMVRSet & SetB)
  519. {
  520. return ! (SetA == SetB);
  521. }
  522. //////////////////////////////////////////////////////////////////////
  523. // class VRSetIterator
  524. class DICOM_API VRSetIterator
  525. {
  526. private:
  527. const DICOMVRSet & DVS;
  528. int TotalVRCount;
  529. VRGroup * CurrentGroup;
  530. VRItem * CurrentItem;
  531. VR * LastVR;
  532. int VRCount;
  533. public:
  534. VRSetIterator (const DICOMVRSet & theDVS) : DVS (theDVS)
  535. {
  536. TotalVRCount = theDVS.GetVRCount ();
  537. CurrentGroup = theDVS.FirstGroup;
  538. CurrentItem = NULL;
  539. LastVR = NULL;
  540. VRCount = 0;
  541. }
  542. VR * operator () (void);
  543. };
  544. #endif