2 //-----------------------------------------------------------------------------
3 #include "gdcmDicomDir.h"
7 #include "gdcmDirList.h"
13 #include <sys/types.h>
15 //-----------------------------------------------------------------------------
16 // For full DICOMDIR description, see:
17 // PS 3.3-2003, pages 731-750
18 //-----------------------------------------------------------------------------
19 // Constructor / Destructor
21 * \ingroup gdcmDicomDir
24 * @param exception_on_error
26 gdcmDicomDir::gdcmDicomDir(const char *FileName, bool parseDir,
27 bool exception_on_error):
28 gdcmParser(FileName,exception_on_error,true)
32 if( GetListEntry().begin()==GetListEntry().end() )
34 dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : entry list empty");
38 dbg.Verbose(0, "gdcmDicomDir::gdcmDicomDir : Parse directory and create the DicomDir");
47 * \ingroup gdcmDicomDir
48 * \brief Canonical destructor
50 gdcmDicomDir::~gdcmDicomDir()
55 for(ListPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
61 //-----------------------------------------------------------------------------
64 * \ingroup gdcmDicomDir
65 * \brief Canonical Printer
67 void gdcmDicomDir::Print(std::ostream &os)
71 metaElems->SetPrintLevel(printLevel);
75 for(ListPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
77 (*cc)->SetPrintLevel(printLevel);
82 //-----------------------------------------------------------------------------
85 * \ingroup gdcmDicomDir
86 * \brief writes on disc a DICOMDIR
87 * \ warning does NOT add the missing elements in the header :
88 * it's up to the user doing it !
89 * @param fileName file to be written to
90 * @return false only when fail to open
92 bool gdcmDicomDir::Write(std::string fileName)
96 fp1=fopen(fileName.c_str(),"wb");
99 printf("Failed to open(write) File [%s] \n",fileName.c_str());
104 filePreamble=(char*)calloc(128,1);
105 fwrite(filePreamble,128,1,fp1);
106 fwrite("DICM",4,1,fp1);
109 WriteEntries(fp1,DICOMDIR);
117 * \ingroup gdcmDicomDir
118 * \brief fills whole the structure
120 void gdcmDicomDir::ParseDirectory(void)
122 NewDicomDir(GetPath());
126 //-----------------------------------------------------------------------------
129 * \ingroup gdcmDicomDir
130 * \brief create a gdcmDicomDir from a root Directory
131 * @param path entry point of the stree-like structure
133 void gdcmDicomDir::NewDicomDir(std::string path)
135 gdcmDirList fileList(path,1);
142 for(gdcmDirList::iterator it=fileList.begin();
143 it!=fileList.end(); ++it)
145 header=new gdcmHeader(it->c_str());
146 if(header->IsReadable())
147 list.push_back(header);
152 std::sort(list.begin(),list.end(),gdcmDicomDir::HeaderLessThan);
154 std::string tmp=fileList.GetDirName();
155 SetElements(tmp,list);
159 * \ingroup gdcmDicomDir
160 * \brief Get the DicomDir path
163 std::string gdcmDicomDir::GetPath(void)
165 std::string path=GetFileName();
167 int pos1=path.rfind("/");
168 int pos2=path.rfind("\\");
177 //-----------------------------------------------------------------------------
180 * \ingroup gdcmDicomDir
181 * \brief create a 'gdcmDicomDir' from a DICOMDIR gdcmHeader
183 void gdcmDicomDir::CreateDicomDir()
185 // The list is parsed. When a tag is found :
186 // 1 - we save the beginning iterator
187 // 2 - we continue to parse
188 // 3 - we find an other tag
189 // + we create the object for the precedent tag
192 gdcmDicomDirType type=gdcmDicomDir::GDCM_META;
193 ListTag::iterator begin;
194 ListTag::iterator end;
196 begin=listEntries.begin();
198 for(ListTag::iterator i=end;i !=listEntries.end();++i)
200 std::string v=(*i)->GetValue();
204 AddObjectToEnd(type,begin,end);
206 type=gdcmDicomDir::GDCM_PATIENT;
213 AddObjectToEnd(type,begin,end);
215 type=gdcmDicomDir::GDCM_STUDY;
222 AddObjectToEnd(type,begin,end);
224 type=gdcmDicomDir::GDCM_SERIE;
231 AddObjectToEnd(type,begin,end);
233 type=gdcmDicomDir::GDCM_IMAGE;
240 end=GetListEntry().end();
241 AddObjectToEnd(type,begin,end);
245 * \ingroup gdcmDicomDir
251 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
258 case gdcmDicomDir::GDCM_META:
259 AddMetaToEnd(begin,end);
261 case gdcmDicomDir::GDCM_PATIENT:
262 AddPatientToEnd(begin,end);
264 case gdcmDicomDir::GDCM_STUDY:
265 AddStudyToEnd(begin,end);
267 case gdcmDicomDir::GDCM_SERIE:
268 AddSerieToEnd(begin,end);
270 case gdcmDicomDir::GDCM_IMAGE:
271 AddImageToEnd(begin,end);
277 * \ingroup gdcmDicomDir
278 * \brief Well ... Not realy to end, there is only one occurence
282 void gdcmDicomDir::AddMetaToEnd(ListTag::iterator begin,ListTag::iterator end)
286 metaElems = new gdcmMeta(begin,end);
290 * \ingroup gdcmDicomDir
295 void gdcmDicomDir::AddPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
297 patients.push_back(new gdcmPatient(begin,end));
301 * \ingroup gdcmDicomDir
306 void gdcmDicomDir::AddStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
308 if(patients.size()>0)
310 ListPatient::iterator itp=patients.end();
312 (*itp)->AddStudy(new gdcmStudy(begin,end));
316 * \ingroup gdcmDicomDir
321 void gdcmDicomDir::AddSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
323 if(patients.size()>0)
325 ListPatient::iterator itp=patients.end();
328 if((*itp)->GetStudies().size()>0)
330 ListStudy::iterator itst=(*itp)->GetStudies().end();
332 (*itst)->AddSerie(new gdcmSerie(begin,end));
338 * \ingroup gdcmDicomDir
343 void gdcmDicomDir::AddImageToEnd(ListTag::iterator begin,ListTag::iterator end)
345 if(patients.size()>0)
347 ListPatient::iterator itp=patients.end();
350 if((*itp)->GetStudies().size()>0)
352 ListStudy::iterator itst=(*itp)->GetStudies().end();
355 if((*itst)->GetSeries().size()>0)
357 ListSerie::iterator its=(*itst)->GetSeries().end();
359 (*its)->AddImage(new gdcmImage(begin,end));
366 * \ingroup gdcmDicomDir
371 void gdcmDicomDir::SetElements(std::string &path,ListHeader &list)
373 std::string patPrevName="", patPrevID="";
374 std::string studPrevInstanceUID="", studPrevID="";
375 std::string serPrevInstanceUID="", serPrevID="";
377 std::string patCurName, patCurID;
378 std::string studCurInstanceUID, studCurID;
379 std::string serCurInstanceUID, serCurID;
381 SetElement(path,GDCM_NONE,NULL);
383 ListTag::iterator debPat=listEntries.begin();
384 for(ListHeader::iterator it=list.begin();it!=list.end();++it)
386 // get the current file characteristics
387 patCurName=(*it)->GetEntryByNumber(0x0010,0x0010);
388 patCurID=(*it)->GetEntryByNumber(0x0010,0x0011);
389 studCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000d);
390 studCurID=(*it)->GetEntryByNumber(0x0020,0x0010);
391 serCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000e);
392 serCurID=(*it)->GetEntryByNumber(0x0020,0x0011);
394 if(patCurName!=patPrevName || patCurID!=patPrevID)
395 SetElement(path,GDCM_PATIENT,*it);
397 // if new Study Deal with 'STUDY' Elements
398 if(studCurInstanceUID!=studPrevInstanceUID || studCurID!=studPrevID)
399 SetElement(path,GDCM_STUDY,*it);
401 // if new Serie Deal with 'SERIE' Elements
402 if(serCurInstanceUID!=serPrevInstanceUID || serCurID!=serPrevID)
403 SetElement(path,GDCM_SERIE,*it);
405 // Always Deal with 'IMAGE' Elements
406 SetElement(path,GDCM_IMAGE,*it);
408 patPrevName=patCurName;
410 studPrevInstanceUID=studCurInstanceUID;
411 studPrevID=studCurID;
412 serPrevInstanceUID=serCurInstanceUID;
418 * \ingroup gdcmDicomDir
424 void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader *header)
426 std::list<gdcmElement> elemList;
427 std::list<gdcmElement>::iterator it;
428 guint16 tmpGr, tmpEl;
429 gdcmDictEntry *dictEntry;
430 gdcmHeaderEntry *entry;
436 elemList=gdcmGlobal::GetDicomDirElements()->GetPatientElements();
439 elemList=gdcmGlobal::GetDicomDirElements()->GetStudyElements();
442 elemList=gdcmGlobal::GetDicomDirElements()->GetSerieElements();
445 elemList=gdcmGlobal::GetDicomDirElements()->GetImageElements();
448 elemList=gdcmGlobal::GetDicomDirElements()->GetMetaElements();
454 for(it=elemList.begin();it!=elemList.end();++it)
459 dictEntry=GetPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
460 entry=new gdcmHeaderEntry(dictEntry);
461 entry->SetOffset(0); // just to avoid missprinting
464 val=header->GetEntryByNumber(tmpGr,tmpEl);
468 if(val==GDCM_UNFOUND)
470 if((tmpGr==0x0004) &&(tmpEl==0x1130) )
472 // TODO force the *end* File Name(remove path)
475 else if( (tmpGr==0x0004) && (tmpEl==0x1500) ) // Only used for image
477 if(header->GetFileName().substr(0,path.length())!=path)
479 dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path of file name is incorrect");
480 val=header->GetFileName();
483 val=&(header->GetFileName()[path.length()]);
490 entry->SetValue(val);
494 if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") )
498 else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") )
502 else if(dictEntry->GetVR()=="SQ")
504 entry->SetLength(0xffffffff);
508 entry->SetLength(entry->GetValue().length());
512 listEntries.push_back(entry);
516 bool gdcmDicomDir::HeaderLessThan(gdcmHeader *header1,gdcmHeader *header2)
518 return(*header1<*header2);
521 //-----------------------------------------------------------------------------