]> Creatis software - clitk.git/blob - common/rtkImagXXMLFileReader.cxx
Merge branch 'master' of git.creatis.insa-lyon.fr:clitk
[clitk.git] / common / rtkImagXXMLFileReader.cxx
1 /*=========================================================================
2  *
3  *  Copyright RTK Consortium
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *=========================================================================*/
18
19 #include "rtkImagXXMLFileReader.h"
20 #include "itkMacro.h"
21
22 #include <itksys/SystemTools.hxx>
23 #include <itkMetaDataObject.h>
24
25 namespace rtk
26 {
27
28 int
29 ImagXXMLFileReader::
30 CanReadFile(const char *name)
31 {
32   if(!itksys::SystemTools::FileExists(name) ||
33      itksys::SystemTools::FileIsDirectory(name) ||
34      itksys::SystemTools::FileLength(name) == 0)
35     return 0;
36   return 1;
37 }
38
39 void
40 ImagXXMLFileReader::
41 StartElement(const char * name, const char ** atts)
42 {
43 #define ENCAPLULATE_META_DATA_INT(metaName) \
44   if(itksys::SystemTools::Strucmp(atts[i], metaName) == 0) { \
45     double d = atof(atts[i+1]); \
46     itk::EncapsulateMetaData<int>(m_Dictionary, metaName, d); \
47     }
48
49 #define ENCAPLULATE_META_DATA_STRING(metaName) \
50   if(itksys::SystemTools::Strucmp(atts[i], metaName) == 0) { \
51     itk::EncapsulateMetaData<std::string>(m_Dictionary, metaName, atts[i+1]); \
52     }
53
54   if(std::string(name) == std::string("image") )
55     {
56     for(int i=0; atts[i] != NULL; i+=2)
57       {
58       ENCAPLULATE_META_DATA_STRING("name");
59       ENCAPLULATE_META_DATA_INT("bitDepth");
60       ENCAPLULATE_META_DATA_STRING("pixelFormat");
61       ENCAPLULATE_META_DATA_STRING("byteOrder");
62       ENCAPLULATE_META_DATA_STRING("modality");
63       ENCAPLULATE_META_DATA_STRING("matrixTransform");
64       ENCAPLULATE_META_DATA_INT("dimensions");
65       ENCAPLULATE_META_DATA_INT("sequence");
66       ENCAPLULATE_META_DATA_STRING("rawFile");
67       }
68     }
69   if(std::string(name) == std::string("size") )
70     {
71     for(int i=0; atts[i] != NULL; i+=2)
72       {
73       ENCAPLULATE_META_DATA_INT("x");
74       ENCAPLULATE_META_DATA_INT("y");
75       ENCAPLULATE_META_DATA_INT("z");
76       }
77     }
78   if(std::string(name) == std::string("spacing") )
79     {
80 #define ENCAPLULATE_META_DATA_DOUBLE(metaName) \
81   if(itksys::SystemTools::Strucmp(atts[i], metaName) == 0) { \
82     double d = atof(atts[i+1]); \
83     itk::EncapsulateMetaData<double>(m_Dictionary, std::string("spacing_") + std::string(metaName), d); \
84     }
85     for(int i=0; atts[i] != NULL; i+=2)
86       {
87       ENCAPLULATE_META_DATA_DOUBLE("x");
88       ENCAPLULATE_META_DATA_DOUBLE("y");
89       ENCAPLULATE_META_DATA_DOUBLE("z");
90       }
91     }
92   m_CurCharacterData = "";
93 }
94
95 void
96 ImagXXMLFileReader::
97 EndElement( const char *itkNotUsed(name) )
98 {
99 }
100
101 void
102 ImagXXMLFileReader::
103 CharacterDataHandler(const char *inData, int inLength)
104 {
105   for(int i = 0; i < inLength; i++)
106     m_CurCharacterData = m_CurCharacterData + inData[i];
107 }
108
109 }