1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef CLITKVOXIMAGEIO_CXX
19 #define CLITKVOXIMAGEIO_CXX
21 -------------------------------------------------
22 * @file clitkVoxImageIO.cxx
23 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24 * @date 17 May 2006 08:03:07
29 -------------------------------------------------*/
32 #include "clitkVoxImageIO.h"
33 #include "clitkCommon.h"
35 // itk include (for itkReadRawBytesAfterSwappingMacro)
36 #include "itkRawImageIO.h"
38 //--------------------------------------------------------------------
39 // Read Image Information
40 void clitk::VoxImageIO::ReadImageInformation()
44 clitk::openFileForReading(is, m_FileName);
50 itkExceptionMacro(<<"read magic number '" << mn << "' while expect VOX");
52 // read vox file version
57 itkExceptionMacro(<<"read old format '" << mn << "'. TODO");
60 // ONLY 3D IMAGES YET ...
62 // read grid size/spacing
63 itk::Vector<unsigned int,3> dim;
64 itk::Vector<double,3> spacing;
65 itk::Vector<double,3> origin;
80 if (d != 3 && d != 2) {
81 itkExceptionMacro(<<"could not read no " << d << "D image (only 2D and 3D). TODO");
85 std::string dataTypeName;
90 m_HeaderSize = is.tellg();
94 // set dimension values
95 SetNumberOfDimensions(d);
96 for(int i=0; i<d; i++) {
97 SetDimensions(i,dim[i]);
98 SetSpacing(i,spacing[i]);
99 SetOrigin(i,origin[i]);
102 // set other information
103 SetByteOrderToLittleEndian();
104 SetPixelType(itk::ImageIOBase::SCALAR);
105 SetNumberOfComponents(1);
107 if (dataTypeName == "char") SetComponentType(itk::ImageIOBase::CHAR);
108 else if (dataTypeName == "schar") SetComponentType(itk::ImageIOBase::CHAR);
109 else if (dataTypeName == "uchar" || dataTypeName == "unsigned_char") SetComponentType(itk::ImageIOBase::UCHAR);
110 else if (dataTypeName == "sshort") SetComponentType(itk::ImageIOBase::SHORT);
111 else if (dataTypeName == "ushort") SetComponentType(itk::ImageIOBase::USHORT);
112 else if (dataTypeName == "int") SetComponentType(itk::ImageIOBase::INT);
113 else if (dataTypeName == "sint") SetComponentType(itk::ImageIOBase::INT);
114 else if (dataTypeName == "uint") SetComponentType(itk::ImageIOBase::UINT);
115 else if (dataTypeName == "float") SetComponentType(itk::ImageIOBase::FLOAT);
116 else if (dataTypeName == "double") SetComponentType(itk::ImageIOBase::DOUBLE);
118 itkExceptionMacro(<<"Read failed: Wanted pixel type "
119 << "(char, uchar, short, ushort, int, uint, float, double)"
120 << " but read " << dataTypeName);
125 //--------------------------------------------------------------------
126 // Read Image Information
127 bool clitk::VoxImageIO::CanReadFile(const char* FileNameToRead)
129 std::string filename(FileNameToRead);
130 std::string filenameext = GetExtension(filename);
131 if (filenameext != std::string("vox")) return false;
135 //--------------------------------------------------------------------
136 // Read Image Content
137 void clitk::VoxImageIO::Read(void * buffer)
139 // Adapted from itkRawImageIO
142 openFileForReading(file, m_FileName);
145 unsigned long streamStart = m_HeaderSize;
146 file.seekg((long)streamStart, std::ios::beg);
148 itkExceptionMacro(<<"File seek failed (Vox Read)");
151 unsigned long numberOfBytesToBeRead = GetComponentSize();
152 for(unsigned int i=0; i<GetNumberOfDimensions(); i++) numberOfBytesToBeRead *= GetDimensions(i);
154 //DD(numberOfBytesToBeRead);
156 if(!this->ReadBufferAsBinary(file, buffer, numberOfBytesToBeRead)) {
157 itkExceptionMacro(<<"Read failed: Wanted "
158 << numberOfBytesToBeRead
159 << " bytes, but read "
160 << file.gcount() << " bytes.");
162 itkDebugMacro(<< "Reading Done");
166 // Swap bytes if necessary
167 if itkReadRawBytesAfterSwappingMacro( unsigned short, USHORT )
168 else if itkReadRawBytesAfterSwappingMacro( short, SHORT )
169 else if itkReadRawBytesAfterSwappingMacro( char, CHAR )
170 else if itkReadRawBytesAfterSwappingMacro( unsigned char, UCHAR )
171 else if itkReadRawBytesAfterSwappingMacro( unsigned int, UINT )
172 else if itkReadRawBytesAfterSwappingMacro( int, INT )
173 else if itkReadRawBytesAfterSwappingMacro( float, FLOAT )
174 else if itkReadRawBytesAfterSwappingMacro( double, DOUBLE );
178 //--------------------------------------------------------------------
179 // Write Image Information
180 void clitk::VoxImageIO::WriteImageInformation(bool keepOfStream)
183 if (GetNumberOfDimensions() != 3 && GetNumberOfDimensions() != 2) {
184 itkExceptionMacro(<<"Write failed: only 3D and 2D image for Vox file format yet.");
188 clitk::openFileForWriting(file, m_FileName);
189 // write magic number
190 file << "VOX v2" << std::endl;
191 // write grid size/spacing
192 file << "# Size" << std::endl;
193 file << GetDimensions(0) << " "
194 << GetDimensions(1) << " "
195 << GetDimensions(2) << std::endl;
196 file << "# Spacing" << std::endl;
198 file << GetSpacing(0) << " "
199 << GetSpacing(1) << " "
200 << GetSpacing(2) << std::endl;
201 file << "# Image dim" << std::endl << "3" << std::endl;
202 file << "# Image type" << std::endl;
204 std::string dataTypeName;
205 if (GetComponentType() == itk::ImageIOBase::CHAR) dataTypeName = "schar";
206 else if (GetComponentType() == itk::ImageIOBase::UCHAR) dataTypeName = "uchar";
207 else if (GetComponentType() == itk::ImageIOBase::SHORT) dataTypeName = "sshort";
208 else if (GetComponentType() == itk::ImageIOBase::USHORT) dataTypeName = "ushort";
209 else if (GetComponentType() == itk::ImageIOBase::INT) dataTypeName = "int";
210 else if (GetComponentType() == itk::ImageIOBase::UINT) dataTypeName = "uint";
211 else if (GetComponentType() == itk::ImageIOBase::FLOAT) dataTypeName = "float";
212 else if (GetComponentType() == itk::ImageIOBase::DOUBLE) dataTypeName = "double";
214 itkExceptionMacro(<<"Write failed: Wanted pixel type "
215 << "(char, uchar, short, ushort, int, uint, float, double)"
216 << " but Vox is : " << dataTypeName);
218 file << dataTypeName << std::endl;
221 if (!keepOfStream) file.close();
224 //--------------------------------------------------------------------
225 // Write Image Information
226 bool clitk::VoxImageIO::CanWriteFile(const char* FileNameToWrite)
228 std::string filename(FileNameToWrite);
229 std::string filenameext = GetExtension(filename);
230 if (filenameext != std::string("vox")) return false;
234 //--------------------------------------------------------------------
236 void clitk::VoxImageIO::Write(const void * buffer)
238 clitk::VoxImageIO::WriteImageInformation(true);
239 SetByteOrderToLittleEndian();
241 //-------------------------------------------
242 // Cut & Paste from itkRawImageIO
243 // (warning BigEndian / LittleEndian)
244 const unsigned long numberOfBytes = this->GetImageSizeInBytes();
245 const unsigned long numberOfComponents = this->GetImageSizeInComponents();
246 // Swap bytes if necessary
248 if itkWriteRawBytesAfterSwappingMacro( unsigned short, USHORT )
249 else if itkWriteRawBytesAfterSwappingMacro( short, SHORT )
250 else if itkWriteRawBytesAfterSwappingMacro( char, CHAR )
251 else if itkWriteRawBytesAfterSwappingMacro( unsigned char, UCHAR )
252 else if itkWriteRawBytesAfterSwappingMacro( unsigned int, UINT )
253 else if itkWriteRawBytesAfterSwappingMacro( int, INT )
254 else if itkWriteRawBytesAfterSwappingMacro( float, FLOAT )
255 else if itkWriteRawBytesAfterSwappingMacro( double, DOUBLE ) ;
256 //-------------------------------------------
261 #endif /* end #define CLITKVOXIMAGEIO_CXX */