]> Creatis software - creaImageIO.git/blob - src2/creaImageIOTreeDescriptor.cpp
c1dca8e78b1d5ee575aee2bc12d6d00614a9d866
[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     const unsigned int AttributeDescriptor::LABEL = 4;
25     //==================================================================
26
27     //==================================================================
28     Descriptor::Descriptor()
29     {
30       CreateLevel0Descriptor();
31     }
32     //==================================================================
33
34    //==================================================================
35     Descriptor::~Descriptor()
36     {
37     }
38     //==================================================================
39
40     //==================================================================
41     void Descriptor::CreateLevel0Descriptor()
42     {
43       Add(LevelDescriptor("Root"));
44     }
45     //==================================================================
46     
47     //==================================================================
48     /// Creates the default descriptor
49     void Descriptor::CreateDefault()
50     {
51       // clears the existing one
52       Clear();
53       
54       // Creates the level 0 descriptor 
55       CreateLevel0Descriptor();
56       // Creates the attribute "Name"
57       Add(AttributeDescriptor("Name","Name",
58                               AttributeDescriptor::LABEL),0);
59       
60       // Patient level
61       Add(LevelDescriptor("Patient"));
62           Add(AttributeDescriptor("NumberOfChildren","#Series",0),1);   // Number of Series
63       Add(AttributeDescriptor(0x0010,0x0010,   // Patient name
64                               AttributeDescriptor::LABEL),1);
65       Add(AttributeDescriptor(0x0010,0x0040),1); // Patient sex
66       Add(AttributeDescriptor(0x0010,0x0030),1); // Patient birthday
67       Add(AttributeDescriptor(0x0010,0x0020,   // Patient ID
68                               AttributeDescriptor::IDENTIFIER),1);
69  
70       // Study-series level
71       Add(LevelDescriptor("Series"));
72           Add(AttributeDescriptor("NumberOfChildren","#Images",0),2);   // Number of images
73       Add(AttributeDescriptor(0x0008,0x0060,    // Modality
74                               AttributeDescriptor::LABEL),2);
75       Add(AttributeDescriptor(0x0008,0x1030),2); // Study Description
76       Add(AttributeDescriptor(0x0008,0x103E),2); // Description
77       Add(AttributeDescriptor(0x0008,0x0080),2); // Institution Name
78       Add(AttributeDescriptor(0x0008,0x0081),2); // Institution Adress
79           Add(AttributeDescriptor(0x0008,0x1010),2); // Station Name
80       Add(AttributeDescriptor(0x0008,0x1048),2); // Physician of Record
81       Add(AttributeDescriptor(0x0008,0x1050),2); // Performing Physician's Name
82       Add(AttributeDescriptor(0x0018,0x1030),2); // Protocol Name
83
84       Add(AttributeDescriptor(0x0020,0x0010),2); // Study ID
85       Add(AttributeDescriptor(0x0008,0x0020),2); // Study Date
86       Add(AttributeDescriptor(0x0008,0x0030),2); // Study Time
87       Add(AttributeDescriptor(0x0008,0x0050),2); // Study Accession Number
88       Add(AttributeDescriptor(0x0008,0x0005),2); // Specific character set
89       Add(AttributeDescriptor(0x0008,0x0021),2); // Series Date
90       Add(AttributeDescriptor(0x0008,0x0031),2); // Series time
91
92       Add(AttributeDescriptor(0x0020,0x000D   // Study Instance UID  
93                                                     ),2);//AttributeDescriptor::IDENTIFIER),2);
94       Add(AttributeDescriptor(0x0020,0x000E,   // Series Instance UID  
95                                                     AttributeDescriptor::IDENTIFIER),2);
96       // |
97       //                                                    AttributeDescriptor::LABEL),2);
98      
99
100       // Image level
101       Add(LevelDescriptor("Image"));
102
103       Add(AttributeDescriptor(0x0020,0x0013),3); // Image Number
104
105       Add(AttributeDescriptor(0x0028,0x0010),3); // Rows
106       Add(AttributeDescriptor(0x0028,0x0011),3); // Columns
107       Add(AttributeDescriptor(0x0028,0x0012),3); // Planes
108       Add(AttributeDescriptor(0x0028,0x0002),3); // Sample per pixels
109       Add(AttributeDescriptor(0x0028,0x0008),3); // Number of Frames 
110       Add(AttributeDescriptor(0x0028,0x0004),3); // Photometric Interpretation
111       Add(AttributeDescriptor(0x0028,0x0103),3); // Pixel Representation
112
113       Add(AttributeDescriptor(0x0020,0x0032),3); // Image Position Patient
114       Add(AttributeDescriptor(0x0020,0x0037),3); // Image Orientation Patient
115       Add(AttributeDescriptor(0x0020,0x1041),3); // Slice Location
116       Add(AttributeDescriptor(0x0028,0x0006),3); // Planar Configuration
117
118       Add(AttributeDescriptor(0x0028,0x0030),3); // Pixel Spacing
119       Add(AttributeDescriptor(0x0028,0x0100),3); // AlocatedBits
120       Add(AttributeDescriptor(0x0028,0x0101),3); // StoredBits
121
122       Add(AttributeDescriptor(0x0008,0x0008),3); // Image Type
123       Add(AttributeDescriptor(0x0008,0x0023),3); // Content Date
124       Add(AttributeDescriptor(0x0008,0x0033),3); // Content Time
125
126       Add(AttributeDescriptor(0x0020,0x4000),3); // Image Comments
127
128       Add(AttributeDescriptor(0x0004,0x1500,   // File Name
129                                                     AttributeDescriptor::LABEL),3);
130       Add(AttributeDescriptor(0x0028,0x1052),3); // Rescale Intercept
131       Add(AttributeDescriptor(0x0028,0x1053),3); // Rescale Slope
132
133       Add(AttributeDescriptor(0x0050,0x0004),3); // Calibration Image
134
135       Add(AttributeDescriptor(0x0020,0x0052   // Frame Reference UID
136                                                     ),3);
137       Add(AttributeDescriptor(0x0008,0x0016),3); // SOP Class UID
138       Add(AttributeDescriptor("FullFileName",  // Full file name
139                                                     "Full file name",
140                                                     AttributeDescriptor::IDENTIFIER),3); 
141                                                                   
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
177                         { 
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)
185                                 {
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);
189                                 }
190                                 
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)
194                                 {
195                                         std::stringstream val;
196                                         val << std::dec << descriptors[3];
197                                         val>> flag;
198                                 }
199
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);
210                                 }
211                                 else // "O" means if user's own tag.
212                                 {       boost::algorithm::replace_all(descriptors[2],"_"," ");
213                                         Add(AttributeDescriptor( descriptors[1].c_str(),descriptors[2].c_str(),flag), ilevel);
214                                 }
215                         }
216                 }
217         }
218
219
220     //==================================================================
221
222     //==================================================================
223     /// Adds a LevelDescriptor at the end of the list
224     void Descriptor::Add(const LevelDescriptor& d)
225     {
226       mLevelDescriptorList.push_back(d);
227     }
228     //==================================================================
229     
230     //==================================================================  
231     /// Adds an AttributeDescriptor to level l
232     void Descriptor::Add(const AttributeDescriptor& d, int l)
233     {
234       mLevelDescriptorList[l].Add(d);
235       // TO DO : update DicomTagToName and NameToDicomTag map
236     }
237     //==================================================================
238
239     //==================================================================
240     /// Clears the Descriptor
241     void Descriptor::Clear()
242     {
243       mLevelDescriptorList.clear();
244     }
245     //==================================================================
246
247     //==================================================================
248     /// Builds the key to value map of all the attributes of the tree
249     void Descriptor::BuildAttributeMap( std::map<std::string,std::string>& map ) const
250     {
251       map.clear();
252       LevelDescriptorListType::const_iterator l;
253       for (l = GetLevelDescriptorList().begin();
254            l!= GetLevelDescriptorList().end();
255            ++l)
256         {
257           LevelDescriptor::AttributeDescriptorListType::const_iterator a;
258           for (a = l->GetAttributeDescriptorList().begin();
259                a!= l->GetAttributeDescriptorList().end();
260                ++a)
261             {
262               map[a->GetKey()]="";
263             }
264         }
265     }
266  
267   }
268 }