]> Creatis software - clitk.git/blob - common/clitkIOCommon.cxx
16ec36127d677b7b3cf377d0b32f007666dddf17
[clitk.git] / common / 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 "clitkIOCommon.h"
32
33 //====================================================================
34 // Open a file for reading
35 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
36   is.open(filename.c_str(), std::ios::in);
37   if ( is.fail() ) {
38     itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
39   }
40 }
41 //====================================================================
42   
43 //====================================================================
44 // Open a file for writing
45 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename)  {
46   os.open(filename.c_str(), std::ios::out);
47   if ( os.fail() ) {
48     itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
49   }
50 }
51 //====================================================================
52
53 //====================================================================
54 // Read a dicom header  
55 gdcm::File * clitk::readDicomHeader(const std::string & filename, 
56                                     const bool verbose) {
57   if (verbose) {
58     std::cout << "Reading DICOM <" << filename << ">" << std::endl;
59   }
60   gdcm::File *header = new gdcm::File();
61   header->SetFileName(filename);
62   header->SetMaxSizeLoadEntry(16384); // required ?
63   header->Load();
64   return header;
65 }
66 //====================================================================
67
68 //====================================================================
69 itk::ImageIOBase::Pointer clitk::readImageHeader(const std::string & filename) {
70   itk::ImageIOBase::Pointer reader =
71     itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
72   if (!reader) return NULL;
73   reader->SetFileName(filename);
74   reader->ReadImageInformation();
75   return reader;
76 }
77 //====================================================================
78
79 //====================================================================
80 void clitk::printImageHeader(itk::ImageIOBase::Pointer header, std::ostream & os, const int level) {
81   unsigned int dim = header->GetNumberOfDimensions();
82   std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
83   std::vector<int> inputSize;
84   std::vector<double> inputSpacing;
85   inputSize.resize(dim);
86   inputSpacing.resize(dim);
87   for(unsigned int i=0; i<dim; i++) {
88     inputSpacing[i] = header->GetSpacing(i);
89     inputSize[i] = header->GetDimensions(i);
90   }
91   int pixelSize = 
92     clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
93   unsigned int nbOfComponents = header->GetNumberOfComponents();
94   if (level == 0) {
95     os << dim << "D ";
96     if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
97     else os << pixelTypeName;
98     os << " ";
99     for(unsigned int i=0; i< dim-1; i++)
100       os << inputSize[i] << "x";
101     os << inputSize[dim-1]
102        << "  ";
103     for(unsigned int i=0; i< dim-1; i++)
104       os << inputSpacing[i] << "x";
105     os << inputSpacing[dim-1];
106   }
107   else {
108     os << "Dim       = " << dim << "D" << std::endl;
109     os << "PixelType = " << pixelTypeName << std::endl;
110     if (nbOfComponents > 1)
111       os << "Vector    = " << nbOfComponents << std::endl;
112     os << "Size      = ";
113     for(unsigned int i=0; i< dim; i++) {
114       os << inputSize[i] << " ";
115     }
116     os << std::endl;
117     os << "Spacing   = ";
118     for(unsigned int i=0; i< dim; i++) {
119       os << inputSpacing[i] << " ";
120     }
121     os << std::endl;
122     if (level > 1) {
123       os << "# voxels  = " << header->GetImageSizeInPixels() << std::endl;
124       os << "Size (mm) = ";
125       for(unsigned int i=0; i< dim; i++) {
126         os << inputSize[i]*inputSpacing[i] << " ";
127       }
128       os << "mm" << std::endl;
129       os << "Volume    = ";
130       double vol=1.0;
131       for(unsigned int i=0; i< dim; i++) {
132         vol *= inputSize[i]*inputSpacing[i]/10.0;
133       }
134       os << vol << " cc" << std::endl;
135       int mem = header->GetImageSizeInPixels()*pixelSize*nbOfComponents;
136       double memKb = (double)mem/1024.0;
137       double memMb = (double)mem/1024.0/1024.0;
138       double memGb = (double)mem/1024.0/1024.0/1024.0;
139       if (lrint(memKb) <= 0)
140         os << "Memory    = " << mem << " bytes" << std::endl;
141       else {
142         if (lrint(memMb) <= 0)
143           os << "Memory    = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
144         else {
145           if (lrint(memGb) <= 0)
146             os << "Memory    = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
147           else 
148             os << "Memory     = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
149         }
150       }
151     }
152   }
153 }
154 //====================================================================
155
156 #endif /* end #define CLITKIO_CXX */
157