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

gdcmDocEntrySet.cxx

Go to the documentation of this file.
00001 /*=========================================================================
00002                                                                                 
00003   Program:   gdcm
00004   Module:    $RCSfile: gdcmDocEntrySet.cxx,v $
00005   Language:  C++
00006   Date:      $Date: 2005/02/05 01:37:08 $
00007   Version:   $Revision: 1.54 $
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 #include "gdcmDocEntrySet.h"
00019 
00020 #include "gdcmDebug.h"
00021 #include "gdcmCommon.h"
00022 #include "gdcmDictSet.h"
00023 #include "gdcmGlobal.h"
00024 #include "gdcmDocEntry.h"
00025 #include "gdcmSeqEntry.h"
00026 #include "gdcmValEntry.h"
00027 #include "gdcmBinEntry.h"
00028 
00029 namespace gdcm 
00030 {
00031 //-----------------------------------------------------------------------------
00032 // Constructor / Destructor
00033 
00034 //-----------------------------------------------------------------------------
00035 // Public
00043 std::string DocEntrySet::GetEntryValue(uint16_t group, uint16_t elem)
00044 {
00045    ContentEntry *entry = dynamic_cast<ContentEntry *>(GetDocEntry(group,elem));
00046    if( entry )
00047       return entry->GetValue();
00048    return GDCM_UNFOUND;
00049 }
00050 
00057 void *DocEntrySet::GetEntryBinArea(uint16_t group, uint16_t elem) 
00058 {
00059    BinEntry *entry = GetBinEntry(group, elem);
00060    if( entry )
00061       return entry->GetBinArea();
00062    return 0;
00063 }
00064 
00073 int DocEntrySet::GetEntryLength(uint16_t group, uint16_t elem)
00074 {
00075    DocEntry *entry = GetDocEntry(group, elem);
00076    if( entry )
00077       return entry->GetLength();
00078    return -1;
00079 }
00080 
00094 std::string DocEntrySet::GetEntryVR(uint16_t group, uint16_t elem)
00095 {
00096    DocEntry *entry = GetDocEntry(group, elem);
00097    if( entry )
00098       return entry->GetVR();
00099    return GDCM_UNFOUND;
00100 }
00101 
00110 ValEntry *DocEntrySet::GetValEntry(uint16_t group, uint16_t elem)
00111 {
00112    DocEntry *currentEntry = GetDocEntry(group, elem);
00113    if ( !currentEntry )
00114       return NULL;
00115 
00116    return dynamic_cast<ValEntry*>(currentEntry);
00117 }
00118 
00127 BinEntry *DocEntrySet::GetBinEntry(uint16_t group, uint16_t elem)
00128 {
00129    DocEntry *currentEntry = GetDocEntry(group, elem);
00130    if ( !currentEntry )
00131    {
00132       gdcmWarningMacro( "No corresponding BinEntry " << std::hex << group <<
00133                          "," << elem);
00134       return NULL;
00135    }
00136 
00137    return dynamic_cast<BinEntry*>(currentEntry);
00138 }
00139 
00148 SeqEntry *DocEntrySet::GetSeqEntry(uint16_t group, uint16_t elem)
00149 {
00150    DocEntry *currentEntry = GetDocEntry(group, elem);
00151    if ( !currentEntry )
00152    {
00153       gdcmWarningMacro( "No corresponding SeqEntry " << std::hex << group <<
00154                         "," << elem);
00155       return NULL;
00156    }
00157 
00158    return dynamic_cast<SeqEntry*>(currentEntry);
00159 }
00160 
00169 bool DocEntrySet::SetValEntry(std::string const &content, 
00170                               uint16_t group, uint16_t elem) 
00171 {
00172    ValEntry *entry = GetValEntry(group, elem);
00173    if (!entry )
00174    {
00175       gdcmWarningMacro( "No corresponding ValEntry " << std::hex << group <<
00176                          "," << elem << " element (try promotion first).");
00177       return false;
00178    }
00179    return SetValEntry(content,entry);
00180 }
00181 
00191 bool DocEntrySet::SetBinEntry(uint8_t *content, int lgth, 
00192                               uint16_t group, uint16_t elem) 
00193 {
00194    BinEntry *entry = GetBinEntry(group, elem);
00195    if (!entry )
00196    {
00197       gdcmWarningMacro( "No corresponding ValEntry " << std::hex << group <<
00198                         "," << elem << " element (try promotion first).");
00199       return false;
00200    }
00201 
00202    return SetBinEntry(content,lgth,entry);
00203 } 
00204 
00211 bool DocEntrySet::SetValEntry(std::string const &content, ValEntry *entry)
00212 {
00213    if(entry)
00214    {
00215       entry->SetValue(content);
00216       return true;
00217    }
00218    return false;
00219 }
00220 
00228 bool DocEntrySet::SetBinEntry(uint8_t *content, int lgth, BinEntry *entry)
00229 {
00230    if(entry)
00231    {
00232       entry->SetBinArea(content);  
00233       entry->SetLength(lgth);
00234       entry->SetValue(GDCM_BINLOADED);
00235       return true;
00236    }
00237    return false;
00238 }
00239 
00250 ValEntry *DocEntrySet::InsertValEntry(std::string const &value, 
00251                                       uint16_t group, uint16_t elem,
00252                                       TagName const &vr )
00253 {
00254    ValEntry *valEntry = 0;
00255    DocEntry *currentEntry = GetDocEntry( group, elem );
00256    
00257    if (currentEntry)
00258    {
00259       valEntry = dynamic_cast<ValEntry *>(currentEntry);
00260 
00261       // Verify the VR
00262       if( valEntry )
00263          if( valEntry->GetVR()!=vr )
00264             valEntry = NULL;
00265 
00266       // if currentEntry doesn't correspond to the requested valEntry
00267       if( !valEntry)
00268       {
00269          if( !RemoveEntry(currentEntry) )
00270          {
00271             gdcmWarningMacro( "Removal of previous DocEntry failed.");
00272 
00273             return NULL;
00274          }
00275       }
00276    }
00277 
00278    // Create a new valEntry if necessary
00279    if( !valEntry )
00280    {
00281       valEntry = NewValEntry( group, elem, vr );
00282 
00283       if ( !AddEntry(valEntry) )
00284       {
00285          gdcmWarningMacro("AddEntry failed although this is a creation.");
00286 
00287          delete valEntry;
00288          return NULL;
00289       }
00290    }
00291 
00292    // Set the binEntry value
00293    SetValEntry(value, valEntry); // The std::string value
00294    return valEntry;
00295 }
00296 
00309 BinEntry *DocEntrySet::InsertBinEntry(uint8_t *binArea, int lgth, 
00310                                       uint16_t group, uint16_t elem,
00311                                       TagName const &vr )
00312 {
00313    BinEntry *binEntry = 0;
00314    DocEntry *currentEntry = GetDocEntry( group, elem );
00315 
00316    // Verify the currentEntry
00317    if (currentEntry)
00318    {
00319       binEntry = dynamic_cast<BinEntry *>(currentEntry);
00320 
00321       // Verify the VR
00322       if( binEntry )
00323          if( binEntry->GetVR()!=vr )
00324             binEntry = NULL;
00325 
00326       // if currentEntry doesn't correspond to the requested valEntry
00327       if( !binEntry)
00328       {
00329          if( !RemoveEntry(currentEntry) )
00330          {
00331             gdcmWarningMacro( "Removal of previous DocEntry failed.");
00332 
00333             return NULL;
00334          }
00335       }
00336    }
00337 
00338    // Create a new binEntry if necessary
00339    if( !binEntry)
00340    {
00341       binEntry = NewBinEntry(group, elem, vr);
00342 
00343       if ( !AddEntry(binEntry) )
00344       {
00345          gdcmWarningMacro( "AddEntry failed allthough this is a creation.");
00346 
00347          delete binEntry;
00348          return NULL;
00349       }
00350    }
00351 
00352    // Set the binEntry value
00353    uint8_t *tmpArea;
00354    if( lgth>0 && binArea )
00355    {
00356       tmpArea = new uint8_t[lgth];
00357       memcpy(tmpArea,binArea,lgth);
00358    }
00359    else
00360    {
00361       tmpArea = 0;
00362    }
00363    if( !SetBinEntry(tmpArea,lgth,binEntry) )
00364    {
00365       if( tmpArea )
00366       {
00367          delete[] tmpArea;
00368       }
00369    }
00370 
00371    return binEntry;
00372 }  
00373 
00382 SeqEntry *DocEntrySet::InsertSeqEntry(uint16_t group, uint16_t elem)
00383 {
00384    SeqEntry *seqEntry = 0;
00385    DocEntry *currentEntry = GetDocEntry( group, elem );
00386 
00387    // Verify the currentEntry
00388    if( currentEntry )
00389    {
00390       seqEntry = dynamic_cast<SeqEntry *>(currentEntry);
00391 
00392       // Verify the VR
00393       if( seqEntry )
00394          seqEntry = NULL;
00395 
00396       // if currentEntry doesn't correspond to the requested seqEntry
00397       if( !seqEntry )
00398       {
00399          if (!RemoveEntry(currentEntry))
00400          {
00401             gdcmWarningMacro( "Removal of previous DocEntry failed.");
00402 
00403             return NULL;
00404          }
00405       }
00406    }
00407    // Create a new seqEntry if necessary
00408    if( !seqEntry )
00409    {
00410       seqEntry = NewSeqEntry(group, elem);
00411 
00412       if( !AddEntry(seqEntry) )
00413       {
00414          gdcmWarningMacro( "AddEntry failed allthough this is a creation.");
00415 
00416          delete seqEntry;
00417          return NULL;
00418       }
00419    }
00420 
00421    // TODO : Find a trick to insert a SequenceDelimitationItem 
00422         //       in the SeqEntry, at the end.
00423    return seqEntry;
00424 } 
00425 
00426 
00427  
00434 bool DocEntrySet::CheckIfEntryExist(uint16_t group, uint16_t elem )
00435 {
00436    return GetDocEntry(group,elem)!=NULL;
00437 }
00438 
00447 ValEntry *DocEntrySet::NewValEntry(uint16_t group,uint16_t elem,
00448                                    TagName const &vr) 
00449 {
00450    DictEntry *dictEntry = GetDictEntry(group, elem, vr);
00451    gdcmAssertMacro(dictEntry);
00452 
00453    ValEntry *newEntry = new ValEntry(dictEntry);
00454    if (!newEntry) 
00455    {
00456       gdcmWarningMacro( "Failed to allocate ValEntry");
00457       return 0;
00458    }
00459    return newEntry;
00460 }
00461 
00462 
00471 BinEntry *DocEntrySet::NewBinEntry(uint16_t group, uint16_t elem,
00472                                    TagName const &vr) 
00473 {
00474    DictEntry *dictEntry = GetDictEntry(group, elem, vr);
00475    gdcmAssertMacro(dictEntry);
00476 
00477    BinEntry *newEntry = new BinEntry(dictEntry);
00478    if (!newEntry) 
00479    {
00480       gdcmWarningMacro( "Failed to allocate BinEntry");
00481       return 0;
00482    }
00483    return newEntry;
00484 }
00485 
00493 SeqEntry* DocEntrySet::NewSeqEntry(uint16_t group, uint16_t elem) 
00494 {
00495    DictEntry *dictEntry = GetDictEntry(group, elem, "SQ");
00496    gdcmAssertMacro(dictEntry);
00497 
00498    SeqEntry *newEntry = new SeqEntry( dictEntry );
00499    if (!newEntry)
00500    {
00501       gdcmWarningMacro( "Failed to allocate SeqEntry");
00502       return 0;
00503    }
00504    return newEntry;
00505 }
00506 
00515 DictEntry* DocEntrySet::NewVirtualDictEntry( uint16_t group, uint16_t elem,
00516                                              TagName const &vr,
00517                                              TagName const &vm,
00518                                              TagName const &name )
00519 {
00520    return Global::GetDicts()->NewVirtualDictEntry(group,elem,vr,vm,name);
00521 }
00522 
00523 //-----------------------------------------------------------------------------
00524 // Protected
00534 DictEntry *DocEntrySet::GetDictEntry(uint16_t group,uint16_t elem) 
00535 {
00536    DictEntry *found = 0;
00537    Dict *pubDict = Global::GetDicts()->GetDefaultPubDict();
00538    if (!pubDict) 
00539    {
00540       gdcmWarningMacro( "We SHOULD have a default dictionary");
00541    }
00542    else
00543    {
00544       found = pubDict->GetEntry(group, elem);  
00545    }
00546    return found;
00547 }
00548 
00558 DictEntry *DocEntrySet::GetDictEntry(uint16_t group, uint16_t elem,
00559                                      TagName const &vr)
00560 {
00561    DictEntry *dictEntry = GetDictEntry(group,elem);
00562    DictEntry *goodEntry = dictEntry;
00563    std::string goodVR = vr;
00564 
00565    if (elem == 0x0000) goodVR="UL";
00566 
00567    if ( goodEntry )
00568    {
00569       if ( goodVR != goodEntry->GetVR()
00570         && goodVR != GDCM_UNKNOWN )
00571       {
00572          goodEntry = NULL;
00573       }
00574    }
00575 
00576    // Create a new virtual DictEntry if necessary
00577    if (!goodEntry)
00578    {
00579       if (dictEntry)
00580       {
00581          goodEntry = NewVirtualDictEntry(group, elem, goodVR, "FIXME", 
00582                                          dictEntry->GetName() );
00583       }
00584       else
00585       {
00586          goodEntry = NewVirtualDictEntry(group, elem, goodVR);
00587       }
00588    }
00589    return goodEntry;
00590 }
00591 
00592 //-----------------------------------------------------------------------------
00593 // Private
00594 
00595 //-----------------------------------------------------------------------------
00596 // Print
00597 
00598 //-----------------------------------------------------------------------------
00599 } // end namespace gdcm

Generated on Thu Feb 10 22:17:31 2005 for gdcm by doxygen 1.3.6