]> Creatis software - clitk.git/blob - common/old/clitkIOCommon.cxx
removed headers
[clitk.git] / common / old / clitkIOCommon.cxx
1 #ifndef CLITKIOCOMMON_CXX
2 #define CLITKIOCOMMON_CXX
3 /**
4    =================================================
5    * @file   clitkIOCommon.cxx
6    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
7    * @date   18 May 2006 11:42:37
8    * 
9    * @brief  
10    * 
11    * 
12    =================================================*/
13
14 // clitk include
15 #include "clitkImageCommon.h"
16 #include <cstdlib>
17
18 //====================================================================
19 // Open a file for reading
20 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
21   is.open(filename.c_str(), std::ios::in);
22   if ( is.fail() ) {
23     itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
24   }
25 }
26 //====================================================================
27   
28 //====================================================================
29 // Open a file for writing
30 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)  {
31   os.open(filename.c_str(), std::ios::out);
32   if ( os.fail() ) {
33     itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
34   }
35 }
36 //====================================================================
37
38 //====================================================================
39 // Read a dicom header  
40 gdcm::File * clitk::readDicomHeader(const std::string & filename, 
41                                     const bool verbose) {
42   if (verbose) {
43     std::cout << "Reading DICOM <" << filename << ">" << std::endl;
44   }
45   gdcm::File *header = new gdcm::File();
46   header->SetFileName(filename);
47   header->SetMaxSizeLoadEntry(16384); // required ?
48   header->Load();
49   return header;
50 }
51 //====================================================================
52
53 //====================================================================
54 itk::ImageIOBase::Pointer clitk::readImageHeader(const std::string & filename, bool exit_on_error) {
55   itk::ImageIOBase::Pointer reader =
56     itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
57   if (!reader) 
58       if (exit_on_error) //default behavior for tools who don't handle the problem
59       {
60           std::cerr "Error reading file " << filename << ", exiting immediately" << std::endl;
61           std::exit(-1);
62       }
63       else
64           return NULL;
65   reader->SetFileName(filename);
66   reader->ReadImageInformation();
67   return reader;
68 }
69 //====================================================================
70
71 //====================================================================
72 void clitk::printImageHeader(itk::ImageIOBase::Pointer header, std::ostream & os, const int level) {
73   unsigned int dim = header->GetNumberOfDimensions();
74   std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
75   std::vector<int> inputSize;
76   std::vector<double> inputSpacing;
77   inputSize.resize(dim);
78   inputSpacing.resize(dim);
79   for(unsigned int i=0; i<dim; i++) {
80     inputSpacing[i] = header->GetSpacing(i);
81     inputSize[i] = header->GetDimensions(i);
82   }
83   int pixelSize = 
84     clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
85   unsigned int nbOfComponents = header->GetNumberOfComponents();
86   if (level == 0) {
87     os << dim << "D ";
88     if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
89     else os << pixelTypeName;
90     os << " ";
91     for(unsigned int i=0; i< dim-1; i++)
92       os << inputSize[i] << "x";
93     os << inputSize[dim-1]
94        << "  ";
95     for(unsigned int i=0; i< dim-1; i++)
96       os << inputSpacing[i] << "x";
97     os << inputSpacing[dim-1];
98   }
99   else {
100     os << "Dim       = " << dim << "D" << std::endl;
101     os << "PixelType = " << pixelTypeName << std::endl;
102     if (nbOfComponents > 1)
103       os << "Vector    = " << nbOfComponents << std::endl;
104     os << "Size      = ";
105     for(unsigned int i=0; i< dim; i++) {
106       os << inputSize[i] << " ";
107     }
108     os << std::endl;
109     os << "Spacing   = ";
110     for(unsigned int i=0; i< dim; i++) {
111       os << inputSpacing[i] << " ";
112     }
113     os << std::endl;
114     if (level > 1) {
115       os << "# voxels  = " << header->GetImageSizeInPixels() << std::endl;
116       os << "Size (mm) = ";
117       for(unsigned int i=0; i< dim; i++) {
118         os << inputSize[i]*inputSpacing[i] << " ";
119       }
120       os << "mm" << std::endl;
121       os << "Volume    = ";
122       double vol=1.0;
123       for(unsigned int i=0; i< dim; i++) {
124         vol *= inputSize[i]*inputSpacing[i]/10.0;
125       }
126       os << vol << " cc" << std::endl;
127       int mem = header->GetImageSizeInPixels()*pixelSize*nbOfComponents;
128       double memKb = (double)mem/1024.0;
129       double memMb = (double)mem/1024.0/1024.0;
130       double memGb = (double)mem/1024.0/1024.0/1024.0;
131       if (lrint(memKb) <= 0)
132         os << "Memory    = " << mem << " bytes" << std::endl;
133       else {
134         if (lrint(memMb) <= 0)
135           os << "Memory    = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
136         else {
137           if (lrint(memGb) <= 0)
138             os << "Memory    = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
139           else 
140             os << "Memory     = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
141         }
142       }
143     }
144   }
145 }
146 //====================================================================
147
148 #endif /* end #define CLITKIO_CXX */
149