]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeDescriptor.cpp
30c8b7aa8ba32fa15d9b81ba01d122bb23d7d485
[creaImageIO.git] / src2 / 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         //////////////////////////////////////////////////////////////
147         // create a descriptor (name, attributes...) from a file)       //
148         // @param : file path                                                                           //
149         // return : -                                                                                           //
150         //////////////////////////////////////////////////////////////
151         void Descriptor::createDescriptorfromFile(const std::string &i_name)
152         {
153                 Clear();
154                 
155                 // read file and put in buffer
156                 std::ifstream i_file(i_name.c_str());
157                 std::stringstream buffer;
158                 buffer << i_file.rdbuf();
159                 std::string line;
160                 bool bname;
161                 int ilevel = -1;
162
163                 
164                 while(std::getline(buffer, line))
165                 {
166                         if(line =="<level>")
167                         {       //increment levels.
168                                 ilevel++;
169                                 bname = true;
170                         }
171                         else if(bname)
172                         {
173                                 // For each level, a name to describe it
174                                 Add(LevelDescriptor(line));
175                                 bname = false;
176                         }
177                         else
178                         { 
179                                 // split line to find all tags
180                                 std::vector<std::string> descriptors;
181                                 std::string separator = " ";
182                                 std::string::size_type last_pos = line.find_first_not_of(separator);
183                                 //find first separator
184                                 std::string::size_type pos = line.find_first_of(separator, last_pos);
185                                 while(std::string::npos != pos || std::string::npos != last_pos)
186                                 {
187                                         descriptors.push_back(line.substr(last_pos, pos - last_pos));
188                                         last_pos = line.find_first_not_of(separator, pos);
189                                         pos = line.find_first_of(separator, last_pos);
190                                 }
191                                 
192                                 // By default, the last tag is at zero and not recorded but if take in count
193                                 unsigned int flag = 0;
194                                 if(descriptors.size() == 4)
195                                 {
196                                         std::stringstream val;
197                                         val << std::dec << descriptors[3];
198                                         val>> flag;
199                                 }
200
201                                 // if Dicom tag, use "group" and "element" descriptor
202                                 if(descriptors[0] == "D")
203                                 {       std::stringstream val, val2;
204                                         unsigned short group;
205                                         unsigned short element;
206                                         val <<   std::dec << descriptors[1] ;
207                                         val >> std::hex >> group;
208                                         val2 << std::dec <<  descriptors[2];
209                                         val2 >> std::hex >> element;
210                                         Add(AttributeDescriptor( group,element,flag), ilevel);
211                                 }
212
213                                 else if(descriptors[0].find("#") != -1)
214                                 {
215                                                 // commented line continue to next line
216                                 }
217                                 else
218                                 {       boost::algorithm::replace_all(descriptors[2],"_"," ");
219                                         Add(AttributeDescriptor( descriptors[1].c_str(),descriptors[2].c_str(),flag), ilevel);
220                                 }
221                         }
222                 }
223         }
224
225
226     //==================================================================
227
228     //==================================================================
229     /// Adds a LevelDescriptor at the end of the list
230     void Descriptor::Add(const LevelDescriptor& d)
231     {
232       mLevelDescriptorList.push_back(d);
233     }
234     //==================================================================
235     
236     //==================================================================  
237     /// Adds an AttributeDescriptor to level l
238     void Descriptor::Add(const AttributeDescriptor& d, int l)
239     {
240       mLevelDescriptorList[l].Add(d);
241       // TO DO : update DicomTagToName and NameToDicomTag map
242     }
243     //==================================================================
244
245     //==================================================================
246     /// Clears the Descriptor
247     void Descriptor::Clear()
248     {
249       mLevelDescriptorList.clear();
250     }
251     //==================================================================
252
253     //==================================================================
254     /// Builds the key to value map of all the attributes of the tree
255     void Descriptor::BuildAttributeMap( std::map<std::string,std::string>& map ) const
256     {
257       map.clear();
258       LevelDescriptorListType::const_iterator l;
259       for (l = GetLevelDescriptorList().begin();
260            l!= GetLevelDescriptorList().end();
261            ++l)
262         {
263           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
264           for (a = l->GetAttributeDescriptorList().begin();
265                a!= l->GetAttributeDescriptorList().end();
266                ++a)
267             {
268               map[a->GetKey()]="";
269             }
270         }
271     }
272  
273   }
274 }