1 #ifndef CLITKIOCOMMON_CXX
2 #define CLITKIOCOMMON_CXX
4 =================================================
5 * @file clitkIOCommon.cxx
6 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
7 * @date 18 May 2006 11:42:37
12 =================================================*/
15 #include "clitkImageCommon.h"
18 //====================================================================
19 // Open a file for reading
20 void clitk::openFileForReading(std::ifstream & is, const std::string & filename) {
21 is.open(filename.c_str(), std::ios::in);
23 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
26 //====================================================================
28 //====================================================================
29 // Open a file for writing
30 void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) {
31 os.open(filename.c_str(), std::ios::out);
33 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
36 //====================================================================
38 //====================================================================
39 // Read a dicom header
40 gdcm::File * clitk::readDicomHeader(const std::string & filename,
43 std::cout << "Reading DICOM <" << filename << ">" << std::endl;
45 gdcm::File *header = new gdcm::File();
46 header->SetFileName(filename);
47 header->SetMaxSizeLoadEntry(16384); // required ?
51 //====================================================================
53 //====================================================================
54 itk::ImageIOBase::Pointer clitk::readImageHeader(const std::string & filename, bool exit_on_error) {
55 itk::ImageIOBase::Pointer reader =
56 itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode);
58 if (exit_on_error) //default behavior for tools who don't handle the problem
60 std::cerr "Error reading file " << filename << ", exiting immediately" << std::endl;
65 reader->SetFileName(filename);
66 reader->ReadImageInformation();
69 //====================================================================
71 //====================================================================
72 void clitk::printImageHeader(itk::ImageIOBase::Pointer header, std::ostream & os, const int level) {
73 unsigned int dim = header->GetNumberOfDimensions();
74 std::string pixelTypeName = header->GetComponentTypeAsString(header->GetComponentType());
75 std::vector<int> inputSize;
76 std::vector<double> inputSpacing;
77 inputSize.resize(dim);
78 inputSpacing.resize(dim);
79 for(unsigned int i=0; i<dim; i++) {
80 inputSpacing[i] = header->GetSpacing(i);
81 inputSize[i] = header->GetDimensions(i);
84 clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
85 unsigned int nbOfComponents = header->GetNumberOfComponents();
88 if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
89 else os << pixelTypeName;
91 for(unsigned int i=0; i< dim-1; i++)
92 os << inputSize[i] << "x";
93 os << inputSize[dim-1]
95 for(unsigned int i=0; i< dim-1; i++)
96 os << inputSpacing[i] << "x";
97 os << inputSpacing[dim-1];
100 os << "Dim = " << dim << "D" << std::endl;
101 os << "PixelType = " << pixelTypeName << std::endl;
102 if (nbOfComponents > 1)
103 os << "Vector = " << nbOfComponents << std::endl;
105 for(unsigned int i=0; i< dim; i++) {
106 os << inputSize[i] << " ";
110 for(unsigned int i=0; i< dim; i++) {
111 os << inputSpacing[i] << " ";
115 os << "# voxels = " << header->GetImageSizeInPixels() << std::endl;
116 os << "Size (mm) = ";
117 for(unsigned int i=0; i< dim; i++) {
118 os << inputSize[i]*inputSpacing[i] << " ";
120 os << "mm" << std::endl;
123 for(unsigned int i=0; i< dim; i++) {
124 vol *= inputSize[i]*inputSpacing[i]/10.0;
126 os << vol << " cc" << std::endl;
127 int mem = header->GetImageSizeInPixels()*pixelSize*nbOfComponents;
128 double memKb = (double)mem/1024.0;
129 double memMb = (double)mem/1024.0/1024.0;
130 double memGb = (double)mem/1024.0/1024.0/1024.0;
131 if (lrint(memKb) <= 0)
132 os << "Memory = " << mem << " bytes" << std::endl;
134 if (lrint(memMb) <= 0)
135 os << "Memory = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
137 if (lrint(memGb) <= 0)
138 os << "Memory = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
140 os << "Memory = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
146 //====================================================================
148 #endif /* end #define CLITKIO_CXX */