forked from BRAINSia/BRAINSTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDWIConvertLib.cxx
609 lines (539 loc) · 14.9 KB
/
DWIConvertLib.cxx
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
//
// Created by Hui Xie on 12/26/16.
//
#include "DWIConvertLib.h"
#include "dcmtk/oflog/helpers/loglog.h"
#include "dcmtk/dcmimgle/dcmimage.h"
#include "dcmtk/dcmjpeg/djdecode.h"
#include "dcmtk/dcmjpls/djdecode.h"
#include "dcmtk/dcmdata/dcrledrg.h"
#include "itksys/SystemTools.hxx"
const std::string emptyString( "" ); // A named empty string
DWIConvert::DWIConvert()
: m_inputFileType{}
, m_outputFileType{}
, m_inputVolume{}
, m_inputDicomDirectory{}
, m_inputBValues{}
, m_inputBVectors{}
, m_gradientVectorFile{} // deprecated
, m_smallGradientThreshold{ 0.2 }
, m_fMRIOutput{ false }
, m_allowLossyConversion{ false }
, m_useIdentityMeasurementFrame{ false }
, m_useBMatrixGradientDirections{ false }
, m_outputVolume{}
, m_outputDirectory{ "." }
, m_outputBValues{}
, m_outputBVectors{}
, m_converter{ nullptr }
{}
DWIConvert::DWIConvert( std::string inputVolume, std::string outputVolume )
: DWIConvert()
{
SetInputFileName( inputVolume );
SetOutputFileName( outputVolume );
}
DWIConvert::~DWIConvert()
{
delete m_converter;
}
int
DWIConvert::read()
{
if ( emptyString == getInputFileType() )
{
std::cerr << "illegal input file type, exit" << std::endl;
return EXIT_FAILURE;
}
// register DCMTK codecs, otherwise they will not be available when
// `itkDCMTKSeriesFileNames` is used to build a list of filenames,
// so reading series with JPEG transfer syntax will fail.
DJDecoderRegistration::registerCodecs();
DcmRLEDecoderRegistration::registerCodecs();
if ( m_fMRIOutput )
{
std::cerr << "Deprecated feature no longer supported: --fMRIOutput" << std::endl;
return EXIT_FAILURE;
}
if ( m_gradientVectorFile != emptyString )
{
std::cerr << "Deprecated feature no longer supported: --gradientVectorFile" << std::endl;
return EXIT_FAILURE;
}
if ( m_outputVolume == emptyString )
{
std::cerr << "Missing output volume name" << std::endl;
return EXIT_FAILURE;
}
// build a NRRD file out of FSL output, which is two text files
// for gradients and b values plus a NIfTI file for the gradient volumes.
if ( "FSL" == getInputFileType() )
{
DWIConverter::FileNamesContainer filesList;
filesList.clear();
filesList.push_back( m_inputVolume );
std::cout << "INPUT VOLUME: " << filesList[0] << std::endl;
FSLDWIConverter * FSLconverter = new FSLDWIConverter( filesList, m_inputBValues, m_inputBVectors );
try
{
FSLconverter->SetAllowLossyConversion( m_allowLossyConversion );
FSLconverter->LoadFromDisk();
FSLconverter->SetThicknessFromSpacing(); // When converting from FSL, use thinkness as spacing[2]
FSLconverter->ExtractDWIData();
}
catch ( itk::ExceptionObject & excp )
{
itkGenericExceptionMacro( << "Exception creating converter " << excp << std::endl );
delete FSLconverter;
}
m_converter = FSLconverter;
}
// make FSL file set from a NRRD file.
else if ( "Nrrd" == getInputFileType() )
{
DWIConverter::FileNamesContainer filesList;
filesList.clear();
filesList.push_back( m_inputVolume );
std::cout << "INPUT VOLUME: " << filesList[0] << std::endl;
NRRDDWIConverter * NRRDconverter = new NRRDDWIConverter( filesList );
try
{
NRRDconverter->SetAllowLossyConversion( m_allowLossyConversion );
NRRDconverter->LoadFromDisk();
NRRDconverter->ExtractDWIData();
}
catch ( itk::ExceptionObject & excp )
{
itkGenericExceptionMacro( << "Exception creating converter " << excp << std::endl );
delete NRRDconverter;
}
m_converter = NRRDconverter;
}
else if ( "Dicom" == getInputFileType() )
{
m_converter = CreateDicomConverter(
m_inputDicomDirectory, m_useBMatrixGradientDirections, m_smallGradientThreshold, m_allowLossyConversion );
}
else
{
std::cerr << "Invalid conversion mode" << std::endl;
return EXIT_FAILURE;
}
return ( nullptr == m_converter ? EXIT_FAILURE : EXIT_SUCCESS );
}
int
DWIConvert::write( const std::string & outputVolume )
{
SetOutputFileName( outputVolume );
const std::string version = "5.0.0";
if ( "FSL" == getOutputFileType() )
{
m_useIdentityMeasurementFrame = true; // Only true is valid for writing FSL
}
if ( m_useIdentityMeasurementFrame )
{
m_converter->ConvertBVectorsToIdentityMeasurementFrame();
}
std::string outputVolumeHeaderName( m_outputVolume );
{ // concatenate with outputDirectory
if ( m_outputVolume.find( "/" ) == std::string::npos && m_outputVolume.find( "\\" ) == std::string::npos )
{
if ( outputVolumeHeaderName.size() != 0 )
{
outputVolumeHeaderName = m_outputDirectory;
outputVolumeHeaderName += "/";
outputVolumeHeaderName += m_outputVolume;
}
}
}
if ( "FSL" == getOutputFileType() )
{
m_converter->ConvertToMutipleBValuesUnitScaledBVectors();
Volume4DType::Pointer img4D = m_converter->OrientForFSLConventions();
// write the image */
m_converter->WriteFSLFormattedFileSet( outputVolumeHeaderName, m_outputBValues, m_outputBVectors, img4D );
}
else if ( "Nrrd" == getOutputFileType() )
{
// NRRD requires scaledDiffusionVectors, so find max BValue
m_converter->ConvertToSingleBValueScaledDiffusionVectors();
//////////////////////////////////////////////
// write header file
// This part follows a DWI NRRD file in NRRD format 5.
// There should be a better way using itkNRRDImageIO.
const std::string commentSection = m_converter->MakeFileComment( version,
m_useBMatrixGradientDirections,
m_useIdentityMeasurementFrame,
m_smallGradientThreshold,
getInputFileType() );
m_converter->ManualWriteNRRDFile( outputVolumeHeaderName, commentSection );
}
else
{
std::cerr << "Invalidate conversion mode" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Wrote file: " << outputVolumeHeaderName << std::endl;
return EXIT_SUCCESS;
}
int
DWIConvert::write()
{
if ( emptyString != m_outputVolume )
{
return write( m_outputVolume );
}
else
{
std::cout << "illegal output Volume name. exit" << std::endl;
return EXIT_FAILURE;
}
}
DWIConverter *
DWIConvert::CreateDicomConverter( const std::string inputDicomDirectory, const bool useBMatrixGradientDirections,
const double smallGradientThreshold, const bool allowLossyConversion )
{
// check for required parameters
if ( inputDicomDirectory == emptyString )
{
std::cerr << "Missing DICOM input directory path" << std::endl;
return nullptr;
}
// use the factor to instantiate a converter object based on the vender.
DWIConverterFactory converterFactory( inputDicomDirectory, useBMatrixGradientDirections, smallGradientThreshold );
DWIConverter * converter;
try
{
converter = converterFactory.New();
}
catch ( itk::ExceptionObject & excp )
{
std::cerr << "Exception creating converter " << excp << std::endl;
return nullptr;
}
if ( nullptr == converter )
{
std::cerr << "Unable to create converter!" << std::endl;
return nullptr;
}
// read Dicom directory
try
{
converter->SetAllowLossyConversion( allowLossyConversion );
converter->LoadFromDisk();
}
catch ( itk::ExceptionObject & excp )
{
std::cerr << "Exception creating converter " << excp << std::endl;
delete converter;
return nullptr;
}
// extract the DWI data
try
{
converter->ExtractDWIData();
}
catch ( itk::ExceptionObject & excp )
{
std::cerr << "Exception extracting gradient vectors " << excp << std::endl;
delete converter;
return nullptr;
}
// this is a punt, it will still write out the volume image
// even if we don't know how to extract gradients.
if ( converterFactory.GetVendor() == "GENERIC" )
{
std::cerr << "Can't extract DWI data from files created by vendor " << converterFactory.GetVendor() << std::endl;
delete converter;
exit( EXIT_FAILURE );
}
return converter;
}
void
DWIConvert::SetInputFileName( std::string inputFilePath )
{
const auto isDirectory = itksys::SystemTools::FileIsDirectory( inputFilePath );
if ( isDirectory )
{
m_inputDicomDirectory = inputFilePath;
}
else
{
const auto isRegularFile = itksys::SystemTools::FileExists( inputFilePath, true );
if ( isRegularFile )
{
m_inputVolume = inputFilePath;
}
else
{
std::cerr << "Error: the inputVolume is neither file nor directory." << std::endl;
}
}
// prefer the inputVolume field if available
if ( ( !m_inputVolume.empty() ) || m_inputDicomDirectory.empty() )
{
const std::string inputExt = itksys::SystemTools::GetFilenameExtension( m_inputVolume );
if ( std::string::npos != inputExt.rfind( ".nii" ) )
{
m_inputFileType = "FSL";
}
else if ( std::string::npos != inputExt.rfind( ".nrrd" ) || std::string::npos != inputExt.rfind( ".nhdr" ) )
{
m_inputFileType = "Nrrd";
}
else
{
std::cerr << "Error: file type of inputVoume is not supported currently" << std::endl;
}
}
else if ( emptyString != m_inputDicomDirectory )
{
m_inputFileType = "Dicom";
}
else
{
std::cerr << "Error: One of inputVoume and inputDicomDirectory must be empty string" << std::endl;
}
}
void
DWIConvert::SetOutputFileName( std::string outputFilePath )
{
m_outputVolume = outputFilePath;
m_outputFileType = detectOuputVolumeType( outputFilePath );
}
std::string
DWIConvert::getInputFileType()
{
return m_inputFileType;
}
std::string
DWIConvert::getOutputFileType()
{
return m_outputFileType;
}
const std::string &
DWIConvert::getInputVolume() const
{
return m_inputVolume;
}
void
DWIConvert::setInputVolume( const std::string & inputVolume )
{
m_inputVolume = inputVolume;
}
const std::string &
DWIConvert::getInputDicomDirectory() const
{
return m_inputDicomDirectory;
}
void
DWIConvert::setInputDicomDirectory( const std::string & inputDicomDirectory )
{
m_inputDicomDirectory = inputDicomDirectory;
}
const std::string &
DWIConvert::getInputBValues() const
{
return m_inputBValues;
}
void
DWIConvert::setInputBValues( const std::string & inputBValues )
{
m_inputBValues = inputBValues;
}
const std::string &
DWIConvert::getInputBVectors() const
{
return m_inputBVectors;
}
void
DWIConvert::setInputBVectors( const std::string & inputBVectors )
{
m_inputBVectors = inputBVectors;
}
const std::string &
DWIConvert::getGradientVectorFile() const
{
return m_gradientVectorFile;
}
void
DWIConvert::setGradientVectorFile( const std::string & gradientVectorFile )
{
m_gradientVectorFile = gradientVectorFile;
}
double
DWIConvert::getSmallGradientThreshold() const
{
return m_smallGradientThreshold;
}
void
DWIConvert::setSmallGradientThreshold( double smallGradientThreshold )
{
m_smallGradientThreshold = smallGradientThreshold;
}
bool
DWIConvert::isfMRIOutput() const
{
return m_fMRIOutput;
}
void
DWIConvert::setfMRIOutput( bool fMRIOutput )
{
m_fMRIOutput = fMRIOutput;
}
bool
DWIConvert::isAllowLossyConversion() const
{
return m_allowLossyConversion;
}
void
DWIConvert::setAllowLossyConversion( bool allowLossyConversion )
{
m_allowLossyConversion = allowLossyConversion;
}
bool
DWIConvert::isUseIdentityMeasurementFrame() const
{
return m_useIdentityMeasurementFrame;
}
void
DWIConvert::setUseIdentityMeasurementFrame( bool useIdentityMeasurementFrame )
{
m_useIdentityMeasurementFrame = useIdentityMeasurementFrame;
}
bool
DWIConvert::isUseBMatrixGradientDirections() const
{
return m_useBMatrixGradientDirections;
}
void
DWIConvert::setUseBMatrixGradientDirections( bool useBMatrixGradientDirections )
{
m_useBMatrixGradientDirections = useBMatrixGradientDirections;
}
const std::string &
DWIConvert::getOutputVolume() const
{
return m_outputVolume;
}
void
DWIConvert::setOutputVolume( const std::string & outputVolume )
{
m_outputVolume = outputVolume;
}
const std::string &
DWIConvert::getOutputDirectory() const
{
return m_outputDirectory;
}
void
DWIConvert::setOutputDirectory( const std::string & outputDirectory )
{
m_outputDirectory = outputDirectory;
}
const std::string &
DWIConvert::getOutputBValues() const
{
return m_outputBValues;
}
void
DWIConvert::setOutputBValues( const std::string & outputBValues )
{
m_outputBValues = outputBValues;
}
const std::string &
DWIConvert::getOutputBVectors() const
{
return m_outputBVectors;
}
void
DWIConvert::setOutputBVectors( const std::string & outputBVectors )
{
m_outputBVectors = outputBVectors;
}
DWIConverter *
DWIConvert::getConverter() const
{
return m_converter;
}
// public utility interface
std::string
detectOuputVolumeType( const std::string & outputVolume )
{
std::string outputFileType = "";
const std::string outputExt = itksys::SystemTools::GetFilenameExtension( outputVolume );
if ( std::string::npos != outputExt.rfind( ".nii" ) )
{
outputFileType = "FSL";
}
else if ( std::string::npos != outputExt.rfind( ".nrrd" ) || std::string::npos != outputExt.rfind( ".nhdr" ) )
{
outputFileType = "Nrrd";
}
else
{
std::cerr << "Error: the output file type is not supported currently" << std::endl;
}
return outputFileType;
}
bool
convertInputVolumeVectorToNrrdOrNifti( const std::string & targetType,
const std::vector< std::string > & inputVolumeVector,
std::vector< std::string > & targetVolumeVector )
{
targetVolumeVector.clear();
int nSize = inputVolumeVector.size();
for ( int i = 0; i < nSize; ++i )
{
std::string targetVolume;
if ( convertInputVolumeToNrrdOrNifti( targetType, inputVolumeVector.at( i ), targetVolume ) )
{
targetVolumeVector.push_back( targetVolume );
}
else
{
return false;
}
}
return true;
}
bool
convertInputVolumeToNrrdOrNifti( const std::string & targetType, const std::string & inputVolume,
std::string & targetVolume )
{
DWIConvert dwiConvert;
dwiConvert.SetInputFileName( inputVolume );
if ( targetType == dwiConvert.getInputFileType() )
{
targetVolume = inputVolume;
}
else
{
if ( "FSL" == targetType )
{
targetVolume = itksys::SystemTools::GetFilenameWithoutExtension( inputVolume ) + "_convert.nii";
}
else if ( "Nrrd" == targetType )
{
targetVolume = itksys::SystemTools::GetFilenameWithoutExtension( inputVolume ) + "_convert.nrrd";
}
else
{
std::cout << "Error: the targetType of DWIConvert is not supported. " << std::endl;
return false;
}
dwiConvert.SetOutputFileName( targetVolume );
int result = dwiConvert.read();
if ( EXIT_SUCCESS == result )
{
dwiConvert.write( targetVolume );
}
else
{
std::cout << "Error: read inputVolume failed." << std::endl;
return false;
}
}
return true;
}