gdcmSQItem.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002   
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmSQItem.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2007/07/26 08:36:49 $
00007   Version:   $Revision: 1.86 $
00008   
00009   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
00010   l'Image). All rights reserved. See Doc/License.txt or
00011   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
00012   
00013      This software is distributed WITHOUT ANY WARRANTY; without even
00014      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00015      PURPOSE.  See the above copyright notices for more information.
00016   
00017 =========================================================================*/
00018 
00019 #include "gdcmSQItem.h"
00020 #include "gdcmSeqEntry.h"
00021 #include "gdcmGlobal.h"
00022 #include "gdcmDictSet.h"
00023 #include "gdcmUtil.h"
00024 #include "gdcmDebug.h"
00025 
00026 #include <fstream>
00027 
00028 namespace GDCM_NAME_SPACE 
00029 {
00030 //-----------------------------------------------------------------------------
00031 // Constructor / Destructor
00035 SQItem::SQItem(int depthLevel ) 
00036           : DocEntrySet( )
00037 {
00038    SQDepthLevel = depthLevel;
00039    SQItemNumber = 0;
00040 }
00041 
00045 SQItem::~SQItem() 
00046 {
00047    ClearEntry();
00048 }
00049 
00050 //-----------------------------------------------------------------------------
00051 // Public
00052 /*
00053  * \brief   canonical Writer
00054  * @param fp     file pointer to an already open file. 
00055  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
00056  */
00057 void SQItem::WriteContent(std::ofstream *fp, FileType filetype, 
00058                                                      bool insideMetaElements)
00059 {
00060    int j;
00061    uint16_t item[4] = { 0xfffe, 0xe000, 0xffff, 0xffff };
00062    uint16_t itemt[4]= { 0xfffe, 0xe00d, 0x0000, 0x0000 };
00063 
00064     //we force the writting of an 'Item' Start Element
00065     // because we want to write the Item as a 'No Length' item
00066    for(j=0;j<4;++j)
00067    {
00068       binary_write( *fp, item[j]);  // fffe e000 ffff ffff 
00069    }
00070      
00071    for (ListDocEntry::iterator it = DocEntries.begin();  
00072                                it != DocEntries.end();
00073                              ++it)
00074    {   
00075       // we skip delimitors (start and end one) because 
00076       // we force them as 'no length'
00077       if ( (*it)->GetGroup() == 0xfffe )
00078       {
00079          continue;
00080       }
00081 
00082       // Fix in order to make some MR PHILIPS images e-film readable
00083       // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
00084       // we just *always* ignore spurious fffe|0000 tag ! 
00085       if ( (*it)->GetGroup() == 0xfffe && (*it)->GetElement() == 0x0000 )
00086       {
00087           break; // FIXME : break or continue; ?!?  
00088                  // --> makes no difference since the only bugged file we have
00089                  // contains 'impossible tag' fffe|0000 in last position !                            
00090       }
00091       // false : we are not in MetaElements
00092       (*it)->WriteContent(fp, filetype, false);
00093    }
00094       
00095     //we force the writting of an 'Item Delimitation' item
00096     // because we wrote the Item as a 'no Length' item
00097    for(j=0;j<4;++j)
00098    {
00099       binary_write( *fp, itemt[j]);  // fffe e000 0000 0000
00100    } 
00101 }
00102 
00107 uint32_t SQItem::ComputeFullLength()
00108 {
00109    uint32_t l = 8;  // Item Starter length
00110    for (ListDocEntry::iterator it = DocEntries.begin();  
00111                                it != DocEntries.end();
00112                              ++it)
00113    {   
00114       // we skip delimitors (start and end one) because 
00115       // we force them as 'no length'
00116       if ( (*it)->GetGroup() == 0xfffe )
00117       {
00118          continue;
00119       }
00120       l += (*it)->ComputeFullLength();
00121    }
00122    l += 8; // 'Item Delimitation' item 
00123    return l;  
00124 }
00125 
00132 bool SQItem::AddEntry(DocEntry *entry)
00133 {   
00134    if (DocEntries.empty() )
00135    {
00136       DocEntries.push_back(entry);
00137       entry->Register();
00138       return true;
00139    }
00140  
00141    ListDocEntry::iterator insertSpot;
00142    ListDocEntry::iterator it = DocEntries.end();
00143    do
00144    {
00145       it--;
00146 
00147       if ( (*it)->IsItemDelimitor() )
00148       {
00149          continue;
00150       }
00151       if ( (*it)->GetGroup() < entry->GetGroup() )
00152          break;
00153       else
00154          if ( (*it)->GetGroup() == entry->GetGroup() &&
00155               (*it)->GetElement() < entry->GetElement() )
00156             break;
00157    } while (it != DocEntries.begin() );
00158   
00159    ++it;
00160    insertSpot = it;
00161    DocEntries.insert(insertSpot, entry); 
00162    entry->Register();
00163    return true;
00164 }   
00165 
00171 bool SQItem::RemoveEntry( DocEntry *entryToRemove )
00172 {
00173    for(ListDocEntry::iterator it = DocEntries.begin();
00174                               it != DocEntries.end();
00175                             ++it)
00176    {
00177       if ( *it == entryToRemove )
00178       {
00179          DocEntries.erase(it);
00180          gdcmDebugMacro( "One element erased: " << entryToRemove->GetKey() );
00181          entryToRemove->Unregister();
00182          return true;
00183       }
00184    }
00185    gdcmWarningMacro( "Entry not found: " << entryToRemove->GetKey() );
00186    return false ;
00187 }
00188 
00192 void SQItem::ClearEntry()
00193 {
00194    for(ListDocEntry::iterator cc = DocEntries.begin();
00195                               cc != DocEntries.end();
00196                             ++cc)
00197    {
00198       (*cc)->Unregister();
00199    }
00200    DocEntries.clear();
00201 }
00202 
00207 DocEntry *SQItem::GetFirstEntry()
00208 {
00209    ItDocEntries = DocEntries.begin();
00210    if ( ItDocEntries != DocEntries.end() )
00211       return *ItDocEntries;
00212    return 0;   
00213 }
00214                                                                                 
00219 DocEntry *SQItem::GetNextEntry()
00220 {
00221    ++ItDocEntries;
00222    if ( ItDocEntries != DocEntries.end() )
00223       return  *ItDocEntries;
00224    return NULL;
00225 }
00226 
00233 DocEntry *SQItem::GetDocEntry(uint16_t group, uint16_t elem)
00234 {
00235    for(ListDocEntry::iterator i =  DocEntries.begin();
00236                               i != DocEntries.end(); 
00237                             ++i)
00238    {
00239       if ( (*i)->GetGroup() == group && (*i)->GetElement() == elem )
00240          return *i;
00241    }
00242    return NULL;
00243 }
00244 
00250 void SQItem::Copy(DocEntrySet *set)
00251 {
00252    // Remove all previous entries
00253    ClearEntry();
00254 
00255    DocEntrySet::Copy(set);
00256 
00257    SQItem *sq = dynamic_cast<SQItem *>(set);
00258    if( sq )
00259    {
00260       SQDepthLevel = sq->SQDepthLevel;
00261       SQItemNumber = sq->SQItemNumber;
00262 
00263       DocEntries = sq->DocEntries;
00264       for(ItDocEntries = DocEntries.begin();ItDocEntries != DocEntries.end();++ItDocEntries)
00265          (*ItDocEntries)->Register();
00266    }
00267 }
00268 
00269 //-----------------------------------------------------------------------------
00270 // Protected
00271 
00272 //-----------------------------------------------------------------------------
00273 // Private
00274 
00275 //-----------------------------------------------------------------------------
00276 // Print
00277 /*
00278  * \brief   canonical Printer
00279  * @param os     Stream to print to. 
00280  * @param indent Indentation string to be prepended during printing.
00281  */
00282 void SQItem::Print(std::ostream &os, std::string const &)
00283 {
00284    std::ostringstream s;
00285 
00286    if (SQDepthLevel > 0)
00287    {
00288       for (int i = 0; i < SQDepthLevel; ++i)
00289       {
00290          s << "   | " ;
00291       }
00292    }
00293    os << s.str() << " --- SQItem number " << SQItemNumber  << std::endl;
00294    for (ListDocEntry::iterator i  = DocEntries.begin();
00295                                i != DocEntries.end();
00296                              ++i)
00297    {
00298       DocEntry *Entry = *i;
00299       bool PrintEndLine = true;
00300 
00301       os << s.str();
00302       Entry->SetPrintLevel(PrintLevel);
00303       Entry->Print(os); 
00304       if ( dynamic_cast<SeqEntry*>(Entry) )
00305       {
00306          PrintEndLine = false;
00307       }
00308       if (PrintEndLine)
00309       {
00310          os << std::endl;
00311       }
00312    } 
00313 }
00314 
00315 //-----------------------------------------------------------------------------
00316 } // end namespace gdcm

Generated on Fri Aug 24 12:59:32 2007 for gdcm by  doxygen 1.4.6