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 values -----
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 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;
64 // BrukerDataSet br_subject;
66 subject = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
67 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
70 std::cout << " Subject : [" << subject << "]" << std::endl;
71 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 "));
77 //exit(0); /// \TODO throw an exception !
83 //br_subject.PrintSelf();
85 // get info for 'Study Description'
87 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
88 std::string subject_name = b_name.GetStringValue()[0];
89 strPatientName = subject_name;
90 cleanString(subject_name);
92 BrukerFieldData b_entry=br_subject.GetFieldData("SUBJECT_entry");
93 std::string subject_entry = b_entry.GetStringValue()[0];
94 //cleanString(subject_entry);
95 subject_entry = subject_entry.substr(11, subject_entry.size()-11);
97 BrukerFieldData b_position=br_subject.GetFieldData("SUBJECT_position");
98 std::string subject_position = b_position.GetStringValue()[0];
99 //cleanString(subject_position);
100 subject_position = subject_position.substr(9, subject_position.size()-9);
102 BrukerFieldData b_date=br_subject.GetFieldData("SUBJECT_date");
103 std::string subject_date = b_date.GetStringValue()[0];
104 strStudyTimeDate = subject_date;
105 cleanString(subject_date);
107 BrukerFieldData b_study_name=br_subject.GetFieldData("SUBJECT_study_name");
108 std::string subject_study_name = b_study_name.GetStringValue()[0];
109 subject_study_name = subject_study_name.substr(1, subject_study_name.size()-2);
110 cleanString(subject_date);
112 // subject_name is already in 'Patient Name'
113 strStudyDescr = /*subject_name + "." + */ subject_study_name + "." + subject_entry + "." + subject_position + "." + subject_date;
115 char outputDirName[(unsigned int) PATH_MAX+2];
117 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
120 // -----------------------------------------------------
121 // Iterate to ALL the objets(files/directories) found in the input directory
122 // (this is level ZERO)
123 // each Directory (name : 1, 2, 3, ...) will be a Dicom Serie
124 // -----------------------------------------------------
126 GDCM_NAME_SPACE::DirListType::iterator it;
128 for (it = fileNames.begin();
129 it != fileNames.end();
132 if ( boost::filesystem::is_directory(*it) )
135 std::cout << "[" << *it << "] is a directory" << std::endl;
137 //BrukerDataSet br_acqp;
140 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
143 br_acqp.LoadFile(strAcqp);
146 std::string acqp_scan_name;
147 std::string acqp_method;
148 std::string acqp_protocol_location;
150 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
151 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
152 cleanString(acqp_protocol_location);
154 BrukerFieldData b_scan_name=br_acqp.GetFieldData("ACQ_scan_name");
155 acqp_scan_name = b_scan_name.GetStringValue()[0];
156 cleanString(acqp_scan_name);
158 BrukerFieldData b_method=br_acqp.GetFieldData("ACQ_method");
159 acqp_method = b_method.GetStringValue()[0];
160 cleanString(acqp_method);
162 BrukerFieldData b_list_size = br_acqp.GetFieldData("ACQ_O1_list_size");
163 //b_list_size.PrintSelf(); //JP
165 nbSlices = b_list_size.GetIntValue()[0];
167 strSerieDescr = GDCM_NAME_SPACE::Util::GetName(*it)
168 /* + "." + acqp_protocol_location */ // always the same (in each acquisition)
169 + "." + acqp_scan_name
170 + "." + acqp_method.c_str();
172 sprintf(outputDirName, "%s%c%s", OutputDirName.c_str(),
173 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
174 strSerieDescr.c_str() );
176 std::cout << " ================================================================================\n"
177 << " === [" << GDCM_NAME_SPACE::Util::GetName(*it) << "] -> [" << strSerieDescr << "]\n"
178 << " ================================================================================"
183 printf ("outputDirName [%s]\n", outputDirName);
185 DealWithNiveau1(*it, outputDirName);
187 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
191 // =====================================================================
193 void Bruker2Dicom::DealWithNiveau1(std::string level1Directory, std::string currentOutputDirName) {
195 // e.g. : at level 1, in B67d1.Bp1/6
197 // acqp fid imnd pdata pulseprogram spnam0 spnam1
199 bool res = CreateDirectory(currentOutputDirName);
202 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
203 throw ( BrukerHopelessException ("Level 1 output directory creation failure "));
206 GDCM_NAME_SPACE::DirList dirList(level1Directory, false, true); // DON'T get recursively the list of files
207 GDCM_NAME_SPACE::DirListType fileNames;
208 fileNames = dirList.GetFilenames();
209 // -----------------------------------------------------
210 // Iterate to ALL the objets(files/directories) found in the input directory
211 // -----------------------------------------------------
212 GDCM_NAME_SPACE::DirListType::iterator it;
214 for (it = fileNames.begin();
215 it != fileNames.end();
218 if ( boost::filesystem::is_regular(*it) )
219 //if ( ! boost::filesystem::is_directory(*it) )
222 std::cout << "--- [" << *it << "] is a file." << std::endl;
226 char outputDirName[(unsigned int) PATH_MAX+2];
227 //std::string firstName;
229 for (it = fileNames.begin();
230 it != fileNames.end();
233 if ( boost::filesystem::is_directory(*it) )
235 // will be always "pdata" ...
237 std::cout << "--- [" << *it << "] is a directory" << std::endl;
240 if (FileLine.startsWith("##$ACQ_size=")) {
241 if (FileLine.startsWith("##$NI=")) {
242 if (FileLine.startsWith("##$NR=")) {
243 if (FileLine.startsWith("##$ACQ_obj_order=")) {
244 if (FileLine.startsWith("##$ACQ_word_size=")) {
245 if (FileLine.startsWith("##$BYTORDA=")) {
246 if (FileLine.startsWith("##$PULPROG=")) {
249 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
250 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
251 GDCM_NAME_SPACE::Util::GetName(*it).c_str());
254 std::string strMethod;
255 //std::string firstName = *(fileNames.begin());
257 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin())) +
258 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
260 // std::cout << "---strMethod (for method)=> [" << strMethod << "]" << std::endl;
261 canOpen = br_method.LoadFile(strMethod);
264 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
265 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
267 //std::cout << "---strMethod (for imnd) => [" << strMethod << "]" << std::endl;
268 canOpen = br_method.LoadFile(strMethod);
271 std::cout << "Hopeless! neither 'method' nor 'imnd' found" << std::endl;
272 throw ( BrukerHopelessException ("Hopeless! neither 'method' nor 'imnd' found "));
273 //exit(0); /// \TODO throw an exception !
277 std::cout << "open => [" << strMethod << "] successfully" << std::endl;
281 ##$PVM_Fov (dimension)
284 dans method (pour perfusion seulement?) :
285 ##$PVM_ObjOrderList=( 8 )
288 ##$PVM_SPackArrNSlices=( 2 )
291 DealWithNiveau2(*it,outputDirName );
296 // =====================================================================
298 void Bruker2Dicom::DealWithNiveau2(std::string level2Directory, std::string currentOutputDirName) {
300 // e.g. : at level 2 in B67d1.Bp1/6/pdata
302 // acqp fid imnd pdata pulseprogram spnam0 spnam1
305 bool res = CreateDirectory(currentOutputDirName);
308 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
309 throw ( BrukerHopelessException ("Hopeless! Level2 output directory creation failure"));
313 GDCM_NAME_SPACE::DirList dirList(level2Directory, false, true); // DON'T get recursively the list of files
315 GDCM_NAME_SPACE::DirListType fileNames;
316 fileNames = dirList.GetFilenames();
318 char outputDirName[(unsigned int) PATH_MAX+2];
320 // -----------------------------------------------------
321 // Iterate to ALL the objets(files/directories) found in the input directory
322 // -----------------------------------------------------
323 GDCM_NAME_SPACE::DirListType::iterator it;
327 for (it = fileNames.begin();
328 it != fileNames.end();
331 if ( ! boost::filesystem::is_directory(*it) )
333 std::cout << "--- --- [" << *it << "] is a file.." << std::endl;
338 for (it = fileNames.begin();
339 it != fileNames.end();
342 if ( boost::filesystem::is_directory(*it) )
346 std::cout << "--- --- [" << *it << "] is a directory" << std::endl;
348 // sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
349 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
350 // GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
354 // (interest of previous method :
355 // If unaware user changed the pdata name, it goes on working
359 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
362 std::string str_isa_func_name;
363 canOpen = br_isa.LoadFile(str_isa);
366 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
367 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
368 GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
373 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
375 str_isa_func_name = b_isa_func_name.GetStringValue()[0];
376 cleanString(str_isa_func_name);
378 sprintf(outputDirName, "%s%c%s.%s", currentOutputDirName.c_str(),
379 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
380 GDCM_NAME_SPACE::Util::GetName(*it).c_str(),
381 str_isa_func_name.c_str());
383 DealWithNiveau3(*it, outputDirName);
390 // =====================================================================
393 void Bruker2Dicom::DealWithNiveau3(std::string level3Directory, std::string currentOutputDirName){
396 // e.g. at level 3, in
398 // just to be able to go on checking // JP
399 if ( GDCM_NAME_SPACE::Util::GetName(level3Directory) != "1")
402 bool res = CreateDirectory(currentOutputDirName);
406 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
407 throw ( BrukerHopelessException ("Hopeless! Level3 output directory creation failure"));
411 GDCM_NAME_SPACE::DirList dirList(level3Directory, false, true); // DON'T get recursively the list of files
412 GDCM_NAME_SPACE::DirListType::iterator it;
413 GDCM_NAME_SPACE::DirListType fileNames;
414 fileNames = dirList.GetFilenames();
416 char original2dseqName [(unsigned int) PATH_MAX+2];
417 char currentOutputMhdDirName [(unsigned int) PATH_MAX+2];
419 char outputMhdFileName [(unsigned int) PATH_MAX+2];
420 char output2dseqSliceFileName[(unsigned int) PATH_MAX+6]; // think about extra '.dcm'
421 char output2dseqName [(unsigned int) PATH_MAX+6];
422 char output2dseqCartoName [(unsigned int) PATH_MAX+6];
424 char copyFile[PATH_MAX + PATH_MAX + 5]; // Should be enough!
427 //-------------- try d3proc;
428 char char_d3proc[(unsigned int) PATH_MAX+2];
430 sprintf(char_d3proc,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"d3proc" );
433 std::cout << "d3proc: --- => [" << char_d3proc << "]" << std::endl;
434 std::string str_d3proc(char_d3proc);
435 canOpen = br_d3proc.LoadFile(str_d3proc);
439 std::cout << "Hopeless! no 'd3proc' found" << std::endl;
440 throw ( BrukerHopelessException ("Hopeless! no 'd3proc' found"));
441 //exit(0); /// \TODO throw an exception !
444 canOpen = br_d3proc.FillMap();
447 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
448 throw ( BrukerHopelessException ("Hopeless! FillMap failed on 'd3proc'"));
449 //exit(0); /// \TODO throw an exception !
452 //-------------- end try d3proc;
455 // -------------------try reco
457 char char_reco[(unsigned int) PATH_MAX+2];
459 sprintf(char_reco,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
460 //str_d3proc = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
461 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
464 std::cout << "reco --- => [" << char_reco << "]" << std::endl;
465 std::string str_reco(char_reco);
466 canOpen = br_reco.LoadFile(str_reco);
468 if (!canOpen) // we try in directory ../1
471 std::cout << "[" << str_reco << "] not found " << std::endl;
472 std::string lastDirName = GDCM_NAME_SPACE::Util::GetPath(level3Directory);
473 //lastDirName = GDCM_NAME_SPACE::Util::GetPath(lastDirName);
474 sprintf(char_reco,"%s%c1%c%s", lastDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
476 canOpen = br_reco.LoadFile(str_reco);
479 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
480 throw ( BrukerHopelessException ("Hopeless! cannot find 'reco'"));
481 //exit(0); /// \TODO throw an exception !
485 canOpen = br_reco.FillMap();
488 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
489 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
490 //exit(0); /// \TODO throw an exception !
492 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
493 // br_reco.PrintSelf();
494 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
496 // -------------------end try reco
499 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
500 int NX = bX.GetIntValue()[0];
503 std::cout << "IM_SIX " << NX << std::endl;
504 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
505 int NY = bY.GetIntValue()[0];
508 std::cout << "IM_SIY " << NY << std::endl;
509 /// \todo : check if there are actually 3 dimensions or only 2
511 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
512 int nbFrames = bZ.GetIntValue()[0];
514 std::cout << "IM_SIZ " << nbFrames << std::endl;
516 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
518 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
520 std::string mhdDataPixelType;
522 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
524 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
525 double fovX = fov.GetDoubleValue()[0];
526 double fovY = fov.GetDoubleValue()[1];
528 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
530 /// \TODO probabely a more sophisticated accessor will be necessary :
531 /// (cf : non contiguous slices, overlapping, slice thickness, space between slices, etc)
532 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
533 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
537 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
538 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "MhdFiles");
539 res = CreateDirectory( currentOutputMhdDirName );
541 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
542 throw ( BrukerHopelessException ("Hopeless!FillMap failed on 'reco'"));
547 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
551 std::cout << "nbFrames " << nbFrames << std::endl;
553 std::cout << "nbSlices " << nbSlices << std::endl;
555 int nbInstants = nbFrames/nbSlices;
557 std::cout << "nbInstants (deduced )" << nbInstants << std::endl;
560 FILE *fp; // for MHD files
562 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "2dseq");
565 // \TODO : tenir compte du bazar precedent
567 // load 2dseq in memory
569 fp = fopen(original2dseqName, "rb");
572 std::cout << "Cannot open [" << original2dseqName << "] for reading" << std::endl;
573 throw ( BrukerHopelessException ("Hopeless! Cannot open '2dseq'"));
577 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
578 ///\ TODO : find a safer way to be sure to read everything!
579 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
581 // This one will be important!
582 // ---------------------------
584 imageSet = CreateImageSet ( );
586 catch (BrukerInitException& e)
588 std::cout << "Exception " << e.what() << std::endl;
593 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
594 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
597 std::cout << "Single instant : do not split" << std::endl;
600 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
601 GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
602 fp=fopen(outputMhdFileName, "w");
605 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
606 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
611 fprintf(fp, "ObjectType = Image\n");
612 fprintf(fp, "NDims = 3\n" );
613 fprintf(fp, "BinaryData = True \n" );
614 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
615 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
616 fprintf(fp, "HeaderSize = %d\n", 0);
617 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
618 fprintf(fp, "Position = 0 0 %d\n", 0 );
619 fprintf(fp, "Offset = 0 0 0\n" );
620 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
621 fprintf(fp, "ElementNumberOfChannels = 1\n" );
622 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
623 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
626 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
627 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
628 fp=fopen(output2dseqSliceFileName, "wb");
631 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
632 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseq file for writting"));
636 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
640 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
644 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
645 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
647 /* ----------- Write Dicom Image ---------------*/
648 MakeDicomImage(buffer_2dseq,
653 fovX/NY, fovY/NY, sliceDistance,
654 output2dseqSliceFileName,
662 0,// index frame number
663 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
666 } // end if nbInstants = 1
668 else // more than ONE instant
671 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
673 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
676 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
680 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
681 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, k );
683 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
684 fp=fopen(outputMhdFileName, "w");
687 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
688 throw ( BrukerHopelessException ("Hopeless! Cannot open mhd file for writting"));
693 /* ----------- Write MHD Image ---------------*/
695 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
696 fprintf(fp, "ObjectType = Image\n");
697 fprintf(fp, "NDims = 3\n" );
698 fprintf(fp, "BinaryData = True \n" );
699 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
700 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
701 fprintf(fp, "HeaderSize = %d\n", 0);
702 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); // slice distance : no meaning for temporal serie
703 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
704 fprintf(fp, "Offset = 0 0 0\n" );
705 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
706 fprintf(fp, "ElementNumberOfChannels = 1\n" );
707 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
708 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb );
712 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
713 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,sliceNb);
714 fp=fopen(output2dseqSliceFileName, "wb");
717 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
718 throw ( BrukerHopelessException ("Hopeless! Cannot open 2dseqSliceFile file for writting"));
721 int frameSize = NX*NY*pixelSize;
722 for (instantNb=0; instantNb<nbInstants; instantNb++)
724 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
725 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
730 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
737 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
738 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
739 fp=fopen(output2dseqSliceFileName, "wb");
742 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
745 int frameSize = NX*NY*pixelSize;
746 for (instantNb=0; instantNb<nbInstants; instantNb++)
748 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
749 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
755 fp=fopen(output2dseqSliceFileName, "rb");
758 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
761 fread( pixelsForCurrentSlice,
762 frameSize*nbInstants,
765 // end of desperate try !
768 /* ----------- Write Dicom Image ---------------*/
770 int frameSize = NX*NY*pixelSize;
771 for (instantNb=0; instantNb<nbInstants; instantNb++)
773 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
776 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
777 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
778 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb);
781 pixelsForCurrentSlice,
786 fovX/NY, fovY/NY, sliceDistance,
787 output2dseqSliceFileName,
796 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
799 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
805 delete [] pixelsForCurrentSlice;
806 } // end nbInstants == 1
807 delete [] buffer_2dseq;
811 // -----------------------------------------------------
812 // deal with MatLab-generated Carto file.
813 // -----------------------------------------------------
815 dealWithCarto(fileNames, NX, NY, nbSlices, fovX, fovY, sliceDistance,
816 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
820 // ===========================================================================================
822 void Bruker2Dicom::dealWithCarto(GDCM_NAME_SPACE::DirListType &fileNames, int NX, int NY, int nbSlices,
823 double fovX, double fovY, double sliceDistance,
824 char *copyFile, std::string ¤tOutputDirName,
825 char *outputMhdFileName, char *output2dseqCartoName)
827 // -----------------------------------------------------
828 // deal with MatLab-generated Carto file.
829 // -----------------------------------------------------
831 char *code[] ={ "ADC", "adc", "TTP", "ttp", "PEAK", "peak", "" }; // add more carto file name identifiers if necessary; end with ""
833 GDCM_NAME_SPACE::DirListType::iterator it;
834 char file_name_ident[500];
837 // Iterate to ALL the objets(files/directories) found in the input directory
838 for (it = fileNames.begin();
839 it != fileNames.end();
842 if ( boost::filesystem::is_regular(*it) )
843 //if ( ! boost::filesystem::is_directory(*it) )
846 std::cout << "--- [" << *it << "] is a file..." << std::endl;
850 while (code[icode][0] != 0)
852 sprintf(file_name_ident, "2dseq.%s",code[icode]); // e.g "2dseq_ADC"
853 std::string::size_type loc = (*it).rfind(file_name_ident);
855 if ( loc != std::string::npos )
858 ///\ TODO : find a safer way to be sure to read everything!
859 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
860 fp = fopen ( (*it).c_str(), "rb");
862 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
865 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
867 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
868 << NX*NY*sizeof(double)*nbSlices << std::endl;
869 std::string lastFileName = GDCM_NAME_SPACE::Util::GetName((*it).c_str());
872 // Copy the data file in the new directory
873 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
874 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str());
876 sprintf(outputMhdFileName, "%s%c%s%s",
877 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".mhd" );
879 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
882 fp=fopen(outputMhdFileName, "w");
885 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
889 fprintf(fp, "ObjectType = Image\n");
890 fprintf(fp, "NDims = 3\n" );
891 fprintf(fp, "BinaryData = True \n" );
892 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
893 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
894 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
895 fprintf(fp, "HeaderSize = %d\n", 0 );
896 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
897 fprintf(fp, "Position = 0 0 0\n" );
898 fprintf(fp, "Offset = 0 0 0\n" );
899 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
900 fprintf(fp, "ElementNumberOfChannels = 1\n" );
901 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
902 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
907 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
910 // ----------- Write Dicom Image ---------------
914 sprintf(output2dseqCartoName, "%s%c%s%s",
915 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".dcm" );
917 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
919 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID(); //New SerieUID for each carto.
920 std::string strNewSerieDescr(strSerieDescr+ "_" +GDCM_NAME_SPACE::Util::GetName((*it).c_str()));
921 MakeDicomImage(buffer_carto,
926 fovX/NY, fovY/NY, sliceDistance,
927 output2dseqCartoName,
936 GDCM_NAME_SPACE::CREATED_IMAGE
940 delete [] buffer_carto;
942 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
943 break; // don't check for more ident on same file name!
949 } // end iterate on files
953 // ==========================================================================================================
955 bool Bruker2Dicom::CreateDirectory(std::string OutputDirName)
957 std::string systemCommand;
960 std::cout << "Check for output directory :[" << OutputDirName << "]."
962 if ( ! boost::filesystem::is_directory(OutputDirName) ) // dirout not found
964 std::string strDirNameout(OutputDirName); // to please gcc 4
965 systemCommand = "mkdir " + strDirNameout; // create it!
967 std::cout << systemCommand << std::endl;
968 system (systemCommand.c_str());
969 if ( ! boost::filesystem::is_directory(OutputDirName) ) // be sure it worked
972 std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
974 /// \todo : THROW AN EXCEPTION
979 std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
985 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
993 // ===========================================================================================
995 /// \TODO move cleanString to 'crea' ?
997 void Bruker2Dicom::cleanString(std::string &s)
1000 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
1005 if (s[l-1] == ' ' ) // blank space
1013 std::string repChar("_");
1014 GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
1019 // ===========================================================================================
1022 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
1024 if(bDPT.GetDataType() == "string")
1026 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1028 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1029 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1031 if (brukerDataPixelType == "ip_short") {
1032 mhdDataPixelType = "MET_USHORT";
1035 if (brukerDataPixelType == "ip_int") {
1036 mhdDataPixelType = "MET_UINT";
1039 if (brukerDataPixelType == "ip_char") {
1040 mhdDataPixelType = "MET_UCHAR";
1043 /// \TODO : finish the list
1045 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1047 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1049 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1051 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1053 case 4 : fp << "ElementType = MET_INT" << std::endl;
1055 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1057 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1059 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1064 int brukerDataPixelType = bDPT.GetIntValue()[0];
1066 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1067 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1069 // Cross your fingers !!!
1071 // pb : found values : 2, 3, 5
1073 if (brukerDataPixelType == 2) {
1074 mhdDataPixelType = "MET_USHORT";
1077 if (brukerDataPixelType == 3) {
1078 mhdDataPixelType = "MET_USHORT";
1081 if (brukerDataPixelType == 1) {
1082 mhdDataPixelType = "MET_UCHAR";
1088 // ===========================================================================================
1090 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1092 std::vector<BrukerImage> imageSet;
1093 br_acqp.SetLoopStructure();
1094 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1095 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1096 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1098 if (result == false)
1100 throw ( BrukerInitException ("Bruker2Dicom::CreateImageSet failure ") );
1103 br_acqp.SetImageLoopStructure();
1104 br_acqp.SetBrukerImageList();
1105 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1107 BrukerImage image(br_acqp,br_reco);
1108 image.Init(br_acqp,br_reco,1);
1110 for(int i=0;i<brukerImageList.size();i++)
1112 image.Init(br_acqp,br_reco,i);
1113 imageSet.push_back(image);
1116 // Just for checking
1119 std::vector<std::vector <double> > imageOrientation;
1120 std::vector <double> imagePosition;
1121 for(int i=0;i<brukerImageList.size();i++)
1123 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1125 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1126 std::cout << "Position " << imagePosition[0] << " "
1127 << imagePosition[1] << " " << imagePosition[2] ;
1128 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1129 std::cout << "\t Orientation " ;
1130 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1131 std::cout << imageOrientation[i1][i2] << " ";
1133 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1134 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1136 std::cout << "\t [";
1137 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1138 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1139 std::cout << "]" << std::endl;
1145 // ===========================================================================================
1147 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1152 double spacingX, double spacingY, double sliceDistance,
1153 std::string dcmImageName,
1154 const std::string &patientName,
1156 std::string &studyUID,
1157 std::string &serieUID,
1158 std::string &studyDescr,
1159 std::string &serieDescr,
1160 std::string &strStudyTimeDate,
1162 GDCM_NAME_SPACE::ImageContentType contentType
1165 std::ostringstream str;
1167 GDCM_NAME_SPACE::File *file;
1168 file = GDCM_NAME_SPACE::File::New();
1170 // Set the image size
1173 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1176 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1182 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1185 // Set the pixel type
1186 // //8, 16, 32, 64 (for double ?)
1189 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1191 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1194 str << pixelSize*8-1;
1195 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1197 // Set the pixel representation // 0/1 , 0=unsigned
1198 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1200 // Set the samples per pixel // 1:Grey level, 3:RGB
1201 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1203 // 0028 0030 DS 2 Pixel Spacing
1205 str << spacingX << "\\" << spacingY;
1206 file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1208 // 0018 0050 DS 1 Slice Thickness
1210 str << sliceDistance;
1211 file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1213 // 0020 0011 IS 1 Series Number
1216 file->InsertEntryString(str.str(),0x0020,0x0011, "IS");
1218 // 0020|0013 [IS] [Instance Number]
1221 str << instanceNumber;
1222 file->InsertEntryString(str.str(),0x0020,0x0013, "IS");
1225 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1226 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1227 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1230 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1232 // 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1233 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1234 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1236 // if (strlen(patientName) != 0)
1237 file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1239 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1240 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1242 // 0008 0020 DA 1 Study Date
1243 // 0008 0030 TM 1 Study Time
1245 /// \TODO split into 2 strings!
1246 file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1247 file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(), 0x0008,0x0030, "TM");
1249 file->InsertEntryString(studyDescr, 0x0008,0x1030, "LO"); // Study Description
1250 file->InsertEntryString(serieDescr, 0x0008,0x103e, "LO"); // Series Description
1252 //0008|0060 [CS] [Modality]
1253 file->InsertEntryString("MR",0x0008,0x0060, "CS");
1255 // 0020 0037 DS 6 Image Orientation (Patient)
1256 char charImageOrientation[256];
1259 std::cout << "charImageOrientation " <<
1260 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1261 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1262 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1263 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1264 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1265 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1267 sprintf(charImageOrientation,"%f\\%f\\%f \\ %f\\%f\\%f",
1268 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0],
1269 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1],
1270 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2],
1271 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0],
1272 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1],
1273 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] ) ;
1275 file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1278 // 0020 0032 DS 3 Image Position (Patient)
1280 char charImagePosition[256];
1281 sprintf(charImagePosition,"%f\\%f\\%f",
1282 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1283 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1284 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1286 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1290 // 0020 0x1041 DS 1 Slice Location
1291 // sprintf(charImagePosition,"%f",float(imgNum));
1292 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1294 // Set Rescale Intercept
1297 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1299 // Set Rescale Slope
1302 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1305 GDCM_NAME_SPACE::FileHelper *fileH;
1306 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1307 fileH->SetContentType(contentType);
1309 // cast is just to avoid warnings (*no* conversion is performed)
1310 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1312 //std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1314 fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1315 fileH->SetWriteModeToRaw();
1316 fileH->SetWriteTypeToDcmExplVR();
1317 if( !fileH->Write(dcmImageName))
1318 std::cout << "Failed for [" << dcmImageName << "]\n"
1319 << " File is unwrittable" << std::endl;