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