]> Creatis software - clitk.git/blob - common/old/clitkIOCommon.cxx
added the new headers
[clitk.git] / common / old / clitkIOCommon.cxx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to: 
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
8
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.
12
13   It is distributed under dual licence
14
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
20 /**
21    =================================================
22    * @file   clitkIOCommon.cxx
23    * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24    * @date   18 May 2006 11:42:37
25    * 
26    * @brief  
27    * 
28    * 
29    =================================================*/
30
31 // clitk include
32 #include "clitkImageCommon.h"
33 #include <cstdlib>
34
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);
39   if ( is.fail() ) {
40     itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
41   }
42 }
43 //====================================================================
44   
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);
49   if ( os.fail() ) {
50     itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
51   }
52 }
53 //====================================================================
54
55 //====================================================================
56 // Read a dicom header  
57 gdcm::File * clitk::readDicomHeader(const std::string & filename, 
58                                     const bool verbose) {
59   if (verbose) {
60     std::cout << "Reading DICOM <" << filename << ">" << std::endl;
61   }
62   gdcm::File *header = new gdcm::File();
63   header->SetFileName(filename);
64   header->SetMaxSizeLoadEntry(16384); // required ?
65   header->Load();
66   return header;
67 }
68 //====================================================================
69
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);
74   if (!reader) 
75       if (exit_on_error) //default behavior for tools who don't handle the problem
76       {
77           std::cerr "Error reading file " << filename << ", exiting immediately" << std::endl;
78           std::exit(-1);
79       }
80       else
81           return NULL;
82   reader->SetFileName(filename);
83   reader->ReadImageInformation();
84   return reader;
85 }
86 //====================================================================
87
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);
99   }
100   int pixelSize = 
101     clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
102   unsigned int nbOfComponents = header->GetNumberOfComponents();
103   if (level == 0) {
104     os << dim << "D ";
105     if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
106     else os << pixelTypeName;
107     os << " ";
108     for(unsigned int i=0; i< dim-1; i++)
109       os << inputSize[i] << "x";
110     os << inputSize[dim-1]
111        << "  ";
112     for(unsigned int i=0; i< dim-1; i++)
113       os << inputSpacing[i] << "x";
114     os << inputSpacing[dim-1];
115   }
116   else {
117     os << "Dim       = " << dim << "D" << std::endl;
118     os << "PixelType = " << pixelTypeName << std::endl;
119     if (nbOfComponents > 1)
120       os << "Vector    = " << nbOfComponents << std::endl;
121     os << "Size      = ";
122     for(unsigned int i=0; i< dim; i++) {
123       os << inputSize[i] << " ";
124     }
125     os << std::endl;
126     os << "Spacing   = ";
127     for(unsigned int i=0; i< dim; i++) {
128       os << inputSpacing[i] << " ";
129     }
130     os << std::endl;
131     if (level > 1) {
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] << " ";
136       }
137       os << "mm" << std::endl;
138       os << "Volume    = ";
139       double vol=1.0;
140       for(unsigned int i=0; i< dim; i++) {
141         vol *= inputSize[i]*inputSpacing[i]/10.0;
142       }
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;
150       else {
151         if (lrint(memMb) <= 0)
152           os << "Memory    = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
153         else {
154           if (lrint(memGb) <= 0)
155             os << "Memory    = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
156           else 
157             os << "Memory     = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
158         }
159       }
160     }
161   }
162 }
163 //====================================================================
164
165 #endif /* end #define CLITKIO_CXX */
166