1 /*=========================================================================
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.
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.
14 =========================================================================*/
16 #ifndef CLITKIOCOMMON_CXX
17 #define CLITKIOCOMMON_CXX
20 =================================================
21 * @file clitkIOCommon.cxx
22 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
23 * @date 18 May 2006 11:42:37
28 =================================================*/
31 #include "clitkImageCommon.h"
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);
39 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
42 //====================================================================
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);
49 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
52 //====================================================================
54 //====================================================================
55 // Read a dicom header
56 gdcm::File * clitk::readDicomHeader(const std::string & filename,
59 std::cout << "Reading DICOM <" << filename << ">" << std::endl;
61 gdcm::File *header = new gdcm::File();
62 header->SetFileName(filename);
63 header->SetMaxSizeLoadEntry(16384); // required ?
67 //====================================================================
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);
74 if (exit_on_error) //default behavior for tools who don't handle the problem
76 std::cerr "Error reading file " << filename << ", exiting immediately" << std::endl;
81 reader->SetFileName(filename);
82 reader->ReadImageInformation();
85 //====================================================================
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);
100 clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
101 unsigned int nbOfComponents = header->GetNumberOfComponents();
104 if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
105 else os << pixelTypeName;
107 for(unsigned int i=0; i< dim-1; i++)
108 os << inputSize[i] << "x";
109 os << inputSize[dim-1]
111 for(unsigned int i=0; i< dim-1; i++)
112 os << inputSpacing[i] << "x";
113 os << inputSpacing[dim-1];
116 os << "Dim = " << dim << "D" << std::endl;
117 os << "PixelType = " << pixelTypeName << std::endl;
118 if (nbOfComponents > 1)
119 os << "Vector = " << nbOfComponents << std::endl;
121 for(unsigned int i=0; i< dim; i++) {
122 os << inputSize[i] << " ";
126 for(unsigned int i=0; i< dim; i++) {
127 os << inputSpacing[i] << " ";
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] << " ";
136 os << "mm" << std::endl;
139 for(unsigned int i=0; i< dim; i++) {
140 vol *= inputSize[i]*inputSpacing[i]/10.0;
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;
150 if (lrint(memMb) <= 0)
151 os << "Memory = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
153 if (lrint(memGb) <= 0)
154 os << "Memory = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
156 os << "Memory = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
162 //====================================================================
164 #endif /* end #define CLITKIO_CXX */