]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOTreeDescriptor.cpp
*** empty log message ***
[creaImageIO.git] / src2 / creaImageIOTreeDescriptor.cpp
index 5b2a447d78cc6a2b26f9b6948a7fb75b6f6643b6..30c8b7aa8ba32fa15d9b81ba01d122bb23d7d485 100644 (file)
@@ -1,4 +1,11 @@
 #include <creaImageIOTreeDescriptor.h>
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/replace.hpp>
+#include <creaImageIOSystem.h>
+#include <boost/filesystem.hpp>
+
+
+#include <fstream>
 
 
 namespace creaImageIO
@@ -12,6 +19,9 @@ namespace creaImageIO
     const unsigned int AttributeDescriptor::PRIVATE = 1;
     /// The attribute enters in unique identifier constitution 
     const unsigned int AttributeDescriptor::IDENTIFIER = 2;
+       /// The attribute can be edited
+       const unsigned int AttributeDescriptor::EDITABLE = 3;
+       /// the attribute describes the node
     const unsigned int AttributeDescriptor::LABEL = 4;
     //==================================================================
 
@@ -50,8 +60,7 @@ namespace creaImageIO
       
       // Patient level
       Add(LevelDescriptor("Patient"));
-         Add(AttributeDescriptor(0x1111,0x0011,   // Number of Series
-                             AttributeDescriptor::LABEL),1);
+         Add(AttributeDescriptor("NumberOfChildren","#Series",0),1);   // Number of Series
       Add(AttributeDescriptor(0x0010,0x0010,   // Patient name
                              AttributeDescriptor::LABEL),1);
       Add(AttributeDescriptor(0x0010,0x0040),1); // Patient sex
@@ -61,8 +70,7 @@ namespace creaImageIO
  
       // Study-series level
       Add(LevelDescriptor("Series"));
-         Add(AttributeDescriptor(0x1111,0x0011,   // Number of Images
-                             AttributeDescriptor::LABEL),2);
+         Add(AttributeDescriptor("NumberOfChildren","#Images",0),2);   // Number of images
       Add(AttributeDescriptor(0x0008,0x0060,    // Modality
                              AttributeDescriptor::LABEL),2);
       Add(AttributeDescriptor(0x0008,0x1030),2); // Study Description
@@ -134,6 +142,87 @@ namespace creaImageIO
                                                                  
      
     }
+
+       //////////////////////////////////////////////////////////////
+       // create a descriptor (name, attributes...) from a file)       //
+       // @param : file path                                                                           //
+       // return : -                                                                                           //
+       //////////////////////////////////////////////////////////////
+       void Descriptor::createDescriptorfromFile(const std::string &i_name)
+       {
+               Clear();
+               
+               // read file and put in buffer
+               std::ifstream i_file(i_name.c_str());
+               std::stringstream buffer;
+               buffer << i_file.rdbuf();
+               std::string line;
+               bool bname;
+               int ilevel = -1;
+
+               
+               while(std::getline(buffer, line))
+               {
+                       if(line =="<level>")
+                       {       //increment levels.
+                               ilevel++;
+                               bname = true;
+                       }
+                       else if(bname)
+                       {
+                               // For each level, a name to describe it
+                               Add(LevelDescriptor(line));
+                               bname = false;
+                       }
+                       else
+                       { 
+                               // split line to find all tags
+                               std::vector<std::string> descriptors;
+                               std::string separator = " ";
+                               std::string::size_type last_pos = line.find_first_not_of(separator);
+                               //find first separator
+                               std::string::size_type pos = line.find_first_of(separator, last_pos);
+                               while(std::string::npos != pos || std::string::npos != last_pos)
+                               {
+                                       descriptors.push_back(line.substr(last_pos, pos - last_pos));
+                                       last_pos = line.find_first_not_of(separator, pos);
+                                       pos = line.find_first_of(separator, last_pos);
+                               }
+                               
+                               // By default, the last tag is at zero and not recorded but if take in count
+                               unsigned int flag = 0;
+                               if(descriptors.size() == 4)
+                               {
+                                       std::stringstream val;
+                                       val << std::dec << descriptors[3];
+                                       val>> flag;
+                               }
+
+                               // if Dicom tag, use "group" and "element" descriptor
+                               if(descriptors[0] == "D")
+                               {       std::stringstream val, val2;
+                                       unsigned short group;
+                                       unsigned short element;
+                                       val <<   std::dec << descriptors[1] ;
+                                       val >> std::hex >> group;
+                                       val2 << std::dec <<  descriptors[2];
+                                       val2 >> std::hex >> element;
+                                       Add(AttributeDescriptor( group,element,flag), ilevel);
+                               }
+
+                               else if(descriptors[0].find("#") != -1)
+                               {
+                                               // commented line continue to next line
+                               }
+                               else
+                               {       boost::algorithm::replace_all(descriptors[2],"_"," ");
+                                       Add(AttributeDescriptor( descriptors[1].c_str(),descriptors[2].c_str(),flag), ilevel);
+                               }
+                       }
+               }
+       }
+
+
     //==================================================================
 
     //==================================================================