2 //-----------------------------------------------------------------------------
3 #include "gdcmDicomDir.h"
7 #include "gdcmDirList.h"
13 #include <sys/types.h>
16 //-----------------------------------------------------------------------------
17 // Constructor / Destructor
19 * \ingroup gdcmDicomDir
22 * @param exception_on_error
24 gdcmDicomDir::gdcmDicomDir(const char *FileName, bool parseDir,
25 bool exception_on_error):
26 gdcmParser(FileName,exception_on_error,true)
28 if( GetListEntry().begin()==GetListEntry().end() )
30 dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : entry list empty");
34 dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : Parse directory and create the DicomDir");
43 * \ingroup gdcmDicomDir
45 * @param exception_on_error
47 /*gdcmDicomDir::gdcmDicomDir(ListTag *l,
48 bool exception_on_error):
49 gdcmParser(exception_on_error )
56 * \ingroup gdcmDicomDir
57 * \brief Canonical destructor
59 gdcmDicomDir::~gdcmDicomDir()
63 for(ListPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
69 //-----------------------------------------------------------------------------
72 * \ingroup gdcmDicomDir
73 * \brief Canonical Printer
75 void gdcmDicomDir::Print(std::ostream &os)
77 (*metaelems).SetPrintLevel(printLevel);
78 (*metaelems).Print(os);
80 for(ListPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
82 (*cc)->SetPrintLevel(printLevel);
87 //-----------------------------------------------------------------------------
90 * \ingroup gdcmDicomDir
91 * \brief writes on disc a DICOMDIR
92 * \ warning does NOT add the missing elements in the header :
93 * it's up to the user doing it !
94 * @param fileName file to be written to
95 * @return false only when fail to open
97 bool gdcmDicomDir::Write(std::string fileName)
101 fp1=fopen(fileName.c_str(),"wb");
104 printf("Failed to open(write) File [%s] \n",fileName.c_str());
109 filePreamble=(char*)calloc(128,1);
110 fwrite(filePreamble,128,1,fp1);
111 fwrite("DICM",4,1,fp1);
114 WriteEntries(fp1,DICOMDIR);
122 * \ingroup gdcmDicomDir
123 * \brief fills whole the structure
125 void gdcmDicomDir::ParseDirectory(void)
127 NewDicomDir(GetPath());
131 //-----------------------------------------------------------------------------
134 * \ingroup gdcmDicomDir
135 * \brief create a gdcmDicomDir from a root Directory
136 * @param path entry point of the stree-like structure
138 void gdcmDicomDir::NewDicomDir(std::string path)
140 gdcmDirList fileList(path,1);
147 for(gdcmDirList::iterator it=fileList.begin();
148 it!=fileList.end(); ++it)
150 header=new gdcmHeader(it->c_str());
151 if(header->IsReadable())
152 list.push_back(header);
157 std::sort(list.begin(),list.end(),gdcmDicomDir::HeaderLessThan);
159 std::string tmp=fileList.GetDirName();
160 SetElements(tmp,list);
164 * \ingroup gdcmDicomDir
165 * \brief Get the DicomDir path
168 std::string gdcmDicomDir::GetPath(void)
170 std::string path=GetFileName();
172 int pos1=path.rfind("/");
173 int pos2=path.rfind("\\");
182 //-----------------------------------------------------------------------------
185 * \ingroup gdcmDicomDir
186 * \brief create a 'gdcmDicomDir' from a DICOMDIR gdcmHeader
188 void gdcmDicomDir::CreateDicomDir()
190 // The list is parsed. When a tag is found :
191 // 1 - we save the beginning iterator
192 // 2 - we continue to parse
193 // 3 - we find an other tag
194 // + we create the object for the precedent tag
197 gdcmDicomDirType type=gdcmDicomDir::GDCM_NONE;
198 ListTag::iterator begin;
199 ListTag::iterator end;
202 begin=listEntries.begin();
205 for(ListTag::iterator j=begin;j !=listEntries.end();++j)
207 if((*j)->GetValue()=="PATIENT ") {
212 AddObjectToEnd(gdcmDicomDir::GDCM_META,begin,k);
214 for(ListTag::iterator i=k;i !=listEntries.end();++i)
216 std::string v=(*i)->GetValue();
220 AddObjectToEnd(type,begin,end);
222 type=gdcmDicomDir::GDCM_PATIENT;
229 AddObjectToEnd(type,begin,end);
231 type=gdcmDicomDir::GDCM_STUDY;
238 AddObjectToEnd(type,begin,end);
240 type=gdcmDicomDir::GDCM_SERIE;
247 AddObjectToEnd(type,begin,end);
249 type=gdcmDicomDir::GDCM_IMAGE;
254 end=GetListEntry().end();
255 AddObjectToEnd(type,begin,end);
258 * \ingroup gdcmDicomDir
264 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
271 case gdcmDicomDir::GDCM_META:
272 AddMetaToEnd(begin,end);
274 case gdcmDicomDir::GDCM_PATIENT:
275 AddPatientToEnd(begin,end);
277 case gdcmDicomDir::GDCM_STUDY:
278 AddStudyToEnd(begin,end);
280 case gdcmDicomDir::GDCM_SERIE:
281 AddSerieToEnd(begin,end);
283 case gdcmDicomDir::GDCM_IMAGE:
284 AddImageToEnd(begin,end);
291 * \ingroup gdcmDicomDir
292 * \brief Well ... Not realy to end, there is only one occurence
296 void gdcmDicomDir::AddMetaToEnd(ListTag::iterator begin,ListTag::iterator end)
298 metaelems = new gdcmMeta(begin,end);
302 * \ingroup gdcmDicomDir
307 void gdcmDicomDir::AddPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
309 patients.push_back(new gdcmPatient(begin,end));
313 * \ingroup gdcmDicomDir
318 void gdcmDicomDir::AddStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
320 if(patients.size()>0)
322 ListPatient::iterator itp=patients.end();
324 (*itp)->AddStudy(new gdcmStudy(begin,end));
328 * \ingroup gdcmDicomDir
333 void gdcmDicomDir::AddSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
335 if(patients.size()>0)
337 ListPatient::iterator itp=patients.end();
340 if((*itp)->GetStudies().size()>0)
342 ListStudy::iterator itst=(*itp)->GetStudies().end();
344 (*itst)->AddSerie(new gdcmSerie(begin,end));
350 * \ingroup gdcmDicomDir
355 void gdcmDicomDir::AddImageToEnd(ListTag::iterator begin,ListTag::iterator end)
357 if(patients.size()>0)
359 ListPatient::iterator itp=patients.end();
362 if((*itp)->GetStudies().size()>0)
364 ListStudy::iterator itst=(*itp)->GetStudies().end();
367 if((*itst)->GetSeries().size()>0)
369 ListSerie::iterator its=(*itst)->GetSeries().end();
371 (*its)->AddImage(new gdcmImage(begin,end));
378 * \ingroup gdcmDicomDir
383 void gdcmDicomDir::SetElements(std::string &path,ListHeader &list)
385 std::string patPrevName="", patPrevID="";
386 std::string studPrevInstanceUID="", studPrevID="";
387 std::string serPrevInstanceUID="", serPrevID="";
389 std::string patCurName, patCurID;
390 std::string studCurInstanceUID, studCurID;
391 std::string serCurInstanceUID, serCurID;
393 SetElement(path,GDCM_NONE,NULL);
395 ListTag::iterator debPat=listEntries.begin();
396 for(ListHeader::iterator it=list.begin();it!=list.end();++it)
398 // get the current file characteristics
399 patCurName=(*it)->GetEntryByNumber(0x0010,0x0010);
400 patCurID=(*it)->GetEntryByNumber(0x0010,0x0011);
401 studCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000d);
402 studCurID=(*it)->GetEntryByNumber(0x0020,0x0010);
403 serCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000e);
404 serCurID=(*it)->GetEntryByNumber(0x0020,0x0011);
406 if(patCurName!=patPrevName || patCurID!=patPrevID)
407 SetElement(path,GDCM_PATIENT,*it);
409 // if new Study Deal with 'STUDY' Elements
410 if(studCurInstanceUID!=studPrevInstanceUID || studCurID!=studPrevID)
411 SetElement(path,GDCM_STUDY,*it);
413 // if new Serie Deal with 'SERIE' Elements
414 if(serCurInstanceUID!=serPrevInstanceUID || serCurID!=serPrevID)
415 SetElement(path,GDCM_SERIE,*it);
417 // Always Deal with 'IMAGE' Elements
418 SetElement(path,GDCM_IMAGE,*it);
420 patPrevName=patCurName;
422 studPrevInstanceUID=studCurInstanceUID;
423 studPrevID=studCurID;
424 serPrevInstanceUID=serCurInstanceUID;
430 * \ingroup gdcmDicomDir
436 void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader *header)
438 std::list<gdcmElement> elemList;
439 std::list<gdcmElement>::iterator it;
440 guint16 tmpGr, tmpEl;
441 gdcmDictEntry *dictEntry;
442 gdcmHeaderEntry *entry;
448 elemList=gdcmGlobal::GetDicomDirElements()->GetPatientElements();
451 elemList=gdcmGlobal::GetDicomDirElements()->GetStudyElements();
454 elemList=gdcmGlobal::GetDicomDirElements()->GetSerieElements();
457 elemList=gdcmGlobal::GetDicomDirElements()->GetImageElements();
460 elemList=gdcmGlobal::GetDicomDirElements()->GetMetaElements();
466 for(it=elemList.begin();it!=elemList.end();++it)
471 dictEntry=GetPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
472 entry=new gdcmHeaderEntry(dictEntry);
473 entry->SetOffset(0); // just to avoid missprinting
476 val=header->GetEntryByNumber(tmpGr,tmpEl);
480 if(val==GDCM_UNFOUND)
482 if((tmpGr==0x0004) &&(tmpEl==0x1130) )
484 // TODO force the *end* File Name(remove path)
487 else if( (tmpGr==0x0004) && (tmpEl==0x1500) ) // Only used for image
489 if(header->GetFileName().substr(0,path.length())!=path)
491 dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path of file name is incorrect");
492 val=header->GetFileName();
495 val=&(header->GetFileName()[path.length()]);
502 entry->SetValue(val);
506 if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") )
510 else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") )
514 else if(dictEntry->GetVR()=="SQ")
516 entry->SetLength(0xffffffff);
520 entry->SetLength(entry->GetValue().length());
524 listEntries.push_back(entry);
528 bool gdcmDicomDir::HeaderLessThan(gdcmHeader *header1,gdcmHeader *header2)
530 return(*header1<*header2);
533 //-----------------------------------------------------------------------------