2 //-----------------------------------------------------------------------------
3 #include "gdcmDicomDir.h"
4 #include "gdcmDicomDirStudy.h"
5 #include "gdcmDicomDirSerie.h"
6 #include "gdcmDicomDirImage.h"
7 #include "gdcmDirList.h"
13 #include <sys/types.h>
16 //-----------------------------------------------------------------------------
17 // For full DICOMDIR description, see:
18 // PS 3.3-2003, pages 731-750
19 //-----------------------------------------------------------------------------
20 // Constructor / Destructor
22 * \ingroup gdcmDicomDir
23 * \brief Constructor : Parses recursively the directory and creates the DicomDir
24 * \ or uses an already built DICOMDIR, depending on 'parseDir' value
25 * @param Name name of the root directory (parseDir = true)
26 * name of the DICOMDIR (parseDir = false)
27 * @param parseDir true if user wants to explore the directories
28 * @param exception_on_error
30 gdcmDicomDir::gdcmDicomDir(const char *Name, bool parseDir,
31 bool exception_on_error):
32 gdcmParser(Name,exception_on_error,true)
37 startMethodArgDelete= NULL;
38 progressMethodArgDelete=NULL;
39 endMethodArgDelete= NULL;
49 if( GetListEntry().begin()==GetListEntry().end() )
51 dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : entry list empty");
55 dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : Parse directory and create the DicomDir");
64 * \ingroup gdcmDicomDir
65 * \brief Canonical destructor
67 gdcmDicomDir::~gdcmDicomDir()
70 SetProgressMethod(NULL);
76 for(ListDicomDirPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
82 //-----------------------------------------------------------------------------
85 * \ingroup gdcmDicomDir
86 * \brief Canonical Printer
88 void gdcmDicomDir::Print(std::ostream &os)
92 metaElems->SetPrintLevel(printLevel);
96 for(ListDicomDirPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
98 (*cc)->SetPrintLevel(printLevel);
103 //-----------------------------------------------------------------------------
106 * \ingroup gdcmDicomDir
107 * \brief This predicate, based on hopefully reasonable heuristics,
108 * decides whether or not the current gdcmParser was properly parsed
109 * and contains the mandatory information for being considered as
110 * a well formed and usable DicomDir.
111 * @return true when gdcmParser is the one of a reasonable DicomDir,
114 bool gdcmDicomDir::IsReadable(void)
116 if(!gdcmParser::IsReadable())
120 if(patients.size()<=0)
127 * \ingroup gdcmDicomDir
128 * \brief fills whole the structure
130 void gdcmDicomDir::ParseDirectory(void)
132 NewDicomDir(GetPath());
137 * \ingroup gdcmDicomDir
138 * \brief Set the start method to call when the parsing of the directory starts
139 * @param method Method to call
140 * @param arg Argument to pass to the method
141 * \warning In python : the arg parameter isn't considered
143 void gdcmDicomDir::SetStartMethod(gdcmMethod *method,void *arg,gdcmMethod *argDelete)
145 if((startArg)&&(startMethodArgDelete))
146 startMethodArgDelete(startArg);
150 startMethodArgDelete=argDelete;
154 * \ingroup gdcmDicomDir
155 * \brief Set the method to delete the argument
156 * The argument is destroyed when the method is changed or when the class
158 * @param method Method to call to delete the argument
160 void gdcmDicomDir::SetStartMethodArgDelete(gdcmMethod *method)
162 startMethodArgDelete=method;
166 * \ingroup gdcmDicomDir
167 * \brief Set the progress method to call when the parsing of the directory progress
168 * @param method Method to call
169 * @param arg Argument to pass to the method
170 * \warning In python : the arg parameter isn't considered
172 void gdcmDicomDir::SetProgressMethod(gdcmMethod *method,void *arg,gdcmMethod *argDelete)
174 if((progressArg)&&(progressMethodArgDelete))
175 progressMethodArgDelete(progressArg);
177 progressMethod=method;
179 progressMethodArgDelete=argDelete;
183 * \ingroup gdcmDicomDir
184 * \brief Set the method to delete the argument
185 * The argument is destroyed when the method is changed or when the class
187 * @param method Method to call to delete the argument
189 void gdcmDicomDir::SetProgressMethodArgDelete(gdcmMethod *method)
191 progressMethodArgDelete=method;
195 * \ingroup gdcmDicomDir
196 * \brief Set the end method to call when the parsing of the directory ends
197 * @param method Method to call
198 * @param arg Argument to pass to the method
199 * \warning In python : the arg parameter isn't considered
201 void gdcmDicomDir::SetEndMethod(gdcmMethod *method,void *arg,gdcmMethod *argDelete)
203 if((endArg)&&(endMethodArgDelete))
204 endMethodArgDelete(endArg);
208 endMethodArgDelete=argDelete;
212 * \ingroup gdcmDicomDir
213 * \brief Set the method to delete the argument
214 * The argument is destroyed when the method is changed or when the class
216 * @param method Method to call to delete the argument
218 void gdcmDicomDir::SetEndMethodArgDelete(gdcmMethod *method)
220 endMethodArgDelete=method;
224 * \ingroup gdcmDicomDir
225 * \brief writes on disc a DICOMDIR
226 * \ warning does NOT add the missing elements in the header :
227 * it's up to the user doing it !
228 * @param fileName file to be written to
229 * @return false only when fail to open
231 bool gdcmDicomDir::Write(std::string fileName)
235 fp1=fopen(fileName.c_str(),"wb");
238 printf("Failed to open(write) File [%s] \n",fileName.c_str());
243 filePreamble=(char*)calloc(128,1);
244 fwrite(filePreamble,128,1,fp1);
245 fwrite("DICM",4,1,fp1);
247 UpdateDirectoryRecordSequenceLength();
248 WriteEntries(fp1,DICOMDIR);
255 //-----------------------------------------------------------------------------
258 * \ingroup gdcmDicomDir
259 * \brief create a gdcmDicomDir from a root Directory
260 * @param path entry point of the stree-like structure
262 void gdcmDicomDir::NewDicomDir(std::string path)
266 gdcmDirList fileList(path,1);
267 unsigned int count=0;
274 for(gdcmDirList::iterator it=fileList.begin();
275 it!=fileList.end(); ++it)
277 progress=(float)(count+1)/(float)fileList.size();
278 CallProgressMethod();
282 header=new gdcmHeader(it->c_str());
283 if(header->IsReadable())
284 list.push_back(header);
291 std::sort(list.begin(),list.end(),gdcmDicomDir::HeaderLessThan);
293 std::string tmp=fileList.GetDirName();
294 SetElements(tmp,list);
300 * \ingroup gdcmDicomDir
301 * \brief Get the DicomDir path
304 std::string gdcmDicomDir::GetPath(void)
306 std::string path=GetFileName();
308 int pos1=path.rfind("/");
309 int pos2=path.rfind("\\");
318 void gdcmDicomDir::CallStartMethod(void)
323 startMethod(startArg);
326 void gdcmDicomDir::CallProgressMethod(void)
329 progressMethod(progressArg);
332 void gdcmDicomDir::CallEndMethod(void)
339 //-----------------------------------------------------------------------------
342 * \ingroup gdcmDicomDir
343 * \brief create a 'gdcmDicomDir' from a DICOMDIR gdcmHeader
345 void gdcmDicomDir::CreateDicomDir()
347 // The list is parsed. When a tag is found :
348 // 1 - we save the beginning iterator
349 // 2 - we continue to parse
350 // 3 - we find an other tag
351 // + we create the object for the precedent tag
354 gdcmDicomDirType type=gdcmDicomDir::GDCM_DICOMDIR_META;
355 ListTag::iterator begin;
356 ListTag::iterator end;
358 begin=listEntries.begin();
360 for(ListTag::iterator i=end;i !=listEntries.end();++i)
362 std::string v=(*i)->GetValue();
366 AddObjectToEnd(type,begin,end);
368 type=gdcmDicomDir::GDCM_DICOMDIR_PATIENT;
375 AddObjectToEnd(type,begin,end);
377 type=gdcmDicomDir::GDCM_DICOMDIR_STUDY;
384 AddObjectToEnd(type,begin,end);
386 type=gdcmDicomDir::GDCM_DICOMDIR_SERIE;
393 AddObjectToEnd(type,begin,end);
395 type=gdcmDicomDir::GDCM_DICOMDIR_IMAGE;
400 end=GetListEntry().end();
402 AddObjectToEnd(type,begin,end);
405 * \ingroup gdcmDicomDir
411 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
418 case gdcmDicomDir::GDCM_DICOMDIR_META:
419 AddDicomDirMetaToEnd(begin,end);
421 case gdcmDicomDir::GDCM_DICOMDIR_PATIENT:
422 AddDicomDirPatientToEnd(begin,end);
424 case gdcmDicomDir::GDCM_DICOMDIR_STUDY:
425 AddDicomDirStudyToEnd(begin,end);
427 case gdcmDicomDir::GDCM_DICOMDIR_SERIE:
428 AddDicomDirSerieToEnd(begin,end);
430 case gdcmDicomDir::GDCM_DICOMDIR_IMAGE:
431 AddDicomDirImageToEnd(begin,end);
437 * \ingroup gdcmDicomDir
438 * \brief Well ... Not realy to end, there is only one occurence
442 void gdcmDicomDir::AddDicomDirMetaToEnd(ListTag::iterator begin,ListTag::iterator end)
446 metaElems = new gdcmDicomDirMeta(begin,end);
450 * \ingroup gdcmDicomDir
455 void gdcmDicomDir::AddDicomDirPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
457 patients.push_back(new gdcmDicomDirPatient(begin,end));
461 * \ingroup gdcmDicomDir
466 void gdcmDicomDir::AddDicomDirStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
468 if(patients.size()>0)
470 ListDicomDirPatient::iterator itp=patients.end();
472 (*itp)->AddDicomDirStudy(new gdcmDicomDirStudy(begin,end));
476 * \ingroup gdcmDicomDir
481 void gdcmDicomDir::AddDicomDirSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
483 if(patients.size()>0)
485 ListDicomDirPatient::iterator itp=patients.end();
488 if((*itp)->GetDicomDirStudies().size()>0)
490 ListDicomDirStudy::iterator itst=(*itp)->GetDicomDirStudies().end();
492 (*itst)->AddDicomDirSerie(new gdcmDicomDirSerie(begin,end));
498 * \ingroup gdcmDicomDir
503 void gdcmDicomDir::AddDicomDirImageToEnd(ListTag::iterator begin,ListTag::iterator end)
505 if(patients.size()>0)
507 ListDicomDirPatient::iterator itp=patients.end();
510 if((*itp)->GetDicomDirStudies().size()>0)
512 ListDicomDirStudy::iterator itst=(*itp)->GetDicomDirStudies().end();
515 if((*itst)->GetDicomDirSeries().size()>0)
517 ListDicomDirSerie::iterator its=(*itst)->GetDicomDirSeries().end();
519 (*its)->AddDicomDirImage(new gdcmDicomDirImage(begin,end));
526 * \ingroup gdcmDicomDir
531 void gdcmDicomDir::SetElements(std::string &path,ListHeader &list)
533 std::string patPrevName="", patPrevID="";
534 std::string studPrevInstanceUID="", studPrevID="";
535 std::string serPrevInstanceUID="", serPrevID="";
537 std::string patCurName, patCurID;
538 std::string studCurInstanceUID, studCurID;
539 std::string serCurInstanceUID, serCurID;
541 SetElement(path,GDCM_DICOMDIR_NONE,NULL);
543 ListTag::iterator debPat=listEntries.begin();
544 for(ListHeader::iterator it=list.begin();it!=list.end();++it)
546 // get the current file characteristics
547 patCurName= (*it)->GetEntryByNumber(0x0010,0x0010);
548 patCurID= (*it)->GetEntryByNumber(0x0010,0x0011);
549 studCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000d);
550 studCurID= (*it)->GetEntryByNumber(0x0020,0x0010);
551 serCurInstanceUID= (*it)->GetEntryByNumber(0x0020,0x000e);
552 serCurID= (*it)->GetEntryByNumber(0x0020,0x0011);
554 if(patCurName!=patPrevName || patCurID!=patPrevID)
555 SetElement(path,GDCM_DICOMDIR_PATIENT,*it);
557 // if new Study Deal with 'STUDY' Elements
558 if(studCurInstanceUID!=studPrevInstanceUID || studCurID!=studPrevID)
559 SetElement(path,GDCM_DICOMDIR_STUDY,*it);
561 // if new Serie Deal with 'SERIE' Elements
562 if(serCurInstanceUID!=serPrevInstanceUID || serCurID!=serPrevID)
563 SetElement(path,GDCM_DICOMDIR_SERIE,*it);
565 // Always Deal with 'IMAGE' Elements
566 SetElement(path,GDCM_DICOMDIR_IMAGE,*it);
568 patPrevName= patCurName;
570 studPrevInstanceUID=studCurInstanceUID;
571 studPrevID= studCurID;
572 serPrevInstanceUID= serCurInstanceUID;
578 * \ingroup gdcmDicomDir
584 void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader *header)
586 std::list<gdcmElement> elemList;
587 std::list<gdcmElement>::iterator it;
588 guint16 tmpGr, tmpEl;
589 gdcmDictEntry *dictEntry;
590 gdcmHeaderEntry *entry;
595 case GDCM_DICOMDIR_PATIENT:
596 elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirPatientElements();
598 case GDCM_DICOMDIR_STUDY:
599 elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirStudyElements();
601 case GDCM_DICOMDIR_SERIE:
602 elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirSerieElements();
604 case GDCM_DICOMDIR_IMAGE:
605 elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirImageElements();
607 case GDCM_DICOMDIR_NONE:
608 elemList=gdcmGlobal::GetDicomDirElements()->GetDicomDirMetaElements();
614 for(it=elemList.begin();it!=elemList.end();++it)
619 dictEntry=GetPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
620 entry=new gdcmHeaderEntry(dictEntry);
621 entry->SetOffset(0); // just to avoid missprinting
624 val=header->GetEntryByNumber(tmpGr,tmpEl);
628 if(val==GDCM_UNFOUND)
630 if((tmpGr==0x0004) &&(tmpEl==0x1130) )
632 // TODO force the *end* File Name(remove path)
635 else if( (tmpGr==0x0004) && (tmpEl==0x1500) ) // Only used for image
637 if(header->GetFileName().substr(0,path.length())!=path)
639 dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path of file name is incorrect");
640 val=header->GetFileName();
643 val=&(header->GetFileName()[path.length()]);
652 if (header->GetEntryLengthByNumber(tmpGr,tmpEl)== 0)
656 entry->SetValue(val);
660 if(dictEntry->GetGroup()==0xfffe)
662 entry->SetLength(entry->GetValue().length()); //JPR
664 else if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") )
668 else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") )
672 else if(dictEntry->GetVR()=="SQ")
674 entry->SetLength(0xffffffff);
678 entry->SetLength(entry->GetValue().length());
681 //listEntries.push_back(entry);
682 AddHeaderEntry(entry);
686 bool gdcmDicomDir::HeaderLessThan(gdcmHeader *header1,gdcmHeader *header2)
688 return(*header1<*header2);
693 void gdcmDicomDir::UpdateDirectoryRecordSequenceLength() {
695 ListTag::iterator it;
698 for(it=listEntries.begin();it!=listEntries.end();++it) {
699 gr = (*it)->GetGroup();
700 el = (*it)->GetElement();
703 if ( (vr == "OB") || (vr == "OW") || (vr == "SQ") ) {
704 offset += 4; // explicit VR AND OB, OW, SQ : 4 more bytes
706 offset += 2 + 2 + 4 + (*it)->GetLength();
708 offset += 4; // delimiters don't have a value.
711 bool res=SetEntryLengthByNumber(offset, 0x0004, 0x1220); // Hope there is no dupps.
715 //-----------------------------------------------------------------------------