1 #include "bruker2dicom.h"
2 #include <boost/filesystem/path.hpp>
3 #include <boost/filesystem/operations.hpp>
4 #include "brukerexception.h"
6 #ifndef PATH_MAX // If not defined yet : do it
10 bool Bruker2Dicom::Execute()
12 // ----- Check input directory name -----
14 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
16 //if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(InputDirName) )
17 if ( ! boost::filesystem::is_directory(InputDirName) )
19 std::cout << "KO : [" << InputDirName << "] is not a Directory." << std::endl;
25 std::cout << "OK : [" << InputDirName << "] is a Directory." << std::endl;
28 // ----- Check output directory name -----
30 std::string strDirNameOut(OutputDirName);
31 bool res=CreateDirectory(strDirNameOut);
33 std::cout << "[" << OutputDirName << "] Directory creation failure " << std::endl;
35 throw ( BrukerHopelessException ("Output directory creation failure "));
39 // e.g : at level 0, in : B67d1.Bp1
41 // 1 2 3 4 5 6 AdjStatePerStudy subject
44 std::string strDirNamein(InputDirName);
47 GDCM_NAME_SPACE::DirList dirList(strDirNamein, false, true); // DON'T get recursively the list of files
49 GDCM_NAME_SPACE::DirListType fileNames;
50 fileNames = dirList.GetFilenames();
52 std::string path = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()));
54 std::string subject = path +
55 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
58 std::string acqp = path +
59 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
62 if ( boost::filesystem::is_regular(subject) )
65 subjectFound = false; // user passed only a 'serie', not a 'study
67 if ( boost::filesystem::is_regular(acqp) )
70 acqpFound = false; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
73 if (subjectFound ) type = 1; // user passed a 'study
74 else if (acqpFound) type = 2; // user passed a 'serie'
75 else type = 3; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
78 // 1 : if subjectFound : user passed a Single Study Directory
79 // 2 : if NOT subjectFound and acqpFound : user passed a Serie Directory
80 // 3 : if NOT subjectFound and NOT acqpFound : user passed a 'non Study Directory' (Hope it's a set of Single Study Directories)
82 //int type = CheckUserDirectory(InputDirName);
87 bool canOpen = br_subject.LoadFile(subject);
90 std::cout << "Hopeless! 'subject' found / cannot be open" << std::endl;
91 throw ( BrukerHopelessException ("Hopeless! 'subject' found in root input directory / cannot be open"));
97 // get info for 'Study Description'
98 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
99 subject_name = b_name.GetStringValue()[0];
100 strPatientName = subject_name;
101 cleanString(subject_name);
102 DealWithSingleStudyDirectory (fileNames);
108 subject_name = "defaultPatName";
109 strPatientName = subject_name;
110 DealWithSingleStudyDirectory (fileNames);
115 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;
116 DealWithMultiStudyDirectory (fileNames);
122 // ----------------------------------------------------------------------------------------------------------
124 void Bruker2Dicom::DealWithMultiStudyDirectory (GDCM_NAME_SPACE::DirListType &dirNames)
126 GDCM_NAME_SPACE::DirListType::iterator it;
128 for (it = dirNames.begin();
129 it != dirNames.end();
132 if ( boost::filesystem::is_directory(*it) )
134 subjectFound = false;
136 std::cout << "in 'DealWithMultiStudyDirectory' [" << *it << "] is a directory" << std::endl;
138 GDCM_NAME_SPACE::DirList dirList(*it, false, true); // DON'T get recursively the list of files
139 GDCM_NAME_SPACE::DirListType fileNames;
140 fileNames = dirList.GetFilenames();
142 std::string path = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()));
143 std::string subject = path +
144 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
146 if (! boost::filesystem::is_regular(subject) )
148 std::cout << "no [" << subject << "] file found" << std::endl;
152 bool canOpen = br_subject.LoadFile(subject);
155 std::cout << "Hopeless! 'subject' found / cannot be open" << std::endl;
156 throw ( BrukerHopelessException ("Hopeless! 'subject' found in root input directory / cannot be open"));
160 br_subject.FillMap();
161 subjectFound = true; // Hope so! (full checking needed!)
163 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
164 subject_name = b_name.GetStringValue()[0];
165 strPatientName = subject_name;
166 cleanString(subject_name);
167 DealWithSingleStudyDirectory(fileNames);
173 std::cout << "[" << *it << "] is NOT a directory; skipped!" << std::endl;
178 // ----------------------------------------------------------------------------------------------------------
180 void Bruker2Dicom::DealWithSingleStudyDirectory (GDCM_NAME_SPACE::DirListType &fileNames)
183 // creation directory : 'nom du patient'
184 std::string tempStringPatDir(OutputDirName);
185 tempStringPatDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + subject_name;
187 res=CreateDirectory(tempStringPatDir);
189 std::cout << "[" << tempStringPatDir << "] Directory creation failure " << std::endl;
190 throw ( BrukerHopelessException ("Patient directory creation failure "));
195 BrukerFieldData b_entry=br_subject.GetFieldData("SUBJECT_entry");
196 subject_entry = b_entry.GetStringValue()[0];
197 //cleanString(subject_entry);
198 subject_entry = subject_entry.substr(11, subject_entry.size()-11);
200 BrukerFieldData b_position=br_subject.GetFieldData("SUBJECT_position");
201 subject_position = b_position.GetStringValue()[0];
202 //cleanString(subject_position);
203 subject_position = subject_position.substr(9, subject_position.size()-9);
205 BrukerFieldData b_date=br_subject.GetFieldData("SUBJECT_date");
206 subject_date = b_date.GetStringValue()[0];
207 strStudyTimeDate = subject_date;
208 cleanString(subject_date);
210 BrukerFieldData b_study_name=br_subject.GetFieldData("SUBJECT_study_name");
211 subject_study_name = b_study_name.GetStringValue()[0];
212 subject_study_name = subject_study_name.substr(1, subject_study_name.size()-2);
213 cleanString(subject_study_name);
215 else // Desperate trick when file 'subject' is missing
217 subject_entry = "HeadFirst"; // Why not?
218 subject_position = "Supine"; // Why not?
219 strStudyTimeDate = "06_06_06_6_June_1666"; // Why not?
220 subject_date = "6_June_1666"; // Why not?
221 subject_study_name = "defStudyName"; // Why not?
223 // subject_name is already in 'Patient Name'
224 if (0) // We don't realy need so much information for 'Study'
225 strStudyDescr = /*subject_name + "." + */ subject_study_name + "." + subject_entry + "." + subject_position + "." + subject_date;
227 strStudyDescr = /*subject_name + "." + */ subject_study_name /*+ "." + subject_entry + "." + subject_position + "." + subject_date*/;
229 // creation directory : 'nom de la Study'
231 std::string tempStringStudyDir(OutputDirName);
232 tempStringStudyDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + strStudyDescr;
234 res=CreateDirectory(tempStringStudyDir);
236 std::cout << "[" << tempStringStudyDir << "] Directory creation failure " << std::endl;
237 throw ( BrukerHopelessException ("Study directory creation failure "));
240 char outputDirName[(unsigned int) PATH_MAX+2];
242 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
246 // -----------------------------------------------------
247 // Iterate to ALL the objets(files/directories) found in the input directory
248 // (this is level ZERO)
249 // each Directory (name : 1, 2, 3, ...) will be a Dicom Serie
250 // -----------------------------------------------------
252 /// \TODO better use directory iterator, from boost! // JPR
253 GDCM_NAME_SPACE::DirListType::iterator it;
255 for (it = fileNames.begin();
256 it != fileNames.end();
259 if ( boost::filesystem::is_directory(*it) )
262 std::cout << "[" << *it << "] is a directory" << std::endl;
264 //if((*it) == "AdjResult") // avoid trouble with new version 'AdjResult' directory
265 if(GDCM_NAME_SPACE::Util::GetName(*it) == "AdjResult")
268 //BrukerDataSet br_acqp;
271 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
274 br_acqp.LoadFile(strAcqp);
277 //std::cout << "out of br_acqp.FillMap " << std::endl;
278 std::string acqp_scan_name;
279 std::string acqp_method;
280 std::string acqp_protocol_location;
282 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
283 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
284 cleanString(acqp_protocol_location);
286 //std::cout << "out of br_acqp.FillMap 1" << std::endl;
287 BrukerFieldData b_scan_name=br_acqp.GetFieldData("ACQ_scan_name");
288 //std::cout << "out of br_acqp.FillMap 11" << std::endl;
289 acqp_scan_name = b_scan_name.GetStringValue()[0];
290 //std::cout << "out of br_acqp.FillMap 12 [" << acqp_scan_name << "]" << std::endl;
291 cleanString(acqp_scan_name);
292 //std::cout << "out of br_acqp.FillMap 2" << std::endl;
293 BrukerFieldData b_method=br_acqp.GetFieldData("ACQ_method");
294 b_method.PrintSelf();
295 acqp_method = b_method.GetStringValue()[0];
297 cleanString(acqp_method);
298 //std::cout << "out of br_acqp.FillMap 3" << std::endl;
299 BrukerFieldData b_list_size = br_acqp.GetFieldData("ACQ_O1_list_size");
300 //std::cout << "out of br_acqp.FillMap 4" << std::endl;
301 //b_list_size.PrintSelf(); //JP
303 nbSlices = b_list_size.GetIntValue()[0];
304 //std::cout << "out of br_acqp.FillMap 5" << std::endl;
305 strSerieDescr = GDCM_NAME_SPACE::Util::GetName(*it)
306 /* + "." + acqp_protocol_location */ // always the same (in each acquisition)
307 + "." + acqp_scan_name
308 + "." + acqp_method.c_str();
310 sprintf(outputDirName, "%s%c%s", tempStringStudyDir.c_str(), //OutputDirName.c_str(),
311 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
312 strSerieDescr.c_str() );
314 std::cout << " ================================================================================\n"
315 << " === [" << GDCM_NAME_SPACE::Util::GetName(*it) << "] -> [" << strSerieDescr << "]\n"
316 << " ================================================================================"
318 std::string strOutputDirName(outputDirName);
320 printf ("outputDirName [%s]\n", outputDirName);
322 DealWithNiveau1(*it, strOutputDirName);
324 catch (BrukerHopelessException &e)
326 std::cout << "And Exception was thrown in DealWithNiveau1 (" << e.what() << ") " << std::endl;
330 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
333 // =====================================================================
335 void Bruker2Dicom::DealWithNiveau1(std::string &level1Directory, std::string ¤tOutputDirName) {
337 // e.g. : at level 1, in B67d1.Bp1/6
339 // acqp fid imnd pdata pulseprogram spnam0 spnam1
341 bool res = CreateDirectory(currentOutputDirName);
344 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
345 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
348 GDCM_NAME_SPACE::DirList dirList(level1Directory, false, true); // DON'T get recursively the list of files
349 GDCM_NAME_SPACE::DirListType fileNames;
350 fileNames = dirList.GetFilenames();
351 // -----------------------------------------------------
352 // Iterate to ALL the objets(files/directories) found in the input directory
353 // -----------------------------------------------------
354 GDCM_NAME_SPACE::DirListType::iterator it;
356 for (it = fileNames.begin();
357 it != fileNames.end();
360 if ( boost::filesystem::is_regular(*it) )
361 //if ( ! boost::filesystem::is_directory(*it) )
364 std::cout << "--- [" << *it << "] is a file." << std::endl;
368 char outputDirName[(unsigned int) PATH_MAX+2];
369 //std::string firstName;
371 for (it = fileNames.begin();
372 it != fileNames.end();
375 if ( boost::filesystem::is_directory(*it) )
377 // will be always "pdata" ...
379 std::cout << "--- [" << *it << "] is a directory" << std::endl;
382 if (FileLine.startsWith("##$ACQ_size=")) {
383 if (FileLine.startsWith("##$NI=")) {
384 if (FileLine.startsWith("##$NR=")) {
385 if (FileLine.startsWith("##$ACQ_obj_order=")) {
386 if (FileLine.startsWith("##$ACQ_word_size=")) {
387 if (FileLine.startsWith("##$BYTORDA=")) {
388 if (FileLine.startsWith("##$PULPROG=")) {
391 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
392 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
393 GDCM_NAME_SPACE::Util::GetName(*it).c_str());
394 std::string strOutputDirName(outputDirName);
397 std::string strMethod;
398 //std::string firstName = *(fileNames.begin());
400 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin())) +
401 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
403 // std::cout << "---strMethod (for method)=> [" << strMethod << "]" << std::endl;
404 canOpen = br_method.LoadFile(strMethod);
407 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
408 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
410 //std::cout << "---strMethod (for imnd) => [" << strMethod << "]" << std::endl;
411 canOpen = br_method.LoadFile(strMethod);
414 std::cout << "Hopeless! neither 'method' nor 'imnd' found in ["
415 << level1Directory << "]; we skip it!" << std::endl;
417 //throw ( BrukerHopelessException ("Hopeless! neither 'method' nor 'imnd' found "));
418 //exit(0); /// \TODO throw an exception !
422 std::cout << "open => [" << strMethod << "] successfully" << std::endl;
426 ##$PVM_Fov (dimension) // ou plutot RECO_fov !
429 dans method (pour perfusion seulement?) :
430 ##$PVM_ObjOrderList=( 8 )
433 ##$PVM_SPackArrNSlices=( 2 )
437 DealWithNiveau2(*it, strOutputDirName);
439 catch (BrukerHopelessException &e)
441 std::cout << "And Exception was thrown in DealWithNiveau2 (" << e.what() << ") " << std::endl;
448 // =====================================================================
450 void Bruker2Dicom::DealWithNiveau2(std::string &level2Directory, std::string ¤tOutputDirName) {
452 // e.g. : at level 2 in B67d1.Bp1/6/pdata
454 // acqp fid imnd pdata pulseprogram spnam0 spnam1
457 bool res = CreateDirectory(currentOutputDirName);
460 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
461 throw ( BrukerHopelessException ("Hopeless! Level2 output directory creation failure"));
465 GDCM_NAME_SPACE::DirList dirList(level2Directory, false, true); // DON'T get recursively the list of files
467 GDCM_NAME_SPACE::DirListType fileNames;
468 fileNames = dirList.GetFilenames();
470 char outputDirName[(unsigned int) PATH_MAX+2];
472 // -----------------------------------------------------
473 // Iterate to ALL the objets(files/directories) found in the input directory
474 // -----------------------------------------------------
475 GDCM_NAME_SPACE::DirListType::iterator it;
479 for (it = fileNames.begin();
480 it != fileNames.end();
483 if ( ! boost::filesystem::is_directory(*it) )
485 std::cout << "--- --- [" << *it << "] is a file.." << std::endl;
489 for (it = fileNames.begin();
490 it != fileNames.end();
493 if ( boost::filesystem::is_directory(*it) )
497 std::cout << "--- --- [" << *it << "] is a directory" << std::endl;
499 // sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
500 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
501 // GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
505 // (interest of previous method :
506 // If unaware user changed the pdata name, it goes on working
510 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
513 std::string str_isa_func_name;
514 canOpen = br_isa.LoadFile(str_isa);
517 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
518 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
519 GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
524 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
526 str_isa_func_name = b_isa_func_name.GetStringValue()[0];
527 cleanString(str_isa_func_name);
529 sprintf(outputDirName, "%s%c%s.%s", currentOutputDirName.c_str(),
530 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
531 GDCM_NAME_SPACE::Util::GetName(*it).c_str(),
532 str_isa_func_name.c_str());
534 std::string strOutputDirName(outputDirName);
536 DealWithNiveau3(*it, strOutputDirName);
538 catch (BrukerHopelessException &e)
540 std::cout << "And Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
541 << " We skip [" << level2Directory << "]" << std::endl;
544 catch (BrukerInitException &e)
546 std::cout << "And Init Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
547 << " We skip [" << level2Directory << "]" << std::endl;
555 // =====================================================================
558 void Bruker2Dicom::DealWithNiveau3(std::string &level3Directory, std::string ¤tOutputDirName){
561 // e.g. at level 3, in
563 // just to be able to go on checking // JP
564 if ( GDCM_NAME_SPACE::Util::GetName(level3Directory) != "1")
567 bool res = CreateDirectory(currentOutputDirName);
571 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
572 throw ( BrukerHopelessException ("Hopeless! Level3 output directory creation failure"));
576 GDCM_NAME_SPACE::DirList dirList(level3Directory, false, true); // DON'T get recursively the list of files
577 GDCM_NAME_SPACE::DirListType::iterator it;
578 GDCM_NAME_SPACE::DirListType fileNames;
579 fileNames = dirList.GetFilenames();
581 char original2dseqName [(unsigned int) PATH_MAX+2];
582 char original2dseqName_XXX [(unsigned int) PATH_MAX+2];
583 char currentOutputMhdDirName [(unsigned int) PATH_MAX+2];
585 char outputMhdFileName [(unsigned int) PATH_MAX+2];
586 char output2dseqSliceFileName[(unsigned int) PATH_MAX+6]; // think about extra '.dcm'
587 char output2dseqName [(unsigned int) PATH_MAX+6];
588 char output2dseqCartoName [(unsigned int) PATH_MAX+6];
590 char copyFile[PATH_MAX + PATH_MAX + 5]; // Should be enough!
593 //-------------- try d3proc;
594 char char_d3proc[(unsigned int) PATH_MAX+2];
596 sprintf(char_d3proc,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"d3proc" );
599 std::cout << "d3proc: --- => [" << char_d3proc << "]" << std::endl;
600 std::string str_d3proc(char_d3proc);
601 canOpen = br_d3proc.LoadFile(str_d3proc);
605 std::cout << "Hopeless! no 'd3proc' found" << std::endl;
606 throw ( BrukerHopelessException ("Hopeless! no 'd3proc' found"));
607 //exit(0); /// \TODO throw an exception !
610 canOpen = br_d3proc.FillMap();
613 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
614 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'd3proc'"));
615 //exit(0); /// \TODO throw an exception !
618 //-------------- end try d3proc;
621 // -------------------try reco
623 char char_reco[(unsigned int) PATH_MAX+2];
625 sprintf(char_reco,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
626 //str_d3proc = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
627 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
630 std::cout << "reco: --- => [" << char_reco << "]" << std::endl;
632 std::string str_reco(char_reco);
633 canOpen = br_reco.LoadFile(str_reco);
635 if (!canOpen) // we try in directory ../1
638 std::cout << "[" << str_reco << "] not found " << std::endl;
639 std::string lastDirName = GDCM_NAME_SPACE::Util::GetPath(level3Directory);
640 //lastDirName = GDCM_NAME_SPACE::Util::GetPath(lastDirName);
641 sprintf(char_reco,"%s%c1%c%s", lastDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
642 //str_reco=char_reco;
643 canOpen = br_reco.LoadFile(str_reco);
646 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
647 throw ( BrukerHopelessException ("Hopeless! cannot find 'reco'"));
648 //exit(0); /// \TODO throw an exception !
652 std::cout << "[" << str_reco << "] successfully Loaded " << std::endl;
655 canOpen = br_reco.FillMap();
658 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
659 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'reco'"));
660 //exit(0); /// \TODO throw an exception !
663 std::cout << "[" << str_reco << "] successfully Mapped" << std::endl;
666 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
667 // br_reco.PrintSelf();
668 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
669 // -------------------end try reco
672 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
673 int NX = bX.GetIntValue()[0];
676 std::cout << "IM_SIX " << NX << std::endl;
677 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
678 int NY = bY.GetIntValue()[0];
681 std::cout << "IM_SIY " << NY << std::endl;
682 /// \todo : check if there are actually 3 dimensions or only 2
684 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
685 int nbFrames = bZ.GetIntValue()[0];
687 std::cout << "IM_SIZ " << nbFrames << std::endl;
689 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
691 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
693 std::string mhdDataPixelType;
695 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
701 // En regle generale il vaut mieux que l'on passe par RECO_*
702 // pour extraire les parametres de l'image
705 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
706 double fovX = fov.GetDoubleValue()[0];
707 double fovY = fov.GetDoubleValue()[1];
709 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
711 BrukerFieldData spatResol = br_method.GetFieldData("PVM_SpatResol");
712 double spatResolX = spatResol.GetDoubleValue()[0];
713 double spatResolY = spatResol.GetDoubleValue()[1];
715 std::cout << "SpatResol (ds method) " << spatResolX << " " << spatResolY << std::endl;
720 // Better we use 'get' accessors from BrukerImage class, as Denis wrote them
722 BrukerFieldData fov = br_reco.GetFieldData("RECO_fov");
723 double fovX = fov.GetDoubleValue()[0];
724 double fovY = fov.GetDoubleValue()[1];
726 std::cout << "FOV (ds reco) " << fovX << " " << fovY << std::endl;
728 BrukerFieldData size = br_reco.GetFieldData("RECO_size");
729 double sizeX = size.GetDoubleValue()[0];
730 double sizeY = size.GetDoubleValue()[1];
733 std::cout << "SIZE (ds reco) " << sizeX << " " << sizeY << std::endl;
735 double spatResolX = fovX / sizeX;
736 double spatResolY = fovY / sizeY;
739 std::cout << "spatResol (ds reco : fov/size) " << spatResolX << " " << spatResolY << std::endl;
743 /// \TODO probabely a more sophisticated accessor will be necessary :
744 /// (cf : non contiguous slices, overlapping, slice thickness, space between slices, etc)
746 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
747 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
750 std::cout << "SPackArrSliceDistance (ds method) " << sliceDistance << std::endl;
752 // ----------------------------------------------------------------------------------------
756 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
757 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "MhdFiles");
759 std::string strCurrentOutputMhdDirName(currentOutputMhdDirName);
760 res = CreateDirectory( strCurrentOutputMhdDirName );
762 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
763 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
768 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
772 std::cout << "nbFrames " << nbFrames << std::endl;
774 std::cout << "nbSlices " << nbSlices << std::endl;
776 int nbInstants = nbFrames/nbSlices;
778 std::cout << "nbInstants (deduced)" << nbInstants << std::endl;
781 FILE *fp; // for MHD files
783 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "2dseq");
786 // \TODO : tenir compte du bazar precedent
788 // load 2dseq in memory
790 fp = fopen(original2dseqName, "rb");
793 // try 2dseq_Angio2D ?!?
795 sprintf( original2dseqName_XXX, "%s%s", original2dseqName, "_Angio2D");
796 fp = fopen(original2dseqName_XXX, "rb");
799 std::cout << "Cannot open [" << original2dseqName << "] nor [" << original2dseqName_XXX << "] for reading" << std::endl;
801 throw ( BrukerHopelessException ("Hopeless! Cannot open '2dseq'"));
806 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
807 ///\ TODO : find a safer way to be sure to read everything!
808 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
810 // This one will be important!
811 // ---------------------------
813 imageSet = CreateImageSet ( );
815 catch (BrukerInitException& e)
818 std::cout << "an Init Exception was thrown in CreateImageSet ( ); " << e.what()
819 << "catched in DealWithNiveau3" << std::endl;
825 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
826 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
829 std::cout << "Single instant : do not split" << std::endl;
832 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
833 GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
834 fp=fopen(outputMhdFileName, "w");
837 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
838 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
843 fprintf(fp, "ObjectType = Image\n");
844 fprintf(fp, "NDims = 3\n" );
845 fprintf(fp, "BinaryData = True \n" );
846 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
847 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
848 fprintf(fp, "HeaderSize = %d\n", 0);
849 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
850 fprintf(fp, "ElementSpacing = %lf %lf %lf\n", spatResolX, spatResolY, sliceDistance );
851 fprintf(fp, "Position = 0 0 %d\n", 0 );
852 fprintf(fp, "Offset = 0 0 0\n" );
853 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
854 fprintf(fp, "ElementNumberOfChannels = 1\n" );
855 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
856 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
859 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
860 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
861 fp=fopen(output2dseqSliceFileName, "wb");
864 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
865 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseq file for writting"));
869 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
873 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
877 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
878 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
880 /* ----------- Write Dicom Image ---------------*/
881 MakeDicomImage(buffer_2dseq,
886 //fovX/NY, fovY/NY, sliceDistance,
887 spatResolX, spatResolY, sliceDistance,
888 output2dseqSliceFileName,
896 0,// index frame number
897 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
900 } // end if nbInstants = 1
902 else // more than ONE instant
905 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
906 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
909 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
913 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
914 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, k );
916 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
917 fp=fopen(outputMhdFileName, "w");
920 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
921 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
926 /* ----------- Write MHD Image ---------------*/
928 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
929 fprintf(fp, "ObjectType = Image\n");
930 fprintf(fp, "NDims = 3\n" );
931 fprintf(fp, "BinaryData = True \n" );
932 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
933 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
934 fprintf(fp, "HeaderSize = %d\n", 0);
935 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); //
936 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, 1.0 ); //slice distance : no meaning for temporal serie
937 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
938 fprintf(fp, "Offset = 0 0 0\n" );
939 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
940 fprintf(fp, "ElementNumberOfChannels = 1\n" );
941 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
942 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb );
946 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
947 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,sliceNb);
948 fp=fopen(output2dseqSliceFileName, "wb");
951 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
952 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseqSliceFile file for writting"));
955 int frameSize = NX*NY*pixelSize;
956 for (instantNb=0; instantNb<nbInstants; instantNb++)
958 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
959 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
965 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
972 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
973 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
974 fp=fopen(output2dseqSliceFileName, "wb");
977 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
980 int frameSize = NX*NY*pixelSize;
981 for (instantNb=0; instantNb<nbInstants; instantNb++)
983 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
984 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
990 fp=fopen(output2dseqSliceFileName, "rb");
993 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
996 fread( pixelsForCurrentSlice,
997 frameSize*nbInstants,
1000 // end of desperate try !
1003 /* ----------- Write Dicom Image ---------------*/
1005 int frameSize = NX*NY*pixelSize;
1006 for (instantNb=0; instantNb<nbInstants; instantNb++)
1008 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
1011 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
1012 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
1013 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb);
1016 pixelsForCurrentSlice,
1021 spatResolX, spatResolY, sliceDistance,
1022 //fovX/NY, fovY/NY, sliceDistance,
1023 output2dseqSliceFileName,
1032 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
1035 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
1041 delete [] pixelsForCurrentSlice;
1042 } // end nbInstants == 1
1043 delete [] buffer_2dseq;
1047 // -----------------------------------------------------
1048 // deal with MatLab-generated Carto file.
1049 // -----------------------------------------------------
1051 dealWithCarto(fileNames, NX, NY, nbSlices, /*fovX, fovY,*/ spatResolX, spatResolY, sliceDistance,
1052 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
1055 // ===========================================================================================
1057 void Bruker2Dicom::dealWithCarto(GDCM_NAME_SPACE::DirListType &fileNames, int NX, int NY, int nbSlices,
1058 /*double fovX, double fovY, */
1059 double spatResolX, double spatResolY, double sliceDistance,
1060 char *copyFile, std::string ¤tOutputDirName,
1061 char *outputMhdFileName, char *output2dseqCartoName)
1063 // -----------------------------------------------------
1064 // deal with MatLab-generated Carto file.
1065 // -----------------------------------------------------
1067 const char *code[] = { "ADC", "adc", "TTP", "ttp", "PEAK", "peak", "" }; // add more carto file name identifiers if necessary; end with ""
1068 const char *separator[] = { "_", ".", "-", "" }; // add more, if necessary, to ckeck for 2dseq.ADC, 2dseq_ADC, 2dseq-ADC, etc; end with ""
1071 GDCM_NAME_SPACE::DirListType::iterator it;
1072 char file_name_ident[500];
1075 // Iterate to ALL the objets(files/directories) found in the input directory
1076 for (it = fileNames.begin();
1077 it != fileNames.end();
1080 if ( boost::filesystem::is_regular(*it) )
1081 //if ( ! boost::filesystem::is_directory(*it) )
1084 std::cout << "--- [" << *it << "] is a file..." << std::endl;
1087 while (code[icode][0] != 0)
1090 while (separator[iseparator][0] != 0)
1092 sprintf(file_name_ident, "2dseq%s%s",separator[iseparator],code[icode]); // e.g "2dseq_ADC"
1094 // std::cout << "check name ["<<(*it) << "] for string [" << file_name_ident << "]" << std::endl;
1095 std::string::size_type loc = (*it).rfind(file_name_ident);
1097 if ( loc != std::string::npos )
1100 ///\ TODO : find a safer way to be sure to read everything!
1101 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
1102 fp = fopen ( (*it).c_str(), "rb");
1104 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
1105 throw ( BrukerHopelessException ("Level 1 Unable to open 'carto' file "));
1107 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
1109 // ?!? sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1110 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
1111 << NX*NY*sizeof(double)*nbSlices << std::endl;
1112 std::string lastFileName = GDCM_NAME_SPACE::Util::GetName((*it).c_str());
1115 // Copy the data file in the new directory
1116 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1117 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str());
1119 sprintf(outputMhdFileName, "%s%c%s%s",
1120 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".mhd" );
1122 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1125 fp=fopen(outputMhdFileName, "w");
1128 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
1132 fprintf(fp, "ObjectType = Image\n");
1133 fprintf(fp, "NDims = 3\n" );
1134 fprintf(fp, "BinaryData = True \n" );
1135 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
1136 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
1137 fprintf(fp, "HeaderSize = %d\n", 0 );
1138 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, sliceDistance );
1139 fprintf(fp, "Position = 0 0 0\n" );
1140 fprintf(fp, "Offset = 0 0 0\n" );
1141 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
1142 fprintf(fp, "ElementNumberOfChannels = 1\n" );
1143 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
1144 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
1149 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1152 // ----------- Write Dicom Image ---------------
1156 sprintf(output2dseqCartoName, "%s%c%s%s",
1157 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".dcm" );
1159 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
1161 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID(); //New SerieUID for each carto.
1162 std::string strNewSerieDescr(strSerieDescr+ "_" +GDCM_NAME_SPACE::Util::GetName((*it).c_str()));
1163 MakeDicomImage(buffer_carto,
1168 //fovX/NY, fovY/NY, sliceDistance,
1169 spatResolX, spatResolY, sliceDistance,
1170 output2dseqCartoName,
1179 GDCM_NAME_SPACE::CREATED_IMAGE
1183 delete [] buffer_carto;
1185 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
1186 break; // don't check for more ident on same file name!
1188 } // end deal with _ADC, -adc, etc
1190 } // end iterate speparators
1192 } // end iterate code
1193 } // end boost::filesystem::is_regular(*it)
1194 } // end iterate on all objects (files, dir, etc)
1197 // ==========================================================================================================
1199 int Bruker2Dicom::CheckUserDirectory(std::string &userDirName)
1201 GDCM_NAME_SPACE::DirList dirList(userDirName, false, true); // DON'T get recursively the list of files
1203 GDCM_NAME_SPACE::DirListType fileNames;
1204 fileNames = dirList.GetFilenames();
1206 std::string path = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()));
1208 std::string subject = path +
1209 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
1212 std::string acqp = path +
1213 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
1216 if ( boost::filesystem::is_regular(subject) )
1217 subjectFound = true; // user passed a 'study
1219 subjectFound = false; // user didnt' pass a 'study
1221 if ( boost::filesystem::is_regular(acqp) )
1222 acqpFound= true; // user passes a 'serie', not a 'study'
1224 acqpFound = false; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
1227 if (subjectFound ) type = 1; // user passed a 'study
1228 else if (acqpFound) type = 2; // user passed a 'serie'
1229 else type = 3; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
1234 // ==========================================================================================================
1236 bool Bruker2Dicom::CreateDirectory(std::string &OutputDirName)
1238 std::string systemCommand;
1241 std::cout << "Check for output directory :[" << OutputDirName << "]."
1243 if ( ! boost::filesystem::is_directory(OutputDirName) ) // dirout not found
1245 systemCommand = "mkdir " + OutputDirName; // create it!
1247 std::cout << systemCommand << std::endl;
1248 system (systemCommand.c_str());
1249 if ( ! boost::filesystem::is_directory(OutputDirName) ) // be sure it worked
1252 std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
1254 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
1259 std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
1265 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
1270 // ===========================================================================================
1272 /// \TODO move cleanString to 'crea' ?
1274 void Bruker2Dicom::cleanString(std::string &s)
1277 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
1282 if (s[l-1] == ' ' ) // blank space
1290 std::string repChar("_");
1291 GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
1294 // ===========================================================================================
1296 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
1298 if(bDPT.GetDataType() == "string")
1300 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1302 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1303 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1305 if (brukerDataPixelType == "ip_short") {
1306 mhdDataPixelType = "MET_USHORT";
1309 if (brukerDataPixelType == "ip_int") {
1310 mhdDataPixelType = "MET_UINT";
1313 if (brukerDataPixelType == "ip_char") {
1314 mhdDataPixelType = "MET_UCHAR";
1317 /// \TODO : finish the list
1319 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1321 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1323 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1325 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1327 case 4 : fp << "ElementType = MET_INT" << std::endl;
1329 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1331 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1333 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1338 int brukerDataPixelType = bDPT.GetIntValue()[0];
1340 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1341 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1343 // Cross your fingers !!!
1345 // pb : found values : 2, 3, 5
1347 if (brukerDataPixelType == 2) {
1348 mhdDataPixelType = "MET_USHORT";
1351 if (brukerDataPixelType == 3) {
1352 mhdDataPixelType = "MET_USHORT";
1355 if (brukerDataPixelType == 1) {
1356 mhdDataPixelType = "MET_UCHAR";
1362 // ===========================================================================================
1364 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1366 std::vector<BrukerImage> imageSet;
1367 br_acqp.SetLoopStructure();
1368 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1369 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1371 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1373 if (result == false)
1375 throw ( BrukerInitException ("ObjectVaryingProperties.init() failure in Bruker2Dicom::CreateImageSet()") );
1378 br_acqp.SetImageLoopStructure();
1379 br_acqp.SetBrukerImageList();
1380 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1382 BrukerImage image(br_acqp,br_reco);
1383 image.Init(br_acqp,br_reco,1);
1385 for(int i=0;i<brukerImageList.size();i++)
1387 image.Init(br_acqp,br_reco,i);
1388 imageSet.push_back(image);
1391 // Just for checking
1394 std::vector<std::vector <double> > imageOrientation;
1395 std::vector <double> imagePosition;
1396 for(int i=0;i<brukerImageList.size();i++)
1398 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1400 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1401 std::cout << "Position " << imagePosition[0] << " "
1402 << imagePosition[1] << " " << imagePosition[2] ;
1403 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1404 std::cout << "\t Orientation " ;
1405 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1406 std::cout << imageOrientation[i1][i2] << " ";CreateImageSet
1408 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1409 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1411 std::cout << "\t [";
1412 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1413 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1414 std::cout << "]" << std::endl;
1420 // ===========================================================================================
1422 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1427 double spacingX, double spacingY, double sliceDistance,
1428 std::string dcmImageName,
1429 const std::string &patientName,
1431 std::string &studyUID,
1432 std::string &serieUID,
1433 std::string &studyDescr,
1434 std::string &serieDescr,
1435 std::string &strStudyTimeDate,
1437 GDCM_NAME_SPACE::ImageContentType contentType
1440 std::ostringstream str;
1442 GDCM_NAME_SPACE::File *file;
1443 file = GDCM_NAME_SPACE::File::New();
1445 // Set the image size
1448 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1451 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1457 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1460 // Set the pixel type
1461 // //8, 16, 32, 64 (for double ?)
1466 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1468 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1471 str << pixelSize*8-1;
1472 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1474 // Set the pixel representation // 0/1 , 0=unsigned
1475 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1477 // Set the samples per pixel // 1:Grey level, 3:RGB
1478 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1480 // 0028 0030 DS 2 Pixel Spacing
1482 str << spacingX << "\\" << spacingY;
1483 file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1485 // 0018 0050 DS 1 Slice Thickness
1487 str << sliceDistance;
1488 file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1490 // 0020 0011 IS 1 Series Number
1493 file->InsertEntryString(str.str(),0x0020,0x0011, "IS");
1495 // 0020|0013 [IS] [Instance Number]
1498 str << instanceNumber;
1499 file->InsertEntryString(str.str(),0x0020,0x0013, "IS");
1501 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1502 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1503 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1505 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1507 // 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1508 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1509 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1511 // if (strlen(patientName) != 0)
1512 file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1513 file->InsertEntryString(patientName.c_str(),0x0010,0x0020, "LO"); // Patient's ID
1515 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1516 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1518 // 0008 0020 DA 1 Study Date
1519 // 0008 0030 TM 1 Study Time
1521 /// \TODO split into 2 strings!
1522 file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1523 file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(), 0x0008,0x0030, "TM");
1525 file->InsertEntryString(studyDescr, 0x0008,0x1030, "LO"); // Study Description
1526 file->InsertEntryString(serieDescr, 0x0008,0x103e, "LO"); // Series Description
1528 //0008|0060 [CS] [Modality]
1529 file->InsertEntryString("MR",0x0008,0x0060, "CS");
1531 // 0020 0037 DS 6 Image Orientation (Patient)
1532 char charImageOrientation[256];
1535 std::cout << "charImageOrientation " <<
1536 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1537 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1538 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1539 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1540 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1541 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1543 sprintf(charImageOrientation,"%f\\%f\\%f \\ %f\\%f\\%f",
1544 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0],
1545 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1],
1546 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2],
1547 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0],
1548 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1],
1549 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] ) ;
1551 file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1553 // 0020 0032 DS 3 Image Position (Patient)
1555 char charImagePosition[256];
1556 sprintf(charImagePosition,"%f\\%f\\%f",
1557 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1558 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1559 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1561 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1566 0029|0010 [LO] [Private Creator] [CREATIS HEADER ]
1567 0029|0011 [LO] [Private Creator] [CREATIS for BRUKER to DICOM]
1568 0029|1031 [LO] [] [4.0.7571167 ]
1569 0029|1032 [UL] [] [1440000] =0x(15f900)
1571 > $$ Thu Jul 30 17:29:46 2009 CEST (UT+2h) mwiart
1572 > $$ /opt/PV4.0/data/mwiart/nmr/dha80.Uw1/4/method
1574 > ##$PVM_DwNDiffDir=3 % 3 directions
1575 > ##$PVM_DwNDiffExpEach=2 % 2 valeurs de b par direction
1576 > ##$PVM_DwAoImages=1 % 1 image pour b=0
1577 > ##$PVM_DwBvalEach=( 2 ) % Valeurs de b
1578 > 1500.000000 3000.000000
1580 > $$ Wed Dec 2 09:56:01 2009 UTC (UT+0h) mwiart
1581 > $$ /opt/Pv5.0/data/mwiart/nmr/ECI7.Wx1/4/method
1583 > ##$PVM_DwNDiffDir=3 % 3 directions
1584 > ##$PVM_DwNDiffExpEach=2 % 2 valeurs de b par direction
1585 > ##$PVM_DwAoImages=1 % 1 image pour b=0
1586 > ##$PVM_DwBvalEach=( 2 ) % Valeurs de b
1589 > $$ Wed Dec 2 10:14:27 2009 UTC (UT+0h) mwiart
1590 > $$ /opt/Pv5.0/data/mwiart/nmr/ECI7.Wx1/5/method
1591 > ##$Method=DtiStandard
1592 > ##$PVM_DwNDiffDir=1 % 1 direction
1593 > ##$PVM_DwNDiffExpEach=2 % 2 valeurs de b
1594 > ##$PVM_DwAoImages=0 % 0 image pour b=0
1595 > ##$PVM_DwBvalEach=( 2 ) % Valeurs de b
1600 file->InsertEntryString("CREATIS HEADER", 0x0029,0x0010, "LO");
1601 file->InsertEntryString("CREATIS FOR BRUKER-TO-DICOM", 0x0029,0x0011, "LO");
1603 BrukerFieldData brf_method = br_method.GetFieldData("Method");
1604 const std::vector<std::string> method = brf_method.GetStringValue();
1606 // std::string method = br_method.GetFieldData("Method").GetStringValue();
1607 file->InsertEntryString(method[0], 0x0029,0x0100, "LO");
1609 std::cout << " method[" << method[0] << "]" << std::endl;
1610 if (method[0] == "DtiEpi" || method[0] == "DtiStandard")
1612 // const std::vector<int> NDiffDir = br_method.GetFieldData("PVM_DwNDiffDir").GetIntValue();
1613 BrukerFieldData brf_NDiffDir = br_method.GetFieldData("PVM_DwNDiffDir");
1614 const std::vector<int> NDiffDir = brf_NDiffDir.GetIntValue();
1616 //const std::vector<int> NDiffExpEach = br_method.GetFieldData("PVM_DwNDiffExpEach").GetIntValue();
1617 BrukerFieldData brf_NDiffExpEach = br_method.GetFieldData("PVM_DwNDiffExpEach");
1618 const std::vector<int> NDiffExpEach =brf_NDiffExpEach.GetIntValue();
1620 // const std::vector<int> AoImages = br_method.GetFieldData("PVM_DwAoImages").GetIntValue();
1621 BrukerFieldData brf_AoImages = br_method.GetFieldData("PVM_DwAoImages");
1622 const std::vector<int> AoImages = brf_AoImages.GetIntValue();
1624 BrukerFieldData brf_DwBvalEach = br_method.GetFieldData("PVM_DwBvalEach");
1625 const std::vector<double> DwBvalEach = brf_DwBvalEach.GetDoubleValue();
1627 std::cout << "nb directions : " << NDiffDir[0] << " nb valeurs de b par direction : " << NDiffExpEach[0]
1628 << " nb images for b=0 : " << AoImages[0] << std::endl;
1632 file->InsertEntryString(str.str(),0x0029,0x0101,"US"); // nb de directions
1634 str << NDiffExpEach[0];
1635 file->InsertEntryString(str.str(),0x0029,0x0102,"US"); // nb valeurs de b par direction
1638 file->InsertEntryString(str.str(),0x0029,0x0103,"US"); // nb images pour b=0
1641 for (unsigned int i=0; i<NDiffExpEach[0]; i++) {
1642 str << DwBvalEach[i];
1646 file->InsertEntryString(str.str(),0x0029,0x0104,"DS"); // Valeurs de b
1649 // 0020 0x1041 DS 1 Slice Location
1650 // sprintf(charImagePosition,"%f",float(imgNum));
1651 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1653 // Set Rescale Intercept
1656 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1658 // Set Rescale Slope
1661 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1664 GDCM_NAME_SPACE::FileHelper *fileH;
1665 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1666 fileH->SetContentType(contentType);
1668 // cast is just to avoid warnings (*no* conversion is performed)
1669 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1671 //std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1673 fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1674 fileH->SetWriteModeToRaw();
1675 fileH->SetWriteTypeToDcmExplVR();
1676 if( !fileH->Write(dcmImageName)) {
1677 std::cout << "Failed for [" << dcmImageName << "]\n"
1678 << " File is unwrittable" << std::endl;
1681 throw ( BrukerHopelessException ("Level 1 Unable to write Dicom file "));