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 "clitkIOCommon.h"
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);
38 itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename);
41 //====================================================================
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);
48 itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename);
51 //====================================================================
53 //====================================================================
54 // Read a dicom header
55 gdcm::File * clitk::readDicomHeader(const std::string & filename,
58 std::cout << "Reading DICOM <" << filename << ">" << std::endl;
60 gdcm::File *header = new gdcm::File();
61 header->SetFileName(filename);
62 header->SetMaxSizeLoadEntry(16384); // required ?
66 //====================================================================
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();
77 //====================================================================
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);
92 clitk::GetTypeSizeFromString(header->GetComponentTypeAsString(header->GetComponentType()));
93 unsigned int nbOfComponents = header->GetNumberOfComponents();
96 if (nbOfComponents !=1) os << nbOfComponents << "x" << pixelTypeName;
97 else os << pixelTypeName;
99 for(unsigned int i=0; i< dim-1; i++)
100 os << inputSize[i] << "x";
101 os << inputSize[dim-1]
103 for(unsigned int i=0; i< dim-1; i++)
104 os << inputSpacing[i] << "x";
105 os << inputSpacing[dim-1];
108 os << "Dim = " << dim << "D" << std::endl;
109 os << "PixelType = " << pixelTypeName << std::endl;
110 if (nbOfComponents > 1)
111 os << "Vector = " << nbOfComponents << std::endl;
113 for(unsigned int i=0; i< dim; i++) {
114 os << inputSize[i] << " ";
118 for(unsigned int i=0; i< dim; i++) {
119 os << inputSpacing[i] << " ";
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] << " ";
128 os << "mm" << std::endl;
131 for(unsigned int i=0; i< dim; i++) {
132 vol *= inputSize[i]*inputSpacing[i]/10.0;
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;
142 if (lrint(memMb) <= 0)
143 os << "Memory = " << memKb << " Kb (" << mem << " bytes)" << std::endl;
145 if (lrint(memGb) <= 0)
146 os << "Memory = " << memMb << " Mb (" << mem << " bytes)" << std::endl;
148 os << "Memory = " << memGb << " Gb (" << mem << " bytes)" << std::endl;
154 //====================================================================
156 #endif /* end #define CLITKIO_CXX */