1 #include <creaImageIOTreeDescriptor.h>
2 #include <boost/algorithm/string/split.hpp>
3 #include <boost/algorithm/string/replace.hpp>
4 #include <creaImageIOSystem.h>
5 #include <boost/filesystem.hpp>
17 //==================================================================
18 /// The attribute is hidden (not visible to user)
19 const unsigned int AttributeDescriptor::PRIVATE = 1;
20 /// The attribute enters in unique identifier constitution
21 const unsigned int AttributeDescriptor::IDENTIFIER = 2;
22 /// The attribute can be edited
23 const unsigned int AttributeDescriptor::EDITABLE = 3;
24 /// the attribute describes the node
25 const unsigned int AttributeDescriptor::LABEL = 4;
26 //==================================================================
28 //==================================================================
29 Descriptor::Descriptor()
31 CreateLevel0Descriptor();
33 //==================================================================
35 //==================================================================
36 Descriptor::~Descriptor()
39 //==================================================================
41 //==================================================================
42 void Descriptor::CreateLevel0Descriptor()
44 Add(LevelDescriptor("Root"));
46 //==================================================================
48 //==================================================================
49 /// Creates the default descriptor
50 void Descriptor::CreateDefault()
52 // clears the existing one
55 // Creates the level 0 descriptor
56 CreateLevel0Descriptor();
57 // Creates the attribute "Name"
58 Add(AttributeDescriptor("Name","Name",
59 AttributeDescriptor::LABEL),0);
62 Add(LevelDescriptor("Patient"));
63 Add(AttributeDescriptor("NumberOfChildren","#Series",0),1); // Number of Series
64 Add(AttributeDescriptor(0x0010,0x0010, // Patient name
65 AttributeDescriptor::LABEL),1);
66 Add(AttributeDescriptor(0x0010,0x0040),1); // Patient sex
67 Add(AttributeDescriptor(0x0010,0x0030),1); // Patient birthday
68 Add(AttributeDescriptor(0x0010,0x0020, // Patient ID
69 AttributeDescriptor::IDENTIFIER),1);
72 Add(LevelDescriptor("Series"));
73 Add(AttributeDescriptor("NumberOfChildren","#Images",0),2); // Number of images
74 Add(AttributeDescriptor(0x0008,0x0060, // Modality
75 AttributeDescriptor::LABEL),2);
76 Add(AttributeDescriptor(0x0008,0x1030),2); // Study Description
77 Add(AttributeDescriptor(0x0008,0x103E),2); // Description
78 Add(AttributeDescriptor(0x0008,0x0080),2); // Institution Name
79 Add(AttributeDescriptor(0x0008,0x0081),2); // Institution Adress
80 Add(AttributeDescriptor(0x0008,0x1010),2); // Station Name
81 Add(AttributeDescriptor(0x0008,0x1048),2); // Physician of Record
82 Add(AttributeDescriptor(0x0008,0x1050),2); // Performing Physician's Name
83 Add(AttributeDescriptor(0x0018,0x1030),2); // Protocol Name
85 Add(AttributeDescriptor(0x0020,0x0010),2); // Study ID
86 Add(AttributeDescriptor(0x0008,0x0020),2); // Study Date
87 Add(AttributeDescriptor(0x0008,0x0030),2); // Study Time
88 Add(AttributeDescriptor(0x0008,0x0050),2); // Study Accession Number
89 Add(AttributeDescriptor(0x0008,0x0005),2); // Specific character set
90 Add(AttributeDescriptor(0x0008,0x0021),2); // Series Date
91 Add(AttributeDescriptor(0x0008,0x0031),2); // Series time
93 Add(AttributeDescriptor(0x0020,0x000D // Study Instance UID
94 ),2);//AttributeDescriptor::IDENTIFIER),2);
95 Add(AttributeDescriptor(0x0020,0x000E, // Series Instance UID
96 AttributeDescriptor::IDENTIFIER),2);
98 // AttributeDescriptor::LABEL),2);
102 Add(LevelDescriptor("Image"));
104 Add(AttributeDescriptor(0x0020,0x0013),3); // Image Number
106 Add(AttributeDescriptor(0x0028,0x0010),3); // Rows
107 Add(AttributeDescriptor(0x0028,0x0011),3); // Columns
108 Add(AttributeDescriptor(0x0028,0x0012),3); // Planes
109 Add(AttributeDescriptor(0x0028,0x0002),3); // Sample per pixels
110 Add(AttributeDescriptor(0x0028,0x0008),3); // Number of Frames
111 Add(AttributeDescriptor(0x0028,0x0004),3); // Photometric Interpretation
112 Add(AttributeDescriptor(0x0028,0x0103),3); // Pixel Representation
114 Add(AttributeDescriptor(0x0020,0x0032),3); // Image Position Patient
115 Add(AttributeDescriptor(0x0020,0x0037),3); // Image Orientation Patient
116 Add(AttributeDescriptor(0x0020,0x1041),3); // Slice Location
117 Add(AttributeDescriptor(0x0028,0x0006),3); // Planar Configuration
119 Add(AttributeDescriptor(0x0028,0x0030),3); // Pixel Spacing
120 Add(AttributeDescriptor(0x0028,0x0100),3); // AlocatedBits
121 Add(AttributeDescriptor(0x0028,0x0101),3); // StoredBits
123 Add(AttributeDescriptor(0x0008,0x0008),3); // Image Type
124 Add(AttributeDescriptor(0x0008,0x0023),3); // Content Date
125 Add(AttributeDescriptor(0x0008,0x0033),3); // Content Time
127 Add(AttributeDescriptor(0x0020,0x4000),3); // Image Comments
129 Add(AttributeDescriptor(0x0004,0x1500, // File Name
130 AttributeDescriptor::LABEL),3);
131 Add(AttributeDescriptor(0x0028,0x1052),3); // Rescale Intercept
132 Add(AttributeDescriptor(0x0028,0x1053),3); // Rescale Slope
134 Add(AttributeDescriptor(0x0050,0x0004),3); // Calibration Image
136 Add(AttributeDescriptor(0x0020,0x0052 // Frame Reference UID
138 Add(AttributeDescriptor(0x0008,0x0016),3); // SOP Class UID
139 Add(AttributeDescriptor("FullFileName", // Full file name
141 AttributeDescriptor::IDENTIFIER),3);
145 //////////////////////////////////////////////////////////////
146 // create a descriptor (name, attributes...) from a file) //
147 // @param : file path //
149 //////////////////////////////////////////////////////////////
150 void Descriptor::createDescriptorfromFile(const std::string &i_name)
154 // read file and put in buffer
155 std::ifstream i_file(i_name.c_str());
156 std::stringstream buffer;
157 buffer << i_file.rdbuf();
163 while(std::getline(buffer, line))
166 { //increment levels.
172 // For each level, a name to describe it
173 Add(LevelDescriptor(line));
178 // split line to find all tags
179 std::vector<std::string> descriptors;
180 std::string separator = " ";
181 std::string::size_type last_pos = line.find_first_not_of(separator);
182 //find first separator
183 std::string::size_type pos = line.find_first_of(separator, last_pos);
184 while(std::string::npos != pos || std::string::npos != last_pos)
186 descriptors.push_back(line.substr(last_pos, pos - last_pos));
187 last_pos = line.find_first_not_of(separator, pos);
188 pos = line.find_first_of(separator, last_pos);
191 // By default, the last tag is at zero and not recorded but if take in count
192 unsigned int flag = 0;
193 if(descriptors.size() == 4)
195 std::stringstream val;
196 val << std::dec << descriptors[3];
200 // if Dicom tag, use "group" and "element" descriptor
201 if(descriptors[0] == "D")
202 { std::stringstream val, val2;
203 unsigned short group;
204 unsigned short element;
205 val << std::dec << descriptors[1] ;
206 val >> std::hex >> group;
207 val2 << std::dec << descriptors[2];
208 val2 >> std::hex >> element;
209 Add(AttributeDescriptor( group,element,flag), ilevel);
212 else if(descriptors[0].find("#") != -1)
214 // commented line continue to next line
217 { boost::algorithm::replace_all(descriptors[2],"_"," ");
218 Add(AttributeDescriptor( descriptors[1].c_str(),descriptors[2].c_str(),flag), ilevel);
224 //==================================================================
226 //==================================================================
227 /// Adds a LevelDescriptor at the end of the list
228 void Descriptor::Add(const LevelDescriptor& d)
230 mLevelDescriptorList.push_back(d);
232 //==================================================================
234 //==================================================================
235 /// Adds an AttributeDescriptor to level l
236 void Descriptor::Add(const AttributeDescriptor& d, int l)
238 mLevelDescriptorList[l].Add(d);
239 // TO DO : update DicomTagToName and NameToDicomTag map
241 //==================================================================
243 //==================================================================
244 /// Clears the Descriptor
245 void Descriptor::Clear()
247 mLevelDescriptorList.clear();
250 //==================================================================
252 //==================================================================
253 /// Builds the key to value map of all the attributes of the tree
254 void Descriptor::BuildAttributeMap( std::map<std::string,std::string>& map ) const
257 LevelDescriptorListType::const_iterator l;
258 for (l = GetLevelDescriptorList().begin();
259 l!= GetLevelDescriptorList().end();
262 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
263 for (a = l->GetAttributeDescriptorList().begin();
264 a!= l->GetAttributeDescriptorList().end();