Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

gdcmSeqEntry.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmSeqEntry.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/12/01 09:12:21 $
00007   Version:   $Revision: 1.64 $
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 "gdcmSeqEntry.h"
00020 #include "gdcmSQItem.h"
00021 #include "gdcmTS.h"
00022 #include "gdcmGlobal.h"
00023 #include "gdcmUtil.h"
00024 #include "gdcmDebug.h"
00025 
00026 #include <iostream>
00027 #include <iomanip>
00028 #include <fstream>
00029 
00030 namespace gdcm 
00031 {
00032 //-----------------------------------------------------------------------------
00033 // Constructor / Destructor
00037 SeqEntry::SeqEntry( DictEntry *e ) 
00038              : DocEntry(e)
00039 {
00040    Length       = 0;
00041    ReadLength   = 0xffffffff;
00042    SQDepthLevel = -1;
00043 
00044    DelimitorMode = false;
00045    SeqTerm  = NULL;
00046 }
00047 
00053 SeqEntry::SeqEntry( DocEntry *e, int depth )
00054              : DocEntry( e->GetDictEntry() )
00055 {
00056    Length       = 0;
00057    ReadLength   = 0xffffffff;
00058    SQDepthLevel = depth;
00059 
00060    ImplicitVR   = e->IsImplicitVR();
00061    Offset       = e->GetOffset();
00062    SeqTerm = NULL;
00063 }
00064 
00068 SeqEntry::~SeqEntry()
00069 {
00070    ClearSQItem();
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 // Public
00075 /*
00076  * \brief   canonical Writer
00077  * @param fp pointer to an already open file
00078  * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
00079  */
00080 void SeqEntry::WriteContent(std::ofstream *fp, FileType filetype)
00081 {
00082    uint16_t seq_term_gr = 0xfffe;
00083    uint16_t seq_term_el = 0xe0dd;
00084    uint32_t seq_term_lg = 0x00000000;
00085  
00086    // ignore 'Zero length' Sequences
00087    if ( GetReadLength() == 0 )
00088       return;
00089        
00090    DocEntry::WriteContent(fp, filetype);
00091    for(ListSQItem::iterator cc  = Items.begin();
00092                             cc != Items.end();
00093                           ++cc)
00094    {        
00095       (*cc)->WriteContent(fp, filetype);
00096    }
00097    
00098    // we force the writting of a Sequence Delimitation item
00099    // because we wrote the Sequence as a 'no Length' sequence
00100    binary_write(*fp, seq_term_gr);
00101    binary_write(*fp, seq_term_el);
00102    binary_write(*fp, seq_term_lg);
00103 }
00104 
00109 uint32_t SeqEntry::ComputeFullLength()
00110 {
00111    uint32_t l = 12; // Tag (4) + VR (explicit) 4 + 4 (length);   
00112    for(ListSQItem::iterator cc  = Items.begin();
00113                             cc != Items.end();
00114                           ++cc)
00115    {        
00116       l += (*cc)->ComputeFullLength();
00117    }   
00118    l += 8; // seq_term Tag (4) +  seq_term_lg (4)
00119    return l;
00120 }
00121 
00128 void SeqEntry::AddSQItem(SQItem *sqItem, int itemNumber)
00129 {
00130 // FIXME : SQItemNumber is supposed to be the ordinal number of the SQItem
00131 //         within the Sequence.
00132 //         Either only 'push_back' is allowed, 
00133 //                and we just have to do something like SeqEntry::lastNb++
00134 //         Or we can add (or remove) anywhere, and SQItemNumber will be broken
00135    sqItem->SetSQItemNumber(itemNumber);
00136    Items.push_back(sqItem);
00137    sqItem->Register();
00138 }
00139 
00143 void SeqEntry::ClearSQItem()
00144 {
00145    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
00146    {
00147       (*cc)->Unregister();
00148    }
00149    if (SeqTerm)
00150    {
00151       SeqTerm->Unregister();
00152    }
00153 }
00154 
00159 SQItem *SeqEntry::GetFirstSQItem()
00160 {
00161    ItSQItem = Items.begin();
00162    if (ItSQItem != Items.end())
00163       return *ItSQItem;
00164    return NULL;
00165 } 
00166 
00173 SQItem *SeqEntry::GetNextSQItem()
00174 {
00175    gdcmAssertMacro (ItSQItem != Items.end())
00176    {
00177       ++ItSQItem;
00178       if (ItSQItem != Items.end())
00179          return *ItSQItem;
00180    }
00181    return NULL;
00182 }
00183  
00190 SQItem *SeqEntry::GetSQItem(int nb)
00191 {
00192    if (nb<0)
00193    {
00194       return *(Items.begin());
00195    }
00196    int count = 0 ;
00197    for(ListSQItem::iterator cc = Items.begin();
00198                            cc != Items.end();
00199                            count ++, ++cc)
00200    {
00201       if (count == nb)
00202       {
00203          return *cc;
00204       }
00205    }
00206    return *(Items.end());
00207 }
00208 
00212 unsigned int SeqEntry::GetNumberOfSQItems()
00213 {
00214    return Items.size();
00215 }
00216 
00221 void SeqEntry::SetDelimitationItem(DocEntry *e)
00222 {
00223    if( SeqTerm != e )
00224    {
00225       if( SeqTerm )
00226          SeqTerm->Unregister();
00227       SeqTerm = e;
00228       if( SeqTerm )
00229          SeqTerm->Register();
00230    }
00231 }
00232 
00238 void SeqEntry::Copy(DocEntry *doc)
00239 {
00240    // Delete previous SQ items
00241    ClearSQItem();
00242 
00243    DocEntry::Copy(doc);
00244    SeqEntry *entry = dynamic_cast<SeqEntry *>(doc);
00245    if ( entry )
00246    {
00247       DelimitorMode = entry->DelimitorMode;
00248       SQDepthLevel = entry->SQDepthLevel;
00249 
00250       SeqTerm = entry->SeqTerm;
00251       if(SeqTerm)
00252          SeqTerm->Register();
00253       Items = entry->Items;
00254       for(ItSQItem = Items.begin();ItSQItem != Items.end(); ++ItSQItem)
00255       {
00256          (*ItSQItem)->Register();
00257       }
00258    }
00259 }
00260 
00261 //-----------------------------------------------------------------------------
00262 // Protected
00263 
00264 //-----------------------------------------------------------------------------
00265 // Private
00266 
00267 //-----------------------------------------------------------------------------
00268 // Print
00272 void SeqEntry::Print( std::ostream &os, std::string const & )
00273 {
00274    // First, Print the Dicom Element itself.
00275    os << "S ";
00276    DocEntry::Print(os);
00277    os << std::endl;
00278 
00279    if (GetReadLength() == 0)
00280       return;
00281 
00282    // Then, Print each SQ Item   
00283    for(ListSQItem::iterator cc = Items.begin(); cc != Items.end(); ++cc)
00284    {
00285       (*cc)->SetPrintLevel(PrintLevel);
00286       (*cc)->Print(os);   
00287    }
00288 
00289    // at end, print the sequence terminator item, if any
00290    if (DelimitorMode)
00291    {
00292       int i;
00293       for ( i = 0; i < SQDepthLevel; i++ )
00294          os << "   | " ;
00295       os << " --- "  << std::endl;
00296       for ( i = 0; i < SQDepthLevel; i++ )
00297          os << "   | " ;
00298       if (SeqTerm != NULL)
00299       {
00300          SeqTerm->SetPrintLevel(PrintLevel);
00301          SeqTerm->Print(os);
00302          os << std::endl;
00303       } 
00304       else 
00305       {
00306          // fuse
00307          gdcmWarningMacro("  -------- should have a sequence terminator item");
00308       }
00309    }
00310 }
00311 
00312 //-----------------------------------------------------------------------------
00313 } // end namespace gdcm

Generated on Fri Jan 20 10:14:26 2006 for gdcm by  doxygen 1.4.4