1 /*=========================================================================
4 Module: $RCSfile: clitkIOCommon.cxx,v $
6 Date: $Date: 2010/01/06 13:32:01 $
7 Version: $Revision: 1.1 $
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.
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.
17 =========================================================================*/
19 #ifndef CLITKIOCOMMON_CXX
20 #define CLITKIOCOMMON_CXX
23 =================================================
24 * @file clitkIOCommon.cxx
25 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
26 * @date 18 May 2006 11:42:37
31 =================================================*/
34 #include "clitkIOCommon.h"
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);
41 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
44 //====================================================================
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);
51 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
54 //====================================================================
56 //====================================================================
57 // Read a dicom header
58 gdcm::File * clitk::readDicomHeader(const std::string & filename,
61 std::cout << "Reading DICOM <" << filename << ">" << std::endl;
63 gdcm::File *header = new gdcm::File();
64 header->SetFileName(filename);
65 header->SetMaxSizeLoadEntry(16384); // required ?
69 //====================================================================
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();
80 //====================================================================
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);
95 clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
96 unsigned int nbOfComponents = header->GetNumberOfComponents();
99 if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
100 else os << pixelTypeName;
102 for(unsigned int i=0; i< dim-1; i++)
103 os << inputSize[i] << "x";
104 os << inputSize[dim-1]
106 for(unsigned int i=0; i< dim-1; i++)
107 os << inputSpacing[i] << "x";
108 os << inputSpacing[dim-1];
111 os << "Dim = " << dim << "D" << std::endl;
112 os << "PixelType = " << pixelTypeName << std::endl;
113 if (nbOfComponents > 1)
114 os << "Vector = " << nbOfComponents << std::endl;
116 for(unsigned int i=0; i< dim; i++) {
117 os << inputSize[i] << " ";
121 for(unsigned int i=0; i< dim; i++) {
122 os << inputSpacing[i] << " ";
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] << " ";
131 os << "mm" << std::endl;
134 for(unsigned int i=0; i< dim; i++) {
135 vol *= inputSize[i]*inputSpacing[i]/10.0;
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;
145 if (lrint(memMb) <= 0)
146 os << "Memory = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
148 if (lrint(memGb) <= 0)
149 os << "Memory = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
151 os << "Memory = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
157 //====================================================================
159 #endif /* end #define CLITKIO_CXX */