1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
19 #ifndef CLITKIMAGECOMMON_CXX
20 #define CLITKIMAGECOMMON_CXX
22 #include "clitkImageCommon.h"
23 #include "clitkCommon.h"
25 //--------------------------------------------------------------------
26 void clitk::ReadImageDimensionAndPixelType(const std::string & filename,
28 std::string & pixeType)
30 itk::ImageIOBase::Pointer genericReader =
31 itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
33 clitkExceptionMacro("Image file format unknown while reading file <" << filename << ">");
35 genericReader->SetFileName(filename.c_str());
36 genericReader->ReadImageInformation();
37 pixeType = genericReader->GetComponentTypeAsString(genericReader->GetComponentType());
38 dimension = genericReader->GetNumberOfDimensions();
40 //--------------------------------------------------------------------
43 //--------------------------------------------------------------------
44 void clitk::ReadImageDimensionAndPixelType(const std::string & filename,
46 std::string & pixeType, int & components)
48 itk::ImageIOBase::Pointer genericReader =
49 itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
51 clitkExceptionMacro("Image file format unknown while reading file <" << filename << ">");
53 genericReader->SetFileName(filename.c_str());
54 genericReader->ReadImageInformation();
55 pixeType = genericReader->GetComponentTypeAsString(genericReader->GetComponentType());
56 dimension = genericReader->GetNumberOfDimensions();
57 components= genericReader->GetNumberOfComponents();
59 //--------------------------------------------------------------------
61 //--------------------------------------------------------------------
62 itk::ImageIOBase::Pointer clitk::readImageHeader(const std::string & filename, bool exit_on_error)
64 itk::ImageIOBase::Pointer reader =
65 itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
67 if (exit_on_error) { //default behavior for tools who don't handle the problem
68 clitkExceptionMacro("Error reading file " << filename << ", exiting immediately");
71 reader->SetFileName(filename);
72 reader->ReadImageInformation();
75 //--------------------------------------------------------------------
77 //--------------------------------------------------------------------
78 void clitk::printImageHeader(itk::ImageIOBase::Pointer header, std::ostream & os, const int level)
80 unsigned int dim = header->GetNumberOfDimensions();
81 std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
82 std::vector<int> inputSize;
83 std::vector<double> inputSpacing;
84 std::vector<double> inputOrigin;
85 inputSize.resize(dim);
86 inputSpacing.resize(dim);
87 inputOrigin.resize(dim);
88 for(unsigned int i=0; i<dim; i++) {
89 inputSpacing[i] = header->GetSpacing(i);
90 inputSize[i] = header->GetDimensions(i);
91 inputOrigin[i] = header->GetOrigin(i);
94 clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
95 unsigned int nbOfComponents = header->GetNumberOfComponents();
98 if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
99 else os << pixelTypeName;
101 for(unsigned int i=0; i< dim-1; i++)
102 os << inputSize[i] << "x";
103 os << inputSize[dim-1]
105 for(unsigned int i=0; i< dim-1; i++)
106 os << inputSpacing[i] << "x";
107 os << inputSpacing[dim-1]
109 for(unsigned int i=0; i< dim-1; i++)
110 os << inputOrigin[i] << "x";
111 os << inputOrigin[dim-1] << " ";
112 os << header->GetImageSizeInPixels() << " ";
114 os << "Dim = " << dim << "D" << std::endl;
115 os << "PixelType = " << pixelTypeName << std::endl;
116 if (nbOfComponents > 1)
117 os << "Vector = " << nbOfComponents << std::endl;
119 for(unsigned int i=0; i< dim; i++) {
120 os << inputSize[i] << " ";
124 for(unsigned int i=0; i< dim; i++) {
125 os << inputSpacing[i] << " ";
129 os << "# voxels = " << header->GetImageSizeInPixels() << std::endl;
130 os << "Size (mm) = ";
131 for(unsigned int i=0; i< dim; i++) {
132 os << inputSize[i]*inputSpacing[i] << " ";
134 os << "mm" << std::endl;
135 os << "Origin (mm)= ";
136 for(unsigned int i=0; i< dim; i++) {
137 os << inputOrigin[i] << " ";
139 os << "mm" << std::endl;
143 for(unsigned int i=0; i< dim; i++) {
144 vol *= inputSize[i]*inputSpacing[i]/10.0;
146 os << vol << " cc" << std::endl;
147 int mem = header->GetImageSizeInPixels()*pixelSize*nbOfComponents;
148 double memKb = (double)mem/1024.0;
149 double memMb = (double)mem/1024.0/1024.0;
150 double memGb = (double)mem/1024.0/1024.0/1024.0;
151 if (lrint(memKb) <= 0)
152 os << "Memory = " << mem << " bytes" << std::endl;
154 if (lrint(memMb) <= 0)
155 os << "Memory = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
157 if (lrint(memGb) <= 0)
158 os << "Memory = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
160 os << "Memory = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
166 //--------------------------------------------------------------------
168 #endif /* end #define CLITKIMAGECOMMON_CXX */