1 #include "bruker2dicom2.h"
2 #include "brukerexception.h"
4 #include <gdcmAttribute.h>
5 #include <boost/filesystem.hpp>
6 #include <boost/algorithm/string.hpp>
9 #ifndef PATH_MAX // If not defined yet : do it
13 bool Bruker2Dicom::Execute()
16 //--- first, Test the system----//
17 bool bigEndian = gdcm::ByteSwap<uint16_t>::SystemIsBigEndian();
18 gdcm::SwapCode sc = gdcm::SwapCode::Unknown;
19 if ( gdcm::ByteSwap<uint16_t>::SystemIsBigEndian() )
21 sc = gdcm::SwapCode::BigEndian;
23 else if ( gdcm::ByteSwap<uint16_t>::SystemIsLittleEndian() )
25 sc = gdcm::SwapCode::LittleEndian;
27 else // sc == gdcm::SwapCode::Unknown
32 // ----- Check input directory name -----
33 if ( ! boost::filesystem::is_directory(InputDirName) )
35 std::cout << "KO : [" << InputDirName << "] is not a Directory." << std::endl;
41 std::cout << "OK : [" << InputDirName << "] is a Directory." << std::endl;
44 // ----- Check output directory name -----
45 if ( ! boost::filesystem::is_directory(OutputDirName) )
47 bool res=CreateDirectory(OutputDirName);
50 std::cout << "[" << OutputDirName << "] Directory creation failure " << std::endl;
51 throw ( BrukerHopelessException ("Output directory creation failure "));
57 std::cout << "[" << OutputDirName << "] already exist " << std::endl;
61 // Level 0 is the original directory
62 // who could contain "1 2 3 4 5 6" directories and "AdjStatePerStudy subject" files
63 std::string strDirNamein(InputDirName);
64 boost::filesystem::directory_iterator it(InputDirName), it_end;
67 for(; it != it_end; ++it)
69 if(! boost::filesystem::is_directory( (*it) ) )
71 if( (*it).filename() == "subject")
75 if( (*it).filename() == "acqp")
82 // 1 : if subjectFound : user passed a Single Study Directory
83 // 2 : if NOT subjectFound and acqpFound : user passed a Serie Directory
84 // 3 : if NOT subjectFound and NOT acqpFound : user passed a 'non Study Directory' (Hope it's a set of Single Study Directories)
86 if (subjectFound ) type = 1; // user passed a 'study
87 else if (acqpFound) type = 2; // user passed a 'serie'
88 else type = 3; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
93 std::string subject = InputDirName + VALID_FILE_SEPARATOR;
95 boost::algorithm::replace_all( subject, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
96 bool canOpen = br_subject.LoadFile(subject);
99 std::cout << "Hopeless! 'subject' found / cannot be open" << std::endl;
100 throw ( BrukerHopelessException ("Hopeless! 'subject' found in root input directory / cannot be open"));
104 br_subject.FillMap();
105 // get info for 'Study Description'
106 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
107 subject_name = b_name.GetStringValue()[0];
108 strPatientName = subject_name;
109 cleanString(subject_name);
110 DealWithSingleStudyDirectory (InputDirName, OutputDirName);
117 subject_name = "defaultPatName";
118 strPatientName = subject_name;
119 DealWithSingleStudyDirectory (InputDirName,OutputDirName);
125 std::cout << " User passed a 'non study' directory; Hope it's a *non recursive* 'set of studies' directory! (recursive not yet dealt with)" << std::endl;
126 // DealWithMultiStudyDirectory (fileNames);
132 // ----------------------------------------------------------------------------------------------------------
134 void Bruker2Dicom::DealWithMultiStudyDirectory (gdcm::Directory::FilenamesType &dirNames,const std::string ¤tOutputDirName)
136 gdcm::Directory::FilenamesType::iterator it;
138 for (it = dirNames.begin();
139 it != dirNames.end();
142 if ( boost::filesystem::is_directory(*it) )
144 subjectFound = false;
146 std::cout << "in 'DealWithMultiStudyDirectory' [" << *it << "] is a directory" << std::endl;
148 gdcm::Directory dirList;
150 gdcm::Directory::FilenamesType fileNames;
151 fileNames = dirList.GetFilenames();
153 gdcm::Filename file(fileNames.begin()->c_str());
154 std::string path = file.GetPath();
155 std::string subject = path +
156 VALID_FILE_SEPARATOR +
158 boost::algorithm::replace_all( subject, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
159 if (! boost::filesystem::is_regular(subject) )
161 std::cout << "no [" << subject << "] file found" << std::endl;
165 bool canOpen = br_subject.LoadFile(subject);
168 std::cout << "Hopeless! 'subject' found / cannot be open" << std::endl;
169 throw ( BrukerHopelessException ("Hopeless! 'subject' found in root input directory / cannot be open"));
173 br_subject.FillMap();
174 subjectFound = true; // Hope so! (full checking needed!)
176 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
177 subject_name = b_name.GetStringValue()[0];
178 strPatientName = subject_name;
179 cleanString(subject_name);
181 DealWithSingleStudyDirectory(dirList.GetDirectories().begin()->c_str(), currentOutputDirName);
187 std::cout << "[" << *it << "] is NOT a directory; skipped!" << std::endl;
192 // ----------------------------------------------------------------------------------------------------------
194 void Bruker2Dicom::DealWithSingleStudyDirectory (const std::string &dirname, const std::string &i_outputDir) //(gdcm::Directory::FilenamesType &fileNames)
197 // creation directory : 'nom du patient'
198 std::string output = createDirectory(subject_name.c_str(), i_outputDir);
203 BrukerFieldData b_entry=br_subject.GetFieldData("SUBJECT_entry");
204 subject_entry = b_entry.GetStringValue()[0];
205 //cleanString(subject_entry);
206 subject_entry = subject_entry.substr(11, subject_entry.size()-11);
208 BrukerFieldData b_position=br_subject.GetFieldData("SUBJECT_position");
209 subject_position = b_position.GetStringValue()[0];
210 //cleanString(subject_position);
211 subject_position = subject_position.substr(9, subject_position.size()-9);
213 BrukerFieldData b_date=br_subject.GetFieldData("SUBJECT_date");
214 subject_date = b_date.GetStringValue()[0];
215 strStudyTimeDate = subject_date;
216 cleanString(subject_date);
218 BrukerFieldData b_study_name=br_subject.GetFieldData("SUBJECT_study_name");
219 subject_study_name = b_study_name.GetStringValue()[0];
220 subject_study_name = subject_study_name.substr(1, subject_study_name.size()-2);
221 cleanString(subject_study_name);
223 else // Desperate trick when file 'subject' is missing
225 subject_entry = "HeadFirst"; // Why not?
226 subject_position = "Supine"; // Why not?
227 strStudyTimeDate = "06_06_06_6_June_1666"; // Why not?
228 subject_date = "6_June_1666"; // Why not?
229 subject_study_name = "defStudyName"; // Why not?
231 // subject_name is already in 'Patient Name'
232 strStudyDescr = /*subject_name + "." + */ subject_study_name /*+ "." + subject_entry + "." + subject_position + "." + subject_date*/;
234 // creation directory : 'nom de la Study'
235 output = createDirectory(strStudyDescr, output);
237 gdcm::UIDGenerator uid;
238 strStudyUID = uid.Generate();
242 // -----------------------------------------------------
243 // Iterate to ALL the objets(files/directories) found in the input directory
244 // (this is level ZERO)
245 // each Directory (name : 1, 2, 3, ...) will be a Dicom Serie
246 // -----------------------------------------------------
247 boost::filesystem::directory_iterator it(dirname), end_iter;
248 for(; it != end_iter; ++it)
250 if(boost::filesystem::is_directory(*it))
252 std::string fname = (*it).filename() ;
253 //if (fname == "AdjResult") continue; what's the point?
254 boost::filesystem::directory_iterator it_level((*it));
256 std::string nextLevel;
258 std::string nameAcqp;
259 for(; it_level != end_iter; ++it_level)
261 if( (*it_level).filename() == "acqp")
263 strAcqp = (*it_level).string();
264 nameAcqp = (*it_level).filename();
265 boost::algorithm::replace_all( strAcqp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
268 else if (boost::filesystem::is_directory( (*it_level) ) && nextLevel == "")
270 nextLevel = (*it_level).string(); // Hope we found only one directory
280 std::string acqp_scan_name;
281 std::string acqp_method;
282 std::string acqp_protocol_location;
284 br_acqp.LoadFile(strAcqp);
286 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
287 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
288 cleanString(acqp_protocol_location);
290 BrukerFieldData b_scan_name=br_acqp.GetFieldData("ACQ_scan_name");
291 acqp_scan_name = b_scan_name.GetStringValue()[0];
292 cleanString(acqp_scan_name);
294 BrukerFieldData b_method=br_acqp.GetFieldData("ACQ_method");
295 b_method.PrintSelf(); // why?
296 acqp_method = b_method.GetStringValue()[0];
297 cleanString(acqp_method);
299 BrukerFieldData b_list_size = br_acqp.GetFieldData("ACQ_O1_list_size");
300 nbSlices = b_list_size.GetIntValue()[0];
301 strSerieDescr = fname
302 + "." + acqp_scan_name
303 + "." + acqp_method.c_str();
306 /*sprintf(outputDirName, "%s%c%s", i_outputDir.c_str(), VALID_FILE_SEPARATOR, strSerieDescr.c_str() );
307 std::string temp(outputDirName);
308 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);*/
309 output = createDirectory(strSerieDescr, output);
311 printf ("outputDirName [%s]\n", output);
314 DealWithNiveau1( (*it).string(), output);
316 catch (BrukerHopelessException &e)
318 std::cout << "And Exception was thrown in DealWithNiveau1 (" << e.what() << ") " << std::endl;
323 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
326 // =====================================================================
328 void Bruker2Dicom::DealWithNiveau1(std::string &level1Directory, std::string ¤tOutputDirName) {
330 // e.g. : at level 1, in B67d1.Bp1/6
332 // acqp fid imnd pdata pulseprogram spnam0 spnam1
335 //gdcm::Directory dirList;
336 //dirList.Load(level1Directory); // DON'T get recursively the list of files
337 //gdcm::Directory::FilenamesType fileNames;
338 //fileNames = dirList.GetFilenames();
340 // -----------------------------------------------------
341 // Iterate to ALL the objets(files/directories) found in the input directory
342 // -----------------------------------------------------
343 bool bmethod = false;
344 //gdcm::Directory::FilenamesType::iterator it = fileNames.begin();
345 boost::filesystem::directory_iterator it(level1Directory), it_end;
346 for(; it != it_end; it++)
348 if((*it).leaf() == "method")
350 bmethod = br_method.LoadFile((*it).string());
355 // if method wasn't found or unable to load, try with imnd file
358 for(; it != it_end; it++)
360 if((*it).leaf() == "imnd")
362 bmethod = br_method.LoadFile((*it).string());
369 std::cout << "Hopeless! neither 'method' nor 'imnd' found in ["
370 << level1Directory << "]; we skip it!" << std::endl;
373 // std::cout << "open => [" << strMethod << "] successfully" << std::endl;
375 ##$PVM_Fov (dimension) // ou plutot RECO_fov !
378 dans method (pour perfusion seulement?) :
379 ##$PVM_ObjOrderList=( 8 )
382 ##$PVM_SPackArrNSlices=( 2 )
389 boost::filesystem::directory_iterator itDir(level1Directory), it_end;
390 for(; itDir != it_end; ++itDir)
392 if ( boost::filesystem::is_directory(*itDir) && (*itDir).leaf() == "pdata")
394 std::string output = createDirectory( (*itDir).leaf() , currentOutputDirName);
395 // will be always "pdata" ...
396 boost::filesystem::directory_iterator itDirInside(*itDir), it_inside_end;
397 for(; itDirInside != it_end; ++it)
399 // we take the first and hope that we have only one!
400 if ( boost::filesystem::is_directory(*itDirInside))
402 output = createDirectory( (*itDirInside).leaf() , output);
403 DealWithNiveau2( (*itDirInside).string(), output);
409 catch(BrukerHopelessException &e)
411 std::cout << "And Exception was thrown in DealWithNiveau2 (" << e.what() << ") " << std::endl;
416 // =====================================================================
418 void Bruker2Dicom::DealWithNiveau2(std::string &level2Directory, std::string ¤tOutputDirName) {
420 // e.g. : at level 2 in B67d1.Bp1/6/pdata
422 // acqp fid imnd pdata pulseprogram spnam0 spnam1
424 std::string output = currentOutputDirName;
427 boost::filesystem::directory_iterator it(level2Directory), it_end;
428 for(; it != it_end; ++it)
430 if((*it).leaf() == "isa")
432 bisa = br_isa.LoadFile((*it).string());
436 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
438 std::string str_isa_func_name = b_isa_func_name.GetStringValue()[0];
439 cleanString(str_isa_func_name);
440 output = createDirectory(str_isa_func_name, currentOutputDirName);
447 DealWithNiveau3(level2Directory, output);
449 catch (BrukerHopelessException &e)
451 std::cout << "And Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
452 << " We skip [" << level2Directory << "]" << std::endl;
455 catch (BrukerInitException &e)
457 std::cout << "And Init Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
458 << " We skip [" << level2Directory << "]" << std::endl;
465 // =====================================================================
468 void Bruker2Dicom::DealWithNiveau3(std::string &level3Directory, std::string ¤tOutputDirName){
471 // e.g. at level 3, in
473 // just to be able to go on checking // JP
480 gdcm::Directory dirList;
481 dirList.Load(level3Directory); // DON'T get recursively the list of files
482 gdcm::Directory::FilenamesType::iterator it;
483 gdcm::Directory::FilenamesType fileNames = dirList.GetFilenames();
485 char original2dseqName [(unsigned int) PATH_MAX+2];
486 char original2dseqName_XXX [(unsigned int) PATH_MAX+2];
487 char currentOutputMhdDirName [(unsigned int) PATH_MAX+2];
489 char outputMhdFileName [(unsigned int) PATH_MAX+2];
490 char output2dseqSliceFileName[(unsigned int) PATH_MAX+6]; // think about extra '.dcm'
491 char output2dseqName [(unsigned int) PATH_MAX+6];
492 char output2dseqCartoName [(unsigned int) PATH_MAX+6];
494 char copyFile[PATH_MAX + PATH_MAX + 5]; // Should be enough!
498 //-------------- try d3proc;
500 canOpen = br_d3proc.LoadFile(findFile( level3Directory,"d3proc"));
504 std::cout << "Hopeless! no 'd3proc' found" << std::endl;
505 throw ( BrukerHopelessException ("Hopeless! no 'd3proc' found"));
506 //exit(0); /// \TODO throw an exception !
510 canOpen = br_d3proc.FillMap();
513 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
514 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'd3proc'"));
518 //-------------- end try d3proc;
521 // -------------------try reco
524 std::string str_reco = level3Directory + VALID_FILE_SEPARATOR + "reco";
525 canOpen = br_reco.LoadFile(findFile( level3Directory, "reco"));
528 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
529 throw ( BrukerHopelessException ("Hopeless! cannot find 'reco'"));
530 //exit(0); /// \TODO throw an exception !
535 std::cout << "[" << str_reco << "] successfully Loaded " << std::endl;
536 canOpen = br_reco.FillMap();
539 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
540 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'reco'"));
545 std::cout << "[" << str_reco << "] successfully Mapped" << std::endl;
549 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
550 // br_reco.PrintSelf();
551 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
552 // -------------------end try reco
555 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
556 int NX = bX.GetIntValue()[0];
559 std::cout << "IM_SIX " << NX << std::endl;
560 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
561 int NY = bY.GetIntValue()[0];
564 std::cout << "IM_SIY " << NY << std::endl;
565 /// \todo : check if there are actually 3 dimensions or only 2
567 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
568 int nbFrames = bZ.GetIntValue()[0];
570 std::cout << "IM_SIZ " << nbFrames << std::endl;
572 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
574 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
576 std::string mhdDataPixelType;
578 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
584 // En regle generale il vaut mieux que l'on passe par RECO_*
585 // pour extraire les parametres de l'image
588 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
589 double fovX = fov.GetDoubleValue()[0];
590 double fovY = fov.GetDoubleValue()[1];
592 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
594 BrukerFieldData spatResol = br_method.GetFieldData("PVM_SpatResol");
595 double spatResolX = spatResol.GetDoubleValue()[0];
596 double spatResolY = spatResol.GetDoubleValue()[1];
598 std::cout << "SpatResol (ds method) " << spatResolX << " " << spatResolY << std::endl;
603 // Better we use 'get' accessors from BrukerImage class, as Denis wrote them
605 BrukerFieldData fov = br_reco.GetFieldData("RECO_fov");
606 double fovX = fov.GetDoubleValue()[0];
607 double fovY = fov.GetDoubleValue()[1];
609 std::cout << "FOV (ds reco) " << fovX << " " << fovY << std::endl;
611 BrukerFieldData size = br_reco.GetFieldData("RECO_size");
612 double sizeX = size.GetDoubleValue()[0];
613 double sizeY = size.GetDoubleValue()[1];
616 std::cout << "SIZE (ds reco) " << sizeX << " " << sizeY << std::endl;
618 double spatResolX = fovX / sizeX;
619 double spatResolY = fovY / sizeY;
622 std::cout << "spatResol (ds reco : fov/size) " << spatResolX << " " << spatResolY << std::endl;
626 /// \TODO probabely a more sophisticated accessor will be necessary :
627 /// (cf : non contiguous slices, overlapping, slice thickness, space between slices, etc)
629 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
630 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
633 std::cout << "SPackArrSliceDistance (ds method) " << sliceDistance << std::endl;
635 // ----------------------------------------------------------------------------------------
639 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
640 VALID_FILE_SEPARATOR, "MhdFiles");
641 std::string temp(currentOutputMhdDirName);
642 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
643 sprintf(currentOutputMhdDirName, "%s", temp);
644 std::string strCurrentOutputMhdDirName(currentOutputMhdDirName);
645 res = CreateDirectory( strCurrentOutputMhdDirName );
647 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
648 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
653 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
657 std::cout << "nbFrames " << nbFrames << std::endl;
659 std::cout << "nbSlices " << nbSlices << std::endl;
661 int nbInstants = nbFrames/nbSlices;
663 std::cout << "nbInstants (deduced)" << nbInstants << std::endl;
666 FILE *fp; // for MHD files
668 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(),VALID_FILE_SEPARATOR, "2dseq");
669 temp = level3Directory + VALID_FILE_SEPARATOR + "2dseq" ;
670 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
671 sprintf(original2dseqName, "%s", temp);
674 // \TODO : tenir compte du bazar precedent
676 // load 2dseq in memory
678 fp = fopen(temp.c_str(), "rb");
681 // try 2dseq_Angio2D ?!?
682 temp = original2dseqName_XXX;
683 sprintf( original2dseqName_XXX, "%s%s", original2dseqName, "_Angio2D");
684 sprintf(original2dseqName_XXX, "%s", temp);
685 fp = fopen(original2dseqName_XXX, "rb");
688 std::cout << "Cannot open [" << original2dseqName << "] nor [" << original2dseqName_XXX << "] for reading" << std::endl;
690 throw ( BrukerHopelessException ("Hopeless! Cannot open '2dseq'"));
695 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
696 ///\ TODO : find a safer way to be sure to read everything!
697 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
699 // This one will be important!
700 // ---------------------------
702 imageSet = CreateImageSet ( );
704 catch (BrukerInitException& e)
707 std::cout << "an Init Exception was thrown in CreateImageSet ( ); " << e.what()
708 << "catched in DealWithNiveau3" << std::endl;
714 gdcm::UIDGenerator uid;
715 strSerieUID = uid.Generate();
717 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
720 std::cout << "Single instant : do not split" << std::endl;
723 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
724 VALID_FILE_SEPARATOR);
725 temp = currentOutputDirName + VALID_FILE_SEPARATOR + "MhdFiles" + VALID_FILE_SEPARATOR +"MhdData_All_the_Slices.mhd";
726 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
727 sprintf(outputMhdFileName,"%s", temp);
728 fp=fopen(temp.c_str(), "w");
731 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
732 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
737 fprintf(fp, "ObjectType = Image\n");
738 fprintf(fp, "NDims = 3\n" );
739 fprintf(fp, "BinaryData = True \n" );
740 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
741 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
742 fprintf(fp, "HeaderSize = %d\n", 0);
743 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
744 fprintf(fp, "ElementSpacing = %lf %lf %lf\n", spatResolX, spatResolY, sliceDistance );
745 fprintf(fp, "Position = 0 0 %d\n", 0 );
746 fprintf(fp, "Offset = 0 0 0\n" );
747 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
748 fprintf(fp, "ElementNumberOfChannels = 1\n" );
749 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
750 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
753 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
754 currentOutputDirName.c_str(), VALID_FILE_SEPARATOR);
755 temp = currentOutputDirName + VALID_FILE_SEPARATOR + "2dseq_All_the_Slices";
756 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
757 sprintf(output2dseqSliceFileName,"%s",temp);
758 fp=fopen(temp.c_str(), "wb");
761 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
762 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseq file for writting"));
766 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
770 gdcm::UIDGenerator uid;
771 strSerieUID = uid.Generate();
776 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
777 VALID_FILE_SEPARATOR);
778 temp = currentOutputDirName + VALID_FILE_SEPARATOR + "2dseq_All_the_Slices.dcm";
779 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
780 sprintf(output2dseqSliceFileName, "%s", temp);
782 /* ----------- Write Dicom Image ---------------*/
783 MakeDicomImage(buffer_2dseq,
788 //fovX/NY, fovY/NY, sliceDistance,
789 spatResolX, spatResolY, sliceDistance,
798 0,// index frame number
799 4 //GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
802 } // end if nbInstants = 1
804 else // more than ONE instant
807 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
808 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
811 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
815 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
816 VALID_FILE_SEPARATOR, k );
817 temp = outputMhdFileName;
818 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
819 sprintf(outputMhdFileName, "%s", temp);
821 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
822 fp=fopen(outputMhdFileName, "w");
825 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
826 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
831 /* ----------- Write MHD Image ---------------*/
833 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
834 fprintf(fp, "ObjectType = Image\n");
835 fprintf(fp, "NDims = 3\n" );
836 fprintf(fp, "BinaryData = True \n" );
837 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
838 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
839 fprintf(fp, "HeaderSize = %d\n", 0);
840 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); //
841 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, 1.0 ); //slice distance : no meaning for temporal serie
842 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
843 fprintf(fp, "Offset = 0 0 0\n" );
844 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
845 fprintf(fp, "ElementNumberOfChannels = 1\n" );
846 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
847 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", VALID_FILE_SEPARATOR, sliceNb );
851 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
852 currentOutputDirName.c_str(), VALID_FILE_SEPARATOR,sliceNb);
853 temp = output2dseqSliceFileName;
854 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
855 sprintf(output2dseqSliceFileName, "%s", temp);
856 fp=fopen(output2dseqSliceFileName, "wb");
859 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
860 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseqSliceFile file for writting"));
863 int frameSize = NX*NY*pixelSize;
864 for (instantNb=0; instantNb<nbInstants; instantNb++)
866 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
867 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
873 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
880 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
881 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
882 fp=fopen(output2dseqSliceFileName, "wb");
885 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
888 int frameSize = NX*NY*pixelSize;
889 for (instantNb=0; instantNb<nbInstants; instantNb++)
891 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
892 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
898 fp=fopen(output2dseqSliceFileName, "rb");
901 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
904 fread( pixelsForCurrentSlice,
905 frameSize*nbInstants,
908 // end of desperate try !
911 /* ----------- Write Dicom Image ---------------*/
913 int frameSize = NX*NY*pixelSize;
914 for (instantNb=0; instantNb<nbInstants; instantNb++)
916 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
919 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
920 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
921 currentOutputDirName.c_str(), VALID_FILE_SEPARATOR, sliceNb);
922 temp= output2dseqSliceFileName;
923 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
924 sprintf(output2dseqSliceFileName, "%s", temp);
927 pixelsForCurrentSlice,
932 spatResolX, spatResolY, sliceDistance,
933 //fovX/NY, fovY/NY, sliceDistance,
934 output2dseqSliceFileName,
943 4 //GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
946 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
952 delete [] pixelsForCurrentSlice;
953 } // end nbInstants == 1
954 delete [] buffer_2dseq;
958 // -----------------------------------------------------
959 // deal with MatLab-generated Carto file.
960 // -----------------------------------------------------
962 dealWithCarto(fileNames, NX, NY, nbSlices, /*fovX, fovY,*/ spatResolX, spatResolY, sliceDistance,
963 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
966 // ===========================================================================================
968 void Bruker2Dicom::dealWithCarto(gdcm::Directory::FilenamesType &fileNames, int NX, int NY, int nbSlices,
969 /*double fovX, double fovY, */
970 double spatResolX, double spatResolY, double sliceDistance,
971 char *copyFile, std::string ¤tOutputDirName,
972 char *outputMhdFileName, char *output2dseqCartoName)
974 // -----------------------------------------------------
975 // deal with MatLab-generated Carto file.
976 // -----------------------------------------------------
978 const char *code[] = { "ADC", "adc", "TTP", "ttp", "PEAK", "peak", "" }; // add more carto file name identifiers if necessary; end with ""
979 const char *separator[] = { "_", ".", "-", "" }; // add more, if necessary, to ckeck for 2dseq.ADC, 2dseq_ADC, 2dseq-ADC, etc; end with ""
982 gdcm::Directory::FilenamesType::iterator it;
983 char file_name_ident[500];
986 // Iterate to ALL the objets(files/directories) found in the input directory
987 for (it = fileNames.begin();
988 it != fileNames.end();
991 if ( boost::filesystem::is_regular(*it) )
992 //if ( ! boost::filesystem::is_directory(*it) )
995 std::cout << "--- [" << *it << "] is a file..." << std::endl;
998 while (code[icode][0] != 0)
1001 while (separator[iseparator][0] != 0)
1003 sprintf(file_name_ident, "2dseq%s%s",separator[iseparator],code[icode]); // e.g "2dseq_ADC"
1005 // std::cout << "check name ["<<(*it) << "] for string [" << file_name_ident << "]" << std::endl;
1006 std::string::size_type loc = (*it).rfind(file_name_ident);
1008 if ( loc != std::string::npos )
1011 ///\ TODO : find a safer way to be sure to read everything!
1012 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
1013 fp = fopen ( (*it).c_str(), "rb");
1015 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
1016 throw ( BrukerHopelessException ("Level 1 Unable to open 'carto' file "));
1018 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
1020 // ?!? sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1021 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
1022 << NX*NY*sizeof(double)*nbSlices << std::endl;
1023 gdcm::Filename file ( (*it).c_str());
1024 std::string lastFileName = file.GetName();
1027 // Copy the data file in the new directory
1028 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1029 currentOutputDirName.c_str(), VALID_FILE_SEPARATOR, lastFileName.c_str());
1030 std::string temp(copyFile);
1031 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
1032 sprintf(copyFile, "%s", temp);
1035 sprintf(outputMhdFileName, "%s%c%s%s",
1036 currentOutputDirName.c_str(),VALID_FILE_SEPARATOR, lastFileName.c_str(), ".mhd" );
1037 temp = outputMhdFileName;
1038 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
1039 sprintf(outputMhdFileName, "%s", temp);
1042 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1045 fp=fopen(outputMhdFileName, "w");
1048 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
1052 fprintf(fp, "ObjectType = Image\n");
1053 fprintf(fp, "NDims = 3\n" );
1054 fprintf(fp, "BinaryData = True \n" );
1055 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
1056 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
1057 fprintf(fp, "HeaderSize = %d\n", 0 );
1058 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, sliceDistance );
1059 fprintf(fp, "Position = 0 0 0\n" );
1060 fprintf(fp, "Offset = 0 0 0\n" );
1061 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
1062 fprintf(fp, "ElementNumberOfChannels = 1\n" );
1063 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
1064 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
1069 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1072 // ----------- Write Dicom Image ---------------
1076 sprintf(output2dseqCartoName, "%s%c%s%s",
1077 currentOutputDirName.c_str(), VALID_FILE_SEPARATOR, lastFileName.c_str(), ".dcm" );
1079 std::string temp(output2dseqCartoName);
1080 boost::algorithm::replace_all( temp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
1081 sprintf(output2dseqCartoName,"%s", temp);
1084 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
1085 gdcm::UIDGenerator uid;
1086 strSerieUID = uid.Generate();
1088 gdcm::Filename file( (*it).c_str() );
1089 std::string strNewSerieDescr(strSerieDescr+ "_" + file.GetName());
1090 MakeDicomImage(buffer_carto,
1095 //fovX/NY, fovY/NY, sliceDistance,
1096 spatResolX, spatResolY, sliceDistance,
1097 output2dseqCartoName,
1106 3// GDCM_NAME_SPACE::CREATED_IMAGE
1110 delete [] buffer_carto;
1112 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
1113 break; // don't check for more ident on same file name!
1115 } // end deal with _ADC, -adc, etc
1117 } // end iterate speparators
1119 } // end iterate code
1120 } // end boost::filesystem::is_regular(*it)
1121 } // end iterate on all objects (files, dir, etc)
1124 // ==========================================================================================================
1126 int Bruker2Dicom::CheckUserDirectory(std::string &userDirName)
1128 gdcm::Directory dirList;
1129 dirList.Load(userDirName); // DON'T get recursively the list of files
1131 gdcm::Directory::FilenamesType fileNames;
1132 fileNames = dirList.GetFilenames();
1134 gdcm::Filename file( fileNames.begin()->c_str());
1135 std::string path = file.GetPath();
1137 std::string subject = path +
1138 VALID_FILE_SEPARATOR +
1140 boost::algorithm::replace_all( subject, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
1141 std::string acqp = path +
1142 VALID_FILE_SEPARATOR +
1144 boost::algorithm::replace_all( acqp, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
1145 if ( boost::filesystem::is_regular(subject) )
1146 subjectFound = true; // user passed a 'study
1148 subjectFound = false; // user didnt' pass a 'study
1150 if ( boost::filesystem::is_regular(acqp) )
1151 acqpFound= true; // user passes a 'serie', not a 'study'
1153 acqpFound = false; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
1156 if (subjectFound ) type = 1; // user passed a 'study
1157 else if (acqpFound) type = 2; // user passed a 'serie'
1158 else type = 3; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
1163 // ==========================================================================================================
1165 bool Bruker2Dicom::CreateDirectory(const std::string &OutputDirName)
1167 std::string systemCommand;
1170 std::cout << "Check for output directory :[" << OutputDirName << "]."
1172 boost::filesystem::directory_entry ent(OutputDirName);
1173 if ( ! boost::filesystem::is_directory(ent) ) // dirout not found
1175 systemCommand = "mkdir " + OutputDirName; // create it!
1177 std::cout << systemCommand << std::endl;
1178 system (systemCommand.c_str());
1179 // if ( ! boost::filesystem::is_directory(OutputDirName) ) // be sure it worked
1182 // std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
1184 //throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
1189 // std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
1195 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
1200 // ===========================================================================================
1202 /// \TODO move cleanString to 'crea' ?
1204 void Bruker2Dicom::cleanString(std::string &s)
1207 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
1212 if (s[l-1] == ' ' ) // blank space
1220 std::string repChar("_");
1221 ////boost::algorithm::replace_all(
1222 //GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
1225 // ===========================================================================================
1227 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
1229 if(bDPT.GetDataType() == "string")
1231 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1233 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1234 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1236 if (brukerDataPixelType == "ip_short") {
1237 mhdDataPixelType = "MET_USHORT";
1240 if (brukerDataPixelType == "ip_int") {
1241 mhdDataPixelType = "MET_UINT";
1244 if (brukerDataPixelType == "ip_char") {
1245 mhdDataPixelType = "MET_UCHAR";
1248 /// \TODO : finish the list
1250 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1252 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1254 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1256 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1258 case 4 : fp << "ElementType = MET_INT" << std::endl;
1260 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1262 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1264 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1269 int brukerDataPixelType = bDPT.GetIntValue()[0];
1271 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1272 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1274 // Cross your fingers !!!
1276 // pb : found values : 2, 3, 5
1278 if (brukerDataPixelType == 2) {
1279 mhdDataPixelType = "MET_USHORT";
1282 if (brukerDataPixelType == 3) {
1283 mhdDataPixelType = "MET_USHORT";
1286 if (brukerDataPixelType == 1) {
1287 mhdDataPixelType = "MET_UCHAR";
1293 // ===========================================================================================
1295 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1297 std::vector<BrukerImage> imageSet;
1298 br_acqp.SetLoopStructure();
1299 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1300 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1302 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1304 if (result == false)
1306 throw ( BrukerInitException ("ObjectVaryingProperties.init() failure in Bruker2Dicom::CreateImageSet()") );
1309 br_acqp.SetImageLoopStructure();
1310 br_acqp.SetBrukerImageList();
1311 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1313 BrukerImage image(br_acqp,br_reco);
1314 image.Init(br_acqp,br_reco,1);
1316 for(int i=0;i<brukerImageList.size();i++)
1318 image.Init(br_acqp,br_reco,i);
1319 imageSet.push_back(image);
1322 // Just for checking
1325 std::vector<std::vector <double> > imageOrientation;
1326 std::vector <double> imagePosition;
1327 for(int i=0;i<brukerImageList.size();i++)
1329 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1331 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1332 std::cout << "Position " << imagePosition[0] << " "
1333 << imagePosition[1] << " " << imagePosition[2] ;
1334 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1335 std::cout << "\t Orientation " ;
1336 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1337 std::cout << imageOrientation[i1][i2] << " ";CreateImageSet
1339 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1340 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1342 std::cout << "\t [";
1343 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1344 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1345 std::cout << "]" << std::endl;
1351 // ===========================================================================================
1353 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1358 double spacingX, double spacingY, double sliceDistance,
1359 std::string dcmImageName,
1360 const std::string &patientName,
1362 std::string &studyUID,
1363 std::string &serieUID,
1364 std::string &studyDescr,
1365 std::string &serieDescr,
1366 std::string &strStudyTimeDate,
1371 std::ostringstream str;
1378 // Set the image size
1381 gdcm::Attribute<0x0028,0x0011, gdcm::VR::US> at;
1383 ds.Replace(at.GetAsDataElement());
1387 gdcm::Attribute<0x0028,0x0010, gdcm::VR::US> at;
1389 ds.Replace(at.GetAsDataElement());
1392 //file.SetDataSet( createDataSet(str.str(),0x0028,0x0011,"US"));
1393 // Set the image size
1397 // file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1400 // file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1404 gdcm::Attribute<0x0028,0x0008, gdcm::VR::IS> at;
1405 at.SetValue(nbFrames);
1406 ds.Replace(at.GetAsDataElement());
1407 // file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1410 // Set the pixel type
1413 gdcm::Attribute<0x0028,0x0100, gdcm::VR::US> at;
1414 at.SetValue(pixelSize*8);
1415 ds.Replace(at.GetAsDataElement());
1419 gdcm::Attribute<0x0028,0x0101, gdcm::VR::US> at;
1420 at.SetValue(pixelSize*8);
1421 ds.Replace(at.GetAsDataElement());
1425 gdcm::Attribute<0x0028,0x0102, gdcm::VR::US> at;
1426 at.SetValue(pixelSize*8 -1);
1427 ds.Replace(at.GetAsDataElement());
1430 // pixel representation
1432 gdcm::Attribute<0x0028,0x0103, gdcm::VR::US> at;
1434 ds.Replace(at.GetAsDataElement());
1438 gdcm::Attribute<0x0028,0x0002, gdcm::VR::US> at;
1440 ds.Replace(at.GetAsDataElement());
1446 sprintf(temp,"%d%s%d",spacingX, "\\", spacingY);
1449 gdcm::Attribute<0x0028,0x0030, gdcm::VR::DS> at;
1451 ds.Replace(at.GetAsDataElement());
1455 gdcm::Attribute<0x0018,0x0050, gdcm::VR::DS> at;
1457 at.SetValue(sliceDistance);
1458 ds.Replace(at.GetAsDataElement());
1462 gdcm::Attribute<0x0020,0x0011, gdcm::VR::IS> at;
1463 at.SetValue(serieNumber);
1464 ds.Replace(at.GetAsDataElement());
1468 gdcm::Attribute<0x0020,0x0013,gdcm::VR::IS> at;
1469 at.SetValue(++instanceNumber);
1470 ds.Replace(at.GetAsDataElement());
1473 // [Media Storage SOP Class UID]
1477 str << "1.2.840.10008.5.1.4.1.1.4";
1478 gdcm::Tag tag(0x0002, 0x0002);
1481 ds.Replace(at.GetAsDataElement());*/
1485 /* gdcm::Attribute<0x0008, 0x0162gdcm::VR::UI> at;
1486 at.SetValue("1.2.840.10008.5.1.4.1.1.4");
1487 ds.Replace(at.GetAsDataElement());*/
1491 gdcm::Attribute<0x0010,0x0010, gdcm::VR::PN> at;
1492 at.SetValue(patientName);
1493 ds.Replace(at.GetAsDataElement());
1497 gdcm::Attribute<0x0010,0x0020, gdcm::VR::LO> at;
1498 at.SetValue(patientName);
1499 ds.Replace(at.GetAsDataElement());
1503 gdcm::Attribute< 0x0020, 0x000d, gdcm::VR::UI> at;
1504 at.SetValue(studyUID);
1505 ds.Replace(at.GetAsDataElement());
1509 gdcm::Attribute< 0x0020, 0x000e, gdcm::VR::UI> at;
1510 at.SetValue(serieUID);
1511 ds.Replace(at.GetAsDataElement());
1514 gdcm::Attribute< 0x0008,0x0020, gdcm::VR::DA> at;
1515 at.SetValue(strStudyTimeDate.substr(10,11).c_str());
1516 ds.Replace(at.GetAsDataElement());
1519 gdcm::Attribute< 0x0008,0x0030, gdcm::VR::TM> at;
1520 at.SetValue(strStudyTimeDate.substr(1,8).c_str());
1521 ds.Replace(at.GetAsDataElement());
1523 // Study Description
1525 gdcm::Attribute< 0x0008,0x1030, gdcm::VR::LO> at;
1526 at.SetValue(studyDescr);
1527 ds.Replace(at.GetAsDataElement());
1529 // Series Description
1531 gdcm::Attribute< 0x0008,0x103e, gdcm::VR::LO> at;
1532 at.SetValue(serieDescr);
1533 ds.Replace(at.GetAsDataElement());
1537 gdcm::Attribute< 0x0008,0x0060, gdcm::VR::CS> at;
1539 ds.Replace(at.GetAsDataElement());
1543 // //8, 16, 32, 64 (for double ?)
1546 // file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1548 // file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1551 str << pixelSize*8-1;
1552 // file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1554 // Set the pixel representation // 0/1 , 0=unsigned
1555 // file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1557 // Set the samples per pixel // 1:Grey level, 3:RGB
1558 // file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1560 // 0028 0030 DS 2 Pixel Spacing
1562 str << spacingX << "\\" << spacingY;
1563 // file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1565 // 0018 0050 DS 1 Slice Thickness
1567 str << sliceDistance;
1568 // file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1570 // 0020 0011 IS 1 Series Number
1573 // file->InsertEntryString(str.str(),0x0020,0x0011, "IS");
1575 // 0020|0013 [IS] [Instance Number]
1578 str << instanceNumber;
1579 // file->InsertEntryString(str.str(),0x0020,0x0013, "IS");
1581 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1582 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1583 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1585 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1587 //// 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1588 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1589 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1591 // // if (strlen(patientName) != 0)
1592 // file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1593 // file->InsertEntryString(patientName.c_str(),0x0010,0x0020, "LO"); // Patient's ID
1595 // file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1596 // file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1598 //// 0008 0020 DA 1 Study Date
1599 //// 0008 0030 TM 1 Study Time
1601 ///// \TODO split into 2 strings!
1602 // file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1603 // file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(), 0x0008,0x0030, "TM");
1605 // file->InsertEntryString(studyDescr, 0x0008,0x1030, "LO"); // Study Description
1606 // file->InsertEntryString(serieDescr, 0x0008,0x103e, "LO"); // Series Description
1608 ////0008|0060 [CS] [Modality]
1609 // file->InsertEntryString("MR",0x0008,0x0060, "CS");
1611 // 0020 0037 DS 6 Image Orientation (Patient)
1612 char charImageOrientation[256];
1615 std::cout << "charImageOrientation " <<
1616 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1617 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1618 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1619 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1620 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1621 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1625 gdcm::Attribute< 0x0020,0x0037, gdcm::VR::DS> at;
1626 at.SetValue( imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0], 0);
1627 at.SetValue( imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1], 1);
1628 at.SetValue( imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2], 2);
1629 at.SetValue( imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0], 3);
1630 at.SetValue( imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1], 4);
1631 at.SetValue( imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2], 4);
1632 ds.Replace(at.GetAsDataElement());
1634 // file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1636 char charImagePosition[256];
1637 sprintf(charImagePosition,"%f\\%f\\%f",
1638 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1639 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1640 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1642 //file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1644 // 0020 0032 DS 3 Image Position (Patient)
1646 gdcm::Attribute< 0x0020,0x0032,gdcm::VR::DS> at;
1647 at.SetValue( imageSet[imgNum].getTranslationVectorRPS2XYZ()[0], 0);
1648 at.SetValue( imageSet[imgNum].getTranslationVectorRPS2XYZ()[1], 1);
1649 at.SetValue(imageSet[imgNum].getTranslationVectorRPS2XYZ()[2], 2);
1650 ds.Replace(at.GetAsDataElement());
1657 0029|0010 [LO] [Private Creator] [CREATIS HEADER ]
1658 0029|0011 [LO] [Private Creator] [CREATIS for BRUKER to DICOM]
1659 0029|1031 [LO] [] [4.0.7571167 ]
1660 0029|1032 [UL] [] [1440000] =0x(15f900)
1662 > $$ Thu Jul 30 17:29:46 2009 CEST (UT+2h) mwiart
1663 > $$ /opt/PV4.0/data/mwiart/nmr/dha80.Uw1/4/method
1665 > ##$PVM_DwNDiffDir=3 % 3 directions
1666 > ##$PVM_DwNDiffExpEach=2 % 2 valeurs de b par direction
1667 > ##$PVM_DwAoImages=1 % 1 image pour b=0
1668 > ##$PVM_DwBvalEach=( 2 ) % Valeurs de b
1669 > 1500.000000 3000.000000
1671 > $$ Wed Dec 2 09:56:01 2009 UTC (UT+0h) mwiart
1672 > $$ /opt/Pv5.0/data/mwiart/nmr/ECI7.Wx1/4/method
1674 > ##$PVM_DwNDiffDir=3 % 3 directions
1675 > ##$PVM_DwNDiffExpEach=2 % 2 valeurs de b par direction
1676 > ##$PVM_DwAoImages=1 % 1 image pour b=0
1677 > ##$PVM_DwBvalEach=( 2 ) % Valeurs de b
1680 > $$ Wed Dec 2 10:14:27 2009 UTC (UT+0h) mwiart
1681 > $$ /opt/Pv5.0/data/mwiart/nmr/ECI7.Wx1/5/method
1682 > ##$Method=DtiStandard
1683 > ##$PVM_DwNDiffDir=1 % 1 direction
1684 > ##$PVM_DwNDiffExpEach=2 % 2 valeurs de b
1685 > ##$PVM_DwAoImages=0 % 0 image pour b=0
1686 > ##$PVM_DwBvalEach=( 2 ) % Valeurs de b
1691 // gdcm::Attribute< 0x0029,0x0010, gdcm::VR::LO> at;
1692 // at.SetValue(.SetValue( "CREATIS HEADER");
1693 // ds.Replace(at.GetAsDataElement());
1696 // gdcm::Attribute< 0x0029,0x0011, gdcm::VR::LO> at;
1697 // // at.SetValue( "CREATIS FOR BRUKER-TO-DICOM");
1698 // ds.Replace(at.GetAsDataElement());
1701 // file->InsertEntryString("CREATIS HEADER", 0x0029,0x0010, "LO");
1702 //file->InsertEntryString("CREATIS FOR BRUKER-TO-DICOM", 0x0029,0x0011, "LO");
1704 BrukerFieldData brf_method = br_method.GetFieldData("Method");
1705 const std::vector<std::string> method = brf_method.GetStringValue();
1707 // std::string method = br_method.GetFieldData("Method").GetStringValue();
1709 // gdcm::Attribute< 0x0029,0x0100, gdcm::VR::LO> at;
1710 // at.SetValue(method[0]);
1711 // ds.Replace(at.GetAsDataElement());
1713 //file->InsertEntryString(method[0], 0x0029,0x0100, "LO");
1715 std::cout << " method[" << method[0] << "]" << std::endl;
1716 if (method[0] == "DtiEpi" || method[0] == "DtiStandard")
1718 // const std::vector<int> NDiffDir = br_method.GetFieldData("PVM_DwNDiffDir").GetIntValue();
1719 BrukerFieldData brf_NDiffDir = br_method.GetFieldData("PVM_DwNDiffDir");
1720 const std::vector<int> NDiffDir = brf_NDiffDir.GetIntValue();
1722 //const std::vector<int> NDiffExpEach = br_method.GetFieldData("PVM_DwNDiffExpEach").GetIntValue();
1723 BrukerFieldData brf_NDiffExpEach = br_method.GetFieldData("PVM_DwNDiffExpEach");
1724 const std::vector<int> NDiffExpEach =brf_NDiffExpEach.GetIntValue();
1726 // const std::vector<int> AoImages = br_method.GetFieldData("PVM_DwAoImages").GetIntValue();
1727 BrukerFieldData brf_AoImages = br_method.GetFieldData("PVM_DwAoImages");
1728 const std::vector<int> AoImages = brf_AoImages.GetIntValue();
1730 BrukerFieldData brf_DwBvalEach = br_method.GetFieldData("PVM_DwBvalEach");
1731 const std::vector<double> DwBvalEach = brf_DwBvalEach.GetDoubleValue();
1733 std::cout << "nb directions : " << NDiffDir[0] << " nb valeurs de b par direction : " << NDiffExpEach[0]
1734 << " nb images for b=0 : " << AoImages[0] << std::endl;
1739 gdcm::Attribute< 0x0029,0x0101, gdcm::VR::US> at;
1740 at.SetValue( NDiffDir[0]);
1741 ds.Replace(at.GetAsDataElement());
1743 // file->InsertEntryString(str.str(),0x0029,0x0101,"US"); // directions
1745 str << NDiffExpEach[0];
1747 gdcm::Attribute< 0x0029,0x0102, gdcm::VR::US> at;
1748 at.SetValue( NDiffExpEach[0]);
1749 ds.Replace(at.GetAsDataElement());
1751 // file->InsertEntryString(str.str(),0x0029,0x0102,"US"); // valeurs de b par direction
1755 gdcm::Attribute< 0x0029,0x0103, gdcm::VR::US> at;
1756 at.SetValue( AoImages[0]);
1757 ds.Replace(at.GetAsDataElement());
1759 // file->InsertEntryString(str.str(),0x0029,0x0103,"US"); // image pour b=0
1762 gdcm::Attribute< 0x0029,0x0104,gdcm::VR::DS> at;
1763 for (unsigned int i=0; i<NDiffExpEach[0]; i++)
1764 at.SetValue( DwBvalEach[i], i);
1765 ds.Replace(at.GetAsDataElement());
1767 // file->InsertEntryString(str.str(),0x0029,0x0104,"DS"); // Valeurs de b
1769 file.SetDataSet(ds);
1771 // 0020 0x1041 DS 1 Slice Location
1772 // sprintf(charImagePosition,"%f",float(imgNum));
1773 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1775 // Set Rescale Intercept
1778 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1780 // Set Rescale Slope
1783 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1785 // dcmImageName = "c:\\toto.dcm";
1786 boost::algorithm::replace_all( dcmImageName, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
1788 //FILE *fil=fopen(dcmImageName.c_str(), "a");
1791 gdcm::ImageWriter writer;
1792 writer.SetFileName(dcmImageName.c_str());
1793 writer.SetFile(file);
1794 gdcm::DataElement pixeldata( gdcm::Tag(0x7fe0,0x0010) );
1795 pixeldata.SetByteValue( (char*)tabPixels ,X*Y*nbFrames*pixelSize );
1797 im.SetDataElement(pixeldata);
1798 writer.SetImage(im);
1800 // GDCM_NAME_SPACE::FileHelper *fileH;
1801 // fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1802 // fileH->SetContentType(contentType);
1804 // // cast is just to avoid warnings (*no* conversion is performed)
1805 // //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1807 ////std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1809 // fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1810 // fileH->SetWriteModeToRaw();
1811 // fileH->SetWriteTypeToDcmExplVR();
1812 // if( !fileH->Write(dcmImageName)) {
1813 // std::cout << "Failed for [" << dcmImageName << "]\n"
1814 // << " File is unwrittable" << std::endl;
1817 // throw ( BrukerHopelessException ("Level 1 Unable to write Dicom file "));
1820 // // file->Print();
1826 //////////////////////////////////////////////////////////////////////
1827 // Generic repertory creation //
1828 //////////////////////////////////////////////////////////////////////
1829 const std::string Bruker2Dicom::createDirectory(const std::string &i_name, const std::string &i_dir)
1832 // creation directory : 'nom du patient'
1833 std::string tempString(i_dir);
1834 tempString = tempString + VALID_FILE_SEPARATOR + i_name;
1835 boost::algorithm::replace_all( tempString, INVALID_FILE_SEPARATOR , VALID_FILE_SEPARATOR);
1836 if (!CreateDirectory(tempString))
1838 std::cout << "[" << tempString << "] Directory creation failure " << std::endl;
1839 throw ( BrukerHopelessException ("Patient directory creation failure "));
1841 return tempString.c_str();
1844 std::string Bruker2Dicom::findFile(const std::string &i_dir, const std::string i_name)
1848 boost::filesystem::directory_iterator it(i_dir), it_end;
1849 for(; it != it_end; ++it)
1851 if((*it).leaf() == i_name)
1853 file = (*it).string();
1861 boost::filesystem::path pth (i_dir);
1862 boost::filesystem::directory_iterator itless(pth.branch_path()), it_end;
1863 for(; itless != it_end; ++itless)
1865 if((*itless).leaf() == i_name)
1867 file = (*itless).string();
1873 return file.c_str();
1877 //const gdcm::DataSet Bruker2Dicom::createDataSet(const std::string &i_val,
1878 // const uint16_t &i_gr, const uint16_t &i_el,
1879 // const gdcm::VR &i_vr)
1882 // gdcm::Attribute<i_gr, i_el> at;
1883 // at.SetValue(i_val.c_str());
1886 // gdcm::DataElement de;
1889 // de.SetTag( gdcm::Tag(i_gr, i_el) );
1891 // gdcm::DataSet ds;
1893 // ds.Replace(at.GetAsDataElement());