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 SetElements(fileList.GetDirName(),list);
158 * \ingroup gdcmDicomDir
159 * \brief Get the dicom dir path
162 std::string gdcmDicomDir::GetPath(void)
164 std::string path=GetFileName();
166 int pos1=path.rfind("/");
167 int pos2=path.rfind("\\");
176 //-----------------------------------------------------------------------------
179 * \ingroup gdcmDicomDir
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_NONE;
193 ListTag::iterator begin;
194 ListTag::iterator end;
196 begin=listEntries.begin();
198 for(ListTag::iterator i=listEntries.begin();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;
238 end=GetListEntry().end();
239 AddObjectToEnd(type,begin,end);
242 * \ingroup gdcmDicomDir
248 void gdcmDicomDir::AddObjectToEnd(gdcmDicomDirType type,ListTag::iterator begin,ListTag::iterator end)
255 case gdcmDicomDir::GDCM_PATIENT:
256 AddPatientToEnd(begin,end);
258 case gdcmDicomDir::GDCM_STUDY:
259 AddStudyToEnd(begin,end);
261 case gdcmDicomDir::GDCM_SERIE:
262 AddSerieToEnd(begin,end);
264 case gdcmDicomDir::GDCM_IMAGE:
265 AddImageToEnd(begin,end);
271 * \ingroup gdcmDicomDir
276 void gdcmDicomDir::AddPatientToEnd(ListTag::iterator begin,ListTag::iterator end)
278 patients.push_back(new gdcmPatient(begin,end));
282 * \ingroup gdcmDicomDir
287 void gdcmDicomDir::AddStudyToEnd(ListTag::iterator begin,ListTag::iterator end)
289 if(patients.size()>0)
291 ListPatient::iterator itp=patients.end();
293 (*itp)->AddStudy(new gdcmStudy(begin,end));
297 * \ingroup gdcmDicomDir
302 void gdcmDicomDir::AddSerieToEnd(ListTag::iterator begin,ListTag::iterator end)
304 if(patients.size()>0)
306 ListPatient::iterator itp=patients.end();
309 if((*itp)->GetStudies().size()>0)
311 ListStudy::iterator itst=(*itp)->GetStudies().end();
313 (*itst)->AddSerie(new gdcmSerie(begin,end));
319 * \ingroup gdcmDicomDir
324 void gdcmDicomDir::AddImageToEnd(ListTag::iterator begin,ListTag::iterator end)
326 if(patients.size()>0)
328 ListPatient::iterator itp=patients.end();
331 if((*itp)->GetStudies().size()>0)
333 ListStudy::iterator itst=(*itp)->GetStudies().end();
336 if((*itst)->GetSeries().size()>0)
338 ListSerie::iterator its=(*itst)->GetSeries().end();
340 (*its)->AddImage(new gdcmImage(begin,end));
347 * \ingroup gdcmDicomDir
352 void gdcmDicomDir::SetElements(std::string &path,ListHeader &list)
354 std::string patPrevName="", patPrevID="";
355 std::string studPrevInstanceUID="", studPrevID="";
356 std::string serPrevInstanceUID="", serPrevID="";
358 std::string patCurName, patCurID;
359 std::string studCurInstanceUID, studCurID;
360 std::string serCurInstanceUID, serCurID;
362 SetElement(path,GDCM_NONE,NULL);
364 ListTag::iterator debPat=listEntries.begin();
365 for(ListHeader::iterator it=list.begin();it!=list.end();++it)
367 // get the current file characteristics
368 patCurName=(*it)->GetEntryByNumber(0x0010,0x0010);
369 patCurID=(*it)->GetEntryByNumber(0x0010,0x0011);
370 studCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000d);
371 studCurID=(*it)->GetEntryByNumber(0x0020,0x0010);
372 serCurInstanceUID=(*it)->GetEntryByNumber(0x0020,0x000e);
373 serCurID=(*it)->GetEntryByNumber(0x0020,0x0011);
375 if(patCurName!=patPrevName || patCurID!=patPrevID)
376 SetElement(path,GDCM_PATIENT,*it);
378 // if new Study Deal with 'STUDY' Elements
379 if(studCurInstanceUID!=studPrevInstanceUID || studCurID!=studPrevID)
380 SetElement(path,GDCM_STUDY,*it);
382 // if new Serie Deal with 'SERIE' Elements
383 if(serCurInstanceUID!=serPrevInstanceUID || serCurID!=serPrevID)
385 SetElement(path,GDCM_SERIE,*it);
388 // Always Deal with 'IMAGE' Elements
389 SetElement(path,GDCM_IMAGE,*it);
391 patPrevName=patCurName;
393 studPrevInstanceUID=studCurInstanceUID;
394 studPrevID=studCurID;
395 serPrevInstanceUID=serCurInstanceUID;
401 * \ingroup gdcmDicomDir
407 void gdcmDicomDir::SetElement(std::string &path,gdcmDicomDirType type,gdcmHeader *header)
409 std::list<gdcmElement> elemList;
410 std::list<gdcmElement>::iterator it;
411 guint16 tmpGr, tmpEl;
412 gdcmDictEntry *dictEntry;
413 gdcmHeaderEntry *entry;
419 elemList=gdcmGlobal::GetDicomDirElements()->GetPatientElements();
422 elemList=gdcmGlobal::GetDicomDirElements()->GetStudyElements();
425 elemList=gdcmGlobal::GetDicomDirElements()->GetSerieElements();
428 elemList=gdcmGlobal::GetDicomDirElements()->GetImageElements();
431 elemList=gdcmGlobal::GetDicomDirElements()->GetMetaElements();
437 for(it=elemList.begin();it!=elemList.end();++it)
442 dictEntry=GetPubDict()->GetDictEntryByNumber(tmpGr,tmpEl);
443 entry=new gdcmHeaderEntry(dictEntry);
444 entry->SetOffset(0); // just to avoid missprinting
447 val=header->GetEntryByNumber(tmpGr,tmpEl);
451 if(val==GDCM_UNFOUND)
453 if((tmpGr==0x0004) &&(tmpEl==0x1130) )
455 // TODO force the *end* File Name(remove path)
458 else if( (tmpGr==0x0004) && (tmpEl==0x1500) ) // Only used for image
460 if(header->GetFileName().substr(0,path.length())!=path)
462 dbg.Verbose(0, "gdcmDicomDir::SetElement : the base path of file name is incorrect");
463 val=header->GetFileName();
466 val=&(header->GetFileName()[path.length()]);
473 entry->SetValue(val);
477 if( (dictEntry->GetVR()=="UL") || (dictEntry->GetVR()=="SL") )
481 else if( (dictEntry->GetVR()=="US") || (dictEntry->GetVR()=="SS") )
485 else if(dictEntry->GetVR()=="SQ")
487 entry->SetLength(0xffffffff);
491 entry->SetLength(entry->GetValue().length());
495 listEntries.push_back(entry);
499 bool gdcmDicomDir::HeaderLessThan(gdcmHeader *header1,gdcmHeader *header2)
501 return(*header1<*header2);
504 //-----------------------------------------------------------------------------