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!
79 // 1 : if subjectFound : user passed a Single Study Directory
80 // 2 : if NOT subjectFound and acqpFound : user passed a Serie Directory
81 // 3 : if NOT subjectFound and NOT acqpFound : user passed a 'non Study Directory' (Hope it's a set of Single Study Directories)
83 // int type = CheckUserDirectory(InputDirName);
89 canOpen =br_subject.LoadFile(subject);
92 std::cout << "Hopeless! 'subject' found / cannot be open" << std::endl;
93 throw ( BrukerHopelessException ("Hopeless! 'subject' found in root input directory / cannot be open"));
99 //br_subject.PrintSelf();
101 // get info for 'Study Description'
103 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
104 subject_name = b_name.GetStringValue()[0];
105 strPatientName = subject_name;
106 cleanString(subject_name);
107 DealWithSingleStudyDirectory (fileNames);
112 subject_name = "defaultPatName";
113 strPatientName = subject_name;
114 DealWithSingleStudyDirectory (fileNames);
119 std::cout << " user passed a 'non study' directory; Hope it's a 'set of studies' directory!, not yet dealt with" << std::endl;
124 void Bruker2Dicom::DealWithSingleStudyDirectory (GDCM_NAME_SPACE::DirListType &fileNames)
127 // creation directory : 'nom du patient'
128 std::string tempStringPatDir(OutputDirName);
129 tempStringPatDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + subject_name;
131 res=CreateDirectory(tempStringPatDir);
133 std::cout << "[" << tempStringPatDir << "] Directory creation failure " << std::endl;
134 throw ( BrukerHopelessException ("Patient directory creation failure "));
139 BrukerFieldData b_entry=br_subject.GetFieldData("SUBJECT_entry");
140 subject_entry = b_entry.GetStringValue()[0];
141 //cleanString(subject_entry);
142 subject_entry = subject_entry.substr(11, subject_entry.size()-11);
144 BrukerFieldData b_position=br_subject.GetFieldData("SUBJECT_position");
145 subject_position = b_position.GetStringValue()[0];
146 //cleanString(subject_position);
147 subject_position = subject_position.substr(9, subject_position.size()-9);
149 BrukerFieldData b_date=br_subject.GetFieldData("SUBJECT_date");
150 subject_date = b_date.GetStringValue()[0];
151 strStudyTimeDate = subject_date;
152 cleanString(subject_date);
154 BrukerFieldData b_study_name=br_subject.GetFieldData("SUBJECT_study_name");
155 subject_study_name = b_study_name.GetStringValue()[0];
156 subject_study_name = subject_study_name.substr(1, subject_study_name.size()-2);
157 cleanString(subject_study_name);
159 else // Desperate trick when file 'subject' is missing
161 subject_entry = "HeadFirst"; // Why not?
162 subject_position = "Supine"; // Why not?
163 strStudyTimeDate = "06_06_06_6_June_1666"; // Why not?
164 subject_date = "6_June_1666"; // Why not?
165 subject_study_name = "defStudyName"; // Why not?
167 // subject_name is already in 'Patient Name'
168 strStudyDescr = /*subject_name + "." + */ subject_study_name + "." + subject_entry + "." + subject_position + "." + subject_date;
170 // creation directory : 'nom de la Study'
172 std::string tempStringStudyDir(OutputDirName);
173 tempStringStudyDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + strStudyDescr;
175 res=CreateDirectory(tempStringStudyDir);
177 std::cout << "[" << tempStringStudyDir << "] Directory creation failure " << std::endl;
178 throw ( BrukerHopelessException ("Study directory creation failure "));
181 char outputDirName[(unsigned int) PATH_MAX+2];
183 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
187 // -----------------------------------------------------
188 // Iterate to ALL the objets(files/directories) found in the input directory
189 // (this is level ZERO)
190 // each Directory (name : 1, 2, 3, ...) will be a Dicom Serie
191 // -----------------------------------------------------
193 GDCM_NAME_SPACE::DirListType::iterator it;
195 for (it = fileNames.begin();
196 it != fileNames.end();
199 if ( boost::filesystem::is_directory(*it) )
202 std::cout << "[" << *it << "] is a directory" << std::endl;
204 if((*it) == "AdjStatePerStudy") // avoid trouble with new version 'AdjStatePerStudy' directory
207 //BrukerDataSet br_acqp;
210 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
213 br_acqp.LoadFile(strAcqp);
216 //std::cout << "out of br_acqp.FillMap " << std::endl;
217 std::string acqp_scan_name;
218 std::string acqp_method;
219 std::string acqp_protocol_location;
221 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
222 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
223 cleanString(acqp_protocol_location);
225 //std::cout << "out of br_acqp.FillMap 1" << std::endl;
226 BrukerFieldData b_scan_name=br_acqp.GetFieldData("ACQ_scan_name");
227 //std::cout << "out of br_acqp.FillMap 11" << std::endl;
228 acqp_scan_name = b_scan_name.GetStringValue()[0];
229 //std::cout << "out of br_acqp.FillMap 12 [" << acqp_scan_name << "]" << std::endl;
230 cleanString(acqp_scan_name);
231 //std::cout << "out of br_acqp.FillMap 2" << std::endl;
232 BrukerFieldData b_method=br_acqp.GetFieldData("ACQ_method");
233 b_method.PrintSelf();
234 acqp_method = b_method.GetStringValue()[0];
236 cleanString(acqp_method);
237 //std::cout << "out of br_acqp.FillMap 3" << std::endl;
238 BrukerFieldData b_list_size = br_acqp.GetFieldData("ACQ_O1_list_size");
239 //std::cout << "out of br_acqp.FillMap 4" << std::endl;
240 //b_list_size.PrintSelf(); //JP
242 nbSlices = b_list_size.GetIntValue()[0];
243 //std::cout << "out of br_acqp.FillMap 5" << std::endl;
244 strSerieDescr = GDCM_NAME_SPACE::Util::GetName(*it)
245 /* + "." + acqp_protocol_location */ // always the same (in each acquisition)
246 + "." + acqp_scan_name
247 + "." + acqp_method.c_str();
249 sprintf(outputDirName, "%s%c%s", tempStringStudyDir.c_str(), //OutputDirName.c_str(),
250 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
251 strSerieDescr.c_str() );
253 std::cout << " ================================================================================\n"
254 << " === [" << GDCM_NAME_SPACE::Util::GetName(*it) << "] -> [" << strSerieDescr << "]\n"
255 << " ================================================================================"
257 std::string strOutputDirName(outputDirName);
259 printf ("outputDirName [%s]\n", outputDirName);
261 DealWithNiveau1(*it, strOutputDirName);
263 catch (BrukerHopelessException &e)
265 std::cout << "And Exception was thrown in DealWithNiveau1 (" << e.what() << ") " << std::endl;
269 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
272 // =====================================================================
274 void Bruker2Dicom::DealWithNiveau1(std::string &level1Directory, std::string ¤tOutputDirName) {
276 // e.g. : at level 1, in B67d1.Bp1/6
278 // acqp fid imnd pdata pulseprogram spnam0 spnam1
280 bool res = CreateDirectory(currentOutputDirName);
283 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
284 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
287 GDCM_NAME_SPACE::DirList dirList(level1Directory, false, true); // DON'T get recursively the list of files
288 GDCM_NAME_SPACE::DirListType fileNames;
289 fileNames = dirList.GetFilenames();
290 // -----------------------------------------------------
291 // Iterate to ALL the objets(files/directories) found in the input directory
292 // -----------------------------------------------------
293 GDCM_NAME_SPACE::DirListType::iterator it;
295 for (it = fileNames.begin();
296 it != fileNames.end();
299 if ( boost::filesystem::is_regular(*it) )
300 //if ( ! boost::filesystem::is_directory(*it) )
303 std::cout << "--- [" << *it << "] is a file." << std::endl;
307 char outputDirName[(unsigned int) PATH_MAX+2];
308 //std::string firstName;
310 for (it = fileNames.begin();
311 it != fileNames.end();
314 if ( boost::filesystem::is_directory(*it) )
316 // will be always "pdata" ...
318 std::cout << "--- [" << *it << "] is a directory" << std::endl;
321 if (FileLine.startsWith("##$ACQ_size=")) {
322 if (FileLine.startsWith("##$NI=")) {
323 if (FileLine.startsWith("##$NR=")) {
324 if (FileLine.startsWith("##$ACQ_obj_order=")) {
325 if (FileLine.startsWith("##$ACQ_word_size=")) {
326 if (FileLine.startsWith("##$BYTORDA=")) {
327 if (FileLine.startsWith("##$PULPROG=")) {
330 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
331 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
332 GDCM_NAME_SPACE::Util::GetName(*it).c_str());
333 std::string strOutputDirName(outputDirName);
336 std::string strMethod;
337 //std::string firstName = *(fileNames.begin());
339 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin())) +
340 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
342 // std::cout << "---strMethod (for method)=> [" << strMethod << "]" << std::endl;
343 canOpen = br_method.LoadFile(strMethod);
346 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
347 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
349 //std::cout << "---strMethod (for imnd) => [" << strMethod << "]" << std::endl;
350 canOpen = br_method.LoadFile(strMethod);
353 std::cout << "Hopeless! neither 'method' nor 'imnd' found in ["
354 << level1Directory << "]; we skip it!" << std::endl;
356 //throw ( BrukerHopelessException ("Hopeless! neither 'method' nor 'imnd' found "));
357 //exit(0); /// \TODO throw an exception !
361 std::cout << "open => [" << strMethod << "] successfully" << std::endl;
365 ##$PVM_Fov (dimension) // ou plutot RECO_fov !
368 dans method (pour perfusion seulement?) :
369 ##$PVM_ObjOrderList=( 8 )
372 ##$PVM_SPackArrNSlices=( 2 )
376 DealWithNiveau2(*it, strOutputDirName);
378 catch (BrukerHopelessException &e)
380 std::cout << "And Exception was thrown in DealWithNiveau2 (" << e.what() << ") " << std::endl;
387 // =====================================================================
389 void Bruker2Dicom::DealWithNiveau2(std::string &level2Directory, std::string ¤tOutputDirName) {
391 // e.g. : at level 2 in B67d1.Bp1/6/pdata
393 // acqp fid imnd pdata pulseprogram spnam0 spnam1
396 bool res = CreateDirectory(currentOutputDirName);
399 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
400 throw ( BrukerHopelessException ("Hopeless! Level2 output directory creation failure"));
404 GDCM_NAME_SPACE::DirList dirList(level2Directory, false, true); // DON'T get recursively the list of files
406 GDCM_NAME_SPACE::DirListType fileNames;
407 fileNames = dirList.GetFilenames();
409 char outputDirName[(unsigned int) PATH_MAX+2];
411 // -----------------------------------------------------
412 // Iterate to ALL the objets(files/directories) found in the input directory
413 // -----------------------------------------------------
414 GDCM_NAME_SPACE::DirListType::iterator it;
418 for (it = fileNames.begin();
419 it != fileNames.end();
422 if ( ! boost::filesystem::is_directory(*it) )
424 std::cout << "--- --- [" << *it << "] is a file.." << std::endl;
428 for (it = fileNames.begin();
429 it != fileNames.end();
432 if ( boost::filesystem::is_directory(*it) )
436 std::cout << "--- --- [" << *it << "] is a directory" << std::endl;
438 // sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
439 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
440 // GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
444 // (interest of previous method :
445 // If unaware user changed the pdata name, it goes on working
449 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
452 std::string str_isa_func_name;
453 canOpen = br_isa.LoadFile(str_isa);
456 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
457 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
458 GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
463 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
465 str_isa_func_name = b_isa_func_name.GetStringValue()[0];
466 cleanString(str_isa_func_name);
468 sprintf(outputDirName, "%s%c%s.%s", currentOutputDirName.c_str(),
469 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
470 GDCM_NAME_SPACE::Util::GetName(*it).c_str(),
471 str_isa_func_name.c_str());
473 std::string strOutputDirName(outputDirName);
475 DealWithNiveau3(*it, strOutputDirName);
477 catch (BrukerHopelessException &e)
479 std::cout << "And Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
480 << " We skip [" << level2Directory << "]" << std::endl;
483 catch (BrukerInitException &e)
485 std::cout << "And Init Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
486 << " We skip [" << level2Directory << "]" << std::endl;
494 // =====================================================================
497 void Bruker2Dicom::DealWithNiveau3(std::string &level3Directory, std::string ¤tOutputDirName){
500 // e.g. at level 3, in
502 // just to be able to go on checking // JP
503 if ( GDCM_NAME_SPACE::Util::GetName(level3Directory) != "1")
506 bool res = CreateDirectory(currentOutputDirName);
510 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
511 throw ( BrukerHopelessException ("Hopeless! Level3 output directory creation failure"));
515 GDCM_NAME_SPACE::DirList dirList(level3Directory, false, true); // DON'T get recursively the list of files
516 GDCM_NAME_SPACE::DirListType::iterator it;
517 GDCM_NAME_SPACE::DirListType fileNames;
518 fileNames = dirList.GetFilenames();
520 char original2dseqName [(unsigned int) PATH_MAX+2];
521 char original2dseqName_XXX [(unsigned int) PATH_MAX+2];
522 char currentOutputMhdDirName [(unsigned int) PATH_MAX+2];
524 char outputMhdFileName [(unsigned int) PATH_MAX+2];
525 char output2dseqSliceFileName[(unsigned int) PATH_MAX+6]; // think about extra '.dcm'
526 char output2dseqName [(unsigned int) PATH_MAX+6];
527 char output2dseqCartoName [(unsigned int) PATH_MAX+6];
529 char copyFile[PATH_MAX + PATH_MAX + 5]; // Should be enough!
532 //-------------- try d3proc;
533 char char_d3proc[(unsigned int) PATH_MAX+2];
535 sprintf(char_d3proc,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"d3proc" );
538 std::cout << "d3proc: --- => [" << char_d3proc << "]" << std::endl;
539 std::string str_d3proc(char_d3proc);
540 canOpen = br_d3proc.LoadFile(str_d3proc);
544 std::cout << "Hopeless! no 'd3proc' found" << std::endl;
545 throw ( BrukerHopelessException ("Hopeless! no 'd3proc' found"));
546 //exit(0); /// \TODO throw an exception !
549 canOpen = br_d3proc.FillMap();
552 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
553 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'd3proc'"));
554 //exit(0); /// \TODO throw an exception !
557 //-------------- end try d3proc;
560 // -------------------try reco
562 char char_reco[(unsigned int) PATH_MAX+2];
564 sprintf(char_reco,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
565 //str_d3proc = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
566 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
569 std::cout << "reco: --- => [" << char_reco << "]" << std::endl;
571 std::string str_reco(char_reco);
572 canOpen = br_reco.LoadFile(str_reco);
574 if (!canOpen) // we try in directory ../1
577 std::cout << "[" << str_reco << "] not found " << std::endl;
578 std::string lastDirName = GDCM_NAME_SPACE::Util::GetPath(level3Directory);
579 //lastDirName = GDCM_NAME_SPACE::Util::GetPath(lastDirName);
580 sprintf(char_reco,"%s%c1%c%s", lastDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
581 //str_reco=char_reco;
582 canOpen = br_reco.LoadFile(str_reco);
585 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
586 throw ( BrukerHopelessException ("Hopeless! cannot find 'reco'"));
587 //exit(0); /// \TODO throw an exception !
591 std::cout << "[" << str_reco << "] successfully Loaded " << std::endl;
594 canOpen = br_reco.FillMap();
597 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
598 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'reco'"));
599 //exit(0); /// \TODO throw an exception !
602 std::cout << "[" << str_reco << "] successfully Mapped" << std::endl;
605 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
606 // br_reco.PrintSelf();
607 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
608 // -------------------end try reco
611 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
612 int NX = bX.GetIntValue()[0];
615 std::cout << "IM_SIX " << NX << std::endl;
616 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
617 int NY = bY.GetIntValue()[0];
620 std::cout << "IM_SIY " << NY << std::endl;
621 /// \todo : check if there are actually 3 dimensions or only 2
623 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
624 int nbFrames = bZ.GetIntValue()[0];
626 std::cout << "IM_SIZ " << nbFrames << std::endl;
628 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
630 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
632 std::string mhdDataPixelType;
634 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
640 // En regle generale il vaut mieux que l'on passe par RECO_*
641 // pour extraire les parametres de l'image
644 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
645 double fovX = fov.GetDoubleValue()[0];
646 double fovY = fov.GetDoubleValue()[1];
648 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
650 BrukerFieldData spatResol = br_method.GetFieldData("PVM_SpatResol");
651 double spatResolX = spatResol.GetDoubleValue()[0];
652 double spatResolY = spatResol.GetDoubleValue()[1];
654 std::cout << "SpatResol (ds method) " << spatResolX << " " << spatResolY << std::endl;
659 // Better we use 'get' accessors from BrukerImage class, as Denis wrote them
661 BrukerFieldData fov = br_reco.GetFieldData("RECO_fov");
662 double fovX = fov.GetDoubleValue()[0];
663 double fovY = fov.GetDoubleValue()[1];
665 std::cout << "FOV (ds reco) " << fovX << " " << fovY << std::endl;
667 BrukerFieldData size = br_reco.GetFieldData("RECO_size");
668 double sizeX = size.GetDoubleValue()[0];
669 double sizeY = size.GetDoubleValue()[1];
672 std::cout << "SIZE (ds reco) " << sizeX << " " << sizeY << std::endl;
674 double spatResolX = fovX / sizeX;
675 double spatResolY = fovY / sizeY;
678 std::cout << "spatResol (ds reco : fov/size) " << spatResolX << " " << spatResolY << std::endl;
682 /// \TODO probabely a more sophisticated accessor will be necessary :
683 /// (cf : non contiguous slices, overlapping, slice thickness, space between slices, etc)
685 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
686 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
689 std::cout << "SPackArrSliceDistance (ds method) " << sliceDistance << std::endl;
691 // ----------------------------------------------------------------------------------------
695 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
696 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "MhdFiles");
697 std::string strCurrentOutputMhdDirName(currentOutputMhdDirName);
699 res = CreateDirectory( strCurrentOutputMhdDirName );
701 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
702 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
707 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
711 std::cout << "nbFrames " << nbFrames << std::endl;
713 std::cout << "nbSlices " << nbSlices << std::endl;
715 int nbInstants = nbFrames/nbSlices;
717 std::cout << "nbInstants (deduced)" << nbInstants << std::endl;
720 FILE *fp; // for MHD files
722 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "2dseq");
725 // \TODO : tenir compte du bazar precedent
727 // load 2dseq in memory
729 fp = fopen(original2dseqName, "rb");
732 // try 2dseq_Angio2D ?!?
734 sprintf( original2dseqName_XXX, "%s%s", original2dseqName, "_Angio2D");
735 fp = fopen(original2dseqName_XXX, "rb");
738 std::cout << "Cannot open [" << original2dseqName << "] nor [" << original2dseqName_XXX << "] for reading" << std::endl;
740 throw ( BrukerHopelessException ("Hopeless! Cannot open '2dseq'"));
745 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
746 ///\ TODO : find a safer way to be sure to read everything!
747 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
749 // This one will be important!
750 // ---------------------------
752 imageSet = CreateImageSet ( );
754 catch (BrukerInitException& e)
757 std::cout << "an Init Exception was thrown in CreateImageSet ( ); " << e.what()
758 << "catched in DealWithNiveau3" << std::endl;
764 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
765 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
768 std::cout << "Single instant : do not split" << std::endl;
771 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
772 GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
773 fp=fopen(outputMhdFileName, "w");
776 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
777 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
782 fprintf(fp, "ObjectType = Image\n");
783 fprintf(fp, "NDims = 3\n" );
784 fprintf(fp, "BinaryData = True \n" );
785 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
786 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
787 fprintf(fp, "HeaderSize = %d\n", 0);
788 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
789 fprintf(fp, "ElementSpacing = %lf %lf %lf\n", spatResolX, spatResolY, sliceDistance );
790 fprintf(fp, "Position = 0 0 %d\n", 0 );
791 fprintf(fp, "Offset = 0 0 0\n" );
792 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
793 fprintf(fp, "ElementNumberOfChannels = 1\n" );
794 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
795 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
798 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
799 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
800 fp=fopen(output2dseqSliceFileName, "wb");
803 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
804 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseq file for writting"));
808 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
812 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
816 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
817 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
819 /* ----------- Write Dicom Image ---------------*/
820 MakeDicomImage(buffer_2dseq,
825 //fovX/NY, fovY/NY, sliceDistance,
826 spatResolX, spatResolY, sliceDistance,
827 output2dseqSliceFileName,
835 0,// index frame number
836 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
839 } // end if nbInstants = 1
841 else // more than ONE instant
844 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
845 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
848 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
852 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
853 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, k );
855 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
856 fp=fopen(outputMhdFileName, "w");
859 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
860 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
865 /* ----------- Write MHD Image ---------------*/
867 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
868 fprintf(fp, "ObjectType = Image\n");
869 fprintf(fp, "NDims = 3\n" );
870 fprintf(fp, "BinaryData = True \n" );
871 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
872 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
873 fprintf(fp, "HeaderSize = %d\n", 0);
874 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); //
875 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, 1.0 ); //slice distance : no meaning for temporal serie
876 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
877 fprintf(fp, "Offset = 0 0 0\n" );
878 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
879 fprintf(fp, "ElementNumberOfChannels = 1\n" );
880 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
881 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb );
885 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
886 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,sliceNb);
887 fp=fopen(output2dseqSliceFileName, "wb");
890 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
891 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseqSliceFile file for writting"));
894 int frameSize = NX*NY*pixelSize;
895 for (instantNb=0; instantNb<nbInstants; instantNb++)
897 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
898 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
903 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
910 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
911 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
912 fp=fopen(output2dseqSliceFileName, "wb");
915 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
918 int frameSize = NX*NY*pixelSize;
919 for (instantNb=0; instantNb<nbInstants; instantNb++)
921 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
922 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
928 fp=fopen(output2dseqSliceFileName, "rb");
931 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
934 fread( pixelsForCurrentSlice,
935 frameSize*nbInstants,
938 // end of desperate try !
941 /* ----------- Write Dicom Image ---------------*/
943 int frameSize = NX*NY*pixelSize;
944 for (instantNb=0; instantNb<nbInstants; instantNb++)
946 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
949 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
950 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
951 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb);
954 pixelsForCurrentSlice,
959 spatResolX, spatResolY, sliceDistance,
960 //fovX/NY, fovY/NY, sliceDistance,
961 output2dseqSliceFileName,
970 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
973 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
979 delete [] pixelsForCurrentSlice;
980 } // end nbInstants == 1
981 delete [] buffer_2dseq;
985 // -----------------------------------------------------
986 // deal with MatLab-generated Carto file.
987 // -----------------------------------------------------
989 dealWithCarto(fileNames, NX, NY, nbSlices, /*fovX, fovY,*/ spatResolX, spatResolY, sliceDistance,
990 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
994 // ===========================================================================================
996 void Bruker2Dicom::dealWithCarto(GDCM_NAME_SPACE::DirListType &fileNames, int NX, int NY, int nbSlices,
997 /*double fovX, double fovY, */
998 double spatResolX, double spatResolY, double sliceDistance,
999 char *copyFile, std::string ¤tOutputDirName,
1000 char *outputMhdFileName, char *output2dseqCartoName)
1002 // -----------------------------------------------------
1003 // deal with MatLab-generated Carto file.
1004 // -----------------------------------------------------
1006 const char *code[] = { "ADC", "adc", "TTP", "ttp", "PEAK", "peak", "" }; // add more carto file name identifiers if necessary; end with ""
1007 const char *separator[] = { "_", ".", "-", "" }; // add more, if necessary, to ckeck for 2dseq.ADC, 2dseq_ADC, 2dseq-ADC, etc; end with ""
1010 GDCM_NAME_SPACE::DirListType::iterator it;
1011 char file_name_ident[500];
1014 // Iterate to ALL the objets(files/directories) found in the input directory
1015 for (it = fileNames.begin();
1016 it != fileNames.end();
1019 if ( boost::filesystem::is_regular(*it) )
1020 //if ( ! boost::filesystem::is_directory(*it) )
1023 std::cout << "--- [" << *it << "] is a file..." << std::endl;
1026 while (code[icode][0] != 0)
1029 while (separator[iseparator][0] != 0)
1031 sprintf(file_name_ident, "2dseq%s%s",separator[iseparator],code[icode]); // e.g "2dseq_ADC"
1033 // std::cout << "check name ["<<(*it) << "] for string [" << file_name_ident << "]" << std::endl;
1034 std::string::size_type loc = (*it).rfind(file_name_ident);
1036 if ( loc != std::string::npos )
1039 ///\ TODO : find a safer way to be sure to read everything!
1040 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
1041 fp = fopen ( (*it).c_str(), "rb");
1043 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
1044 throw ( BrukerHopelessException ("Level 1 Unable to open 'carto' file "));
1046 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
1048 // ?!? sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1049 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
1050 << NX*NY*sizeof(double)*nbSlices << std::endl;
1051 std::string lastFileName = GDCM_NAME_SPACE::Util::GetName((*it).c_str());
1054 // Copy the data file in the new directory
1055 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1056 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str());
1058 sprintf(outputMhdFileName, "%s%c%s%s",
1059 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".mhd" );
1061 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1064 fp=fopen(outputMhdFileName, "w");
1067 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
1071 fprintf(fp, "ObjectType = Image\n");
1072 fprintf(fp, "NDims = 3\n" );
1073 fprintf(fp, "BinaryData = True \n" );
1074 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
1075 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
1076 fprintf(fp, "HeaderSize = %d\n", 0 );
1077 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, sliceDistance );
1078 fprintf(fp, "Position = 0 0 0\n" );
1079 fprintf(fp, "Offset = 0 0 0\n" );
1080 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
1081 fprintf(fp, "ElementNumberOfChannels = 1\n" );
1082 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
1083 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
1088 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1091 // ----------- Write Dicom Image ---------------
1095 sprintf(output2dseqCartoName, "%s%c%s%s",
1096 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".dcm" );
1098 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
1100 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID(); //New SerieUID for each carto.
1101 std::string strNewSerieDescr(strSerieDescr+ "_" +GDCM_NAME_SPACE::Util::GetName((*it).c_str()));
1102 MakeDicomImage(buffer_carto,
1107 //fovX/NY, fovY/NY, sliceDistance,
1108 spatResolX, spatResolY, sliceDistance,
1109 output2dseqCartoName,
1118 GDCM_NAME_SPACE::CREATED_IMAGE
1122 delete [] buffer_carto;
1124 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
1125 break; // don't check for more ident on same file name!
1127 } // end deal with _ADC, -adc, etc
1129 } // end iterate speparators
1131 } // end iterate code
1132 } // end boost::filesystem::is_regular(*it)
1133 } // end iterate on all objects (files, dir, etc)
1137 // ==========================================================================================================
1139 int Bruker2Dicom::CheckUserDirectory(std::string &userDirName)
1141 GDCM_NAME_SPACE::DirList dirList(userDirName, false, true); // DON'T get recursively the list of files
1143 GDCM_NAME_SPACE::DirListType fileNames;
1144 fileNames = dirList.GetFilenames();
1146 std::string path = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()));
1148 std::string subject = path +
1149 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
1152 std::string acqp = path +
1153 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
1156 if ( boost::filesystem::is_regular(subject) )
1157 subjectFound = true; // user passed a 'study
1159 subjectFound = false; // user didnt' pass a 'study
1161 if ( boost::filesystem::is_regular(acqp) )
1162 acqpFound= true; // user passes a 'serie', not a 'study'
1164 acqpFound = false; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
1167 if (subjectFound ) type = 1; // user passed a 'study
1168 else if (acqpFound) type = 2; // user passed a 'serie'
1169 else type = 3; // user passed a 'non study' directory; Hope it's a 'set of studies' directory!
1175 // ==========================================================================================================
1177 bool Bruker2Dicom::CreateDirectory(std::string &OutputDirName)
1179 std::string systemCommand;
1182 std::cout << "Check for output directory :[" << OutputDirName << "]."
1184 if ( ! boost::filesystem::is_directory(OutputDirName) ) // dirout not found
1186 systemCommand = "mkdir " + OutputDirName; // create it!
1188 std::cout << systemCommand << std::endl;
1189 system (systemCommand.c_str());
1190 if ( ! boost::filesystem::is_directory(OutputDirName) ) // be sure it worked
1193 std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
1195 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
1200 std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
1206 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
1212 // ===========================================================================================
1214 /// \TODO move cleanString to 'crea' ?
1216 void Bruker2Dicom::cleanString(std::string &s)
1219 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
1224 if (s[l-1] == ' ' ) // blank space
1232 std::string repChar("_");
1233 GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
1238 // ===========================================================================================
1241 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
1243 if(bDPT.GetDataType() == "string")
1245 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1247 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1248 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1250 if (brukerDataPixelType == "ip_short") {
1251 mhdDataPixelType = "MET_USHORT";
1254 if (brukerDataPixelType == "ip_int") {
1255 mhdDataPixelType = "MET_UINT";
1258 if (brukerDataPixelType == "ip_char") {
1259 mhdDataPixelType = "MET_UCHAR";
1262 /// \TODO : finish the list
1264 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1266 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1268 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1270 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1272 case 4 : fp << "ElementType = MET_INT" << std::endl;
1274 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1276 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1278 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1283 int brukerDataPixelType = bDPT.GetIntValue()[0];
1285 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1286 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1288 // Cross your fingers !!!
1290 // pb : found values : 2, 3, 5
1292 if (brukerDataPixelType == 2) {
1293 mhdDataPixelType = "MET_USHORT";
1296 if (brukerDataPixelType == 3) {
1297 mhdDataPixelType = "MET_USHORT";
1300 if (brukerDataPixelType == 1) {
1301 mhdDataPixelType = "MET_UCHAR";
1307 // ===========================================================================================
1309 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1311 std::vector<BrukerImage> imageSet;
1312 br_acqp.SetLoopStructure();
1313 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1314 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1316 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1318 if (result == false)
1320 throw ( BrukerInitException ("ObjectVaryingProperties.init() failure in Bruker2Dicom::CreateImageSet()") );
1323 br_acqp.SetImageLoopStructure();
1324 br_acqp.SetBrukerImageList();
1325 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1327 BrukerImage image(br_acqp,br_reco);
1328 image.Init(br_acqp,br_reco,1);
1330 for(int i=0;i<brukerImageList.size();i++)
1332 image.Init(br_acqp,br_reco,i);
1333 imageSet.push_back(image);
1336 // Just for checking
1339 std::vector<std::vector <double> > imageOrientation;
1340 std::vector <double> imagePosition;
1341 for(int i=0;i<brukerImageList.size();i++)
1343 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1345 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1346 std::cout << "Position " << imagePosition[0] << " "
1347 << imagePosition[1] << " " << imagePosition[2] ;
1348 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1349 std::cout << "\t Orientation " ;
1350 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1351 std::cout << imageOrientation[i1][i2] << " ";CreateImageSet
1353 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1354 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1356 std::cout << "\t [";
1357 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1358 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1359 std::cout << "]" << std::endl;
1365 // ===========================================================================================
1367 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1372 double spacingX, double spacingY, double sliceDistance,
1373 std::string dcmImageName,
1374 const std::string &patientName,
1376 std::string &studyUID,
1377 std::string &serieUID,
1378 std::string &studyDescr,
1379 std::string &serieDescr,
1380 std::string &strStudyTimeDate,
1382 GDCM_NAME_SPACE::ImageContentType contentType
1385 std::ostringstream str;
1387 GDCM_NAME_SPACE::File *file;
1388 file = GDCM_NAME_SPACE::File::New();
1390 // Set the image size
1393 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1396 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1402 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1405 // Set the pixel type
1406 // //8, 16, 32, 64 (for double ?)
1409 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1411 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1414 str << pixelSize*8-1;
1415 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1417 // Set the pixel representation // 0/1 , 0=unsigned
1418 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1420 // Set the samples per pixel // 1:Grey level, 3:RGB
1421 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1423 // 0028 0030 DS 2 Pixel Spacing
1425 str << spacingX << "\\" << spacingY;
1426 file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1428 // 0018 0050 DS 1 Slice Thickness
1430 str << sliceDistance;
1431 file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1433 // 0020 0011 IS 1 Series Number
1436 file->InsertEntryString(str.str(),0x0020,0x0011, "IS");
1438 // 0020|0013 [IS] [Instance Number]
1441 str << instanceNumber;
1442 file->InsertEntryString(str.str(),0x0020,0x0013, "IS");
1444 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1445 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1446 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1448 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1450 // 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1451 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1452 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1454 // if (strlen(patientName) != 0)
1455 file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1456 file->InsertEntryString(patientName.c_str(),0x0010,0x0020, "LO"); // Patient's ID
1458 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1459 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1461 // 0008 0020 DA 1 Study Date
1462 // 0008 0030 TM 1 Study Time
1464 /// \TODO split into 2 strings!
1465 file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1466 file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(), 0x0008,0x0030, "TM");
1468 file->InsertEntryString(studyDescr, 0x0008,0x1030, "LO"); // Study Description
1469 file->InsertEntryString(serieDescr, 0x0008,0x103e, "LO"); // Series Description
1471 //0008|0060 [CS] [Modality]
1472 file->InsertEntryString("MR",0x0008,0x0060, "CS");
1474 // 0020 0037 DS 6 Image Orientation (Patient)
1475 char charImageOrientation[256];
1478 std::cout << "charImageOrientation " <<
1479 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1480 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1481 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1482 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1483 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1484 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1486 sprintf(charImageOrientation,"%f\\%f\\%f \\ %f\\%f\\%f",
1487 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0],
1488 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1],
1489 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2],
1490 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0],
1491 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1],
1492 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] ) ;
1494 file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1496 // 0020 0032 DS 3 Image Position (Patient)
1498 char charImagePosition[256];
1499 sprintf(charImagePosition,"%f\\%f\\%f",
1500 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1501 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1502 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1504 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1506 // 0020 0x1041 DS 1 Slice Location
1507 // sprintf(charImagePosition,"%f",float(imgNum));
1508 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1510 // Set Rescale Intercept
1513 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1515 // Set Rescale Slope
1518 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1521 GDCM_NAME_SPACE::FileHelper *fileH;
1522 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1523 fileH->SetContentType(contentType);
1525 // cast is just to avoid warnings (*no* conversion is performed)
1526 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1528 //std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1530 fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1531 fileH->SetWriteModeToRaw();
1532 fileH->SetWriteTypeToDcmExplVR();
1533 if( !fileH->Write(dcmImageName)) {
1534 std::cout << "Failed for [" << dcmImageName << "]\n"
1535 << " File is unwrittable" << std::endl;
1538 throw ( BrukerHopelessException ("Level 1 Unable to write Dicom file "));