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://oncora1.lyon.fnclcc.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 ======================================================================-====*/
18 #ifndef CLITKIOCOMMON_CXX
19 #define CLITKIOCOMMON_CXX
21 =================================================
22 * @file clitkIOCommon.cxx
23 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24 * @date 18 May 2006 11:42:37
29 =================================================*/
32 #include "clitkImageCommon.h"
35 //====================================================================
36 // Open a file for reading
37 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
38 is.open(filename.c_str(), std::ios::in);
40 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
43 //====================================================================
45 //====================================================================
46 // Open a file for writing
47 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) {
48 os.open(filename.c_str(), std::ios::out);
50 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
53 //====================================================================
55 //====================================================================
56 // Read a dicom header
57 gdcm::File * clitk::readDicomHeader(const std::string & filename,
60 std::cout << "Reading DICOM <" << filename << ">" << std::endl;
62 gdcm::File *header = new gdcm::File();
63 header->SetFileName(filename);
64 header->SetMaxSizeLoadEntry(16384); // required ?
68 //====================================================================
70 //====================================================================
71 itk::ImageIOBase::Pointer clitk::readImageHeader(const std::string & filename, bool exit_on_error) {
72 itk::ImageIOBase::Pointer reader =
73 itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
75 if (exit_on_error) //default behavior for tools who don't handle the problem
77 std::cerr "Error reading file " << filename << ", exiting immediately" << std::endl;
82 reader->SetFileName(filename);
83 reader->ReadImageInformation();
86 //====================================================================
88 //====================================================================
89 void clitk::printImageHeader(itk::ImageIOBase::Pointer header, std::ostream & os, const int level) {
90 unsigned int dim = header->GetNumberOfDimensions();
91 std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
92 std::vector<int> inputSize;
93 std::vector<double> inputSpacing;
94 inputSize.resize(dim);
95 inputSpacing.resize(dim);
96 for(unsigned int i=0; i<dim; i++) {
97 inputSpacing[i] = header->GetSpacing(i);
98 inputSize[i] = header->GetDimensions(i);
101 clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
102 unsigned int nbOfComponents = header->GetNumberOfComponents();
105 if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
106 else os << pixelTypeName;
108 for(unsigned int i=0; i< dim-1; i++)
109 os << inputSize[i] << "x";
110 os << inputSize[dim-1]
112 for(unsigned int i=0; i< dim-1; i++)
113 os << inputSpacing[i] << "x";
114 os << inputSpacing[dim-1];
117 os << "Dim = " << dim << "D" << std::endl;
118 os << "PixelType = " << pixelTypeName << std::endl;
119 if (nbOfComponents > 1)
120 os << "Vector = " << nbOfComponents << std::endl;
122 for(unsigned int i=0; i< dim; i++) {
123 os << inputSize[i] << " ";
127 for(unsigned int i=0; i< dim; i++) {
128 os << inputSpacing[i] << " ";
132 os << "# voxels = " << header->GetImageSizeInPixels() << std::endl;
133 os << "Size (mm) = ";
134 for(unsigned int i=0; i< dim; i++) {
135 os << inputSize[i]*inputSpacing[i] << " ";
137 os << "mm" << std::endl;
140 for(unsigned int i=0; i< dim; i++) {
141 vol *= inputSize[i]*inputSpacing[i]/10.0;
143 os << vol << " cc" << std::endl;
144 int mem = header->GetImageSizeInPixels()*pixelSize*nbOfComponents;
145 double memKb = (double)mem/1024.0;
146 double memMb = (double)mem/1024.0/1024.0;
147 double memGb = (double)mem/1024.0/1024.0/1024.0;
148 if (lrint(memKb) <= 0)
149 os << "Memory = " << mem << " bytes" << std::endl;
151 if (lrint(memMb) <= 0)
152 os << "Memory = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
154 if (lrint(memGb) <= 0)
155 os << "Memory = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
157 os << "Memory = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
163 //====================================================================
165 #endif /* end #define CLITKIO_CXX */