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 const unsigned int AttributeDescriptor::LABEL = 4;
23 //==================================================================
25 //==================================================================
26 Descriptor::Descriptor()
28 CreateLevel0Descriptor();
30 //==================================================================
32 //==================================================================
33 Descriptor::~Descriptor()
36 //==================================================================
38 //==================================================================
39 void Descriptor::CreateLevel0Descriptor()
41 Add(LevelDescriptor("Root"));
43 //==================================================================
45 //==================================================================
46 /// Creates the default descriptor
47 void Descriptor::CreateDefault()
49 // clears the existing one
52 // Creates the level 0 descriptor
53 CreateLevel0Descriptor();
54 // Creates the attribute "Name"
55 Add(AttributeDescriptor("Name","Name",
56 AttributeDescriptor::LABEL),0);
59 Add(LevelDescriptor("Patient"));
60 Add(AttributeDescriptor("NumberOfChildren","#Series",0),1); // Number of Series
61 Add(AttributeDescriptor(0x0010,0x0010, // Patient name
62 AttributeDescriptor::LABEL),1);
63 Add(AttributeDescriptor(0x0010,0x0040),1); // Patient sex
64 Add(AttributeDescriptor(0x0010,0x0030),1); // Patient birthday
65 Add(AttributeDescriptor(0x0010,0x0020, // Patient ID
66 AttributeDescriptor::IDENTIFIER),1);
69 Add(LevelDescriptor("Series"));
70 Add(AttributeDescriptor("NumberOfChildren","#Images",0),2); // Number of images
71 Add(AttributeDescriptor(0x0008,0x0060, // Modality
72 AttributeDescriptor::LABEL),2);
73 Add(AttributeDescriptor(0x0008,0x1030),2); // Study Description
74 Add(AttributeDescriptor(0x0008,0x103E),2); // Description
75 Add(AttributeDescriptor(0x0008,0x0080),2); // Institution Name
76 Add(AttributeDescriptor(0x0008,0x0081),2); // Institution Adress
77 Add(AttributeDescriptor(0x0008,0x1010),2); // Station Name
78 Add(AttributeDescriptor(0x0008,0x1048),2); // Physician of Record
79 Add(AttributeDescriptor(0x0008,0x1050),2); // Performing Physician's Name
80 Add(AttributeDescriptor(0x0018,0x1030),2); // Protocol Name
82 Add(AttributeDescriptor(0x0020,0x0010),2); // Study ID
83 Add(AttributeDescriptor(0x0008,0x0020),2); // Study Date
84 Add(AttributeDescriptor(0x0008,0x0030),2); // Study Time
85 Add(AttributeDescriptor(0x0008,0x0050),2); // Study Accession Number
86 Add(AttributeDescriptor(0x0008,0x0005),2); // Specific character set
87 Add(AttributeDescriptor(0x0008,0x0021),2); // Series Date
88 Add(AttributeDescriptor(0x0008,0x0031),2); // Series time
90 Add(AttributeDescriptor(0x0020,0x000D // Study Instance UID
91 ),2);//AttributeDescriptor::IDENTIFIER),2);
92 Add(AttributeDescriptor(0x0020,0x000E, // Series Instance UID
93 AttributeDescriptor::IDENTIFIER),2);
95 // AttributeDescriptor::LABEL),2);
99 Add(LevelDescriptor("Image"));
101 Add(AttributeDescriptor(0x0020,0x0013),3); // Image Number
103 Add(AttributeDescriptor(0x0028,0x0010),3); // Rows
104 Add(AttributeDescriptor(0x0028,0x0011),3); // Columns
105 Add(AttributeDescriptor(0x0028,0x0012),3); // Planes
106 Add(AttributeDescriptor(0x0028,0x0002),3); // Sample per pixels
107 Add(AttributeDescriptor(0x0028,0x0008),3); // Number of Frames
108 Add(AttributeDescriptor(0x0028,0x0004),3); // Photometric Interpretation
109 Add(AttributeDescriptor(0x0028,0x0103),3); // Pixel Representation
111 Add(AttributeDescriptor(0x0020,0x0032),3); // Image Position Patient
112 Add(AttributeDescriptor(0x0020,0x0037),3); // Image Orientation Patient
113 Add(AttributeDescriptor(0x0020,0x1041),3); // Slice Location
114 Add(AttributeDescriptor(0x0028,0x0006),3); // Planar Configuration
116 Add(AttributeDescriptor(0x0028,0x0030),3); // Pixel Spacing
117 Add(AttributeDescriptor(0x0028,0x0100),3); // AlocatedBits
118 Add(AttributeDescriptor(0x0028,0x0101),3); // StoredBits
120 Add(AttributeDescriptor(0x0008,0x0008),3); // Image Type
121 Add(AttributeDescriptor(0x0008,0x0023),3); // Content Date
122 Add(AttributeDescriptor(0x0008,0x0033),3); // Content Time
124 Add(AttributeDescriptor(0x0020,0x4000),3); // Image Comments
126 Add(AttributeDescriptor(0x0004,0x1500, // File Name
127 AttributeDescriptor::LABEL),3);
128 Add(AttributeDescriptor(0x0028,0x1052),3); // Rescale Intercept
129 Add(AttributeDescriptor(0x0028,0x1053),3); // Rescale Slope
131 Add(AttributeDescriptor(0x0050,0x0004),3); // Calibration Image
133 Add(AttributeDescriptor(0x0020,0x0052 // Frame Reference UID
135 Add(AttributeDescriptor(0x0008,0x0016),3); // SOP Class UID
136 Add(AttributeDescriptor("FullFileName", // Full file name
138 AttributeDescriptor::IDENTIFIER),3);
143 //////////////////////////////////////////////////////////////
144 // create a descriptor (name, attributes...) from a file) //
145 // @param : file path //
147 //////////////////////////////////////////////////////////////
148 void Descriptor::createDescriptorfromFile(std::string &i_name)
152 // read file and put in buffer
153 std::ifstream i_file(i_name.c_str());
154 std::stringstream buffer;
155 buffer << i_file.rdbuf();
161 while(std::getline(buffer, line))
164 { //increment levels.
170 // For each level, a name to describe it
171 Add(LevelDescriptor(line));
176 // split line to find all tags
177 std::vector<std::string> descriptors;
178 std::string separator = " ";
179 std::string::size_type last_pos = line.find_first_not_of(separator);
180 //find first separator
181 std::string::size_type pos = line.find_first_of(separator, last_pos);
182 while(std::string::npos != pos || std::string::npos != last_pos)
184 descriptors.push_back(line.substr(last_pos, pos - last_pos));
185 last_pos = line.find_first_not_of(separator, pos);
186 pos = line.find_first_of(separator, last_pos);
189 // By default, the last tag is at zero and not recorded but if take in count
190 unsigned int flag = 0;
191 if(descriptors.size() == 4)
193 std::stringstream val;
194 val << std::dec << descriptors[3];
198 // if Dicom tag, use "group" and "element" descriptor
199 if(descriptors[0] == "D")
200 { std::stringstream val, val2;
201 unsigned short group;
202 unsigned short element;
203 val << std::dec << descriptors[1] ;
204 val >> std::hex >> group;
205 val2 << std::dec << descriptors[2];
206 val2 >> std::hex >> element;
207 Add(AttributeDescriptor( group,element,flag), ilevel);
209 else // "O" means if user's own tag.
210 { boost::algorithm::replace_all(descriptors[2],"_"," ");
211 Add(AttributeDescriptor( descriptors[1].c_str(),descriptors[2].c_str(),flag), ilevel);
218 //==================================================================
220 //==================================================================
221 /// Adds a LevelDescriptor at the end of the list
222 void Descriptor::Add(const LevelDescriptor& d)
224 mLevelDescriptorList.push_back(d);
226 //==================================================================
228 //==================================================================
229 /// Adds an AttributeDescriptor to level l
230 void Descriptor::Add(const AttributeDescriptor& d, int l)
232 mLevelDescriptorList[l].Add(d);
233 // TO DO : update DicomTagToName and NameToDicomTag map
235 //==================================================================
237 //==================================================================
238 /// Clears the Descriptor
239 void Descriptor::Clear()
241 mLevelDescriptorList.clear();
243 //==================================================================
245 //==================================================================
246 /// Builds the key to value map of all the attributes of the tree
247 void Descriptor::BuildAttributeMap( std::map<std::string,std::string>& map ) const
250 LevelDescriptorListType::const_iterator l;
251 for (l = GetLevelDescriptorList().begin();
252 l!= GetLevelDescriptorList().end();
255 LevelDescriptor::AttributeDescriptorListType::const_iterator a;
256 for (a = l->GetAttributeDescriptorList().begin();
257 a!= l->GetAttributeDescriptorList().end();