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
12 bool Bruker2Dicom::Execute()
14 // ----- Check input directory name -----
16 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
18 //if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(InputDirName) )
19 if ( ! boost::filesystem::is_directory(InputDirName) )
21 std::cout << "KO : [" << InputDirName << "] is not a Directory." << std::endl;
27 std::cout << "OK : [" << InputDirName << "] is a Directory." << std::endl;
30 // ----- Check output directory name -----
32 std::string strDirNameOut(OutputDirName);
33 bool res=CreateDirectory(strDirNameOut);
35 std::cout << "[" << OutputDirName << "] Directory creation failure " << std::endl;
37 throw ( BrukerHopelessException ("Output directory creation failure "));
40 std::string strDirNamein(InputDirName);
41 GDCM_NAME_SPACE::DirList dirList(strDirNamein, false, true); // DON'T get recursively the list of files
42 std::string strDirNameout(OutputDirName);
47 std::cout << "------------List of found files ------------" << std::endl;
49 std::cout << std::endl;
55 // e.g : at level 0, in : B67d1.Bp1
57 // 1 2 3 4 5 6 AdjStatePerStudy subject
60 GDCM_NAME_SPACE::DirListType fileNames;
61 fileNames = dirList.GetFilenames();
63 std::string outputFileName;
66 // BrukerDataSet br_subject;
68 subject = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
69 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
72 std::cout << " Subject : [" << subject << "]" << std::endl;
75 canOpen =br_subject.LoadFile(subject);
78 //std::cout << "Hopeless! no 'subject' found" << std::endl;
79 //throw ( BrukerHopelessException ("Hopeless! no 'subject' found in root input directory "));
81 std::cout << "Not too much hope! no 'subject' found (Desperate try will be performed -default values- )" << std::endl;
89 //br_subject.PrintSelf();
91 // get info for 'Study Description'
95 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
96 subject_name = b_name.GetStringValue()[0];
97 strPatientName = subject_name;
98 cleanString(subject_name);
101 subject_name ="defaultPatName";
103 // creation directory : 'nom du patient'
104 std::string tempStringPatDir(OutputDirName);
105 tempStringPatDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + subject_name;
107 res=CreateDirectory(tempStringPatDir);
109 std::cout << "[" << tempStringPatDir << "] Directory creation failure " << std::endl;
110 throw ( BrukerHopelessException ("Patient directory creation failure "));
113 std::string subject_entry;
114 std::string subject_position;
115 std::string subject_date;
116 std::string subject_study_name;
120 BrukerFieldData b_entry=br_subject.GetFieldData("SUBJECT_entry");
121 subject_entry = b_entry.GetStringValue()[0];
122 //cleanString(subject_entry);
123 subject_entry = subject_entry.substr(11, subject_entry.size()-11);
125 BrukerFieldData b_position=br_subject.GetFieldData("SUBJECT_position");
126 subject_position = b_position.GetStringValue()[0];
127 //cleanString(subject_position);
128 subject_position = subject_position.substr(9, subject_position.size()-9);
130 BrukerFieldData b_date=br_subject.GetFieldData("SUBJECT_date");
131 subject_date = b_date.GetStringValue()[0];
132 strStudyTimeDate = subject_date;
133 cleanString(subject_date);
135 BrukerFieldData b_study_name=br_subject.GetFieldData("SUBJECT_study_name");
136 subject_study_name = b_study_name.GetStringValue()[0];
137 subject_study_name = subject_study_name.substr(1, subject_study_name.size()-2);
138 cleanString(subject_study_name);
140 else // Desperate trick when file 'subject' is missing
142 subject_entry = "HeadFirst"; // Why not?
143 subject_position = "Supine"; // Why not?
144 strStudyTimeDate = "06_06_06_6_June_1666"; // Why not?
145 subject_date = "6_June_1666"; // Why not?
146 subject_study_name = "defStudyName"; // Why not?
148 // subject_name is already in 'Patient Name'
149 strStudyDescr = /*subject_name + "." + */ subject_study_name + "." + subject_entry + "." + subject_position + "." + subject_date;
151 // creation directory : 'nom de la Study'
153 std::string tempStringStudyDir(OutputDirName);
154 tempStringStudyDir = tempStringPatDir + GDCM_NAME_SPACE::GDCM_FILESEPARATOR + strStudyDescr;
156 res=CreateDirectory(tempStringStudyDir);
158 std::cout << "[" << tempStringStudyDir << "] Directory creation failure " << std::endl;
159 throw ( BrukerHopelessException ("Study directory creation failure "));
162 char outputDirName[(unsigned int) PATH_MAX+2];
164 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
168 // -----------------------------------------------------
169 // Iterate to ALL the objets(files/directories) found in the input directory
170 // (this is level ZERO)
171 // each Directory (name : 1, 2, 3, ...) will be a Dicom Serie
172 // -----------------------------------------------------
174 GDCM_NAME_SPACE::DirListType::iterator it;
176 for (it = fileNames.begin();
177 it != fileNames.end();
180 if ( boost::filesystem::is_directory(*it) )
183 std::cout << "[" << *it << "] is a directory" << std::endl;
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 << " ================================================================================"
238 printf ("outputDirName [%s]\n", outputDirName);
240 DealWithNiveau1(*it, outputDirName);
242 catch (BrukerHopelessException &e)
244 std::cout << "And Exception was thrown in DealWithNiveau1 (" << e.what() << ") " << std::endl;
248 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
251 // =====================================================================
253 void Bruker2Dicom::DealWithNiveau1(std::string level1Directory, std::string currentOutputDirName) {
255 // e.g. : at level 1, in B67d1.Bp1/6
257 // acqp fid imnd pdata pulseprogram spnam0 spnam1
259 bool res = CreateDirectory(currentOutputDirName);
262 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
263 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
266 GDCM_NAME_SPACE::DirList dirList(level1Directory, false, true); // DON'T get recursively the list of files
267 GDCM_NAME_SPACE::DirListType fileNames;
268 fileNames = dirList.GetFilenames();
269 // -----------------------------------------------------
270 // Iterate to ALL the objets(files/directories) found in the input directory
271 // -----------------------------------------------------
272 GDCM_NAME_SPACE::DirListType::iterator it;
274 for (it = fileNames.begin();
275 it != fileNames.end();
278 if ( boost::filesystem::is_regular(*it) )
279 //if ( ! boost::filesystem::is_directory(*it) )
282 std::cout << "--- [" << *it << "] is a file." << std::endl;
286 char outputDirName[(unsigned int) PATH_MAX+2];
287 //std::string firstName;
289 for (it = fileNames.begin();
290 it != fileNames.end();
293 if ( boost::filesystem::is_directory(*it) )
295 // will be always "pdata" ...
297 std::cout << "--- [" << *it << "] is a directory" << std::endl;
300 if (FileLine.startsWith("##$ACQ_size=")) {
301 if (FileLine.startsWith("##$NI=")) {
302 if (FileLine.startsWith("##$NR=")) {
303 if (FileLine.startsWith("##$ACQ_obj_order=")) {
304 if (FileLine.startsWith("##$ACQ_word_size=")) {
305 if (FileLine.startsWith("##$BYTORDA=")) {
306 if (FileLine.startsWith("##$PULPROG=")) {
309 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
310 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
311 GDCM_NAME_SPACE::Util::GetName(*it).c_str());
314 std::string strMethod;
315 //std::string firstName = *(fileNames.begin());
317 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin())) +
318 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
320 // std::cout << "---strMethod (for method)=> [" << strMethod << "]" << std::endl;
321 canOpen = br_method.LoadFile(strMethod);
324 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
325 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
327 //std::cout << "---strMethod (for imnd) => [" << strMethod << "]" << std::endl;
328 canOpen = br_method.LoadFile(strMethod);
331 std::cout << "Hopeless! neither 'method' nor 'imnd' found in ["
332 << level1Directory << "]; we skip it!" << std::endl;
334 //throw ( BrukerHopelessException ("Hopeless! neither 'method' nor 'imnd' found "));
335 //exit(0); /// \TODO throw an exception !
339 std::cout << "open => [" << strMethod << "] successfully" << std::endl;
343 ##$PVM_Fov (dimension)
346 dans method (pour perfusion seulement?) :
347 ##$PVM_ObjOrderList=( 8 )
350 ##$PVM_SPackArrNSlices=( 2 )
354 DealWithNiveau2(*it, outputDirName);
356 catch (BrukerHopelessException &e)
358 std::cout << "And Exception was thrown in DealWithNiveau2 (" << e.what() << ") " << std::endl;
365 // =====================================================================
367 void Bruker2Dicom::DealWithNiveau2(std::string level2Directory, std::string currentOutputDirName) {
369 // e.g. : at level 2 in B67d1.Bp1/6/pdata
371 // acqp fid imnd pdata pulseprogram spnam0 spnam1
374 bool res = CreateDirectory(currentOutputDirName);
377 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
378 throw ( BrukerHopelessException ("Hopeless! Level2 output directory creation failure"));
382 GDCM_NAME_SPACE::DirList dirList(level2Directory, false, true); // DON'T get recursively the list of files
384 GDCM_NAME_SPACE::DirListType fileNames;
385 fileNames = dirList.GetFilenames();
387 char outputDirName[(unsigned int) PATH_MAX+2];
389 // -----------------------------------------------------
390 // Iterate to ALL the objets(files/directories) found in the input directory
391 // -----------------------------------------------------
392 GDCM_NAME_SPACE::DirListType::iterator it;
396 for (it = fileNames.begin();
397 it != fileNames.end();
400 if ( ! boost::filesystem::is_directory(*it) )
402 std::cout << "--- --- [" << *it << "] is a file.." << std::endl;
407 for (it = fileNames.begin();
408 it != fileNames.end();
411 if ( boost::filesystem::is_directory(*it) )
415 std::cout << "--- --- [" << *it << "] is a directory" << std::endl;
417 // sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
418 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
419 // GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
423 // (interest of previous method :
424 // If unaware user changed the pdata name, it goes on working
428 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
431 std::string str_isa_func_name;
432 canOpen = br_isa.LoadFile(str_isa);
435 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
436 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
437 GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
442 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
444 str_isa_func_name = b_isa_func_name.GetStringValue()[0];
445 cleanString(str_isa_func_name);
447 sprintf(outputDirName, "%s%c%s.%s", currentOutputDirName.c_str(),
448 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
449 GDCM_NAME_SPACE::Util::GetName(*it).c_str(),
450 str_isa_func_name.c_str());
453 DealWithNiveau3(*it, outputDirName);
455 catch (BrukerHopelessException &e)
457 std::cout << "And Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
458 << " We skip [" << level2Directory << "]" << std::endl;
461 catch (BrukerInitException &e)
463 std::cout << "And Init Exception was thrown in DealWithNiveau3 (" << e.what() << "); "
464 << " We skip [" << level2Directory << "]" << std::endl;
473 // =====================================================================
476 void Bruker2Dicom::DealWithNiveau3(std::string level3Directory, std::string currentOutputDirName){
479 // e.g. at level 3, in
481 // just to be able to go on checking // JP
482 if ( GDCM_NAME_SPACE::Util::GetName(level3Directory) != "1")
485 bool res = CreateDirectory(currentOutputDirName);
489 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
490 throw ( BrukerHopelessException ("Hopeless! Level3 output directory creation failure"));
494 GDCM_NAME_SPACE::DirList dirList(level3Directory, false, true); // DON'T get recursively the list of files
495 GDCM_NAME_SPACE::DirListType::iterator it;
496 GDCM_NAME_SPACE::DirListType fileNames;
497 fileNames = dirList.GetFilenames();
499 char original2dseqName [(unsigned int) PATH_MAX+2];
500 char original2dseqName_XXX [(unsigned int) PATH_MAX+2];
501 char currentOutputMhdDirName [(unsigned int) PATH_MAX+2];
503 char outputMhdFileName [(unsigned int) PATH_MAX+2];
504 char output2dseqSliceFileName[(unsigned int) PATH_MAX+6]; // think about extra '.dcm'
505 char output2dseqName [(unsigned int) PATH_MAX+6];
506 char output2dseqCartoName [(unsigned int) PATH_MAX+6];
508 char copyFile[PATH_MAX + PATH_MAX + 5]; // Should be enough!
511 //-------------- try d3proc;
512 char char_d3proc[(unsigned int) PATH_MAX+2];
514 sprintf(char_d3proc,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"d3proc" );
517 std::cout << "d3proc: --- => [" << char_d3proc << "]" << std::endl;
518 std::string str_d3proc(char_d3proc);
519 canOpen = br_d3proc.LoadFile(str_d3proc);
523 std::cout << "Hopeless! no 'd3proc' found" << std::endl;
524 throw ( BrukerHopelessException ("Hopeless! no 'd3proc' found"));
525 //exit(0); /// \TODO throw an exception !
528 canOpen = br_d3proc.FillMap();
531 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
532 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'd3proc'"));
533 //exit(0); /// \TODO throw an exception !
536 //-------------- end try d3proc;
539 // -------------------try reco
541 char char_reco[(unsigned int) PATH_MAX+2];
543 sprintf(char_reco,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
544 //str_d3proc = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
545 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
548 std::cout << "reco --- => [" << char_reco << "]" << std::endl;
549 std::string str_reco(char_reco);
550 canOpen = br_reco.LoadFile(str_reco);
552 if (!canOpen) // we try in directory ../1
555 std::cout << "[" << str_reco << "] not found " << std::endl;
556 std::string lastDirName = GDCM_NAME_SPACE::Util::GetPath(level3Directory);
557 //lastDirName = GDCM_NAME_SPACE::Util::GetPath(lastDirName);
558 sprintf(char_reco,"%s%c1%c%s", lastDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
560 canOpen = br_reco.LoadFile(str_reco);
563 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
564 throw ( BrukerHopelessException ("Hopeless! cannot find 'reco'"));
565 //exit(0); /// \TODO throw an exception !
569 canOpen = br_reco.FillMap();
572 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
573 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'reco'"));
574 //exit(0); /// \TODO throw an exception !
576 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
577 // br_reco.PrintSelf();
578 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
579 // -------------------end try reco
582 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
583 int NX = bX.GetIntValue()[0];
586 std::cout << "IM_SIX " << NX << std::endl;
587 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
588 int NY = bY.GetIntValue()[0];
591 std::cout << "IM_SIY " << NY << std::endl;
592 /// \todo : check if there are actually 3 dimensions or only 2
594 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
595 int nbFrames = bZ.GetIntValue()[0];
597 std::cout << "IM_SIZ " << nbFrames << std::endl;
599 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
601 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
603 std::string mhdDataPixelType;
605 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
607 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
608 double fovX = fov.GetDoubleValue()[0];
609 double fovY = fov.GetDoubleValue()[1];
611 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
613 BrukerFieldData spatResol = br_method.GetFieldData("PVM_SpatResol");
614 double spatResolX = spatResol.GetDoubleValue()[0];
615 double spatResolY = spatResol.GetDoubleValue()[1];
617 std::cout << "SpatResol (ds method) " << spatResolX << " " << spatResolY << std::endl;
620 /// \TODO probabely a more sophisticated accessor will be necessary :
621 /// (cf : non contiguous slices, overlapping, slice thickness, space between slices, etc)
622 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
623 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
627 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
628 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "MhdFiles");
629 res = CreateDirectory( currentOutputMhdDirName );
631 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
632 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
637 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
641 std::cout << "nbFrames " << nbFrames << std::endl;
643 std::cout << "nbSlices " << nbSlices << std::endl;
645 int nbInstants = nbFrames/nbSlices;
647 std::cout << "nbInstants (deduced )" << nbInstants << std::endl;
650 FILE *fp; // for MHD files
652 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "2dseq");
655 // \TODO : tenir compte du bazar precedent
657 // load 2dseq in memory
659 fp = fopen(original2dseqName, "rb");
662 // try 2dseq_Angio2D ?!?
664 sprintf( original2dseqName_XXX, "%s%s", original2dseqName, "_Angio2D");
665 fp = fopen(original2dseqName_XXX, "rb");
668 std::cout << "Cannot open [" << original2dseqName << "] nor [" << original2dseqName_XXX << "] for reading" << std::endl;
670 throw ( BrukerHopelessException ("Hopeless! Cannot open '2dseq'"));
675 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
676 ///\ TODO : find a safer way to be sure to read everything!
677 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
679 // This one will be important!
680 // ---------------------------
682 imageSet = CreateImageSet ( );
684 catch (BrukerInitException& e)
687 std::cout << "an Init Exception was thrown in CreateImageSet ( ); " << e.what()
688 << "catched in DealWithNiveau3" << std::endl;
694 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
695 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
698 std::cout << "Single instant : do not split" << std::endl;
701 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
702 GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
703 fp=fopen(outputMhdFileName, "w");
706 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
707 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
712 fprintf(fp, "ObjectType = Image\n");
713 fprintf(fp, "NDims = 3\n" );
714 fprintf(fp, "BinaryData = True \n" );
715 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
716 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
717 fprintf(fp, "HeaderSize = %d\n", 0);
718 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
719 fprintf(fp, "ElementSpacing = %lf %lf %lf\n", spatResolX, spatResolY, sliceDistance );
720 fprintf(fp, "Position = 0 0 %d\n", 0 );
721 fprintf(fp, "Offset = 0 0 0\n" );
722 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
723 fprintf(fp, "ElementNumberOfChannels = 1\n" );
724 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
725 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
728 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
729 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
730 fp=fopen(output2dseqSliceFileName, "wb");
733 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
734 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseq file for writting"));
738 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
742 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
746 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
747 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
749 /* ----------- Write Dicom Image ---------------*/
750 MakeDicomImage(buffer_2dseq,
755 //fovX/NY, fovY/NY, sliceDistance,
756 spatResolX, spatResolY, sliceDistance,
757 output2dseqSliceFileName,
765 0,// index frame number
766 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
769 } // end if nbInstants = 1
771 else // more than ONE instant
774 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
775 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
778 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
782 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
783 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, k );
785 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
786 fp=fopen(outputMhdFileName, "w");
789 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
790 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
795 /* ----------- Write MHD Image ---------------*/
797 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
798 fprintf(fp, "ObjectType = Image\n");
799 fprintf(fp, "NDims = 3\n" );
800 fprintf(fp, "BinaryData = True \n" );
801 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
802 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
803 fprintf(fp, "HeaderSize = %d\n", 0);
804 //fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); //
805 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, 1.0 ); //slice distance : no meaning for temporal serie
806 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
807 fprintf(fp, "Offset = 0 0 0\n" );
808 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
809 fprintf(fp, "ElementNumberOfChannels = 1\n" );
810 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
811 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb );
815 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
816 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,sliceNb);
817 fp=fopen(output2dseqSliceFileName, "wb");
820 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
821 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseqSliceFile file for writting"));
824 int frameSize = NX*NY*pixelSize;
825 for (instantNb=0; instantNb<nbInstants; instantNb++)
827 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
828 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
833 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
840 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
841 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
842 fp=fopen(output2dseqSliceFileName, "wb");
845 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
848 int frameSize = NX*NY*pixelSize;
849 for (instantNb=0; instantNb<nbInstants; instantNb++)
851 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
852 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
858 fp=fopen(output2dseqSliceFileName, "rb");
861 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
864 fread( pixelsForCurrentSlice,
865 frameSize*nbInstants,
868 // end of desperate try !
871 /* ----------- Write Dicom Image ---------------*/
873 int frameSize = NX*NY*pixelSize;
874 for (instantNb=0; instantNb<nbInstants; instantNb++)
876 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
879 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
880 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
881 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb);
884 pixelsForCurrentSlice,
889 spatResolX, spatResolY, sliceDistance,
890 //fovX/NY, fovY/NY, sliceDistance,
891 output2dseqSliceFileName,
900 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
903 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
909 delete [] pixelsForCurrentSlice;
910 } // end nbInstants == 1
911 delete [] buffer_2dseq;
915 // -----------------------------------------------------
916 // deal with MatLab-generated Carto file.
917 // -----------------------------------------------------
919 dealWithCarto(fileNames, NX, NY, nbSlices, /*fovX, fovY,*/ spatResolX, spatResolY, sliceDistance,
920 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
924 // ===========================================================================================
926 void Bruker2Dicom::dealWithCarto(GDCM_NAME_SPACE::DirListType &fileNames, int NX, int NY, int nbSlices,
927 /*double fovX, double fovY, */
928 double spatResolX, double spatResolY, double sliceDistance,
929 char *copyFile, std::string ¤tOutputDirName,
930 char *outputMhdFileName, char *output2dseqCartoName)
932 // -----------------------------------------------------
933 // deal with MatLab-generated Carto file.
934 // -----------------------------------------------------
936 char *code[] = { "ADC", "adc", "TTP", "ttp", "PEAK", "peak", "" }; // add more carto file name identifiers if necessary; end with ""
937 char *separator[] = { "_", ".", "-", "" }; // add more, if necessary, to ckeck for 2dseq.ADC, 2dseq_ADC, 2dseq-ADC, etc; end with ""
940 GDCM_NAME_SPACE::DirListType::iterator it;
941 char file_name_ident[500];
944 // Iterate to ALL the objets(files/directories) found in the input directory
945 for (it = fileNames.begin();
946 it != fileNames.end();
949 if ( boost::filesystem::is_regular(*it) )
950 //if ( ! boost::filesystem::is_directory(*it) )
953 std::cout << "--- [" << *it << "] is a file..." << std::endl;
956 while (code[icode][0] != 0)
959 while (separator[iseparator][0] != 0)
961 sprintf(file_name_ident, "2dseq%s%s",separator[iseparator],code[icode]); // e.g "2dseq_ADC"
963 // std::cout << "check name ["<<(*it) << "] for string [" << file_name_ident << "]" << std::endl;
964 std::string::size_type loc = (*it).rfind(file_name_ident);
966 if ( loc != std::string::npos )
969 ///\ TODO : find a safer way to be sure to read everything!
970 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
971 fp = fopen ( (*it).c_str(), "rb");
973 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
974 throw ( BrukerHopelessException ("Level 1 Unable to open 'carto' file "));
976 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
978 // ?!? sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
979 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
980 << NX*NY*sizeof(double)*nbSlices << std::endl;
981 std::string lastFileName = GDCM_NAME_SPACE::Util::GetName((*it).c_str());
984 // Copy the data file in the new directory
985 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
986 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str());
988 sprintf(outputMhdFileName, "%s%c%s%s",
989 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".mhd" );
991 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
994 fp=fopen(outputMhdFileName, "w");
997 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
1001 fprintf(fp, "ObjectType = Image\n");
1002 fprintf(fp, "NDims = 3\n" );
1003 fprintf(fp, "BinaryData = True \n" );
1004 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
1005 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
1006 fprintf(fp, "HeaderSize = %d\n", 0 );
1007 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",spatResolX, spatResolY, sliceDistance );
1008 fprintf(fp, "Position = 0 0 0\n" );
1009 fprintf(fp, "Offset = 0 0 0\n" );
1010 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
1011 fprintf(fp, "ElementNumberOfChannels = 1\n" );
1012 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
1013 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
1018 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
1021 // ----------- Write Dicom Image ---------------
1025 sprintf(output2dseqCartoName, "%s%c%s%s",
1026 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".dcm" );
1028 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
1030 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID(); //New SerieUID for each carto.
1031 std::string strNewSerieDescr(strSerieDescr+ "_" +GDCM_NAME_SPACE::Util::GetName((*it).c_str()));
1032 MakeDicomImage(buffer_carto,
1037 //fovX/NY, fovY/NY, sliceDistance,
1038 spatResolX, spatResolY, sliceDistance,
1039 output2dseqCartoName,
1048 GDCM_NAME_SPACE::CREATED_IMAGE
1052 delete [] buffer_carto;
1054 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
1055 break; // don't check for more ident on same file name!
1057 } // end deal with _ADC, -adc, etc
1059 } // end iterate speparators
1061 } // end iterate code
1062 } // end boost::filesystem::is_regular(*it)
1063 } // end iterate on all objects (files, dir, etc)
1067 // ==========================================================================================================
1069 bool Bruker2Dicom::CreateDirectory(std::string OutputDirName)
1071 std::string systemCommand;
1074 std::cout << "Check for output directory :[" << OutputDirName << "]."
1076 if ( ! boost::filesystem::is_directory(OutputDirName) ) // dirout not found
1078 std::string strDirNameout(OutputDirName); // to please gcc 4
1079 systemCommand = "mkdir " + strDirNameout; // create it!
1081 std::cout << systemCommand << std::endl;
1082 system (systemCommand.c_str());
1083 if ( ! boost::filesystem::is_directory(OutputDirName) ) // be sure it worked
1086 std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
1088 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
1093 std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
1099 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
1107 // ===========================================================================================
1109 /// \TODO move cleanString to 'crea' ?
1111 void Bruker2Dicom::cleanString(std::string &s)
1114 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
1119 if (s[l-1] == ' ' ) // blank space
1127 std::string repChar("_");
1128 GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
1133 // ===========================================================================================
1136 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
1138 if(bDPT.GetDataType() == "string")
1140 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1142 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1143 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1145 if (brukerDataPixelType == "ip_short") {
1146 mhdDataPixelType = "MET_USHORT";
1149 if (brukerDataPixelType == "ip_int") {
1150 mhdDataPixelType = "MET_UINT";
1153 if (brukerDataPixelType == "ip_char") {
1154 mhdDataPixelType = "MET_UCHAR";
1157 /// \TODO : finish the list
1159 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1161 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1163 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1165 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1167 case 4 : fp << "ElementType = MET_INT" << std::endl;
1169 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1171 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1173 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1178 int brukerDataPixelType = bDPT.GetIntValue()[0];
1180 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1181 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1183 // Cross your fingers !!!
1185 // pb : found values : 2, 3, 5
1187 if (brukerDataPixelType == 2) {
1188 mhdDataPixelType = "MET_USHORT";
1191 if (brukerDataPixelType == 3) {
1192 mhdDataPixelType = "MET_USHORT";
1195 if (brukerDataPixelType == 1) {
1196 mhdDataPixelType = "MET_UCHAR";
1202 // ===========================================================================================
1204 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1206 std::vector<BrukerImage> imageSet;
1207 br_acqp.SetLoopStructure();
1208 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1209 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1211 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1213 if (result == false)
1215 throw ( BrukerInitException ("ObjectVaryingProperties.init() failure in Bruker2Dicom::CreateImageSet()") );
1218 br_acqp.SetImageLoopStructure();
1219 br_acqp.SetBrukerImageList();
1220 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1222 BrukerImage image(br_acqp,br_reco);
1223 image.Init(br_acqp,br_reco,1);
1225 for(int i=0;i<brukerImageList.size();i++)
1227 image.Init(br_acqp,br_reco,i);
1228 imageSet.push_back(image);
1231 // Just for checking
1234 std::vector<std::vector <double> > imageOrientation;
1235 std::vector <double> imagePosition;
1236 for(int i=0;i<brukerImageList.size();i++)
1238 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1240 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1241 std::cout << "Position " << imagePosition[0] << " "
1242 << imagePosition[1] << " " << imagePosition[2] ;
1243 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1244 std::cout << "\t Orientation " ;
1245 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1246 std::cout << imageOrientation[i1][i2] << " ";CreateImageSet
1248 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1249 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1251 std::cout << "\t [";
1252 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1253 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1254 std::cout << "]" << std::endl;
1260 // ===========================================================================================
1262 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1267 double spacingX, double spacingY, double sliceDistance,
1268 std::string dcmImageName,
1269 const std::string &patientName,
1271 std::string &studyUID,
1272 std::string &serieUID,
1273 std::string &studyDescr,
1274 std::string &serieDescr,
1275 std::string &strStudyTimeDate,
1277 GDCM_NAME_SPACE::ImageContentType contentType
1280 std::ostringstream str;
1282 GDCM_NAME_SPACE::File *file;
1283 file = GDCM_NAME_SPACE::File::New();
1285 // Set the image size
1288 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1291 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1297 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1300 // Set the pixel type
1301 // //8, 16, 32, 64 (for double ?)
1304 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1306 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1309 str << pixelSize*8-1;
1310 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1312 // Set the pixel representation // 0/1 , 0=unsigned
1313 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1315 // Set the samples per pixel // 1:Grey level, 3:RGB
1316 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1318 // 0028 0030 DS 2 Pixel Spacing
1320 str << spacingX << "\\" << spacingY;
1321 file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1323 // 0018 0050 DS 1 Slice Thickness
1325 str << sliceDistance;
1326 file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1328 // 0020 0011 IS 1 Series Number
1331 file->InsertEntryString(str.str(),0x0020,0x0011, "IS");
1333 // 0020|0013 [IS] [Instance Number]
1336 str << instanceNumber;
1337 file->InsertEntryString(str.str(),0x0020,0x0013, "IS");
1340 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1341 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1342 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1345 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1347 // 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1348 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1349 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1351 // if (strlen(patientName) != 0)
1352 file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1354 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1355 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1357 // 0008 0020 DA 1 Study Date
1358 // 0008 0030 TM 1 Study Time
1360 /// \TODO split into 2 strings!
1361 file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1362 file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(), 0x0008,0x0030, "TM");
1364 file->InsertEntryString(studyDescr, 0x0008,0x1030, "LO"); // Study Description
1365 file->InsertEntryString(serieDescr, 0x0008,0x103e, "LO"); // Series Description
1367 //0008|0060 [CS] [Modality]
1368 file->InsertEntryString("MR",0x0008,0x0060, "CS");
1370 // 0020 0037 DS 6 Image Orientation (Patient)
1371 char charImageOrientation[256];
1374 std::cout << "charImageOrientation " <<
1375 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1376 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1377 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1378 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1379 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1380 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1382 sprintf(charImageOrientation,"%f\\%f\\%f \\ %f\\%f\\%f",
1383 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0],
1384 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1],
1385 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2],
1386 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0],
1387 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1],
1388 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] ) ;
1390 file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1393 // 0020 0032 DS 3 Image Position (Patient)
1395 char charImagePosition[256];
1396 sprintf(charImagePosition,"%f\\%f\\%f",
1397 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1398 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1399 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1401 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1405 // 0020 0x1041 DS 1 Slice Location
1406 // sprintf(charImagePosition,"%f",float(imgNum));
1407 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1409 // Set Rescale Intercept
1412 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1414 // Set Rescale Slope
1417 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1420 GDCM_NAME_SPACE::FileHelper *fileH;
1421 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1422 fileH->SetContentType(contentType);
1424 // cast is just to avoid warnings (*no* conversion is performed)
1425 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1427 //std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1429 fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1430 fileH->SetWriteModeToRaw();
1431 fileH->SetWriteTypeToDcmExplVR();
1432 if( !fileH->Write(dcmImageName)) {
1433 std::cout << "Failed for [" << dcmImageName << "]\n"
1434 << " File is unwrittable" << std::endl;
1437 throw ( BrukerHopelessException ("Level 1 Unable to write Dicom file "));