1 #include "bruker2dicom.h"
3 #ifndef PATH_MAX // If not defined yet : do it
9 bool Bruker2Dicom::Execute()
11 // ----- Check input values -----
13 bool bigEndian = GDCM_NAME_SPACE::Util::IsCurrentProcessorBigEndian();
15 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(InputDirName) )
17 std::cout << "KO : [" << InputDirName << "] is not a Directory." << std::endl;
23 std::cout << "OK : [" << InputDirName << "] is a Directory." << std::endl;
26 std::string strDirNameOut(OutputDirName);
27 bool res=CreateDirectory(strDirNameOut);
29 std::cout << "[" << OutputDirName << "] Directory creation failure " << std::endl;
33 std::string strDirNamein(InputDirName);
34 GDCM_NAME_SPACE::DirList dirList(strDirNamein, false, true); // DON'T get recursively the list of files
35 std::string strDirNameout(OutputDirName);
40 std::cout << "------------List of found files ------------" << std::endl;
42 std::cout << std::endl;
48 // e.g : at level 0, in : B67d1.Bp1
50 // 1 2 3 4 5 6 AdjStatePerStudy subject
53 GDCM_NAME_SPACE::DirListType fileNames;
54 fileNames = dirList.GetFilenames();
56 std::string outputFileName;
58 // BrukerDataSet br_subject;
60 subject = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
61 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
64 std::cout << " Subject : [" << subject << "]" << std::endl;
65 canOpen =br_subject.LoadFile(subject);
69 std::cout << "Hopeless! no 'subject' found" << std::endl;
70 exit(0); /// \TODO throw an exception !
76 //br_subject.PrintSelf();
78 // get info for 'Study Description'
81 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
82 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
83 cleanString(acqp_protocol_location);
85 BrukerFieldData b_name=br_subject.GetFieldData("SUBJECT_name_string");
86 std::string subject_name = b_name.GetStringValue()[0];
87 strPatientName = subject_name;
88 cleanString(subject_name);
90 BrukerFieldData b_entry=br_subject.GetFieldData("SUBJECT_entry");
91 std::string subject_entry = b_entry.GetStringValue()[0];
92 //cleanString(subject_entry);
93 subject_entry = subject_entry.substr(11, subject_entry.size()-11);
95 BrukerFieldData b_position=br_subject.GetFieldData("SUBJECT_position");
96 std::string subject_position = b_position.GetStringValue()[0];
97 //cleanString(subject_position);
98 subject_position = subject_position.substr(9, subject_position.size()-9);
100 BrukerFieldData b_date=br_subject.GetFieldData("SUBJECT_date");
101 std::string subject_date = b_date.GetStringValue()[0];
102 strStudyTimeDate = subject_date;
103 cleanString(subject_date);
105 BrukerFieldData b_study_name=br_subject.GetFieldData("SUBJECT_study_name");
106 std::string subject_study_name = b_study_name.GetStringValue()[0];
107 subject_study_name = subject_study_name.substr(1, subject_study_name.size()-2);
108 cleanString(subject_date);
110 strStudyDescr = subject_name + "." + subject_study_name + "." + subject_entry + "." + subject_position + "." + subject_date;
112 char outputDirName[(unsigned int) PATH_MAX+2];
114 strStudyUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
116 // -----------------------------------------------------
117 // Iterate to ALL the objets(files/directories) found in the input directory
118 // (this is level ZERO)
119 // each Directory (name : 1, 2, 3, ...) will be a Dicom Serie
120 // -----------------------------------------------------
122 GDCM_NAME_SPACE::DirListType::iterator it;
124 for (it = fileNames.begin();
125 it != fileNames.end();
128 if ( GDCM_NAME_SPACE::DirList::IsDirectory(*it) )
131 std::cout << "[" << *it << "] is a directory" << std::endl;
133 //BrukerDataSet br_acqp;
136 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
139 br_acqp.LoadFile(strAcqp);
142 std::string acqp_scan_name;
143 std::string acqp_method;
144 std::string acqp_protocol_location;
146 BrukerFieldData b_protocol_location=br_acqp.GetFieldData("ACQ_protocol_location");
147 acqp_protocol_location = b_protocol_location.GetStringValue()[0];
148 cleanString(acqp_protocol_location);
150 BrukerFieldData b_scan_name=br_acqp.GetFieldData("ACQ_scan_name");
151 acqp_scan_name = b_scan_name.GetStringValue()[0];
152 cleanString(acqp_scan_name);
154 BrukerFieldData b_method=br_acqp.GetFieldData("ACQ_method");
155 acqp_method = b_method.GetStringValue()[0];
156 cleanString(acqp_method);
158 BrukerFieldData b_list_size = br_acqp.GetFieldData("ACQ_O1_list_size");
159 b_list_size.PrintSelf(); //JP
161 nbSlices = b_list_size.GetIntValue()[0];
163 strSerieDescr = GDCM_NAME_SPACE::Util::GetName(*it)
164 + "." + acqp_protocol_location
165 + "." + acqp_scan_name
166 + "." + acqp_method.c_str();
168 sprintf(outputDirName, "%s%c%s", OutputDirName.c_str(),
169 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
170 strSerieDescr.c_str() );
172 std::cout << " ================================================================================\n"
173 << " === [" << GDCM_NAME_SPACE::Util::GetName(*it) << "] -> [" << strSerieDescr << "]\n"
174 << " ================================================================================"
178 std::cout << " ================================================================================\n"
179 << " ========================= [" << GDCM_NAME_SPACE::Util::GetName(*it).c_str()
180 << acqp_protocol_location.c_str()
181 << acqp_scan_name.c_str()
182 << acqp_method.c_str()
184 << " ================================================================================"
188 printf ("outputDirName [%s]\n", outputDirName);
190 DealWithNiveau1(*it, outputDirName);
192 } // end of : for (GDCM_NAME_SPACE::DirListType::iterator it
196 // =====================================================================
198 void Bruker2Dicom::DealWithNiveau1(std::string level1Directory, std::string currentOutputDirName) {
200 // e.g. : at level 1, in B67d1.Bp1/6
202 // acqp fid imnd pdata pulseprogram spnam0 spnam1
204 bool res = CreateDirectory(currentOutputDirName);
207 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
210 GDCM_NAME_SPACE::DirList dirList(level1Directory, false, true); // DON'T get recursively the list of files
211 GDCM_NAME_SPACE::DirListType fileNames;
212 fileNames = dirList.GetFilenames();
213 // -----------------------------------------------------
214 // Iterate to ALL the objets(files/directories) found in the input directory
215 // -----------------------------------------------------
216 GDCM_NAME_SPACE::DirListType::iterator it;
218 for (it = fileNames.begin();
219 it != fileNames.end();
222 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(*it) )
225 std::cout << "--- [" << *it << "] is a file" << std::endl;
229 char outputDirName[(unsigned int) PATH_MAX+2];
230 //std::string firstName;
232 for (it = fileNames.begin();
233 it != fileNames.end();
236 if ( GDCM_NAME_SPACE::DirList::IsDirectory(*it) )
238 // will be always "pdata" ...
240 std::cout << "--- [" << *it << "] is a directory" << std::endl;
243 if (FileLine.startsWith("##$ACQ_size=")) {
244 if (FileLine.startsWith("##$NI=")) {
245 if (FileLine.startsWith("##$NR=")) {
246 if (FileLine.startsWith("##$ACQ_obj_order=")) {
247 if (FileLine.startsWith("##$ACQ_word_size=")) {
248 if (FileLine.startsWith("##$BYTORDA=")) {
249 if (FileLine.startsWith("##$PULPROG=")) {
252 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
253 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
254 GDCM_NAME_SPACE::Util::GetName(*it).c_str());
257 std::string strMethod;
258 //std::string firstName = *(fileNames.begin());
260 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin())) +
261 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
263 // std::cout << "---strMethod (for method)=> [" << strMethod << "]" << std::endl;
264 canOpen = br_method.LoadFile(strMethod);
267 strMethod = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
268 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
270 //std::cout << "---strMethod (for imnd) => [" << strMethod << "]" << std::endl;
271 canOpen = br_method.LoadFile(strMethod);
274 std::cout << "Hopeless! neither 'method' nor 'imnd' found" << std::endl;
275 exit(0); /// \TODO throw an exception !
279 std::cout << "open => [" << strMethod << "] successfully" << std::endl;
283 ##$PVM_Fov (dimension)
286 dans method (pour perfusion seulement?) :
287 ##$PVM_ObjOrderList=( 8 )
290 ##$PVM_SPackArrNSlices=( 2 )
293 DealWithNiveau2(*it,outputDirName );
298 // =====================================================================
300 void Bruker2Dicom::DealWithNiveau2(std::string level2Directory, std::string currentOutputDirName) {
302 // e.g. : at level 2 in B67d1.Bp1/6/pdata
304 // acqp fid imnd pdata pulseprogram spnam0 spnam1
307 bool res = CreateDirectory(currentOutputDirName);
310 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
314 GDCM_NAME_SPACE::DirList dirList(level2Directory, false, true); // DON'T get recursively the list of files
316 GDCM_NAME_SPACE::DirListType fileNames;
317 fileNames = dirList.GetFilenames();
319 char outputDirName[(unsigned int) PATH_MAX+2];
321 // -----------------------------------------------------
322 // Iterate to ALL the objets(files/directories) found in the input directory
323 // -----------------------------------------------------
324 GDCM_NAME_SPACE::DirListType::iterator it;
328 for (it = fileNames.begin();
329 it != fileNames.end();
332 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(*it) )
334 std::cout << "--- --- [" << *it << "] is a file" << std::endl;
339 for (it = fileNames.begin();
340 it != fileNames.end();
343 if ( GDCM_NAME_SPACE::DirList::IsDirectory(*it) )
347 std::cout << "--- --- [" << *it << "] is a directory" << std::endl;
349 // sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
350 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
351 // GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
355 // (interest of previous method :
356 // If unaware user changed the pdata name, it goes on working
360 GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
363 std::string str_isa_func_name;
364 canOpen = br_isa.LoadFile(str_isa);
367 sprintf(outputDirName, "%s%c%s", currentOutputDirName.c_str(),
368 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
369 GDCM_NAME_SPACE::Util::GetName(*it).c_str() );
374 BrukerFieldData b_isa_func_name = br_isa.GetFieldData("ISA_func_name");
376 str_isa_func_name = b_isa_func_name.GetStringValue()[0];
377 cleanString(str_isa_func_name);
379 sprintf(outputDirName, "%s%c%s.%s", currentOutputDirName.c_str(),
380 GDCM_NAME_SPACE::GDCM_FILESEPARATOR,
381 GDCM_NAME_SPACE::Util::GetName(*it).c_str(),
382 str_isa_func_name.c_str());
384 DealWithNiveau3(*it, outputDirName);
391 // =====================================================================
394 void Bruker2Dicom::DealWithNiveau3(std::string level3Directory, std::string currentOutputDirName){
397 // e.g. at level 3, in
399 // just to be able to go on checking // JP
400 if ( GDCM_NAME_SPACE::Util::GetName(level3Directory) != "1")
403 bool res = CreateDirectory(currentOutputDirName);
407 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
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 << "--- => [" << 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 exit(0); /// \TODO throw an exception !
443 canOpen = br_d3proc.FillMap();
446 std::cout << "Hopeless! FillMap failed on 'd3proc'" << std::endl;
447 exit(0); /// \TODO throw an exception !
450 //-------------- end try d3proc;
453 // -------------------try reco
455 char char_reco[(unsigned int) PATH_MAX+2];
457 sprintf(char_reco,"%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
458 //str_d3proc = GDCM_NAME_SPACE::Util::GetPath(*(fileNames.begin()))+
459 // GDCM_NAME_SPACE::GDCM_FILESEPARATOR +
462 std::cout << "--- => [" << char_reco << "]" << std::endl;
463 std::string str_reco(char_reco);
464 canOpen = br_reco.LoadFile(str_reco);
466 if (!canOpen) // we try in directory ../1
469 std::cout << "[" << str_reco << "] not found " << std::endl;
470 std::string lastDirName = GDCM_NAME_SPACE::Util::GetPath(level3Directory);
471 //lastDirName = GDCM_NAME_SPACE::Util::GetPath(lastDirName);
472 sprintf(char_reco,"%s%c1%c%s", lastDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,GDCM_NAME_SPACE::GDCM_FILESEPARATOR,"reco" );
474 canOpen = br_reco.LoadFile(str_reco);
477 std::cout << "Hopeless! cannot find 'reco' in [" << str_reco << "]" << std::endl;
478 exit(0); /// \TODO throw an exception !
482 canOpen = br_reco.FillMap();
485 std::cout << "Hopeless! FillMap failed on [" << str_reco << "]" << std::endl;
486 exit(0); /// \TODO throw an exception !
488 //std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
489 // br_reco.PrintSelf();
490 // std::cout << "------------------------------------------------------------------------------------------------" << std::cout;
492 // -------------------end try reco
495 BrukerFieldData bX = br_d3proc.GetFieldData("IM_SIX");
496 int NX = bX.GetIntValue()[0];
498 std::cout << "IM_SIX " << NX << std::endl;
499 BrukerFieldData bY=br_d3proc.GetFieldData("IM_SIY");
500 int NY = bY.GetIntValue()[0];
502 std::cout << "IM_SIY " << NY << std::endl;
503 /// \todo : check if there are actually 3 dimensions or only 2
505 BrukerFieldData bZ= br_d3proc.GetFieldData("IM_SIZ");
506 int nbFrames = bZ.GetIntValue()[0];
507 std::cout << "IM_SIZ " << nbFrames << std::endl;
509 // WARNING DATTYPE is, either in {ip_short, ip_int, ip_char, ...}, or in {1, 2, 3, ...}
511 BrukerFieldData bDPT = br_d3proc.GetFieldData("DATTYPE");
513 std::string mhdDataPixelType;
515 getImhDataType(bDPT, mhdDataPixelType, pixelSize);
517 BrukerFieldData fov = br_method.GetFieldData("PVM_Fov");
518 double fovX = fov.GetDoubleValue()[0];
519 double fovY = fov.GetDoubleValue()[1];
521 std::cout << "FOV (ds method) " << fovX << " " << fovY << std::endl;
523 /// \TODO probabely a more sophisticated accessor will be necessary :
524 /// (cf : non contiguous slices, overlapping, slice thickness, space between clices, etc)
525 BrukerFieldData bsliceDistance = br_method.GetFieldData("PVM_SPackArrSliceDistance");
526 double sliceDistance = bsliceDistance.GetDoubleValue()[0];
530 sprintf(currentOutputMhdDirName, "%s%c%s", currentOutputDirName.c_str(),
531 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "MhdFiles");
532 res = CreateDirectory( currentOutputMhdDirName );
534 std::cout << "[" << currentOutputDirName << "] Directory creation failure " << std::endl;
539 std::cout << "Directory creation [" << currentOutputDirName << "]" << std::endl;
542 std::cout << "nbFrames " << nbFrames << std::endl;
543 std::cout << "nbSlices " << nbSlices << std::endl;
545 int nbInstants = nbFrames/nbSlices;
546 std::cout << "nbInstants (deduced )" << nbInstants << std::endl;
549 FILE *fp; // for MHD files
551 sprintf( original2dseqName, "%s%c%s", level3Directory.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, "2dseq");
554 // \TODO : tenir compte du bazar precedent
556 // load 2dseq in memory
558 fp = fopen(original2dseqName, "rb");
561 std::cout << "Cannot open [" << original2dseqName << "] for reading" << std::endl;
565 unsigned char *buffer_2dseq = new unsigned char[NX*NY*pixelSize*nbSlices*nbInstants];
566 ///\ TODO : find a safer way to be sure to read everything!
567 size_t lgr = fread(buffer_2dseq, 1, NX*NY*pixelSize*nbSlices*nbInstants, fp);
569 // This one will be important!
570 // ---------------------------
571 imageSet = CreateImageSet ( );
573 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
574 if (nbInstants==1) // creer un seul fichier .mhd pour toutes les Slices! (images natives)
576 std::cout << "Single instant : do not split" << std::endl;
579 sprintf(outputMhdFileName, "%s%cMhdData_All_the_Slices.mhd", currentOutputMhdDirName,
580 GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
581 fp=fopen(outputMhdFileName, "w");
584 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
589 fprintf(fp, "ObjectType = Image\n");
590 fprintf(fp, "NDims = 3\n" );
591 fprintf(fp, "BinaryData = True \n" );
592 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
593 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices );
594 fprintf(fp, "HeaderSize = %d\n", 0);
595 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
596 fprintf(fp, "Position = 0 0 %d\n", 0 );
597 fprintf(fp, "Offset = 0 0 0\n" );
598 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
599 fprintf(fp, "ElementNumberOfChannels = 1\n" );
600 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
601 fprintf(fp, "ElementDataFile = %s\n", "../2dseq_All_the_Slices" );
604 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices",
605 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
606 fp=fopen(output2dseqSliceFileName, "wb");
609 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
613 fwrite( buffer_2dseq, NX*NY*pixelSize, nbSlices, fp);
616 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID();
620 sprintf(output2dseqSliceFileName, "%s%c2dseq_All_the_Slices.dcm",
621 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
623 /* ----------- Write Dicom Image ---------------*/
624 MakeDicomImage(buffer_2dseq,
629 fovX/NY, fovY/NY, sliceDistance,
630 output2dseqSliceFileName,
638 0,// index frame number
639 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
642 } // end if nbInstants = 1
644 else // more than ONE instant
647 // it's (slice1,slide2, ...)t1 ; (slice1,slide2, ...)t2 ; ...
649 unsigned char *pixelsForCurrentSlice = new unsigned char[NX*NY*pixelSize*nbInstants];
652 for (sliceNb=0; sliceNb<nbSlices; sliceNb++)
656 sprintf(outputMhdFileName, "%s%cMhdData_%03d.mhd", currentOutputMhdDirName,
657 GDCM_NAME_SPACE::GDCM_FILESEPARATOR, k );
659 std::cout << "--- Output MHD file [" << outputMhdFileName << "]" << std::endl;
660 fp=fopen(outputMhdFileName, "w");
663 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
668 /* ----------- Write MHD Image ---------------*/
670 // std::cout << "Open sucessfully[" << outputMhdFileName << "] for writting" << std::endl;
671 fprintf(fp, "ObjectType = Image\n");
672 fprintf(fp, "NDims = 3\n" );
673 fprintf(fp, "BinaryData = True \n" );
674 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
675 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbInstants);
676 fprintf(fp, "HeaderSize = %d\n", 0);
677 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, 1.0 ); // slice distance : no meaning for temporal serie
678 fprintf(fp, "Position = 0 0 %d\n", sliceNb );
679 fprintf(fp, "Offset = 0 0 0\n" );
680 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
681 fprintf(fp, "ElementNumberOfChannels = 1\n" );
682 fprintf(fp, "ElementType = %s\n", mhdDataPixelType.c_str() );
683 fprintf(fp, "ElementDataFile = ..%c2dseq_Slice_%d\n", GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb );
687 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d",
688 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR,sliceNb);
689 fp=fopen(output2dseqSliceFileName, "wb");
692 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
695 int frameSize = NX*NY*pixelSize;
696 for (instantNb=0; instantNb<nbInstants; instantNb++)
698 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
699 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
704 // std::cout << "end writting[" << output2dseqSliceFileName << "]" << std::endl;
711 sprintf(output2dseqSliceFileName, "%sdummy_buffer",
712 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR);
713 fp=fopen(output2dseqSliceFileName, "wb");
716 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for writting" << std::endl;
719 int frameSize = NX*NY*pixelSize;
720 for (instantNb=0; instantNb<nbInstants; instantNb++)
722 // std::cout << "------------SN " << sliceNb << " IN " << instantNb << " T " << nbSlices*instantNb + sliceNb << std::endl;
723 fwrite( buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize,
729 fp=fopen(output2dseqSliceFileName, "rb");
732 std::cout << "Cannot open [" << output2dseqSliceFileName << "] for reading" << std::endl;
735 fread( pixelsForCurrentSlice,
736 frameSize*nbInstants,
739 // end of desperate try !
742 /* ----------- Write Dicom Image ---------------*/
744 int frameSize = NX*NY*pixelSize;
745 for (instantNb=0; instantNb<nbInstants; instantNb++)
747 memcpy(pixelsForCurrentSlice + frameSize*instantNb, buffer_2dseq +(nbSlices*instantNb + sliceNb)*frameSize, frameSize);
750 int indOfFirsImageWithinImageSet = nbSlices*instantNb;
751 sprintf(output2dseqSliceFileName, "%s%c2dseq_Slice_%d.dcm",
752 currentOutputDirName.c_str(), GDCM_NAME_SPACE::GDCM_FILESEPARATOR, sliceNb);
755 pixelsForCurrentSlice,
760 fovX/NY, fovY/NY, sliceDistance,
761 output2dseqSliceFileName,
770 GDCM_NAME_SPACE::UNMODIFIED_PIXELS_IMAGE
773 std::cout << "--- Output DCM file [" << output2dseqSliceFileName << "]" << std::endl;
779 delete [] pixelsForCurrentSlice;
780 } // end nbInstants == 1
781 delete [] buffer_2dseq;
785 // -----------------------------------------------------
786 // deal with MatLab-generated Carto file.
787 // -----------------------------------------------------
789 dealWithCarto(fileNames, NX, NY, nbSlices, fovX, fovY, sliceDistance,
790 copyFile, currentOutputDirName, outputMhdFileName, output2dseqCartoName);
794 // ===========================================================================================
796 void Bruker2Dicom::dealWithCarto(GDCM_NAME_SPACE::DirListType &fileNames, int NX, int NY, int nbSlices,
797 double fovX, double fovY, double sliceDistance,
798 char *copyFile, std::string ¤tOutputDirName,
799 char *outputMhdFileName, char *output2dseqCartoName)
801 // -----------------------------------------------------
802 // deal with MatLab-generated Carto file.
803 // -----------------------------------------------------
805 char *code[] ={ "ADC", "adc", "TTP", "ttp", "PEAK", "peak" , ""}; // add more carto file name identifiers if necessary; end with ""
807 GDCM_NAME_SPACE::DirListType::iterator it;
808 char file_name_ident[500];
811 // Iterate to ALL the objets(files/directories) found in the input directory
812 for (it = fileNames.begin();
813 it != fileNames.end();
816 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(*it) )
819 std::cout << "--- [" << *it << "] is a file" << std::endl;
823 while (code[icode][0] != 0)
825 sprintf(file_name_ident, "2dseq_%s",code[icode]); // e.g "2dseq_ADC"
826 std::string::size_type loc = (*it).rfind(file_name_ident);
828 if ( loc != std::string::npos )
831 ///\ TODO : find a safer way to be sure to read everything!
832 unsigned char *buffer_carto = new unsigned char[NX*NY*sizeof(double)*nbSlices];
833 fp = fopen ( (*it).c_str(), "rb");
835 std::cout << "Cannot open [" << *it << "] for reading" << std::endl;
838 fread(buffer_carto, NX*NY*sizeof(double), nbSlices, fp);
840 std::cout << "Deal with Carto file :[" <<*it << "], computed length : "
841 << NX*NY*sizeof(double)*nbSlices << std::endl;
842 std::string lastFileName = GDCM_NAME_SPACE::Util::GetName((*it).c_str());
845 // Copy the data file in the new directory
846 sprintf(copyFile, "cp %s %s%c%s", (*it).c_str() ,
847 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str());
849 sprintf(outputMhdFileName, "%s%c%s%s",
850 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".mhd" );
852 std::cout << "--- Output Carto MHD file [" << outputMhdFileName << "]" << std::endl;
855 fp=fopen(outputMhdFileName, "w");
858 std::cout << "Cannot open [" << outputMhdFileName << "] for writting" << std::endl;
862 fprintf(fp, "ObjectType = Image\n");
863 fprintf(fp, "NDims = 3\n" );
864 fprintf(fp, "BinaryData = True \n" );
865 fprintf(fp, "BinaryDataByteOrderMSB = False\n" );
866 fprintf(fp, "DimSize = %d %d %d\n", NX, NY, nbSlices);
867 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
868 fprintf(fp, "HeaderSize = %d\n", 0 );
869 fprintf(fp, "ElementSpacing = %lf %lf %lf\n",fovX/NY, fovY/NY, sliceDistance );
870 fprintf(fp, "Position = 0 0 0\n" );
871 fprintf(fp, "Offset = 0 0 0\n" );
872 fprintf(fp, "CenterOfRotation = 0 0 0\n" );
873 fprintf(fp, "ElementNumberOfChannels = 1\n" );
874 fprintf(fp, "ElementType = %s\n", "MET_DOUBLE" );
875 fprintf(fp, "ElementDataFile = %s\n", lastFileName.c_str() );
880 std::cout << "--- end write Carto MHD file [" << outputMhdFileName << "]" << std::endl;
883 // ----------- Write Dicom Image ---------------
887 sprintf(output2dseqCartoName, "%s%c%s%s",
888 currentOutputDirName.c_str(),GDCM_NAME_SPACE::GDCM_FILESEPARATOR, lastFileName.c_str(), ".dcm" );
890 std::cout << "--- end create name output2dseqCartoName file [" << output2dseqCartoName << "]" << std::endl;
892 strSerieUID = GDCM_NAME_SPACE::Util::CreateUniqueUID(); //New SerieUID for each carto.
893 std::string strNewSerieDescr(strSerieDescr+ "_" +GDCM_NAME_SPACE::Util::GetName((*it).c_str()));
894 MakeDicomImage(buffer_carto,
899 fovX/NY, fovY/NY, sliceDistance,
900 output2dseqCartoName,
909 GDCM_NAME_SPACE::CREATED_IMAGE
913 delete [] buffer_carto;
915 std::cout << "--- End writing Carto DICOM file [" << output2dseqCartoName << "]" << std::endl;
916 break; // don't check for more ident on same file name!
922 } // end iterate on files
926 // ==========================================================================================================
928 bool Bruker2Dicom::CreateDirectory(std::string OutputDirName)
930 std::string systemCommand;
933 std::cout << "Check for output directory :[" << OutputDirName << "]."
935 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(OutputDirName) ) // dirout not found
937 std::string strDirNameout(OutputDirName); // to please gcc 4
938 systemCommand = "mkdir " + strDirNameout; // create it!
940 std::cout << systemCommand << std::endl;
941 system (systemCommand.c_str());
942 if ( ! GDCM_NAME_SPACE::DirList::IsDirectory(OutputDirName) ) // be sure it worked
945 std::cout << "KO : not a dir : [" << OutputDirName << "] (creation failure ?)" << std::endl;
947 /// \todo : THROW AN EXCEPTION
952 std::cout << "Directory [" << OutputDirName << "] created." << std::endl;
958 std::cout << "Output Directory [" << OutputDirName << "] already exists; Used as is." << std::endl;
966 // ===========================================================================================
968 /// \TODO move cleanString to 'crea' ?
970 void Bruker2Dicom::cleanString(std::string &s)
973 if (s[l-1] == 0x0A || s[l-1] == 0x0D ) // CR or NL
978 if (s[l-1] == ' ' ) // blank space
986 std::string repChar("_");
987 GDCM_NAME_SPACE::Util::ReplaceSpecChar(s, repChar);
992 // ===========================================================================================
995 void Bruker2Dicom::getImhDataType(BrukerFieldData &bDPT, std::string &mhdDataPixelType, int &pixelSize)
997 if(bDPT.GetDataType() == "string")
999 std::string brukerDataPixelType = bDPT.GetStringValue()[0];
1000 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1001 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1003 if (brukerDataPixelType == "ip_short") {
1004 mhdDataPixelType = "MET_USHORT";
1007 if (brukerDataPixelType == "ip_int") {
1008 mhdDataPixelType = "MET_UINT";
1011 if (brukerDataPixelType == "ip_char") {
1012 mhdDataPixelType = "MET_UCHAR";
1015 /// \TODO : finish the list
1017 case 0 : fp << "ElementType = MET_CHAR" << std::endl;
1019 case 1 : fp << "ElementType = MET_UCHAR" << std::endl;
1021 case 2 : fp << "ElementType = MET_SHORT" << std::endl;
1023 case 3 : fp << "ElementType = MET_USHORT" << std::endl;
1025 case 4 : fp << "ElementType = MET_INT" << std::endl;
1027 case 5 : fp << "ElementType = MET_UINT" << std::endl;
1029 case 6 : fp << "ElementType = MET_FLOAT" << std::endl;
1031 case 7 : fp << "ElementType = MET_DOUBLE" << std::endl;
1036 int brukerDataPixelType = bDPT.GetIntValue()[0];
1037 std::cout << "DATTYPE " << brukerDataPixelType << std::endl;
1038 //std::string brukerDataPixelType = br_d3proc.GetFieldData("DATTYPE").GetStringValue()[0];
1040 // Cross your fingers !!!
1042 // pb : found values : 2, 3, 5
1044 if (brukerDataPixelType == 2) {
1045 mhdDataPixelType = "MET_USHORT";
1048 if (brukerDataPixelType == 3) {
1049 mhdDataPixelType = "MET_USHORT";
1052 if (brukerDataPixelType == 1) {
1053 mhdDataPixelType = "MET_UCHAR";
1059 // ===========================================================================================
1061 std::vector<BrukerImage> Bruker2Dicom::CreateImageSet ( )
1063 std::vector<BrukerImage> imageSet;
1064 br_acqp.SetLoopStructure();
1065 std::vector<int> tempVect = br_acqp.GetLoopStructure() ;
1066 std::map<std::string, BrukerFieldData> map = br_acqp.GetBrukerHeaderMap();
1067 bool result = br_acqp.ObjectVaryingProperties.init(map,tempVect);
1069 br_acqp.SetImageLoopStructure();
1070 br_acqp.SetBrukerImageList();
1071 std::vector<std::vector<int> > brukerImageList = br_acqp.GetBrukerImageList();
1073 BrukerImage image(br_acqp,br_reco);
1074 image.Init(br_acqp,br_reco,1);
1076 for(int i=0;i<brukerImageList.size();i++)
1078 image.Init(br_acqp,br_reco,i);
1079 imageSet.push_back(image);
1082 // Just for checking
1085 std::vector<std::vector <double> > imageOrientation;
1086 std::vector <double> imagePosition;
1087 for(int i=0;i<brukerImageList.size();i++)
1089 // fread(buffer_2dseq, NX*NY*pixelSize*nbSlices*nbInstants, 1, fp);
1091 imagePosition = imageSet[i].getTranslationVectorRPS2XYZ();
1092 std::cout << "Position " << imagePosition[0] << " "
1093 << imagePosition[1] << " " << imagePosition[2] ;
1094 imageOrientation = imageSet[i].getRotationMatrixRPS2XYZ();
1095 std::cout << "\t Orientation " ;
1096 for(int i1=0; i1<3;i1++)for(int i2=0; i2<3;i2++)
1097 std::cout << imageOrientation[i1][i2] << " ";
1099 //std::cout << "\t Abs Time " << imageSet[i].getAbsoluteTimePosition();
1100 std::cout << "\t Relat Time " << imageSet[i].getRelativeTimePosition();
1102 std::cout << "\t [";
1103 for (int i3=0; i3<imageSet[i].getLoopStamp().size();i3++)
1104 std::cout << " " << imageSet[i].getLoopStamp()[i3];
1105 std::cout << "]" << std::endl;
1112 // ===========================================================================================
1114 void Bruker2Dicom::MakeDicomImage(unsigned char *tabPixels,
1119 double spacingX, double spacingY, double sliceDistance,
1120 std::string dcmImageName,
1121 const std::string &patientName,
1123 std::string &studyUID,
1124 std::string &serieUID,
1125 std::string &studyDescr,
1126 std::string &serieDescr,
1127 std::string &strStudyTimeDate,
1129 GDCM_NAME_SPACE::ImageContentType contentType
1132 std::ostringstream str;
1134 GDCM_NAME_SPACE::File *file;
1135 file = GDCM_NAME_SPACE::File::New();
1137 // Set the image size
1140 file->InsertEntryString(str.str(),0x0028,0x0011,"US"); // Columns
1143 file->InsertEntryString(str.str(),0x0028,0x0010,"US"); // Rows
1149 file->InsertEntryString(str.str(),0x0028,0x0008,"IS"); // Number of Frames
1152 // Set the pixel type
1153 // //8, 16, 32, 64 (for double ?)
1156 file->InsertEntryString(str.str(),0x0028,0x0100,"US"); // Bits Allocated
1158 file->InsertEntryString(str.str(),0x0028,0x0101,"US"); // Bits Stored
1161 str << pixelSize*8-1;
1162 file->InsertEntryString(str.str(),0x0028,0x0102,"US"); // High Bit
1164 // Set the pixel representation // 0/1 , 0=unsigned
1165 file->InsertEntryString("1",0x0028,0x0103, "US"); // Pixel Representation
1167 // Set the samples per pixel // 1:Grey level, 3:RGB
1168 file->InsertEntryString("1",0x0028,0x0002, "US"); // Samples per Pixel
1170 // 0028 0030 DS 2 Pixel Spacing
1172 str << spacingX << "\\" << spacingY;
1173 file->InsertEntryString(str.str(),0x0028,0x0030, "DS"); // Pixel Spacing
1175 // 0018 0050 DS 1 Slice Thickness
1177 str << sliceDistance;
1178 file->InsertEntryString(str.str(),0x0018,0x0050, "DS");
1180 // 1.2.840.10008.5.1.4.1.1.4.1 : Enhanced MR Image Storage
1181 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1182 // file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4.1" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1185 // OK : MR is NOT multiframe, but I want just a quick an dirty solution
1186 // 1.2.840.10008.5.1.4.1.1.4 MR Image Storage
1187 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0002, 0x0002, "UI"); // [Media Storage SOP Class UID]
1188 file->InsertEntryString("1.2.840.10008.5.1.4.1.1.4" , 0x0008, 0x0016, "UI"); // [SOP Class UID]
1190 // if (strlen(patientName) != 0)
1191 file->InsertEntryString(patientName.c_str(),0x0010,0x0010, "PN"); // Patient's Name
1193 file->InsertEntryString(studyUID, 0x0020, 0x000d, "UI");
1194 file->InsertEntryString(serieUID, 0x0020, 0x000e, "UI");
1196 // 0008 0020 DA 1 Study Date
1197 // 0008 0030 TM 1 Study Time
1199 /// \TODO split into 2 strings!
1200 file->InsertEntryString(strStudyTimeDate.substr(10,11).c_str(),0x0008,0x0020, "DA");
1201 file->InsertEntryString(strStudyTimeDate.substr(1,8).c_str(),0x0008,0x0030, "TM");
1203 file->InsertEntryString(studyDescr,0x0008,0x1030, "LO"); // Study Description
1204 file->InsertEntryString(serieDescr,0x0008,0x103e, "LO"); // Series Description
1206 //0008|0060 [CS] [Modality]
1207 file->InsertEntryString("MR",0x0008,0x0060, "CS");
1209 // 0020 0037 DS 6 Image Orientation (Patient)
1210 char charImageOrientation[256];
1213 std::cout << "try charImageOrientation " <<
1214 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0] << " " <<
1215 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1] << " " <<
1216 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2] << " " <<
1217 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0] << " " <<
1218 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1] << " " <<
1219 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] << std::endl ;
1221 sprintf(charImageOrientation,"%f\\%f\\%f \\ %f\\%f\\%f",
1222 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][0],
1223 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][1],
1224 imageSet[imgNum].getRotationMatrixRPS2XYZ()[0][2],
1225 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][0],
1226 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][1],
1227 imageSet[imgNum].getRotationMatrixRPS2XYZ()[1][2] ) ;
1229 file->InsertEntryString(charImageOrientation,0x0020,0x0037, "DS");
1232 // 0020 0032 DS 3 Image Position (Patient)
1234 char charImagePosition[256];
1235 sprintf(charImagePosition,"%f\\%f\\%f",
1236 imageSet[imgNum].getTranslationVectorRPS2XYZ()[0],
1237 imageSet[imgNum].getTranslationVectorRPS2XYZ()[1],
1238 imageSet[imgNum].getTranslationVectorRPS2XYZ()[2]);
1240 file->InsertEntryString(charImagePosition,0x0020,0x0032, "DS"); //0020 0032 DS 3 Image Position (Patient)
1244 // 0020 0x1041 DS 1 Slice Location
1245 // sprintf(charImagePosition,"%f",float(imgNum));
1246 // file->InsertEntryString(charImagePosition,0x0020,0x1041, "DS");
1248 // Set Rescale Intercept
1251 file->InsertEntryString(str.str(),0x0028,0x1052,"DS");
1253 // Set Rescale Slope
1256 file->InsertEntryString(str.str(),0x0028,0x1053,"DS");
1259 GDCM_NAME_SPACE::FileHelper *fileH;
1260 fileH = GDCM_NAME_SPACE::FileHelper::New(file);
1261 fileH->SetContentType(contentType);
1263 // cast is just to avoid warnings (*no* conversion is performed)
1264 //fileH->SetImageData((uint8_t *)img,int(maxX*maxY)*sizeof(uint16_t)); // troubles when maxX, mayY are *actually* float!
1266 //std::cout << "-------------------------------- X*Y*nbFrames*pixelSize " << X << " " << Y << " " << nbFrames << " " << pixelSize << std::endl;
1268 fileH->SetImageData((uint8_t *)tabPixels, X*Y*nbFrames*pixelSize);
1269 fileH->SetWriteModeToRaw();
1270 fileH->SetWriteTypeToDcmExplVR();
1271 if( !fileH->Write(dcmImageName))
1272 std::cout << "Failed for [" << dcmImageName << "]\n"
1273 << " File is unwrittable" << std::endl;