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 //BrukerDataSet br_acqp;
185 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
188 br_acqp.LoadFile(strAcqp);
191 //std::cout << "out of br_acqp.FillMap " << std::endl;
192 std::string acqp_scan_name;
193 std::string acqp_method;
194 std::string acqp_protocol_location;
196 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
197 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
198 cleanString(acqp_protocol_location);
200 //std::cout << "out of br_acqp.FillMap 1" << std::endl;
201 BrukerFieldData b_scan_name=br_acqp.GetFieldData("ACQ_scan_name");
202 //std::cout << "out of br_acqp.FillMap 11" << std::endl;
203 acqp_scan_name = b_scan_name.GetStringValue()[0];
204 //std::cout << "out of br_acqp.FillMap 12 [" << acqp_scan_name << "]" << std::endl;
205 cleanString(acqp_scan_name);
206 //std::cout << "out of br_acqp.FillMap 2" << std::endl;
207 BrukerFieldData b_method=br_acqp.GetFieldData("ACQ_method");
208 b_method.PrintSelf();
209 acqp_method = b_method.GetStringValue()[0];
211 cleanString(acqp_method);
212 //std::cout << "out of br_acqp.FillMap 3" << std::endl;
213 BrukerFieldData b_list_size = br_acqp.GetFieldData("ACQ_O1_list_size");
214 //std::cout << "out of br_acqp.FillMap 4" << std::endl;
215 //b_list_size.PrintSelf(); //JP
217 nbSlices = b_list_size.GetIntValue()[0];
218 //std::cout << "out of br_acqp.FillMap 5" << std::endl;
219 strSerieDescr = GDCM_NAME_SPACE::Util::GetName(*it)
220 /* + "." + acqp_protocol_location */ // always the same (in each acquisition)
221 + "." + acqp_scan_name
222 + "." + acqp_method.c_str();
224 sprintf(outputDirName, "%s%c%s", tempStringStudyDir.c_str(), //OutputDirName.c_str(),
225 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
226 strSerieDescr.c_str() );
228 std::cout << " ================================================================================\n"
229 << " === [" << GDCM_NAME_SPACE::Util::GetName(*it) << "] -> [" << strSerieDescr << "]\n"
230 << " ================================================================================"
234 printf ("outputDirName [%s]\n", outputDirName);
236 DealWithNiveau1(*it, outputDirName);
238 catch (BrukerHopelessException &e)
240 std::cout << "And Exception was thrown in DealWithNiveau1 (" << e.what() << ") " << std::endl;
244 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
247 // =====================================================================
249 void Bruker2Dicom::DealWithNiveau1(std::string level1Directory, std::string currentOutputDirName) {
251 // e.g. : at level 1, in B67d1.Bp1/6
253 // acqp fid imnd pdata pulseprogram spnam0 spnam1
255 bool res = CreateDirectory(currentOutputDirName);
258 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
259 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
262 GDCM_NAME_SPACE::DirList dirList(level1Directory, false, true); // DON'T get recursively the list of files
263 GDCM_NAME_SPACE::DirListType fileNames;
264 fileNames = dirList.GetFilenames();
265 // -----------------------------------------------------
266 // Iterate to ALL the objets(files/directories) found in the input directory
267 // -----------------------------------------------------
268 GDCM_NAME_SPACE::DirListType::iterator it;
270 for (it = fileNames.begin();
271 it != fileNames.end();
274 if ( boost::filesystem::is_regular(*it) )
275 //if ( ! boost::filesystem::is_directory(*it) )
278 std::cout << "--- [" << *it << "] is a file." << std::endl;
282 char outputDirName[(unsigned int) PATH_MAX+2];
283 //std::string firstName;
285 for (it = fileNames.begin();
286 it != fileNames.end();
289 if ( boost::filesystem::is_directory(*it) )
291 // will be always "pdata" ...
293 std::cout << "--- [" << *it << "] is a directory" << std::endl;
296 if (FileLine.startsWith("##$ACQ_size=")) {
297 if (FileLine.startsWith("##$NI=")) {
298 if (FileLine.startsWith("##$NR=")) {
299 if (FileLine.startsWith("##$ACQ_obj_order=")) {
300 if (FileLine.startsWith("##$ACQ_word_size=")) {
301 if (FileLine.startsWith("##$BYTORDA=")) {
302 if (FileLine.startsWith("##$PULPROG=")) {
305 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
306 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
307 GDCM_NAME_SPACE::Util::GetName(*it).c_str());
310 std::string strMethod;
311 //std::string firstName = *(fileNames.begin());
313 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin())) +
314 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
316 // std::cout << "---strMethod (for method)=> [" << strMethod << "]" << std::endl;
317 canOpen = br_method.LoadFile(strMethod);
320 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
321 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
323 //std::cout << "---strMethod (for imnd) => [" << strMethod << "]" << std::endl;
324 canOpen = br_method.LoadFile(strMethod);
327 std::cout << "Hopeless! neither 'method' nor 'imnd' found in ["
328 << level1Directory << "]; we skip it!" << std::endl;
330 //throw ( BrukerHopelessException ("Hopeless! neither 'method' nor 'imnd' found "));
331 //exit(0); /// \TODO throw an exception !
335 std::cout << "open => [" << strMethod << "] successfully" << std::endl;
339 ##$PVM_Fov (dimension) // ou plutot RECO_fov !
342 dans method (pour perfusion seulement?) :
343 ##$PVM_ObjOrderList=( 8 )
346 ##$PVM_SPackArrNSlices=( 2 )
350 DealWithNiveau2(*it, outputDirName);
352 catch (BrukerHopelessException &e)
354 std::cout << "And Exception was thrown in DealWithNiveau2 (" << e.what() << ") " << std::endl;
361 // =====================================================================
363 void Bruker2Dicom::DealWithNiveau2(std::string level2Directory, std::string currentOutputDirName) {
365 // e.g. : at level 2 in B67d1.Bp1/6/pdata
367 // acqp fid imnd pdata pulseprogram spnam0 spnam1
370 bool res = CreateDirectory(currentOutputDirName);
373 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
374 throw ( BrukerHopelessException ("Hopeless! Level2 output directory creation failure"));
378 GDCM_NAME_SPACE::DirList dirList(level2Directory, false, true); // DON'T get recursively the list of files
380 GDCM_NAME_SPACE::DirListType fileNames;
381 fileNames = dirList.GetFilenames();
383 char outputDirName[(unsigned int) PATH_MAX+2];
385 // -----------------------------------------------------
386 // Iterate to ALL the objets(files/directories) found in the input directory
387 // -----------------------------------------------------
388 GDCM_NAME_SPACE::DirListType::iterator it;
392 for (it = fileNames.begin();
393 it != fileNames.end();
396 if ( ! boost::filesystem::is_directory(*it) )
398 std::cout << "--- --- [" << *it << "] is a file.." << std::endl;
402 for (it = fileNames.begin();
403 it != fileNames.end();
406 if ( boost::filesystem::is_directory(*it) )
410 std::cout << "--- --- [" << *it << "] is a directory" << std::endl;
412 // sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
413 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
414 // GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
418 // (interest of previous method :
419 // If unaware user changed the pdata name, it goes on working
423 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
426 std::string str_isa_func_name;
427 canOpen = br_isa.LoadFile(str_isa);
430 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
431 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
432 GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
437 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
439 str_isa_func_name = b_isa_func_name.GetStringValue()[0];
440 cleanString(str_isa_func_name);
442 sprintf(outputDirName, "%s%c%s.%s", currentOutputDirName.c_str(),
443 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
444 GDCM_NAME_SPACE::Util::GetName(*it).c_str(),
445 str_isa_func_name.c_str());
448 DealWithNiveau3(*it, outputDirName);
450 catch (BrukerHopelessException &e)
452 std::cout << "And Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
453 << " We skip [" << level2Directory << "]" << std::endl;
456 catch (BrukerInitException &e)
458 std::cout << "And Init Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
459 << " We skip [" << level2Directory << "]" << std::endl;
467 // =====================================================================
470 void Bruker2Dicom::DealWithNiveau3(std::string level3Directory, std::string currentOutputDirName){
473 // e.g. at level 3, in
475 // just to be able to go on checking // JP
476 if ( GDCM_NAME_SPACE::Util::GetName(level3Directory) != "1")
479 bool res = CreateDirectory(currentOutputDirName);
483 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
484 throw ( BrukerHopelessException ("Hopeless! Level3 output directory creation failure"));
488 GDCM_NAME_SPACE::DirList dirList(level3Directory, false, true); // DON'T get recursively the list of files
489 GDCM_NAME_SPACE::DirListType::iterator it;
490 GDCM_NAME_SPACE::DirListType fileNames;
491 fileNames = dirList.GetFilenames();
493 char original2dseqName [(unsigned int) PATH_MAX+2];
494 char original2dseqName_XXX [(unsigned int) PATH_MAX+2];
495 char currentOutputMhdDirName [(unsigned int) PATH_MAX+2];
497 char outputMhdFileName [(unsigned int) PATH_MAX+2];
498 char output2dseqSliceFileName[(unsigned int) PATH_MAX+6]; // think about extra '.dcm'
499 char output2dseqName [(unsigned int) PATH_MAX+6];
500 char output2dseqCartoName [(unsigned int) PATH_MAX+6];
502 char copyFile[PATH_MAX + PATH_MAX + 5]; // Should be enough!
505 //-------------- try d3proc;
506 char char_d3proc[(unsigned int) PATH_MAX+2];
508 sprintf(char_d3proc,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"d3proc" );
511 std::cout << "d3proc: --- => [" << char_d3proc << "]" << std::endl;
512 std::string str_d3proc(char_d3proc);
513 canOpen = br_d3proc.LoadFile(str_d3proc);
517 std::cout << "Hopeless! no 'd3proc' found" << std::endl;
518 throw ( BrukerHopelessException ("Hopeless! no 'd3proc' found"));
519 //exit(0); /// \TODO throw an exception !
522 canOpen = br_d3proc.FillMap();
525 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
526 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'd3proc'"));
527 //exit(0); /// \TODO throw an exception !
530 //-------------- end try d3proc;
533 // -------------------try reco
535 char char_reco[(unsigned int) PATH_MAX+2];
537 sprintf(char_reco,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
538 //str_d3proc = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
539 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
542 std::cout << "reco: --- => [" << char_reco << "]" << std::endl;
544 std::string str_reco(char_reco);
545 canOpen = br_reco.LoadFile(str_reco);
547 if (!canOpen) // we try in directory ../1
550 std::cout << "[" << str_reco << "] not found " << std::endl;
551 std::string lastDirName = GDCM_NAME_SPACE::Util::GetPath(level3Directory);
552 //lastDirName = GDCM_NAME_SPACE::Util::GetPath(lastDirName);
553 sprintf(char_reco,"%s%c1%c%s", lastDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
554 //str_reco=char_reco;
555 canOpen = br_reco.LoadFile(str_reco);
558 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
559 throw ( BrukerHopelessException ("Hopeless! cannot find 'reco'"));
560 //exit(0); /// \TODO throw an exception !
564 std::cout << "[" << str_reco << "] successfully Loaded " << std::endl;
567 canOpen = br_reco.FillMap();
570 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
571 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'reco'"));
572 //exit(0); /// \TODO throw an exception !
575 std::cout << "[" << str_reco << "] successfully Mapped" << std::endl;
578 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
579 // br_reco.PrintSelf();
580 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
581 // -------------------end try reco
584 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
585 int NX = bX.GetIntValue()[0];
588 std::cout << "IM_SIX " << NX << std::endl;
589 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
590 int NY = bY.GetIntValue()[0];
593 std::cout << "IM_SIY " << NY << std::endl;
594 /// \todo : check if there are actually 3 dimensions or only 2
596 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
597 int nbFrames = bZ.GetIntValue()[0];
599 std::cout << "IM_SIZ " << nbFrames << std::endl;
601 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
603 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
605 std::string mhdDataPixelType;
607 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
613 // En regle generale il vaut mieux que l'on passe par RECO_*
614 // pour extraire les parametres de l'image
617 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
618 double fovX = fov.GetDoubleValue()[0];
619 double fovY = fov.GetDoubleValue()[1];
621 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
623 BrukerFieldData spatResol = br_method.GetFieldData("PVM_SpatResol");
624 double spatResolX = spatResol.GetDoubleValue()[0];
625 double spatResolY = spatResol.GetDoubleValue()[1];
627 std::cout << "SpatResol (ds method) " << spatResolX << " " << spatResolY << std::endl;
632 // Better we use 'get' accessors from BrukerImage class, as Denis wrote them
634 BrukerFieldData fov = br_reco.GetFieldData("RECO_fov");
635 double fovX = fov.GetDoubleValue()[0];
636 double fovY = fov.GetDoubleValue()[1];
638 std::cout << "FOV (ds reco) " << fovX << " " << fovY << std::endl;
640 BrukerFieldData size = br_reco.GetFieldData("RECO_size");
641 double sizeX = size.GetDoubleValue()[0];
642 double sizeY = size.GetDoubleValue()[1];
645 std::cout << "SIZE (ds reco) " << sizeX << " " << sizeY << std::endl;
647 double spatResolX = fovX / sizeX;
648 double spatResolY = fovY / sizeY;
651 std::cout << "spatResol (ds reco : fov/size) " << spatResolX << " " << spatResolY << std::endl;
655 /// \TODO probabely a more sophisticated accessor will be necessary :
656 /// (cf : non contiguous slices, overlapping, slice thickness, space between slices, etc)
658 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
659 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
662 std::cout << "SPackArrSliceDistance (ds method) " << sliceDistance << std::endl;
664 // ----------------------------------------------------------------------------------------
668 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
669 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "MhdFiles");
670 res = CreateDirectory( currentOutputMhdDirName );
672 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
673 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
678 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
682 std::cout << "nbFrames " << nbFrames << std::endl;
684 std::cout << "nbSlices " << nbSlices << std::endl;
686 int nbInstants = nbFrames/nbSlices;
688 std::cout << "nbInstants (deduced)" << nbInstants << std::endl;
691 FILE *fp; // for MHD files
693 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "2dseq");
696 // \TODO : tenir compte du bazar precedent
698 // load 2dseq in memory
700 fp = fopen(original2dseqName, "rb");
703 // try 2dseq_Angio2D ?!?
705 sprintf( original2dseqName_XXX, "%s%s", original2dseqName, "_Angio2D");
706 fp = fopen(original2dseqName_XXX, "rb");
709 std::cout << "Cannot open [" << original2dseqName << "] nor [" << original2dseqName_XXX << "] for reading" << std::endl;
711 throw ( BrukerHopelessException ("Hopeless! Cannot open '2dseq'"));
716 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
717 ///\ TODO : find a safer way to be sure to read everything!
718 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
720 // This one will be important!
721 // ---------------------------
723 imageSet = CreateImageSet ( );
725 catch (BrukerInitException& e)
728 std::cout << "an Init Exception was thrown in CreateImageSet ( ); " << e.what()
729 << "catched in DealWithNiveau3" << std::endl;
735 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
736 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
739 std::cout << "Single instant : do not split" << std::endl;
742 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
743 GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
744 fp=fopen(outputMhdFileName, "w");
747 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
748 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
753 fprintf(fp, "ObjectType = Image\n");
754 fprintf(fp, "NDims = 3\n" );
755 fprintf(fp, "BinaryData = True \n" );
756 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
757 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
758 fprintf(fp, "HeaderSize = %d\n", 0);
759 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
760 fprintf(fp, "ElementSpacing = %lf %lf %lf\n", spatResolX, spatResolY, sliceDistance );
761 fprintf(fp, "Position = 0 0 %d\n", 0 );
762 fprintf(fp, "Offset = 0 0 0\n" );
763 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
764 fprintf(fp, "ElementNumberOfChannels = 1\n" );
765 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
766 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
769 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
770 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
771 fp=fopen(output2dseqSliceFileName, "wb");
774 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
775 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseq file for writting"));
779 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
783 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
787 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
788 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
790 /* ----------- Write Dicom Image ---------------*/
791 MakeDicomImage(buffer_2dseq,
796 //fovX/NY, fovY/NY, sliceDistance,
797 spatResolX, spatResolY, sliceDistance,
798 output2dseqSliceFileName,
806 0,// index frame number
807 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
810 } // end if nbInstants = 1
812 else // more than ONE instant
815 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
816 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
819 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
823 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
824 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, k );
826 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
827 fp=fopen(outputMhdFileName, "w");
830 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
831 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
836 /* ----------- Write MHD Image ---------------*/
838 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
839 fprintf(fp, "ObjectType = Image\n");
840 fprintf(fp, "NDims = 3\n" );
841 fprintf(fp, "BinaryData = True \n" );
842 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
843 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
844 fprintf(fp, "HeaderSize = %d\n", 0);
845 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); //
846 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, 1.0 ); //slice distance : no meaning for temporal serie
847 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
848 fprintf(fp, "Offset = 0 0 0\n" );
849 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
850 fprintf(fp, "ElementNumberOfChannels = 1\n" );
851 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
852 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb );
856 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
857 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,sliceNb);
858 fp=fopen(output2dseqSliceFileName, "wb");
861 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
862 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseqSliceFile file for writting"));
865 int frameSize = NX*NY*pixelSize;
866 for (instantNb=0; instantNb<nbInstants; instantNb++)
868 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
869 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
874 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
881 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
882 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
883 fp=fopen(output2dseqSliceFileName, "wb");
886 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
889 int frameSize = NX*NY*pixelSize;
890 for (instantNb=0; instantNb<nbInstants; instantNb++)
892 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
893 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
899 fp=fopen(output2dseqSliceFileName, "rb");
902 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
905 fread( pixelsForCurrentSlice,
906 frameSize*nbInstants,
909 // end of desperate try !
912 /* ----------- Write Dicom Image ---------------*/
914 int frameSize = NX*NY*pixelSize;
915 for (instantNb=0; instantNb<nbInstants; instantNb++)
917 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
920 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
921 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
922 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb);
925 pixelsForCurrentSlice,
930 spatResolX, spatResolY, sliceDistance,
931 //fovX/NY, fovY/NY, sliceDistance,
932 output2dseqSliceFileName,
941 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
944 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
950 delete [] pixelsForCurrentSlice;
951 } // end nbInstants == 1
952 delete [] buffer_2dseq;
956 // -----------------------------------------------------
957 // deal with MatLab-generated Carto file.
958 // -----------------------------------------------------
960 dealWithCarto(fileNames, NX, NY, nbSlices, /*fovX, fovY,*/ spatResolX, spatResolY, sliceDistance,
961 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
965 // ===========================================================================================
967 void Bruker2Dicom::dealWithCarto(GDCM_NAME_SPACE::DirListType &fileNames, int NX, int NY, int nbSlices,
968 /*double fovX, double fovY, */
969 double spatResolX, double spatResolY, double sliceDistance,
970 char *copyFile, std::string ¤tOutputDirName,
971 char *outputMhdFileName, char *output2dseqCartoName)
973 // -----------------------------------------------------
974 // deal with MatLab-generated Carto file.
975 // -----------------------------------------------------
977 const char *code[] = { "ADC", "adc", "TTP", "ttp", "PEAK", "peak", "" }; // add more carto file name identifiers if necessary; end with ""
978 const char *separator[] = { "_", ".", "-", "" }; // add more, if necessary, to ckeck for 2dseq.ADC, 2dseq_ADC, 2dseq-ADC, etc; end with ""
981 GDCM_NAME_SPACE::DirListType::iterator it;
982 char file_name_ident[500];
985 // Iterate to ALL the objets(files/directories) found in the input directory
986 for (it = fileNames.begin();
987 it != fileNames.end();
990 if ( boost::filesystem::is_regular(*it) )
991 //if ( ! boost::filesystem::is_directory(*it) )
994 std::cout << "--- [" << *it << "] is a file..." << std::endl;
997 while (code[icode][0] != 0)
1000 while (separator[iseparator][0] != 0)
1002 sprintf(file_name_ident, "2dseq%s%s",separator[iseparator],code[icode]); // e.g "2dseq_ADC"
1004 // std::cout << "check name ["<<(*it) << "] for string [" << file_name_ident << "]" << std::endl;
1005 std::string::size_type loc = (*it).rfind(file_name_ident);
1007 if ( loc != std::string::npos )
1010 ///\ TODO : find a safer way to be sure to read everything!
1011 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
1012 fp = fopen ( (*it).c_str(), "rb");
1014 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
1015 throw ( BrukerHopelessException ("Level 1 Unable to open 'carto' file "));
1017 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
1019 // ?!? sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1020 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
1021 << NX*NY*sizeof(double)*nbSlices << std::endl;
1022 std::string lastFileName = GDCM_NAME_SPACE::Util::GetName((*it).c_str());
1025 // Copy the data file in the new directory
1026 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
1027 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str());
1029 sprintf(outputMhdFileName, "%s%c%s%s",
1030 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".mhd" );
1032 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1035 fp=fopen(outputMhdFileName, "w");
1038 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
1042 fprintf(fp, "ObjectType = Image\n");
1043 fprintf(fp, "NDims = 3\n" );
1044 fprintf(fp, "BinaryData = True \n" );
1045 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
1046 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
1047 fprintf(fp, "HeaderSize = %d\n", 0 );
1048 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, sliceDistance );
1049 fprintf(fp, "Position = 0 0 0\n" );
1050 fprintf(fp, "Offset = 0 0 0\n" );
1051 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
1052 fprintf(fp, "ElementNumberOfChannels = 1\n" );
1053 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
1054 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
1059 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1062 // ----------- Write Dicom Image ---------------
1066 sprintf(output2dseqCartoName, "%s%c%s%s",
1067 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".dcm" );
1069 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
1071 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID(); //New SerieUID for each carto.
1072 std::string strNewSerieDescr(strSerieDescr+ "_" +GDCM_NAME_SPACE::Util::GetName((*it).c_str()));
1073 MakeDicomImage(buffer_carto,
1078 //fovX/NY, fovY/NY, sliceDistance,
1079 spatResolX, spatResolY, sliceDistance,
1080 output2dseqCartoName,
1089 GDCM_NAME_SPACE::CREATED_IMAGE
1093 delete [] buffer_carto;
1095 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
1096 break; // don't check for more ident on same file name!
1098 } // end deal with _ADC, -adc, etc
1100 } // end iterate speparators
1102 } // end iterate code
1103 } // end boost::filesystem::is_regular(*it)
1104 } // end iterate on all objects (files, dir, etc)
1108 // ==========================================================================================================
1110 bool Bruker2Dicom::CreateDirectory(std::string OutputDirName)
1112 std::string systemCommand;
1115 std::cout << "Check for output directory :[" << OutputDirName << "]."
1117 if ( ! boost::filesystem::is_directory(OutputDirName) ) // dirout not found
1119 std::string strDirNameout(OutputDirName); // to please gcc 4
1120 systemCommand = "mkdir " + strDirNameout; // create it!
1122 std::cout << systemCommand << std::endl;
1123 system (systemCommand.c_str());
1124 if ( ! boost::filesystem::is_directory(OutputDirName) ) // be sure it worked
1127 std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
1129 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
1134 std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
1140 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
1148 // ===========================================================================================
1150 /// \TODO move cleanString to 'crea' ?
1152 void Bruker2Dicom::cleanString(std::string &s)
1155 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
1160 if (s[l-1] == ' ' ) // blank space
1168 std::string repChar("_");
1169 GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
1174 // ===========================================================================================
1177 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
1179 if(bDPT.GetDataType() == "string")
1181 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1183 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1184 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1186 if (brukerDataPixelType == "ip_short") {
1187 mhdDataPixelType = "MET_USHORT";
1190 if (brukerDataPixelType == "ip_int") {
1191 mhdDataPixelType = "MET_UINT";
1194 if (brukerDataPixelType == "ip_char") {
1195 mhdDataPixelType = "MET_UCHAR";
1198 /// \TODO : finish the list
1200 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1202 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1204 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1206 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1208 case 4 : fp << "ElementType = MET_INT" << std::endl;
1210 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1212 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1214 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1219 int brukerDataPixelType = bDPT.GetIntValue()[0];
1221 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1222 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1224 // Cross your fingers !!!
1226 // pb : found values : 2, 3, 5
1228 if (brukerDataPixelType == 2) {
1229 mhdDataPixelType = "MET_USHORT";
1232 if (brukerDataPixelType == 3) {
1233 mhdDataPixelType = "MET_USHORT";
1236 if (brukerDataPixelType == 1) {
1237 mhdDataPixelType = "MET_UCHAR";
1243 // ===========================================================================================
1245 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1247 std::vector<BrukerImage> imageSet;
1248 br_acqp.SetLoopStructure();
1249 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1250 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1252 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1254 if (result == false)
1256 throw ( BrukerInitException ("ObjectVaryingProperties.init() failure in Bruker2Dicom::CreateImageSet()") );
1259 br_acqp.SetImageLoopStructure();
1260 br_acqp.SetBrukerImageList();
1261 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1263 BrukerImage image(br_acqp,br_reco);
1264 image.Init(br_acqp,br_reco,1);
1266 for(int i=0;i<brukerImageList.size();i++)
1268 image.Init(br_acqp,br_reco,i);
1269 imageSet.push_back(image);
1272 // Just for checking
1275 std::vector<std::vector <double> > imageOrientation;
1276 std::vector <double> imagePosition;
1277 for(int i=0;i<brukerImageList.size();i++)
1279 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1281 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1282 std::cout << "Position " << imagePosition[0] << " "
1283 << imagePosition[1] << " " << imagePosition[2] ;
1284 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1285 std::cout << "\t Orientation " ;
1286 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1287 std::cout << imageOrientation[i1][i2] << " ";CreateImageSet
1289 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1290 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1292 std::cout << "\t [";
1293 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1294 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1295 std::cout << "]" << std::endl;
1301 // ===========================================================================================
1303 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1308 double spacingX, double spacingY, double sliceDistance,
1309 std::string dcmImageName,
1310 const std::string &patientName,
1312 std::string &studyUID,
1313 std::string &serieUID,
1314 std::string &studyDescr,
1315 std::string &serieDescr,
1316 std::string &strStudyTimeDate,
1318 GDCM_NAME_SPACE::ImageContentType contentType
1321 std::ostringstream str;
1323 GDCM_NAME_SPACE::File *file;
1324 file = GDCM_NAME_SPACE::File::New();
1326 // Set the image size
1329 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1332 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1338 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1341 // Set the pixel type
1342 // //8, 16, 32, 64 (for double ?)
1345 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1347 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1350 str << pixelSize*8-1;
1351 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1353 // Set the pixel representation // 0/1 , 0=unsigned
1354 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1356 // Set the samples per pixel // 1:Grey level, 3:RGB
1357 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1359 // 0028 0030 DS 2 Pixel Spacing
1361 str << spacingX << "\\" << spacingY;
1362 file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1364 // 0018 0050 DS 1 Slice Thickness
1366 str << sliceDistance;
1367 file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1369 // 0020 0011 IS 1 Series Number
1372 file->InsertEntryString(str.str(),0x0020,0x0011, "IS");
1374 // 0020|0013 [IS] [Instance Number]
1377 str << instanceNumber;
1378 file->InsertEntryString(str.str(),0x0020,0x0013, "IS");
1381 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1382 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1383 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1386 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1388 // 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1389 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1390 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1392 // if (strlen(patientName) != 0)
1393 file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1395 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1396 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1398 // 0008 0020 DA 1 Study Date
1399 // 0008 0030 TM 1 Study Time
1401 /// \TODO split into 2 strings!
1402 file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1403 file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(), 0x0008,0x0030, "TM");
1405 file->InsertEntryString(studyDescr, 0x0008,0x1030, "LO"); // Study Description
1406 file->InsertEntryString(serieDescr, 0x0008,0x103e, "LO"); // Series Description
1408 //0008|0060 [CS] [Modality]
1409 file->InsertEntryString("MR",0x0008,0x0060, "CS");
1411 // 0020 0037 DS 6 Image Orientation (Patient)
1412 char charImageOrientation[256];
1415 std::cout << "charImageOrientation " <<
1416 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1417 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1418 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1419 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1420 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1421 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1423 sprintf(charImageOrientation,"%f\\%f\\%f \\ %f\\%f\\%f",
1424 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0],
1425 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1],
1426 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2],
1427 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0],
1428 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1],
1429 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] ) ;
1431 file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1434 // 0020 0032 DS 3 Image Position (Patient)
1436 char charImagePosition[256];
1437 sprintf(charImagePosition,"%f\\%f\\%f",
1438 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1439 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1440 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1442 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1446 // 0020 0x1041 DS 1 Slice Location
1447 // sprintf(charImagePosition,"%f",float(imgNum));
1448 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1450 // Set Rescale Intercept
1453 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1455 // Set Rescale Slope
1458 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1461 GDCM_NAME_SPACE::FileHelper *fileH;
1462 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1463 fileH->SetContentType(contentType);
1465 // cast is just to avoid warnings (*no* conversion is performed)
1466 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1468 //std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1470 fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1471 fileH->SetWriteModeToRaw();
1472 fileH->SetWriteTypeToDcmExplVR();
1473 if( !fileH->Write(dcmImageName)) {
1474 std::cout << "Failed for [" << dcmImageName << "]\n"
1475 << " File is unwrittable" << std::endl;
1478 throw ( BrukerHopelessException ("Level 1 Unable to write Dicom file "));