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

gdcmDocEntry.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmDocEntry.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/12/21 14:52:12 $
00007   Version:   $Revision: 1.80 $
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 "gdcmDocEntry.h"
00020 #include "gdcmDataEntry.h"
00021 #include "gdcmTS.h"
00022 #include "gdcmVR.h"
00023 #include "gdcmGlobal.h"
00024 #include "gdcmUtil.h"
00025 #include "gdcmDebug.h"
00026 
00027 #include <iomanip> // for std::ios::left, ...
00028 #include <fstream>
00029 
00030 namespace gdcm 
00031 {
00032 //-----------------------------------------------------------------------------
00033 
00034 // Constructor / Destructor
00039 DocEntry::DocEntry(DictEntry *in)
00040 {
00041    ImplicitVR = false;
00042    DicomDict  = in;
00043    Offset     = 0 ; // To avoid further missprinting
00044 
00045    // init some variables
00046    ReadLength = 0;
00047    Length = 0;
00048 
00049    gdcmAssertMacro(DicomDict);
00050    DicomDict->Register();
00051 }
00052 
00056 DocEntry::~DocEntry()
00057 {
00058    gdcmAssertMacro(DicomDict);
00059 
00060    DicomDict->Unregister();
00061 }
00062 
00063 //-----------------------------------------------------------------------------
00064 // Public
00070 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype)
00071 {
00072    uint32_t ffff  = 0xffffffff;
00073    uint16_t group = GetGroup();
00074    
00076    
00077    VRKey vr       = GetVR();
00078    uint16_t elem  = GetElement();
00079    uint32_t lgth  = GetLength();
00080 
00081    if ( group == 0xfffe && elem == 0x0000 )
00082    {
00083      // Fix in order to make some MR PHILIPS images e-film readable
00084      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
00085      // we just *always* ignore spurious fffe|0000 tag !   
00086       return;
00087    }
00088  
00089    //
00090    // ----------- Writes the common part
00091    //
00092     // To avoid gdcm to propagate oddities.
00093     // --> Don't forget to *write* an even length value   
00094    if (lgth%2)
00095       lgth ++;
00096    
00097  // ----------- Writes the common part : the Tag   
00098    binary_write( *fp, group); //group number
00099    binary_write( *fp, elem);  //element number
00100 
00101    // Dicom V3 group 0x0002 is *always* Explicit VR !
00102    if ( filetype == ExplicitVR || filetype == JPEG || group == 0x0002 )
00103    {
00104 
00105 // ----------- Writes the common part : the VR + the length 
00106   
00107           // Special case of delimiters:
00108       if (group == 0xfffe)
00109       {
00110          // Delimiters have NO Value Representation
00111          // Hence we skip writing the VR.
00112          //
00113          // In order to avoid further troubles, we choose to write them
00114          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
00115          // We shall force the end of a given SeqItem by writting 
00116          //  a Item Delimitation Item (fffe, e00d)
00117 
00118          uint32_t ff = 0xffffffff;
00119          binary_write(*fp, ff);
00120          return;
00121       }
00122 
00123       uint16_t zero = 0;
00124       uint16_t shortLgr = (uint16_t)lgth;
00125 
00126       if( IsVRUnknown() )
00127       {
00128          // GDCM_VRUNKNOWN was stored in the Entry VR;
00129          // deal with Entry as if TS were Implicit VR
00130  
00131          binary_write(*fp, lgth);
00132       }
00133       else
00134       {
00135          binary_write(*fp, vr.str());
00136 
00137          // See PS 3.5-2004 page 33, 36                  
00138          if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF") 
00139           ||  (vr == "UN") || (vr == "UT") )
00140          {
00141             binary_write(*fp, zero);
00142             if (vr == "SQ")
00143             {
00144                // we set SQ length to ffffffff
00145                // and  we shall write a Sequence Delimitor Item 
00146                // at the end of the Sequence! 
00147                binary_write(*fp, ffff);
00148             }
00149             else
00150             {
00151                binary_write(*fp, lgth);
00152             }
00153          }
00154          else
00155          {
00156             binary_write(*fp, shortLgr);
00157          }
00158       }
00159    } 
00160    else // IMPLICIT VR 
00161    { 
00162 // ----------- Writes the common part : the VR  
00163       if (vr == "SQ")
00164       {
00165          binary_write(*fp, ffff);
00166       }
00167       else
00168       {
00169          binary_write(*fp, lgth);
00170       }
00171    }
00172 }
00173 
00178 uint32_t DocEntry::GetFullLength()
00179 {
00180    uint32_t l = GetReadLength();
00181    if ( IsImplicitVR() )
00182    {
00183       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
00184    }
00185    else
00186    {
00187       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
00188       {
00189          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
00190       }
00191       else
00192       {
00193          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
00194       }
00195    }
00196    return l;
00197 }
00198 
00203 bool DocEntry::IsItemDelimitor()
00204 {
00205    return (GetGroup() == 0xfffe && GetElement() == 0xe00d);
00206 }
00207 
00212 bool DocEntry::IsItemStarter()
00213 {
00214    return (GetGroup() == 0xfffe && GetElement() == 0xe000);
00215 }
00216 
00221 bool DocEntry::IsSequenceDelimitor()
00222 {
00223    return (GetGroup() == 0xfffe && GetElement() == 0xe0dd);
00224 }
00225 
00230 void DocEntry::Copy(DocEntry *doc)
00231 {
00232    Length     = doc->Length;
00233    ReadLength = doc->ReadLength;
00234    ImplicitVR = doc->ImplicitVR;
00235    Offset     = doc->Offset;
00236 }
00237 
00238 //-----------------------------------------------------------------------------
00239 // Protected
00240 
00241 //-----------------------------------------------------------------------------
00242 // Private
00243 
00244 //-----------------------------------------------------------------------------
00245 // Print
00251 void DocEntry::Print(std::ostream &os, std::string const & )
00252 {
00253    size_t o;
00254    std::string st;
00255    TSKey v;
00256    std::string d2;
00257    VRKey vr;
00258    std::ostringstream s;
00259    uint32_t lgth;
00260 
00261    o  = GetOffset();
00262    vr = GetVR();
00263    if ( vr == GDCM_VRUNKNOWN )
00264       vr = "  ";
00265 
00266    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
00267 
00268    if (PrintLevel >= 2)
00269    {
00270       s << " lg : ";
00271       lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
00272       if (lgth == 0xffffffff)
00273       {
00274          st = " ffff ";
00275          s.setf(std::ios::left);
00276          s << std::setw(4);  
00277          s << "    x(ffff) ";
00278          s.setf(std::ios::left);
00279          s << std::setw(8) << "-1"; 
00280       }
00281       else
00282       {
00283          st = Util::Format("x(%x)",lgth); // we may keep it
00284          s.setf(std::ios::left);
00285          s << std::setw(11-st.size()) << " ";
00286          s << st << " ";
00287          s.setf(std::ios::left);
00288          s << std::setw(8) << lgth; 
00289       }
00290       s << " Off.: ";
00291       st = Util::Format("x(%x)",o);  // we may keep it
00292       s << std::setw(11-st.size()) << " ";
00293       s << st << " ";
00294       s << std::setw(8) << o; 
00295    }
00296    if (PrintLevel >= 1)
00297       s << " ";
00298 
00299    s << "[" << vr  << "] ";
00300 
00301    std::string name;
00302    if ( GetElement() == 0x0000 )
00303       name = "Group Length";
00304    else
00305       name = GetName();
00306 
00307    if (PrintLevel >= 1)
00308    {
00309       s.setf(std::ios::left);
00310       s << std::setw(66-name.length()) << " ";
00311    }
00312     
00313    s << "[" << name << "]";
00314    os << s.str();      
00315 }
00316 
00317 //-----------------------------------------------------------------------------
00318 } // end namespace gdcm

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