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 "));
38 std::string strDirNamein(InputDirName);
39 GDCM_NAME_SPACE::DirList dirList(strDirNamein, false, true); // DON'T get recursively the list of files
40 std::string strDirNameout(OutputDirName);
45 std::cout << "------------List of found files ------------" << std::endl;
47 std::cout << std::endl;
53 // e.g : at level 0, in : B67d1.Bp1
55 // 1 2 3 4 5 6 AdjStatePerStudy subject
58 GDCM_NAME_SPACE::DirListType fileNames;
59 fileNames = dirList.GetFilenames();
61 std::string outputFileName;
63 // BrukerDataSet br_subject;
65 subject = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
66 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
69 std::cout << " Subject : [" << subject << "]" << std::endl;
72 canOpen =br_subject.LoadFile(subject);
75 //std::cout << "Hopeless! no 'subject' found" << std::endl;
76 //throw ( BrukerHopelessException ("Hopeless! no 'subject' found in root input directory "));
78 std::cout << "Not too much hope! no 'subject' found (Desperate try will be performed -default values- )" << std::endl;
86 //br_subject.PrintSelf();
88 // get info for 'Study Description'
92 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
93 subject_name = b_name.GetStringValue()[0];
94 strPatientName = subject_name;
95 cleanString(subject_name);
98 subject_name ="defaultPatName";
100 // creation directory : 'nom du patient'
101 std::string tempStringPatDir(OutputDirName);
102 tempStringPatDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + subject_name;
104 res=CreateDirectory(tempStringPatDir);
106 std::cout << "[" << tempStringPatDir << "] Directory creation failure " << std::endl;
107 throw ( BrukerHopelessException ("Patient directory creation failure "));
110 std::string subject_entry;
111 std::string subject_position;
112 std::string subject_date;
113 std::string subject_study_name;
117 BrukerFieldData b_entry=br_subject.GetFieldData("SUBJECT_entry");
118 subject_entry = b_entry.GetStringValue()[0];
119 //cleanString(subject_entry);
120 subject_entry = subject_entry.substr(11, subject_entry.size()-11);
122 BrukerFieldData b_position=br_subject.GetFieldData("SUBJECT_position");
123 subject_position = b_position.GetStringValue()[0];
124 //cleanString(subject_position);
125 subject_position = subject_position.substr(9, subject_position.size()-9);
127 BrukerFieldData b_date=br_subject.GetFieldData("SUBJECT_date");
128 subject_date = b_date.GetStringValue()[0];
129 strStudyTimeDate = subject_date;
130 cleanString(subject_date);
132 BrukerFieldData b_study_name=br_subject.GetFieldData("SUBJECT_study_name");
133 subject_study_name = b_study_name.GetStringValue()[0];
134 subject_study_name = subject_study_name.substr(1, subject_study_name.size()-2);
135 cleanString(subject_study_name);
137 else // Desperate trick when file 'subject' is missing
139 subject_entry = "HeadFirst"; // Why not?
140 subject_position = "Supine"; // Why not?
141 strStudyTimeDate = "06_06_06_6_June_1666"; // Why not?
142 subject_date = "6_June_1666"; // Why not?
143 subject_study_name = "defStudyName"; // Why not?
145 // subject_name is already in 'Patient Name'
146 strStudyDescr = /*subject_name + "." + */ subject_study_name + "." + subject_entry + "." + subject_position + "." + subject_date;
148 // creation directory : 'nom de la Study'
150 std::string tempStringStudyDir(OutputDirName);
151 tempStringStudyDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + strStudyDescr;
153 res=CreateDirectory(tempStringStudyDir);
155 std::cout << "[" << tempStringStudyDir << "] Directory creation failure " << std::endl;
156 throw ( BrukerHopelessException ("Study directory creation failure "));
159 char outputDirName[(unsigned int) PATH_MAX+2];
161 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
165 // -----------------------------------------------------
166 // Iterate to ALL the objets(files/directories) found in the input directory
167 // (this is level ZERO)
168 // each Directory (name : 1, 2, 3, ...) will be a Dicom Serie
169 // -----------------------------------------------------
171 GDCM_NAME_SPACE::DirListType::iterator it;
173 for (it = fileNames.begin();
174 it != fileNames.end();
177 if ( boost::filesystem::is_directory(*it) )
180 std::cout << "[" << *it << "] is a directory" << std::endl;
182 if((*it) == "AdjStatePerStudy") // avoid trouble with new version 'AdjStatePerStudy' directory
185 //BrukerDataSet br_acqp;
188 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
191 br_acqp.LoadFile(strAcqp);
194 //std::cout << "out of br_acqp.FillMap " << std::endl;
195 std::string acqp_scan_name;
196 std::string acqp_method;
197 std::string acqp_protocol_location;
199 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
200 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
201 cleanString(acqp_protocol_location);
203 //std::cout << "out of br_acqp.FillMap 1" << std::endl;
204 BrukerFieldData b_scan_name=br_acqp.GetFieldData("ACQ_scan_name");
205 //std::cout << "out of br_acqp.FillMap 11" << std::endl;
206 acqp_scan_name = b_scan_name.GetStringValue()[0];
207 //std::cout << "out of br_acqp.FillMap 12 [" << acqp_scan_name << "]" << std::endl;
208 cleanString(acqp_scan_name);
209 //std::cout << "out of br_acqp.FillMap 2" << std::endl;
210 BrukerFieldData b_method=br_acqp.GetFieldData("ACQ_method");
211 b_method.PrintSelf();
212 acqp_method = b_method.GetStringValue()[0];
214 cleanString(acqp_method);
215 //std::cout << "out of br_acqp.FillMap 3" << std::endl;
216 BrukerFieldData b_list_size = br_acqp.GetFieldData("ACQ_O1_list_size");
217 //std::cout << "out of br_acqp.FillMap 4" << std::endl;
218 //b_list_size.PrintSelf(); //JP
220 nbSlices = b_list_size.GetIntValue()[0];
221 //std::cout << "out of br_acqp.FillMap 5" << std::endl;
222 strSerieDescr = GDCM_NAME_SPACE::Util::GetName(*it)
223 /* + "." + acqp_protocol_location */ // always the same (in each acquisition)
224 + "." + acqp_scan_name
225 + "." + acqp_method.c_str();
227 sprintf(outputDirName, "%s%c%s", tempStringStudyDir.c_str(), //OutputDirName.c_str(),
228 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
229 strSerieDescr.c_str() );
231 std::cout << " ================================================================================\n"
232 << " === [" << GDCM_NAME_SPACE::Util::GetName(*it) << "] -> [" << strSerieDescr << "]\n"
233 << " ================================================================================"
237 printf ("outputDirName [%s]\n", outputDirName);
239 DealWithNiveau1(*it, outputDirName);
241 catch (BrukerHopelessException &e)
243 std::cout << "And Exception was thrown in DealWithNiveau1 (" << e.what() << ") " << std::endl;
247 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
250 // =====================================================================
252 void Bruker2Dicom::DealWithNiveau1(std::string level1Directory, std::string currentOutputDirName) {
254 // e.g. : at level 1, in B67d1.Bp1/6
256 // acqp fid imnd pdata pulseprogram spnam0 spnam1
258 bool res = CreateDirectory(currentOutputDirName);
261 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
262 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
265 GDCM_NAME_SPACE::DirList dirList(level1Directory, false, true); // DON'T get recursively the list of files
266 GDCM_NAME_SPACE::DirListType fileNames;
267 fileNames = dirList.GetFilenames();
268 // -----------------------------------------------------
269 // Iterate to ALL the objets(files/directories) found in the input directory
270 // -----------------------------------------------------
271 GDCM_NAME_SPACE::DirListType::iterator it;
273 for (it = fileNames.begin();
274 it != fileNames.end();
277 if ( boost::filesystem::is_regular(*it) )
278 //if ( ! boost::filesystem::is_directory(*it) )
281 std::cout << "--- [" << *it << "] is a file." << std::endl;
285 char outputDirName[(unsigned int) PATH_MAX+2];
286 //std::string firstName;
288 for (it = fileNames.begin();
289 it != fileNames.end();
292 if ( boost::filesystem::is_directory(*it) )
294 // will be always "pdata" ...
296 std::cout << "--- [" << *it << "] is a directory" << std::endl;
299 if (FileLine.startsWith("##$ACQ_size=")) {
300 if (FileLine.startsWith("##$NI=")) {
301 if (FileLine.startsWith("##$NR=")) {
302 if (FileLine.startsWith("##$ACQ_obj_order=")) {
303 if (FileLine.startsWith("##$ACQ_word_size=")) {
304 if (FileLine.startsWith("##$BYTORDA=")) {
305 if (FileLine.startsWith("##$PULPROG=")) {
308 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
309 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
310 GDCM_NAME_SPACE::Util::GetName(*it).c_str());
313 std::string strMethod;
314 //std::string firstName = *(fileNames.begin());
316 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin())) +
317 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
319 // std::cout << "---strMethod (for method)=> [" << strMethod << "]" << std::endl;
320 canOpen = br_method.LoadFile(strMethod);
323 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
324 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
326 //std::cout << "---strMethod (for imnd) => [" << strMethod << "]" << std::endl;
327 canOpen = br_method.LoadFile(strMethod);
330 std::cout << "Hopeless! neither 'method' nor 'imnd' found in ["
331 << level1Directory << "]; we skip it!" << std::endl;
333 //throw ( BrukerHopelessException ("Hopeless! neither 'method' nor 'imnd' found "));
334 //exit(0); /// \TODO throw an exception !
338 std::cout << "open => [" << strMethod << "] successfully" << std::endl;
342 ##$PVM_Fov (dimension) // ou plutot RECO_fov !
345 dans method (pour perfusion seulement?) :
346 ##$PVM_ObjOrderList=( 8 )
349 ##$PVM_SPackArrNSlices=( 2 )
353 DealWithNiveau2(*it, outputDirName);
355 catch (BrukerHopelessException &e)
357 std::cout << "And Exception was thrown in DealWithNiveau2 (" << e.what() << ") " << std::endl;
364 // =====================================================================
366 void Bruker2Dicom::DealWithNiveau2(std::string level2Directory, std::string currentOutputDirName) {
368 // e.g. : at level 2 in B67d1.Bp1/6/pdata
370 // acqp fid imnd pdata pulseprogram spnam0 spnam1
373 bool res = CreateDirectory(currentOutputDirName);
376 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
377 throw ( BrukerHopelessException ("Hopeless! Level2 output directory creation failure"));
381 GDCM_NAME_SPACE::DirList dirList(level2Directory, false, true); // DON'T get recursively the list of files
383 GDCM_NAME_SPACE::DirListType fileNames;
384 fileNames = dirList.GetFilenames();
386 char outputDirName[(unsigned int) PATH_MAX+2];
388 // -----------------------------------------------------
389 // Iterate to ALL the objets(files/directories) found in the input directory
390 // -----------------------------------------------------
391 GDCM_NAME_SPACE::DirListType::iterator it;
395 for (it = fileNames.begin();
396 it != fileNames.end();
399 if ( ! boost::filesystem::is_directory(*it) )
401 std::cout << "--- --- [" << *it << "] is a file.." << std::endl;
405 for (it = fileNames.begin();
406 it != fileNames.end();
409 if ( boost::filesystem::is_directory(*it) )
413 std::cout << "--- --- [" << *it << "] is a directory" << std::endl;
415 // sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
416 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
417 // GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
421 // (interest of previous method :
422 // If unaware user changed the pdata name, it goes on working
426 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
429 std::string str_isa_func_name;
430 canOpen = br_isa.LoadFile(str_isa);
433 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
434 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
435 GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
440 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
442 str_isa_func_name = b_isa_func_name.GetStringValue()[0];
443 cleanString(str_isa_func_name);
445 sprintf(outputDirName, "%s%c%s.%s", currentOutputDirName.c_str(),
446 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
447 GDCM_NAME_SPACE::Util::GetName(*it).c_str(),
448 str_isa_func_name.c_str());
451 DealWithNiveau3(*it, outputDirName);
453 catch (BrukerHopelessException &e)
455 std::cout << "And Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
456 << " We skip [" << level2Directory << "]" << std::endl;
459 catch (BrukerInitException &e)
461 std::cout << "And Init Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
462 << " We skip [" << level2Directory << "]" << std::endl;
470 // =====================================================================
473 void Bruker2Dicom::DealWithNiveau3(std::string level3Directory, std::string currentOutputDirName){
476 // e.g. at level 3, in
478 // just to be able to go on checking // JP
479 if ( GDCM_NAME_SPACE::Util::GetName(level3Directory) != "1")
482 bool res = CreateDirectory(currentOutputDirName);
486 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
487 throw ( BrukerHopelessException ("Hopeless! Level3 output directory creation failure"));
491 GDCM_NAME_SPACE::DirList dirList(level3Directory, false, true); // DON'T get recursively the list of files
492 GDCM_NAME_SPACE::DirListType::iterator it;
493 GDCM_NAME_SPACE::DirListType fileNames;
494 fileNames = dirList.GetFilenames();
496 char original2dseqName [(unsigned int) PATH_MAX+2];
497 char original2dseqName_XXX [(unsigned int) PATH_MAX+2];
498 char currentOutputMhdDirName [(unsigned int) PATH_MAX+2];
500 char outputMhdFileName [(unsigned int) PATH_MAX+2];
501 char output2dseqSliceFileName[(unsigned int) PATH_MAX+6]; // think about extra '.dcm'
502 char output2dseqName [(unsigned int) PATH_MAX+6];
503 char output2dseqCartoName [(unsigned int) PATH_MAX+6];
505 char copyFile[PATH_MAX + PATH_MAX + 5]; // Should be enough!
508 //-------------- try d3proc;
509 char char_d3proc[(unsigned int) PATH_MAX+2];
511 sprintf(char_d3proc,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"d3proc" );
514 std::cout << "d3proc: --- => [" << char_d3proc << "]" << std::endl;
515 std::string str_d3proc(char_d3proc);
516 canOpen = br_d3proc.LoadFile(str_d3proc);
520 std::cout << "Hopeless! no 'd3proc' found" << std::endl;
521 throw ( BrukerHopelessException ("Hopeless! no 'd3proc' found"));
522 //exit(0); /// \TODO throw an exception !
525 canOpen = br_d3proc.FillMap();
528 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
529 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'd3proc'"));
530 //exit(0); /// \TODO throw an exception !
533 //-------------- end try d3proc;
536 // -------------------try reco
538 char char_reco[(unsigned int) PATH_MAX+2];
540 sprintf(char_reco,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
541 //str_d3proc = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
542 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
545 std::cout << "reco: --- => [" << char_reco << "]" << std::endl;
547 std::string str_reco(char_reco);
548 canOpen = br_reco.LoadFile(str_reco);
550 if (!canOpen) // we try in directory ../1
553 std::cout << "[" << str_reco << "] not found " << std::endl;
554 std::string lastDirName = GDCM_NAME_SPACE::Util::GetPath(level3Directory);
555 //lastDirName = GDCM_NAME_SPACE::Util::GetPath(lastDirName);
556 sprintf(char_reco,"%s%c1%c%s", lastDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
557 //str_reco=char_reco;
558 canOpen = br_reco.LoadFile(str_reco);
561 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
562 throw ( BrukerHopelessException ("Hopeless! cannot find 'reco'"));
563 //exit(0); /// \TODO throw an exception !
567 std::cout << "[" << str_reco << "] successfully Loaded " << std::endl;
570 canOpen = br_reco.FillMap();
573 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
574 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'reco'"));
575 //exit(0); /// \TODO throw an exception !
578 std::cout << "[" << str_reco << "] successfully Mapped" << std::endl;
581 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
582 // br_reco.PrintSelf();
583 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
584 // -------------------end try reco
587 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
588 int NX = bX.GetIntValue()[0];
591 std::cout << "IM_SIX " << NX << std::endl;
592 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
593 int NY = bY.GetIntValue()[0];
596 std::cout << "IM_SIY " << NY << std::endl;
597 /// \todo : check if there are actually 3 dimensions or only 2
599 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
600 int nbFrames = bZ.GetIntValue()[0];
602 std::cout << "IM_SIZ " << nbFrames << std::endl;
604 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
606 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
608 std::string mhdDataPixelType;
610 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
616 // En regle generale il vaut mieux que l'on passe par RECO_*
617 // pour extraire les parametres de l'image
620 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
621 double fovX = fov.GetDoubleValue()[0];
622 double fovY = fov.GetDoubleValue()[1];
624 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
626 BrukerFieldData spatResol = br_method.GetFieldData("PVM_SpatResol");
627 double spatResolX = spatResol.GetDoubleValue()[0];
628 double spatResolY = spatResol.GetDoubleValue()[1];
630 std::cout << "SpatResol (ds method) " << spatResolX << " " << spatResolY << std::endl;
635 // Better we use 'get' accessors from BrukerImage class, as Denis wrote them
637 BrukerFieldData fov = br_reco.GetFieldData("RECO_fov");
638 double fovX = fov.GetDoubleValue()[0];
639 double fovY = fov.GetDoubleValue()[1];
641 std::cout << "FOV (ds reco) " << fovX << " " << fovY << std::endl;
643 BrukerFieldData size = br_reco.GetFieldData("RECO_size");
644 double sizeX = size.GetDoubleValue()[0];
645 double sizeY = size.GetDoubleValue()[1];
648 std::cout << "SIZE (ds reco) " << sizeX << " " << sizeY << std::endl;
650 double spatResolX = fovX / sizeX;
651 double spatResolY = fovY / sizeY;
654 std::cout << "spatResol (ds reco : fov/size) " << spatResolX << " " << spatResolY << std::endl;
658 /// \TODO probabely a more sophisticated accessor will be necessary :
659 /// (cf : non contiguous slices, overlapping, slice thickness, space between slices, etc)
661 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
662 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
665 std::cout << "SPackArrSliceDistance (ds method) " << sliceDistance << std::endl;
667 // ----------------------------------------------------------------------------------------
671 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
672 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "MhdFiles");
673 res = CreateDirectory( currentOutputMhdDirName );
675 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
676 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
681 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
685 std::cout << "nbFrames " << nbFrames << std::endl;
687 std::cout << "nbSlices " << nbSlices << std::endl;
689 int nbInstants = nbFrames/nbSlices;
691 std::cout << "nbInstants (deduced)" << nbInstants << std::endl;
694 FILE *fp; // for MHD files
696 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "2dseq");
699 // \TODO : tenir compte du bazar precedent
701 // load 2dseq in memory
703 fp = fopen(original2dseqName, "rb");
706 // try 2dseq_Angio2D ?!?
708 sprintf( original2dseqName_XXX, "%s%s", original2dseqName, "_Angio2D");
709 fp = fopen(original2dseqName_XXX, "rb");
712 std::cout << "Cannot open [" << original2dseqName << "] nor [" << original2dseqName_XXX << "] for reading" << std::endl;
714 throw ( BrukerHopelessException ("Hopeless! Cannot open '2dseq'"));
719 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
720 ///\ TODO : find a safer way to be sure to read everything!
721 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
723 // This one will be important!
724 // ---------------------------
726 imageSet = CreateImageSet ( );
728 catch (BrukerInitException& e)
731 std::cout << "an Init Exception was thrown in CreateImageSet ( ); " << e.what()
732 << "catched in DealWithNiveau3" << std::endl;
738 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
739 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
742 std::cout << "Single instant : do not split" << std::endl;
745 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
746 GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
747 fp=fopen(outputMhdFileName, "w");
750 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
751 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
756 fprintf(fp, "ObjectType = Image\n");
757 fprintf(fp, "NDims = 3\n" );
758 fprintf(fp, "BinaryData = True \n" );
759 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
760 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
761 fprintf(fp, "HeaderSize = %d\n", 0);
762 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
763 fprintf(fp, "ElementSpacing = %lf %lf %lf\n", spatResolX, spatResolY, sliceDistance );
764 fprintf(fp, "Position = 0 0 %d\n", 0 );
765 fprintf(fp, "Offset = 0 0 0\n" );
766 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
767 fprintf(fp, "ElementNumberOfChannels = 1\n" );
768 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
769 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
772 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
773 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
774 fp=fopen(output2dseqSliceFileName, "wb");
777 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
778 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseq file for writting"));
782 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
786 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
790 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
791 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
793 /* ----------- Write Dicom Image ---------------*/
794 MakeDicomImage(buffer_2dseq,
799 //fovX/NY, fovY/NY, sliceDistance,
800 spatResolX, spatResolY, sliceDistance,
801 output2dseqSliceFileName,
809 0,// index frame number
810 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
813 } // end if nbInstants = 1
815 else // more than ONE instant
818 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
819 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
822 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
826 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
827 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, k );
829 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
830 fp=fopen(outputMhdFileName, "w");
833 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
834 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
839 /* ----------- Write MHD Image ---------------*/
841 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
842 fprintf(fp, "ObjectType = Image\n");
843 fprintf(fp, "NDims = 3\n" );
844 fprintf(fp, "BinaryData = True \n" );
845 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
846 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
847 fprintf(fp, "HeaderSize = %d\n", 0);
848 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); //
849 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, 1.0 ); //slice distance : no meaning for temporal serie
850 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
851 fprintf(fp, "Offset = 0 0 0\n" );
852 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
853 fprintf(fp, "ElementNumberOfChannels = 1\n" );
854 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
855 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb );
859 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
860 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,sliceNb);
861 fp=fopen(output2dseqSliceFileName, "wb");
864 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
865 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseqSliceFile file for writting"));
868 int frameSize = NX*NY*pixelSize;
869 for (instantNb=0; instantNb<nbInstants; instantNb++)
871 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
872 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
877 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
884 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
885 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
886 fp=fopen(output2dseqSliceFileName, "wb");
889 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
892 int frameSize = NX*NY*pixelSize;
893 for (instantNb=0; instantNb<nbInstants; instantNb++)
895 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
896 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
902 fp=fopen(output2dseqSliceFileName, "rb");
905 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
908 fread( pixelsForCurrentSlice,
909 frameSize*nbInstants,
912 // end of desperate try !
915 /* ----------- Write Dicom Image ---------------*/
917 int frameSize = NX*NY*pixelSize;
918 for (instantNb=0; instantNb<nbInstants; instantNb++)
920 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
923 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
924 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
925 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb);
928 pixelsForCurrentSlice,
933 spatResolX, spatResolY, sliceDistance,
934 //fovX/NY, fovY/NY, sliceDistance,
935 output2dseqSliceFileName,
944 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
947 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
953 delete [] pixelsForCurrentSlice;
954 } // end nbInstants == 1
955 delete [] buffer_2dseq;
959 // -----------------------------------------------------
960 // deal with MatLab-generated Carto file.
961 // -----------------------------------------------------
963 dealWithCarto(fileNames, NX, NY, nbSlices, /*fovX, fovY,*/ spatResolX, spatResolY, sliceDistance,
964 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
968 // ===========================================================================================
970 void Bruker2Dicom::dealWithCarto(GDCM_NAME_SPACE::DirListType &fileNames, int NX, int NY, int nbSlices,
971 /*double fovX, double fovY, */
972 double spatResolX, double spatResolY, double sliceDistance,
973 char *copyFile, std::string ¤tOutputDirName,
974 char *outputMhdFileName, char *output2dseqCartoName)
976 // -----------------------------------------------------
977 // deal with MatLab-generated Carto file.
978 // -----------------------------------------------------
980 const char *code[] = { "ADC", "adc", "TTP", "ttp", "PEAK", "peak", "" }; // add more carto file name identifiers if necessary; end with ""
981 const char *separator[] = { "_", ".", "-", "" }; // add more, if necessary, to ckeck for 2dseq.ADC, 2dseq_ADC, 2dseq-ADC, etc; end with ""
984 GDCM_NAME_SPACE::DirListType::iterator it;
985 char file_name_ident[500];
988 // Iterate to ALL the objets(files/directories) found in the input directory
989 for (it = fileNames.begin();
990 it != fileNames.end();
993 if ( boost::filesystem::is_regular(*it) )
994 //if ( ! boost::filesystem::is_directory(*it) )
997 std::cout << "--- [" << *it << "] is a file..." << std::endl;
1000 while (code[icode][0] != 0)
1003 while (separator[iseparator][0] != 0)
1005 sprintf(file_name_ident, "2dseq%s%s",separator[iseparator],code[icode]); // e.g "2dseq_ADC"
1007 // std::cout << "check name ["<<(*it) << "] for string [" << file_name_ident << "]" << std::endl;
1008 std::string::size_type loc = (*it).rfind(file_name_ident);
1010 if ( loc != std::string::npos )
1013 ///\ TODO : find a safer way to be sure to read everything!
1014 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
1015 fp = fopen ( (*it).c_str(), "rb");
1017 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
1018 throw ( BrukerHopelessException ("Level 1 Unable to open 'carto' file "));
1020 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
1022 // ?!? sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1023 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
1024 << NX*NY*sizeof(double)*nbSlices << std::endl;
1025 std::string lastFileName = GDCM_NAME_SPACE::Util::GetName((*it).c_str());
1028 // Copy the data file in the new directory
1029 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1030 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str());
1032 sprintf(outputMhdFileName, "%s%c%s%s",
1033 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".mhd" );
1035 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1038 fp=fopen(outputMhdFileName, "w");
1041 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
1045 fprintf(fp, "ObjectType = Image\n");
1046 fprintf(fp, "NDims = 3\n" );
1047 fprintf(fp, "BinaryData = True \n" );
1048 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
1049 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
1050 fprintf(fp, "HeaderSize = %d\n", 0 );
1051 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, sliceDistance );
1052 fprintf(fp, "Position = 0 0 0\n" );
1053 fprintf(fp, "Offset = 0 0 0\n" );
1054 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
1055 fprintf(fp, "ElementNumberOfChannels = 1\n" );
1056 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
1057 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
1062 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1065 // ----------- Write Dicom Image ---------------
1069 sprintf(output2dseqCartoName, "%s%c%s%s",
1070 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".dcm" );
1072 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
1074 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID(); //New SerieUID for each carto.
1075 std::string strNewSerieDescr(strSerieDescr+ "_" +GDCM_NAME_SPACE::Util::GetName((*it).c_str()));
1076 MakeDicomImage(buffer_carto,
1081 //fovX/NY, fovY/NY, sliceDistance,
1082 spatResolX, spatResolY, sliceDistance,
1083 output2dseqCartoName,
1092 GDCM_NAME_SPACE::CREATED_IMAGE
1096 delete [] buffer_carto;
1098 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
1099 break; // don't check for more ident on same file name!
1101 } // end deal with _ADC, -adc, etc
1103 } // end iterate speparators
1105 } // end iterate code
1106 } // end boost::filesystem::is_regular(*it)
1107 } // end iterate on all objects (files, dir, etc)
1111 // ==========================================================================================================
1113 bool Bruker2Dicom::CreateDirectory(std::string OutputDirName)
1115 std::string systemCommand;
1118 std::cout << "Check for output directory :[" << OutputDirName << "]."
1120 if ( ! boost::filesystem::is_directory(OutputDirName) ) // dirout not found
1122 std::string strDirNameout(OutputDirName); // to please gcc 4
1123 systemCommand = "mkdir " + strDirNameout; // create it!
1125 std::cout << systemCommand << std::endl;
1126 system (systemCommand.c_str());
1127 if ( ! boost::filesystem::is_directory(OutputDirName) ) // be sure it worked
1130 std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
1132 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
1137 std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
1143 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
1151 // ===========================================================================================
1153 /// \TODO move cleanString to 'crea' ?
1155 void Bruker2Dicom::cleanString(std::string &s)
1158 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
1163 if (s[l-1] == ' ' ) // blank space
1171 std::string repChar("_");
1172 GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
1177 // ===========================================================================================
1180 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
1182 if(bDPT.GetDataType() == "string")
1184 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1186 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1187 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1189 if (brukerDataPixelType == "ip_short") {
1190 mhdDataPixelType = "MET_USHORT";
1193 if (brukerDataPixelType == "ip_int") {
1194 mhdDataPixelType = "MET_UINT";
1197 if (brukerDataPixelType == "ip_char") {
1198 mhdDataPixelType = "MET_UCHAR";
1201 /// \TODO : finish the list
1203 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1205 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1207 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1209 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1211 case 4 : fp << "ElementType = MET_INT" << std::endl;
1213 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1215 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1217 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1222 int brukerDataPixelType = bDPT.GetIntValue()[0];
1224 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1225 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1227 // Cross your fingers !!!
1229 // pb : found values : 2, 3, 5
1231 if (brukerDataPixelType == 2) {
1232 mhdDataPixelType = "MET_USHORT";
1235 if (brukerDataPixelType == 3) {
1236 mhdDataPixelType = "MET_USHORT";
1239 if (brukerDataPixelType == 1) {
1240 mhdDataPixelType = "MET_UCHAR";
1246 // ===========================================================================================
1248 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1250 std::vector<BrukerImage> imageSet;
1251 br_acqp.SetLoopStructure();
1252 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1253 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1255 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1257 if (result == false)
1259 throw ( BrukerInitException ("ObjectVaryingProperties.init() failure in Bruker2Dicom::CreateImageSet()") );
1262 br_acqp.SetImageLoopStructure();
1263 br_acqp.SetBrukerImageList();
1264 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1266 BrukerImage image(br_acqp,br_reco);
1267 image.Init(br_acqp,br_reco,1);
1269 for(int i=0;i<brukerImageList.size();i++)
1271 image.Init(br_acqp,br_reco,i);
1272 imageSet.push_back(image);
1275 // Just for checking
1278 std::vector<std::vector <double> > imageOrientation;
1279 std::vector <double> imagePosition;
1280 for(int i=0;i<brukerImageList.size();i++)
1282 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1284 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1285 std::cout << "Position " << imagePosition[0] << " "
1286 << imagePosition[1] << " " << imagePosition[2] ;
1287 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1288 std::cout << "\t Orientation " ;
1289 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1290 std::cout << imageOrientation[i1][i2] << " ";CreateImageSet
1292 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1293 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1295 std::cout << "\t [";
1296 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1297 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1298 std::cout << "]" << std::endl;
1304 // ===========================================================================================
1306 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1311 double spacingX, double spacingY, double sliceDistance,
1312 std::string dcmImageName,
1313 const std::string &patientName,
1315 std::string &studyUID,
1316 std::string &serieUID,
1317 std::string &studyDescr,
1318 std::string &serieDescr,
1319 std::string &strStudyTimeDate,
1321 GDCM_NAME_SPACE::ImageContentType contentType
1324 std::ostringstream str;
1326 GDCM_NAME_SPACE::File *file;
1327 file = GDCM_NAME_SPACE::File::New();
1329 // Set the image size
1332 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1335 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1341 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1344 // Set the pixel type
1345 // //8, 16, 32, 64 (for double ?)
1348 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1350 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1353 str << pixelSize*8-1;
1354 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1356 // Set the pixel representation // 0/1 , 0=unsigned
1357 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1359 // Set the samples per pixel // 1:Grey level, 3:RGB
1360 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1362 // 0028 0030 DS 2 Pixel Spacing
1364 str << spacingX << "\\" << spacingY;
1365 file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1367 // 0018 0050 DS 1 Slice Thickness
1369 str << sliceDistance;
1370 file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1372 // 0020 0011 IS 1 Series Number
1375 file->InsertEntryString(str.str(),0x0020,0x0011, "IS");
1377 // 0020|0013 [IS] [Instance Number]
1380 str << instanceNumber;
1381 file->InsertEntryString(str.str(),0x0020,0x0013, "IS");
1383 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1384 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1385 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1387 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1389 // 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1390 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1391 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1393 // if (strlen(patientName) != 0)
1394 file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1395 file->InsertEntryString(patientName.c_str(),0x0010,0x0020, "LO"); // Patient's ID
1397 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1398 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1400 // 0008 0020 DA 1 Study Date
1401 // 0008 0030 TM 1 Study Time
1403 /// \TODO split into 2 strings!
1404 file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1405 file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(), 0x0008,0x0030, "TM");
1407 file->InsertEntryString(studyDescr, 0x0008,0x1030, "LO"); // Study Description
1408 file->InsertEntryString(serieDescr, 0x0008,0x103e, "LO"); // Series Description
1410 //0008|0060 [CS] [Modality]
1411 file->InsertEntryString("MR",0x0008,0x0060, "CS");
1413 // 0020 0037 DS 6 Image Orientation (Patient)
1414 char charImageOrientation[256];
1417 std::cout << "charImageOrientation " <<
1418 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1419 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1420 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1421 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1422 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1423 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1425 sprintf(charImageOrientation,"%f\\%f\\%f \\ %f\\%f\\%f",
1426 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0],
1427 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1],
1428 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2],
1429 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0],
1430 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1],
1431 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] ) ;
1433 file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1435 // 0020 0032 DS 3 Image Position (Patient)
1437 char charImagePosition[256];
1438 sprintf(charImagePosition,"%f\\%f\\%f",
1439 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1440 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1441 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1443 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1445 // 0020 0x1041 DS 1 Slice Location
1446 // sprintf(charImagePosition,"%f",float(imgNum));
1447 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1449 // Set Rescale Intercept
1452 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1454 // Set Rescale Slope
1457 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1460 GDCM_NAME_SPACE::FileHelper *fileH;
1461 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1462 fileH->SetContentType(contentType);
1464 // cast is just to avoid warnings (*no* conversion is performed)
1465 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1467 //std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1469 fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1470 fileH->SetWriteModeToRaw();
1471 fileH->SetWriteTypeToDcmExplVR();
1472 if( !fileH->Write(dcmImageName)) {
1473 std::cout << "Failed for [" << dcmImageName << "]\n"
1474 << " File is unwrittable" << std::endl;
1477 throw ( BrukerHopelessException ("Level 1 Unable to write Dicom file "));