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()
61 for(ListPatient::iterator cc=patients.begin();cc!=patients.end();++cc)
67 //-----------------------------------------------------------------------------
70 * \ingroup gdcmDicomDir
71 * \brief Canonical Printer
73 void gdcmDicomDir::Print(std::ostream &os)
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
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 dicom dir 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
184 void gdcmDicomDir::CreateDicomDir()
186 // The list is parsed. When a tag is found :
187 // 1 - we save the beginning iterator
188 // 2 - we continue to parse
189 // 3 - we find an other tag
190 // + we create the object for the precedent tag
193 gdcmDicomDirType type=gdcmDicomDir::GDCM_NONE;
194 ListTag::iterator begin;
195 ListTag::iterator end;
197 begin=listEntries.begin();
199 for(ListTag::iterator i=listEntries.begin();i !=listEntries.end();++i)
201 std::string v=(*i)->GetValue();
205 AddObjectToEnd(type,begin,end);
207 type=gdcmDicomDir::GDCM_PATIENT;
214 AddObjectToEnd(type,begin,end);
216 type=gdcmDicomDir::GDCM_STUDY;
223 AddObjectToEnd(type,begin,end);
225 type=gdcmDicomDir::GDCM_SERIE;
232 AddObjectToEnd(type,begin,end);
234 type=gdcmDicomDir::GDCM_IMAGE;
239 end=GetListEntry().end();
240 AddObjectToEnd(type,begin,end);
243 * \ingroup gdcmDicomDir
249 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
256 case gdcmDicomDir::GDCM_PATIENT:
257 AddPatientToEnd(begin,end);
259 case gdcmDicomDir::GDCM_STUDY:
260 AddStudyToEnd(begin,end);
262 case gdcmDicomDir::GDCM_SERIE:
263 AddSerieToEnd(begin,end);
265 case gdcmDicomDir::GDCM_IMAGE:
266 AddImageToEnd(begin,end);
272 * \ingroup gdcmDicomDir
277 void gdcmDicomDir::AddPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
279 patients.push_back(new gdcmPatient(begin,end));
283 * \ingroup gdcmDicomDir
288 void gdcmDicomDir::AddStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
290 if(patients.size()>0)
292 ListPatient::iterator itp=patients.end();
294 (*itp)->AddStudy(new gdcmStudy(begin,end));
298 * \ingroup gdcmDicomDir
303 void gdcmDicomDir::AddSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
305 if(patients.size()>0)
307 ListPatient::iterator itp=patients.end();
310 if((*itp)->GetStudies().size()>0)
312 ListStudy::iterator itst=(*itp)->GetStudies().end();
314 (*itst)->AddSerie(new gdcmSerie(begin,end));
320 * \ingroup gdcmDicomDir
325 void gdcmDicomDir::AddImageToEnd(ListTag::iterator begin,ListTag::iterator end)
327 if(patients.size()>0)
329 ListPatient::iterator itp=patients.end();
332 if((*itp)->GetStudies().size()>0)
334 ListStudy::iterator itst=(*itp)->GetStudies().end();
337 if((*itst)->GetSeries().size()>0)
339 ListSerie::iterator its=(*itst)->GetSeries().end();
341 (*its)->AddImage(new gdcmImage(begin,end));
348 * \ingroup gdcmDicomDir
353 void gdcmDicomDir::SetElements(std::string &path,ListHeader &list)
355 std::string patPrevName="", patPrevID="";
356 std::string studPrevInstanceUID="", studPrevID="";
357 std::string serPrevInstanceUID="", serPrevID="";
359 std::string patCurName, patCurID;
360 std::string studCurInstanceUID, studCurID;
361 std::string serCurInstanceUID, serCurID;
363 SetElement(path,GDCM_NONE,NULL);
365 ListTag::iterator debPat=listEntries.begin();
366 for(ListHeader::iterator it=list.begin();it!=list.end();++it)
368 // get the current file characteristics
369 patCurName=(*it)->GetEntryByNumber(0x0010,0x0010);
370 patCurID=(*it)->GetEntryByNumber(0x0010,0x0011);
371 studCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000d);
372 studCurID=(*it)->GetEntryByNumber(0x0020,0x0010);
373 serCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000e);
374 serCurID=(*it)->GetEntryByNumber(0x0020,0x0011);
376 if(patCurName!=patPrevName || patCurID!=patPrevID)
377 SetElement(path,GDCM_PATIENT,*it);
379 // if new Study Deal with 'STUDY' Elements
380 if(studCurInstanceUID!=studPrevInstanceUID || studCurID!=studPrevID)
381 SetElement(path,GDCM_STUDY,*it);
383 // if new Serie Deal with 'SERIE' Elements
384 if(serCurInstanceUID!=serPrevInstanceUID || serCurID!=serPrevID)
386 SetElement(path,GDCM_SERIE,*it);
389 // Always Deal with 'IMAGE' Elements
390 SetElement(path,GDCM_IMAGE,*it);
392 patPrevName=patCurName;
394 studPrevInstanceUID=studCurInstanceUID;
395 studPrevID=studCurID;
396 serPrevInstanceUID=serCurInstanceUID;
402 * \ingroup gdcmDicomDir
408 void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader *header)
410 std::list<gdcmElement> elemList;
411 std::list<gdcmElement>::iterator it;
412 guint16 tmpGr, tmpEl;
413 gdcmDictEntry *dictEntry;
414 gdcmHeaderEntry *entry;
420 elemList=gdcmGlobal::GetDicomDirElements()->GetPatientElements();
423 elemList=gdcmGlobal::GetDicomDirElements()->GetStudyElements();
426 elemList=gdcmGlobal::GetDicomDirElements()->GetSerieElements();
429 elemList=gdcmGlobal::GetDicomDirElements()->GetImageElements();
432 elemList=gdcmGlobal::GetDicomDirElements()->GetMetaElements();
438 for(it=elemList.begin();it!=elemList.end();++it)
443 dictEntry=GetPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
444 entry=new gdcmHeaderEntry(dictEntry);
445 entry->SetOffset(0); // just to avoid missprinting
448 val=header->GetEntryByNumber(tmpGr,tmpEl);
452 if(val==GDCM_UNFOUND)
454 if((tmpGr==0x0004) &&(tmpEl==0x1130) )
456 // TODO force the *end* File Name(remove path)
459 else if( (tmpGr==0x0004) && (tmpEl==0x1500) ) // Only used for image
461 if(header->GetFileName().substr(0,path.length())!=path)
463 dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path of file name is incorrect");
464 val=header->GetFileName();
467 val=&(header->GetFileName()[path.length()]);
474 entry->SetValue(val);
478 if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") )
482 else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") )
486 else if(dictEntry->GetVR()=="SQ")
488 entry->SetLength(0xffffffff);
492 entry->SetLength(entry->GetValue().length());
496 listEntries.push_back(entry);
500 bool gdcmDicomDir::HeaderLessThan(gdcmHeader *header1,gdcmHeader *header2)
502 return(*header1<*header2);
505 //-----------------------------------------------------------------------------