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: 2007/07/27 09:49:31 $
00007   Version:   $Revision: 1.91 $
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 #include "gdcmDictSet.h"
00027 #include <iomanip> // for std::ios::left, ...
00028 #include <fstream>
00029 
00030 namespace GDCM_NAME_SPACE 
00031 {
00032 //-----------------------------------------------------------------------------
00033 
00034 // Constructor / Destructor
00041 DocEntry::DocEntry(uint16_t group, uint16_t elem, VRKey const &vr)
00042 {
00043    ImplicitVR = false;
00044    DicomDict  = 0;   
00045    Offset     = 0 ; // To avoid further missprinting
00046 
00047    // init some variables
00048    ReadLength = 0;
00049    Length = 0;
00050 
00051    VR = vr;
00052    Key.SetGroupElem(group,elem);
00053 }
00054 
00058 DocEntry::~DocEntry()
00059 {
00060    if (DicomDict)
00061    {
00062       gdcmAssertMacro(DicomDict);
00063       DicomDict->Unregister();
00064    }
00065 }
00066 
00067 //-----------------------------------------------------------------------------
00068 // Public
00074 void DocEntry::WriteContent(std::ofstream *fp, FileType filetype, bool insideMetaElements)
00075 {
00076    uint32_t ffff  = 0xffffffff;
00077    uint16_t group = GetGroup();
00078 
00080  
00081    VRKey vr       = GetVR();
00082    uint16_t elem  = GetElement();
00083    uint32_t lgth  = GetLength();
00084   
00085    if ( group == 0xfffe && elem == 0x0000 )
00086    {
00087      // Fix in order to make some MR PHILIPS images e-film readable
00088      // see gdcmData/gdcm-MR-PHILIPS-16-Multi-Seq.dcm:
00089      // we just *always* ignore spurious fffe|0000 tag !   
00090       return;
00091    }
00092 
00093    //
00094    // ----------- Writes the common part
00095    //
00096     // To avoid gdcm to propagate oddities.
00097     // --> Don't forget to *write* an even length value   
00098    if (lgth%2)
00099       lgth ++;
00100    
00101  // ----------- Writes the common part : the Tag   
00102    binary_write( *fp, group); //group number
00103    binary_write( *fp, elem);  //element number
00104 
00105    // Dicom V3 group 0x0002 is *always* Explicit VR !
00106    if ( filetype == ExplicitVR || filetype == JPEG || filetype == JPEG2000 || (group == 0x0002 && insideMetaElements) )
00107    {
00108 // ----------- Writes the common part : the VR + the length 
00109   
00110       // Special case of delimiters:
00111       if (group == 0xfffe)
00112       {   
00113          // Delimiters have NO Value Representation
00114          // Hence we skip writing the VR.
00115          //
00116          // In order to avoid further troubles, we choose to write them
00117          // as 'no-length' Item Delimitors (we pad by writing 0xffffffff)
00118          // We shall force the end of a given SeqItem by writting 
00119          //  a Item Delimitation Item (fffe, e00d)
00120 
00121          uint32_t ff = 0xffffffff;
00122          binary_write(*fp, ff);
00123          return;
00124       }
00125       uint16_t zero = 0;
00126       uint16_t shortLgr = (uint16_t)lgth;
00127 
00128 /*
00129       if( IsVRUnknown() )
00130       { 
00131          // GDCM_VRUNKNOWN was stored in the Entry VR;
00132          // deal with Entry as if TS were Implicit VR
00133          binary_write(*fp, lgth);
00134       }
00135       else
00136 */
00137       if( IsVRUnknown() )  
00138       {
00139       // if VR was not set, we set it to "UN"
00140       // (FileHelper::Write has no longer to switch to ImplicitVR 
00141       // when undocumented VR DataElements exist!)
00142          SetVR("UN");
00143          vr=GetVR();
00144       }      
00145      
00146       //{
00147          binary_write(*fp, vr.str());
00148          // See PS 3.5-2004 page 33, 36                  
00149          if ( (vr == "SQ") || (vr == "OB") || (vr == "OW") || (vr == "OF") 
00150           ||  (vr == "UN") || (vr == "UT") )
00151          {
00152             binary_write(*fp, zero);
00153            if ( (filetype == JPEG || filetype == JPEG2000) && group == 0x7fe0 && elem == 0x0010)
00154             {
00155                gdcmAssertMacro( GetVR() == "OW" );
00156                binary_write(*fp, ffff);
00157             }  
00158             else if (vr == "SQ")
00159             {
00160                // we set SQ length to ffffffff
00161                // and  we shall write a Sequence Delimitor Item 
00162                // at the end of the Sequence! 
00163                binary_write(*fp, ffff);
00164             }
00165             else
00166             {
00167                binary_write(*fp, lgth);
00168             }
00169          }
00170          else
00171          {
00172             binary_write(*fp, shortLgr);
00173          }
00174       //}
00175    } 
00176    else // IMPLICIT VR 
00177    { 
00178 // ----------- Writes the common part : the VR  
00179       if (vr == "SQ")
00180       {
00181          binary_write(*fp, ffff);
00182       }
00183       else
00184       {
00185          binary_write(*fp, lgth);
00186       }
00187    }
00188 }
00189 
00192 std::string const &DocEntry::GetName() 
00193 { 
00194    if (DicomDict == 0)
00195       DicomDict =
00196                  Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
00197    if (DicomDict == 0)
00198       return GDCM_UNKNOWN;
00199    else
00200    {
00201       DicomDict->Register();
00202       return DicomDict->GetName();
00203    }
00204 }
00205 
00209 std::string const &DocEntry::GetVM()
00210 {
00211    if (DicomDict == 0)
00212       DicomDict =
00213                  Global::GetDicts()->GetDefaultPubDict()->GetEntry(Key[0],Key[1]);
00214    if (DicomDict == 0)
00215       return GDCM_UNKNOWN;
00216    else
00217    {
00218       DicomDict->Register();
00219       return DicomDict->GetVM();
00220    }
00221 }
00222 
00227 uint32_t DocEntry::GetFullLength()
00228 {
00229    uint32_t l = GetReadLength();
00230    if ( IsImplicitVR() )
00231    {
00232       l = l + 8;  // 2 (gr) + 2 (el) + 4 (lgth) 
00233    }
00234    else
00235    {
00236       if ( GetVR()=="OB" || GetVR()=="OW" || GetVR()=="SQ" )
00237       {
00238          l = l + 12; // 2 (gr) + 2 (el) + 2 (vr) + 2 (unused) + 4 (lgth)
00239       }
00240       else
00241       {
00242          l = l + 8;  // 2 (gr) + 2 (el) + 2 (vr) + 2 (lgth)
00243       }
00244    }
00245    return l;
00246 }
00247 
00252 void DocEntry::Copy(DocEntry *doc)
00253 {
00254    Length     = doc->Length;
00255    ReadLength = doc->ReadLength;
00256    ImplicitVR = doc->ImplicitVR;
00257    Offset     = doc->Offset;
00258 }
00259 
00260 //-----------------------------------------------------------------------------
00261 // Protected
00262 
00263 //-----------------------------------------------------------------------------
00264 // Private
00265 
00266 //-----------------------------------------------------------------------------
00267 // Print
00273 void DocEntry::Print(std::ostream &os, std::string const & )
00274 {
00275    size_t o;
00276    std::string st;
00277    TSKey v;
00278    std::string d2;
00279    VRKey vr;
00280    std::ostringstream s;
00281    uint32_t lgth;
00282 
00283    o  = GetOffset();
00284    vr = GetVR();
00285    if ( vr == GDCM_VRUNKNOWN )
00286       vr = "  ";
00287 
00288    s << DictEntry::TranslateToKey(GetGroup(),GetElement()); 
00289 
00290    if (PrintLevel >= 2)
00291    {
00292       s << " lg : ";
00293       lgth = GetReadLength(); // ReadLength, as opposed to (usable) Length
00294       if (lgth == 0xffffffff)
00295       {
00296          st = " ffff ";
00297          s.setf(std::ios::left);
00298          s << std::setw(4);  
00299          s << "    x(ffff) ";
00300          s.setf(std::ios::left);
00301          s << std::setw(8) << "-1"; 
00302       }
00303       else
00304       {
00305          st = Util::Format("x(%x)",lgth); // we may keep it
00306          s.setf(std::ios::left);
00307          s << std::setw(11-st.size()) << " ";
00308          s << st << " ";
00309          s.setf(std::ios::left);
00310          s << std::setw(8) << lgth; 
00311       }
00312       s << " Off.: ";
00313       st = Util::Format("x(%x)",o);  // we may keep it
00314       s << std::setw(11-st.size()) << " ";
00315       s << st << " ";
00316       s << std::setw(8) << o; 
00317    }
00318    if (PrintLevel >= 1)
00319       s << " ";
00320 
00321    s << "[" << vr  << "] ";
00322 
00323    std::string name;
00324    uint16_t e = GetElement();
00325    if ( e == 0x0000 )
00326       name = "Group Length";
00327    else if ( GetGroup()%2 == 1 )
00328    {
00329       if ( e >= 0x0010 && e <= 0x00ff )
00330          name = "Private Creator";
00331       else if (e == 0x0001)
00332          name = "Private Group Length To End";
00333    }
00334    else
00335    {
00336       name = GetName();
00337       // prevent Print from any CR at end of name (hope it's enought!)
00338       if (name[name.length()-1] == 0x0d || name[name.length()-1] == 0x0a)
00339       {  
00340          name.replace(name.length()-1, 1, 1, ' ');
00341       }
00342    }
00343    if (PrintLevel >= 1)
00344    {
00345       s.setf(std::ios::left);
00346       s << std::setw(66-name.length()) << " ";
00347    }
00348     
00349    s << "[" << name << "]";
00350    os << s.str();      
00351 }
00352 
00353 //-----------------------------------------------------------------------------
00354 } // end namespace gdcm

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