1 /*=========================================================================
2 
3   Program: GDCM (Grassroots DICOM). A DICOM library
4 
5   Copyright (c) 2006-2011 Mathieu Malaterre
6   All rights reserved.
7   See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
8 
9      This software is distributed WITHOUT ANY WARRANTY; without even
10      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11      PURPOSE.  See the above copyright notice for more information.
12 
13 =========================================================================*/
14 #include "gdcmExplicitDataElement.h"
15 #include "gdcmStringStream.h"
16 #include "gdcmSwapper.h"
17 
TestExplicitDataElement1(const uint16_t group,const uint16_t element,const char * vr,const uint32_t vl)18 int TestExplicitDataElement1(const uint16_t group,
19                              const uint16_t element,
20                              const char* vr,
21                              const uint32_t vl)
22 {
23   const char *str;
24   std::stringstream ss;
25   str = reinterpret_cast<const char*>(&group);
26   ss.write(str, sizeof(group));
27   str = reinterpret_cast<const char*>(&element);
28   ss.write(str, sizeof(element));
29   str = vr;
30   ss.write(str, 2);
31   ss.write("\0\0", 2);
32   str = reinterpret_cast<const char*>(&vl);
33   ss.write(str, sizeof(vl));
34 
35   gdcm::ExplicitDataElement de;
36   if( !de.Read<gdcm::SwapperNoOp>(ss) )
37     {
38     std::cerr << de << std::endl;
39     return 1;
40     }
41   if( de.GetTag().GetGroup()   != group ||
42       de.GetTag().GetElement() != element ||
43       de.GetVL()               != vl )
44     {
45     std::cerr << de << std::endl;
46     return 1;
47     }
48   std::cout << de << std::endl;
49   return 0;
50 }
51 
TestExplicitDataElement2(const uint16_t group,const uint16_t element,const char * vr,const char * value)52 int TestExplicitDataElement2(const uint16_t group,
53                              const uint16_t element,
54                              const char *vr,
55                              const char *value)
56 {
57   const char *str;
58   const uint32_t vl = strlen(value);
59   std::stringstream ss;
60   str = reinterpret_cast<const char*>(&group);
61   ss.write(str, sizeof(group));
62   str = reinterpret_cast<const char*>(&element);
63   ss.write(str, sizeof(element));
64   str = vr;
65   ss.write(str, 2);
66   ss.write("\0\0", 2);
67   str = reinterpret_cast<const char*>(&vl);
68   ss.write(str, sizeof(vl));
69   str = value;
70   ss.write(str, vl);
71 
72   gdcm::ExplicitDataElement de;
73   if( !de.Read<gdcm::SwapperNoOp>(ss) )
74     {
75     std::cerr << de << std::endl;
76     return 1;
77     }
78   if( de.GetTag().GetGroup()   != group ||
79       de.GetTag().GetElement() != element ||
80       de.GetVL()               != vl )
81     {
82     std::cerr << de << std::endl;
83     return 1;
84     }
85   std::cout << de << std::endl;
86   return 0;
87 }
88 
WriteRead(gdcm::DataElement const & w,gdcm::DataElement & r)89 inline void WriteRead(gdcm::DataElement const &w, gdcm::DataElement &r)
90 {
91   // w will be written
92   // r will be read back
93   std::stringstream ss;
94   w.Write<gdcm::SwapperNoOp>(ss);
95   r.Read<gdcm::SwapperNoOp>(ss);
96 }
97 
TestExplicitDataElement(int,char * [])98 int TestExplicitDataElement(int, char *[])
99 {
100   const uint16_t group   = 0x0010;
101   const uint16_t element = 0x0012;
102   const char vr[]        = "UN"; // UN => 4bytes vl
103   const uint32_t vl      = 0x0; // 4 bytes
104   const char value[]     = "GDCM";
105 
106   int r = 0;
107   r += TestExplicitDataElement1(group, element, vr, vl);
108   r += TestExplicitDataElement2(group, element, vr, value);
109 
110   gdcm::ExplicitDataElement de1(gdcm::Tag(0x1234, 0x5678), 0x4321);
111   gdcm::ExplicitDataElement de2(gdcm::Tag(0x1234, 0x6789), 0x9876);
112   WriteRead(de1, de2);
113   if( !(de1 == de2) )
114     {
115     std::cerr << de1 << std::endl;
116     std::cerr << de2 << std::endl;
117     return 1;
118     }
119 
120   return r;
121 }
122