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