]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeDescriptor.cpp
6b176c90af68ec6e746551a166500dcec76774c4
[creaImageIO.git] / src / creaImageIOTreeDescriptor.cpp
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>
6
7
8 #include <fstream>
9
10
11 namespace creaImageIO
12 {
13
14   namespace tree
15   {
16
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     //==================================================================
27
28     //==================================================================
29     Descriptor::Descriptor()
30     {
31       CreateLevel0Descriptor();
32     }
33     //==================================================================
34
35    //==================================================================
36     Descriptor::~Descriptor()
37     {
38     }
39     //==================================================================
40
41     //==================================================================
42     void Descriptor::CreateLevel0Descriptor()
43     {
44       Add(LevelDescriptor("Root"));
45     }
46     //==================================================================
47     
48     //==================================================================
49     /// Creates the default descriptor
50     void Descriptor::CreateDefault()
51     {
52       // clears the existing one
53       Clear();
54       
55       // Creates the level 0 descriptor 
56       CreateLevel0Descriptor();
57       // Creates the attribute "Name"
58       Add(AttributeDescriptor("Name","Name",
59                               AttributeDescriptor::LABEL),0);
60       
61       // Patient level
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);
70
71       // Study-series level
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
84
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
92
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);
97       // |
98       //                        AttributeDescriptor::LABEL),2);
99      
100
101       // Image level
102       Add(LevelDescriptor("Image"));
103
104       Add(AttributeDescriptor(0x0020,0x0013),3); // Image Number
105
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
113
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
118
119       Add(AttributeDescriptor(0x0028,0x0030),3); // Pixel Spacing
120       Add(AttributeDescriptor(0x0028,0x0100),3); // AlocatedBits
121       Add(AttributeDescriptor(0x0028,0x0101),3); // StoredBits
122
123       Add(AttributeDescriptor(0x0008,0x0008),3); // Image Type
124       Add(AttributeDescriptor(0x0008,0x0023),3); // Content Date
125       Add(AttributeDescriptor(0x0008,0x0033),3); // Content Time
126
127       Add(AttributeDescriptor(0x0020,0x4000),3); // Image Comments
128
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
133
134       Add(AttributeDescriptor(0x0050,0x0004),3); // Calibration Image
135
136       Add(AttributeDescriptor(0x0020,0x0052   // Frame Reference UID
137                                            ),3);
138       Add(AttributeDescriptor(0x0008,0x0016),3); // SOP Class UID
139       Add(AttributeDescriptor("FullFileName",  // Full file name
140                                 "Full file name",
141                                 AttributeDescriptor::IDENTIFIER),3); 
142
143     }
144
145         //////////////////////////////////////////////////////////////
146         // create a descriptor (name, attributes...) from a file)       //
147         // @param : file path                                           //
148         // return : -                                                   //
149         //////////////////////////////////////////////////////////////
150         void Descriptor::createDescriptorfromFile(const std::string &i_name)
151         {
152                 Clear();
153                 
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();
158                 std::string line;
159                 bool bname;
160                 int ilevel = -1;
161
162                 
163                 while(std::getline(buffer, line))
164                 {
165                         if(line =="<level>")
166                         {       //increment levels.
167                                 ilevel++;
168                                 bname = true;
169                         }
170                         else if(bname)
171                         {
172                                 // For each level, a name to describe it
173                                 Add(LevelDescriptor(line));
174                                 bname = false;
175                         }
176                         else if(line.empty()) // to avoid end line
177                         {
178                                 return;
179                         }
180                         else
181                         { 
182                                 // split line to find all tags
183                                 std::vector<std::string> descriptors;
184                                 std::string separator = " ";
185                                 std::string::size_type last_pos = line.find_first_not_of(separator);
186                                 //find first separator
187                                 std::string::size_type pos = line.find_first_of(separator, last_pos);
188                                 while(std::string::npos != pos || std::string::npos != last_pos)
189                                 {
190                                         descriptors.push_back(line.substr(last_pos, pos - last_pos));
191                                         last_pos = line.find_first_not_of(separator, pos);
192                                         pos = line.find_first_of(separator, last_pos);
193                                 }
194                                 
195                                 // By default, the last tag is at zero and not recorded but if take in count
196                                 unsigned int flag = 0;
197                                 if(descriptors.size() == 4)
198                                 {
199                                         std::stringstream val;
200                                         val << std::dec << descriptors[3];
201                                         val>> flag;
202                                 }
203
204                                 // if Dicom tag, use "group" and "element" descriptor
205                                 if(descriptors[0] == "D")
206                                 {       std::stringstream val, val2;
207                                         unsigned short group;
208                                         unsigned short element;
209                                         val <<   std::dec << descriptors[1] ;
210                                         val >> std::hex >> group;
211                                         val2 << std::dec <<  descriptors[2];
212                                         val2 >> std::hex >> element;
213                                         Add(AttributeDescriptor( group,element,flag), ilevel);
214                                 }
215
216                                 else if(descriptors[0].find("#") != -1)
217                                 {
218                                         // commented line continue to next line
219                                 }
220                                 else
221                                 {       boost::algorithm::replace_all(descriptors[2],"_"," ");
222                                         Add(AttributeDescriptor( descriptors[1].c_str(),descriptors[2].c_str(),flag), ilevel);
223                                 }
224                         }
225                 }
226         }
227
228     //==================================================================
229
230     //==================================================================
231     /// Adds a LevelDescriptor at the end of the list
232     void Descriptor::Add(const LevelDescriptor& d)
233     {
234       mLevelDescriptorList.push_back(d);
235     }
236     //==================================================================
237     
238     //==================================================================  
239     /// Adds an AttributeDescriptor to level l
240     void Descriptor::Add(const AttributeDescriptor& d, int l)
241     {
242       mLevelDescriptorList[l].Add(d);
243       // TO DO : update DicomTagToName and NameToDicomTag map
244     }
245     //==================================================================
246
247     //==================================================================
248     /// Clears the Descriptor
249     void Descriptor::Clear()
250     {
251       mLevelDescriptorList.clear();
252     }
253
254     //==================================================================
255
256     //==================================================================
257     /// Builds the key to value map of all the attributes of the tree
258     void Descriptor::BuildAttributeMap( std::map<std::string,std::string>& map ) const
259     {
260       map.clear();
261       LevelDescriptorListType::const_iterator l;
262       for (l = GetLevelDescriptorList().begin();
263            l!= GetLevelDescriptorList().end();
264            ++l)
265         {
266           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
267           for (a = l->GetAttributeDescriptorList().begin();
268                a!= l->GetAttributeDescriptorList().end();
269                ++a)
270             {
271               map[a->GetKey()]="";
272             }
273         }
274     }
275
276         
277     //==================================================================
278         // test if an attribute is present in DescriptionList
279         // return level's name
280         const std::string Descriptor::isExist(const std::string i_attr)
281         {
282                 std::string name = "";
283                 LevelDescriptorListType::const_iterator l = GetLevelDescriptorList().begin();
284                 for (;     l!= GetLevelDescriptorList().end(); ++l)
285                 {
286                         LevelDescriptor::AttributeDescriptorListType::const_iterator a = l->GetAttributeDescriptorList().begin();
287                 for(;a!= l->GetAttributeDescriptorList().end(); ++a)
288                         {
289                                 if (a->GetKey() == i_attr)
290                                 {
291                                         name = l->GetName();
292                                         break;
293                                 }
294                         }
295                 }
296                 return name.c_str();
297         }
298   }
299 }