-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitkFDFImageIO.cxx
More file actions
457 lines (394 loc) · 11.1 KB
/
itkFDFImageIO.cxx
File metadata and controls
457 lines (394 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* Copyright (C) 2004 Glenn Pierce.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "itkFDFImageIO.h"
#include "itkFDFCommonImageIO.h"
#include "itkByteSwapper.h"
#include "itkRGBPixel.h"
#include "itkRGBAPixel.h"
#include <stdio.h>
#include <fstream>
namespace itk
{
bool FDFImageIO::CanReadFile(const char* file)
{
this->SetFileName(file);
resolutions = new float[3];
// First check the extension
std::string filename = file;
if( filename == "" )
{
itkDebugMacro(<<"No filename specified.");
return false;
}
bool extensionFound = false;
std::string::size_type FDFPos = filename.rfind(".fdf");
if ((FDFPos != std::string::npos)
&& (FDFPos == filename.length() - 4))
{
extensionFound = true;
}
FDFPos = filename.rfind(".FDF");
if ((FDFPos != std::string::npos)
&& (FDFPos == filename.length() - 4))
{
extensionFound = true;
}
if( !extensionFound )
{
itkDebugMacro(<<"The filename extension is not recognized");
return false;
}
std::ifstream inFile;
inFile.open(m_FileName.c_str(), std::ios::in | std::ios::binary );
if( !inFile )
{
ExceptionObject exception(__FILE__, __LINE__);
std::string msg = "File \"" + m_FileName + "\" cannot be read.";
exception.SetDescription(msg.c_str());
throw exception;
}
// Check for a neccessary header variable
// HERE
return true;
}
void FDFImageIO::ReadImageInformation()
{
if(!this->CanReadFile(m_FileName.c_str()))
RAISE_EXCEPTION();
std::string line;
std::vector<std::string> tokens;
std::string type, name, value;
int matrix_size = 1;
std::ifstream inFile(m_FileName.c_str(), std::ios::in | std::ios::binary);
// Check if there was an error opening the file
if (!inFile)
{
std::cout << "Unable to open the file\n";
RAISE_EXCEPTION();
}
while (getline(inFile, line, '\n'))
{
if ( line == "\0" )
{
break;
}
// Formats the lines in the FDF header such as removing whitespace between {}
line = ParseLine(line);
Tokenize(line, tokens, " ;");
if(tokens.size() == 4)
{
type = tokens[0];
name = tokens[1];
value = tokens[3];
if (name == "spatial_rank")
{
this->spatial_rank = value;
}
if (name == "matrix")
{
StringToVector(value, matrix);
// Set the number of dimensions
this->SetNumberOfDimensions(matrix.size());
for(unsigned int i=0; i<matrix.size(); i++)
{
matrix_size *= this->matrix[i];
// Set the size of each dimension
this->SetDimensions(i,this->matrix[i]);
}
}
if (name == "span")
{
StringToVector(value, span);
}
if (name == "roi")
{
StringToVector(value, roi);
}
if (name == "location")
{
StringToVector(value, location);
}
// Get the binary data type
if (name == "storage")
{
this->storage = value;
this->SetPixelType( SCALAR );
if (value == "double" )
this->SetComponentType( DOUBLE );
if (value == "float" )
this->SetComponentType( FLOAT );
if (value == "long" )
this->SetComponentType( LONG );
if (value == "unsigned long" )
this->SetComponentType( ULONG);
if (value == "int" )
this->SetComponentType( INT );
if (value == "unsigned int" )
this->SetComponentType( UINT );
if (value == "short" )
this->SetComponentType( SHORT );
if (value == "unsigned short" )
this->SetComponentType( USHORT );
if (value == "char" )
this->SetComponentType( CHAR );
if (value == "unsigned char" )
this->SetComponentType( UCHAR );
}
// Get the bits
if (name == "bits")
{
ConvertFromString (value, this->bits);
}
// Get the checksum
if (name == "checksum")
{
ConvertFromString (value, this->checksum);
}
}
tokens.clear();
}
inFile.seekg (0, std::ios::end);
long int fileSize = inFile.tellg();
this->m_InputPosition = fileSize - (matrix_size * 4);
this->resolutions[0] = (this->roi[0] * 10 ) / this->matrix[0];
this->resolutions[1] = (this->roi[1] * 10 ) / this->matrix[1];
this->resolutions[2] = (this->roi[2] * 10 ) / this->matrix[2];
}
void FDFImageIO::ReadVolume(void*)
{
}
/*
const itk::ImageIOBase::IOPixelType FDFImageIO::GetPixelType() const
{
switch(m_PixelType)
{
case UCHAR:
return typeid(unsigned char);
case USHORT:
return typeid(unsigned short);
case CHAR:
return typeid(char);
case SHORT:
return typeid(short);
case UINT:
return typeid(unsigned int);
case INT:
return typeid(int);
case ULONG:
return typeid(unsigned long);
case LONG:
return typeid(long);
case FLOAT:
return typeid(float);
case DOUBLE:
return typeid(double);
case RGB:
return typeid(RGBPixel<unsigned char>);
case RGBA:
return typeid(RGBAPixel<unsigned char>);
default:
{
itkExceptionMacro ("Invalid type: " << m_PixelType << ", only unsigned char, unsigned short, RGB<unsigned char> are allowed.");
return this->ConvertToTypeInfo(m_PixelType);
}
case UNKNOWN:
itkExceptionMacro ("Unknown pixel type: " << m_PixelType);
}
return typeid(ImageIOBase::UnknownType);
}
*/
unsigned int FDFImageIO::GetComponentSize() const
{
switch(m_PixelType)
{
case UCHAR:
return sizeof(unsigned char);
case USHORT:
return sizeof(unsigned short);
case CHAR:
return sizeof(char);
case SHORT:
return sizeof(short);
case UINT:
return sizeof(unsigned int);
case INT:
return sizeof(int);
case ULONG:
return sizeof(unsigned long);
case LONG:
return sizeof(long);
case FLOAT:
return sizeof(float);
case DOUBLE:
return sizeof(double);
default:
{
itkExceptionMacro ("Invalid type: " << m_PixelType
<< ", only unsigned char and unsigned short are allowed.");
return 0;
}
}
return 1;
}
void FDFImageIO::Read(void* buffer)
{
unsigned int dimensions = this->GetNumberOfDimensions();
unsigned int numberOfPixels = 1;
for( unsigned int dim=0; dim< dimensions; dim++ )
{
numberOfPixels *= m_Dimensions[ dim ];
}
std::ifstream inFile(m_FileName.c_str(), std::ios::in | std::ios::binary);
// Check if there was an error opening the file
if (!inFile)
{
RAISE_EXCEPTION();
}
inFile.seekg( m_InputPosition );
if (!inFile)
{
RAISE_EXCEPTION();
}
char * p = static_cast<char *>(buffer);
inFile.read( p, this->GetImageSizeInBytes() );
bool success = !inFile.bad();
inFile.close();
if( !success )
{
itkExceptionMacro("Error reading image data.");
}
SwapBytesIfNecessary( buffer, numberOfPixels );
}
FDFImageIO::FDFImageIO()
{
this->SetNumberOfDimensions(2);
this->m_FileType = Binary;
this->m_ByteOrder = BigEndian;
}
FDFImageIO::~FDFImageIO() {}
void FDFImageIO::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "PixelType " << m_PixelType << "\n";
os << indent << "Start of image in bytes from start of file " << this->m_InputPosition << "\n";
os << indent << "Number of pixels in image: " << this->GetImageSizeInPixels() << "\n";
os << indent << "Image size in bytes: " << this->GetImageSizeInBytes() << "\n";
os << indent << "Checksum: " << this->checksum << "\n";
os << indent << "Storage: " << this->storage << "\n";
os << indent << "Spatial Rank: " << this->spatial_rank << "\n";
os << indent << "Bits: " << this->bits << "\n";
os << indent; PrintVector(os, "Matrix", this->matrix);
os << indent; PrintVector(os, "Location", this->location);
os << indent; PrintVector(os, "ROI", this->roi);
os << indent; PrintVector(os, "Span", this->span);
os << indent << "resolutions: " << this->resolutions[0] << ", " << this->resolutions[1]
<< ", " << this->resolutions[2] << "\n";
}
bool FDFImageIO::CanWriteFile( const char * /* name */)
{
//not possible to write a fdf file
return false;
}
void FDFImageIO::SwapBytesIfNecessary( void* buffer, unsigned long numberOfPixels )
{
switch(m_PixelType)
{
case CHAR:
{
if ( m_ByteOrder == LittleEndian )
{
ByteSwapper<int>::SwapRangeFromSystemToLittleEndian(
(int*)buffer, numberOfPixels );
}
else if ( m_ByteOrder == BigEndian )
{
ByteSwapper<int>::SwapRangeFromSystemToBigEndian(
(int *)buffer, numberOfPixels );
}
break;
}
case FLOAT:
{
if ( m_ByteOrder == LittleEndian )
{
ByteSwapper<float>::SwapRangeFromSystemToLittleEndian(
(float *)buffer, numberOfPixels );
}
else if ( m_ByteOrder == BigEndian )
{
ByteSwapper<float>::SwapRangeFromSystemToBigEndian(
(float *)buffer, numberOfPixels );
}
break;
}
case UCHAR:
{
if ( m_ByteOrder == LittleEndian )
{
ByteSwapper<unsigned int>::SwapRangeFromSystemToLittleEndian(
(unsigned int*)buffer, numberOfPixels );
}
else if ( m_ByteOrder == BigEndian )
{
ByteSwapper<unsigned int>::SwapRangeFromSystemToBigEndian(
(unsigned int *)buffer, numberOfPixels );
}
break;
}
case SHORT:
{
if ( m_ByteOrder == LittleEndian )
{
ByteSwapper<short>::SwapRangeFromSystemToLittleEndian(
(short*)buffer, numberOfPixels );
}
else if ( m_ByteOrder == BigEndian )
{
ByteSwapper<short>::SwapRangeFromSystemToBigEndian(
(short *)buffer, numberOfPixels );
}
break;
}
case USHORT:
{
if ( m_ByteOrder == LittleEndian )
{
ByteSwapper<unsigned short>::SwapRangeFromSystemToLittleEndian(
(unsigned short*)buffer, numberOfPixels );
}
else if ( m_ByteOrder == BigEndian )
{
ByteSwapper<unsigned short>::SwapRangeFromSystemToBigEndian(
(unsigned short *)buffer, numberOfPixels );
}
break;
}
default:
ExceptionObject exception(__FILE__, __LINE__);
exception.SetDescription("Pixel Type Unknown");
throw exception;
}
}
void FDFImageIO::WriteImageInformation(void)
{
//not possible to write a fdf file
}
void FDFImageIO::Write(const void* /* buffer */)
{
//not possible to write a fdf file
}
} // end namespace itk