Program: gdcm
Module: $RCSfile: gdcmDocument.cxx,v $
Language: C++
- Date: $Date: 2004/09/10 14:32:04 $
- Version: $Revision: 1.73 $
+ Date: $Date: 2004/09/17 13:11:16 $
+ Version: $Revision: 1.81 $
Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
l'Image). All rights reserved. See Doc/License.txt or
long beg = ftell(Fp);
lgt -= beg;
- SQDepthLevel = 0;
-
- long l = ParseDES( this, beg, lgt, false); // le Load sera fait a la volee
- (void)l; //is l used anywhere ?
+ (void)ParseDES( this, beg, lgt, false); // le Load sera fait a la volee
rewind(Fp);
if( PhotometricInterpretation == "PALETTE COLOR " )
{
LoadEntryVoidArea(0x0028,0x1200); // gray LUT
+ /// FIXME FIXME FIXME
+ /// The tags refered by the three following lines used to be CORRECTLY
+ /// defined as having an US Value Representation in the public
+ /// dictionnary. BUT the semantics implied by the three following
+ /// lines state that the corresponding tag contents are in fact
+ /// the ones of a gdcmBinEntry.
+ /// In order to fix things "Quick and Dirty" the dictionnary was
+ /// altered on PURPOUS but now contains a WRONG value.
+ /// In order to fix things and restore the dictionary to its
+ /// correct value, one needs to decided of the semantics by deciding
+ /// wether the following tags are either:
+ /// - multivaluated US, and hence loaded as gdcmValEntry, but afterwards
+ /// also used as gdcmBinEntry, which requires the proper conversion,
+ /// - OW, and hence loaded as gdcmBinEntry, but afterwards also used
+ /// as gdcmValEntry, which requires the proper conversion.
LoadEntryVoidArea(0x0028,0x1201); // R LUT
LoadEntryVoidArea(0x0028,0x1202); // G LUT
LoadEntryVoidArea(0x0028,0x1203); // B LUT
- LoadEntryVoidArea(0x0028,0x1221); // Segmented Red Palette Color LUT Data
- LoadEntryVoidArea(0x0028,0x1222); // Segmented Green Palette Color LUT Data
- LoadEntryVoidArea(0x0028,0x1223); // Segmented Blue Palette Color LUT Data
+ // Segmented Red Palette Color LUT Data
+ LoadEntryVoidArea(0x0028,0x1221);
+ // Segmented Green Palette Color LUT Data
+ LoadEntryVoidArea(0x0028,0x1222);
+ // Segmented Blue Palette Color LUT Data
+ LoadEntryVoidArea(0x0028,0x1223);
}
//FIXME later : how to use it?
LoadEntryVoidArea(0x0028,0x3006); //LUT Data (CTX dependent)
for (TagDocEntryHT::const_iterator it = TagHT.begin();
it != TagHT.end(); ++it )
{
- delete it->second;
+ //delete it->second; //temp remove
}
TagHT.clear();
}
* \return pointer to the modified/created Header Entry (NULL when creation
* failed).
*/
-
+
+/*
gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
std::string const & value,
uint16_t group,
return valEntry;
}
+*/
+
+/**
+ * \brief Modifies the value of a given Header Entry (Dicom Element)
+ * when it exists. Create it with the given value when unexistant.
+ * @param value (string) Value to be set
+ * @param group Group number of the Entry
+ * @param elem Element number of the Entry
+ * @param VR V(alue) R(epresentation) of the Entry -if private Entry-
+ * \return pointer to the modified/created Header Entry (NULL when creation
+ * failed).
+ */
+
+ // TODO : write something clever, using default value for VR
+ // to avoid code duplication
+ // (I don't know how to tell NewDocEntryByNumber
+ // that ReplaceOrCreateByNumber was called with a default value)
+
+gdcmValEntry * gdcmDocument::ReplaceOrCreateByNumber(
+ std::string const & value,
+ uint16_t group,
+ uint16_t elem,
+ std::string const & VR )
+{
+ gdcmValEntry* valEntry = 0;
+ gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem);
+
+ if (!currentEntry)
+ {
+ // check if (group,element) DictEntry exists
+ // if it doesn't, create an entry in gdcmDictSet::VirtualEntry
+ // and use it
+
+ // Find out if the tag we received is in the dictionaries:
+ gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
+ gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem);
+ if (!dictEntry)
+ {
+ currentEntry = NewDocEntryByNumber(group, elem,VR);
+ }
+ else
+ {
+ currentEntry = NewDocEntryByNumber(group, elem);
+ }
+
+ if (!currentEntry)
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: call to"
+ " NewDocEntryByNumber failed.");
+ return NULL;
+ }
+ valEntry = new gdcmValEntry(currentEntry);
+ if ( !AddEntry(valEntry))
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: AddEntry"
+ " failed allthough this is a creation.");
+ }
+ }
+ else
+ {
+ valEntry = dynamic_cast< gdcmValEntry* >(currentEntry);
+ if ( !valEntry ) // Euuuuh? It wasn't a ValEntry
+ // then we change it to a ValEntry ?
+ // Shouldn't it be considered as an error ?
+ {
+ // We need to promote the gdcmDocEntry to a gdcmValEntry:
+ valEntry = new gdcmValEntry(currentEntry);
+ if (!RemoveEntry(currentEntry))
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: removal"
+ " of previous DocEntry failed.");
+ return NULL;
+ }
+ if ( !AddEntry(valEntry))
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: adding"
+ " promoted ValEntry failed.");
+ return NULL;
+ }
+ }
+ }
+
+ SetEntryByNumber(value, group, elem);
+
+ return valEntry;
+}
/*
* \brief Modifies the value of a given Header Entry (Dicom Element)
void *voidArea,
int lgth,
uint16_t group,
- uint16_t elem)
+ uint16_t elem,
+ std::string const & VR )
{
- gdcmBinEntry* b = 0;
- gdcmDocEntry* a = GetDocEntryByNumber( group, elem);
- if (!a)
+ gdcmBinEntry* binEntry = 0;
+ gdcmDocEntry* currentEntry = GetDocEntryByNumber( group, elem);
+ if (!currentEntry)
{
- a = NewBinEntryByNumber(group, elem);
- if (!a)
+
+ // check if (group,element) DictEntry exists
+ // if it doesn't, create an entry in gdcmDictSet::VirtualEntry
+ // and use it
+
+ // Find out if the tag we received is in the dictionaries:
+ gdcmDict *pubDict = gdcmGlobal::GetDicts()->GetDefaultPubDict();
+ gdcmDictEntry *dictEntry = pubDict->GetDictEntryByNumber(group, elem);
+
+ if (!dictEntry)
{
- return 0;
+ currentEntry = NewDocEntryByNumber(group, elem,VR);
+ }
+ else
+ {
+ currentEntry = NewDocEntryByNumber(group, elem);
+ }
+ if (!currentEntry)
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: call to"
+ " NewDocEntryByNumber failed.");
+ return NULL;
+ }
+ binEntry = new gdcmBinEntry(currentEntry);
+ if ( !AddEntry(binEntry))
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: AddEntry"
+ " failed allthough this is a creation.");
+ }
+ }
+ else
+ {
+ binEntry = dynamic_cast< gdcmBinEntry* >(currentEntry);
+ if ( !binEntry ) // Euuuuh? It wasn't a BinEntry
+ // then we change it to a BinEntry ?
+ // Shouldn't it be considered as an error ?
+ {
+ // We need to promote the gdcmDocEntry to a gdcmBinEntry:
+ binEntry = new gdcmBinEntry(currentEntry);
+ if (!RemoveEntry(currentEntry))
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: removal"
+ " of previous DocEntry failed.");
+ return NULL;
+ }
+ if ( !AddEntry(binEntry))
+ {
+ dbg.Verbose(0, "gdcmDocument::ReplaceOrCreateByNumber: adding"
+ " promoted BinEntry failed.");
+ return NULL;
+ }
}
+ }
- b = new gdcmBinEntry(a);
- AddEntry(b);
- b->SetVoidArea(voidArea);
- }
SetEntryByNumber(voidArea, lgth, group, elem);
- //b->SetVoidArea(voidArea); //what if b == 0 !!
- return b;
+ return binEntry;
}
uint16_t group,
uint16_t element)
{
+ int c;
+ int l;
+
gdcmValEntry* valEntry = GetValEntryByNumber(group, element);
if (!valEntry )
{
return false;
}
// Non even content must be padded with a space (020H)...
- std::string evenContent = content;
- if( evenContent.length() % 2 )
+ std::string finalContent = content;
+ if( finalContent.length() % 2 )
{
- evenContent += '\0'; // ... therefore we padd with (000H) .!?!
+ finalContent += '\0'; // ... therefore we padd with (000H) .!?!
}
- valEntry->SetValue(evenContent);
+ valEntry->SetValue(finalContent);
// Integers have a special treatement for their length:
- gdcmVRKey vr = valEntry->GetVR();
- if( vr == "US" || vr == "SS" )
- {
- int c = CountSubstring(content, "\\"); // for multivaluated items
- valEntry->SetLength((c+1)*2);
- }
- else if( vr == "UL" || vr == "SL" )
- {
- int c = CountSubstring(content, "\\"); // for multivaluated items
- valEntry->SetLength((c+1)*4);
- }
- else
- {
- valEntry->SetLength(evenContent.length());
- }
+ l = finalContent.length();
+ if ( l != 0) // To avoid to be cheated by 'zero length' integers
+ {
+ gdcmVRKey vr = valEntry->GetVR();
+ if( vr == "US" || vr == "SS" )
+ {
+ c = CountSubstring(content, "\\") + 1; // for multivaluated items
+ l = c*2;
+ }
+ else if( vr == "UL" || vr == "SL" )
+ {
+ c = CountSubstring(content, "\\") + 1; // for multivaluated items
+ l = c*4;;
+ }
+ }
+ valEntry->SetLength(l);
return true;
}
*/
gdcmBinEntry* a = (gdcmBinEntry *)TagHT[key];
a->SetVoidArea(content);
- //a->SetLength(lgth); // ???
+ a->SetLength(lgth);
return true;
}
* @param group group number of the Entry
* @param elem element number of the Entry
*/
-void *gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem)
+void* gdcmDocument::LoadEntryVoidArea(uint16_t group, uint16_t elem)
{
gdcmDocEntry *docElement = GetDocEntryByNumber(group, elem);
if ( !docElement )
return NULL;
}
/// \todo Drop any already existing void area! JPR
- SetEntryVoidAreaByNumber(a, group, elem);
+ if( !SetEntryVoidAreaByNumber( a, group, elem ) );
+ {
+ dbg.Verbose(0, "gdcmDocument::LoadEntryVoidArea setting failed.");
+ }
return a;
}
uint16_t group,
uint16_t element)
{
- gdcmTagKey key = gdcmDictEntry::TranslateToKey(group, element);
- if ( !TagHT.count(key))
+ gdcmDocEntry* currentEntry = GetDocEntryByNumber(group, element);
+ if ( !currentEntry )
{
return false;
}
-
- // This was for multimap ?
- (( gdcmBinEntry *)( ((TagHT.equal_range(key)).first)->second ))->SetVoidArea(area);
-
+ if ( gdcmBinEntry* binEntry = dynamic_cast<gdcmBinEntry*>(currentEntry) )
+ {
+ binEntry->SetVoidArea( area );
+ return true;
+ }
return true;
}
bool delim_mode)
{
gdcmDocEntry *newDocEntry = 0;
- gdcmValEntry *newValEntry = 0;
unsigned long l = 0;
- int depth = set->GetDepthLevel();
while (true)
{
if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
if ( gdcmGlobal::GetVR()->IsVROfGdcmStringRepresentable(vr) )
{
- /////// ValEntry
- newValEntry = new gdcmValEntry(newDocEntry->GetDictEntry());
- newValEntry->Copy(newDocEntry);
- newValEntry->SetDepthLevel(depth);
- set->AddEntry(newValEntry);
- LoadDocEntry(newValEntry);
+ /////////////////////// ValEntry
+ gdcmValEntry* newValEntry =
+ new gdcmValEntry( newDocEntry->GetDictEntry() );
+ newValEntry->Copy( newDocEntry );
+
+ // When "set" is a gdcmDocument, then we are at the top of the
+ // hierarchy and the Key is simply of the form ( group, elem )...
+ if (gdcmDocument* dummy = dynamic_cast< gdcmDocument* > ( set ) )
+ {
+ newValEntry->SetKey( newValEntry->GetKey() );
+ }
+ // ...but when "set" is a gdcmSQItem, we are inserting this new
+ // valEntry in a sequence item. Hence the key has the
+ // generalized form (refer to \ref gdcmBaseTagKey):
+ if (gdcmSQItem* parentSQItem = dynamic_cast< gdcmSQItem* > ( set ) )
+ {
+ newValEntry->SetKey( parentSQItem->GetBaseTagKey()
+ + newValEntry->GetKey() );
+ }
+
+ set->AddEntry( newValEntry );
+ LoadDocEntry( newValEntry );
if (newValEntry->IsItemDelimitor())
{
break;
"nor BinEntry. Probably unknown VR.");
}
- ////// BinEntry or UNKOWN VR:
- gdcmBinEntry *bn = new gdcmBinEntry(newDocEntry->GetDictEntry());
- bn->Copy(newDocEntry);
- set->AddEntry(bn);
- LoadDocEntry(bn);
+ //////////////////// BinEntry or UNKOWN VR:
+ gdcmBinEntry* newBinEntry =
+ new gdcmBinEntry( newDocEntry->GetDictEntry() );
+ newBinEntry->Copy( newDocEntry );
+
+ // When "this" is a gdcmDocument the Key is simply of the
+ // form ( group, elem )...
+ if (gdcmDocument* dummy = dynamic_cast< gdcmDocument* > ( set ) )
+ {
+ newBinEntry->SetKey( newBinEntry->GetKey() );
+ }
+ // but when "this" is a SQItem, we are inserting this new
+ // valEntry in a sequence item, and the kay has the
+ // generalized form (refer to \ref gdcmBaseTagKey):
+ if (gdcmSQItem* parentSQItem = dynamic_cast< gdcmSQItem* > ( set ) )
+ {
+ newBinEntry->SetKey( parentSQItem->GetBaseTagKey()
+ + newBinEntry->GetKey() );
+ }
+
+ set->AddEntry( newBinEntry );
+ LoadDocEntry( newBinEntry );
}
if (newDocEntry->GetGroup() == 0x7fe0 &&
}
}
// no other way to create it ...
- gdcmSeqEntry *sq = new gdcmSeqEntry(newDocEntry->GetDictEntry(),
- set->GetDepthLevel());
- sq->Copy(newDocEntry);
- sq->SetDelimitorMode(delim_mode);
- sq->SetDepthLevel(depth);
+ gdcmSeqEntry* newSeqEntry =
+ new gdcmSeqEntry( newDocEntry->GetDictEntry() );
+ newSeqEntry->Copy( newDocEntry );
+ newSeqEntry->SetDelimitorMode( delim_mode );
+
+ // At the top of the hierarchy, stands a gdcmDocument. When "set"
+ // is a gdcmDocument, then we are building the first depth level.
+ // Hence the gdcmSeqEntry we are building simply has a depth
+ // level of one:
+ if (gdcmDocument* dummy = dynamic_cast< gdcmDocument* > ( set ) )
+ {
+ newSeqEntry->SetDepthLevel( 1 );
+ newSeqEntry->SetKey( newSeqEntry->GetKey() );
+ }
+ // But when "set" is allready a SQItem, we are building a nested
+ // sequence, and hence the depth level of the new gdcmSeqEntry
+ // we are building, is one level deeper:
+ if (gdcmSQItem* parentSQItem = dynamic_cast< gdcmSQItem* > ( set ) )
+ {
+ newSeqEntry->SetDepthLevel( parentSQItem->GetDepthLevel() + 1 );
+ newSeqEntry->SetKey( parentSQItem->GetBaseTagKey()
+ + newSeqEntry->GetKey() );
+ }
if ( l != 0 )
{ // Don't try to parse zero-length sequences
- long lgt = ParseSQ( sq,
- newDocEntry->GetOffset(),
- l, delim_mode);
- (void)lgt; //not used...
+ (void)ParseSQ( newSeqEntry,
+ newDocEntry->GetOffset(),
+ l, delim_mode);
}
- set->AddEntry(sq);
+ set->AddEntry( newSeqEntry );
if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
{
break;
* \brief Parses a Sequence ( SeqEntry after SeqEntry)
* @return parsed length for this level
*/
-long gdcmDocument::ParseSQ(gdcmSeqEntry *set,
- long offset, long l_max, bool delim_mode)
+long gdcmDocument::ParseSQ( gdcmSeqEntry* seqEntry,
+ long offset, long l_max, bool delim_mode)
{
int SQItemNumber = 0;
bool dlm_mod;
- //int depth = set->GetDepthLevel();
- //(void)depth; //not used
while (true)
{
- gdcmDocEntry *newDocEntry = ReadNextDocEntry();
+ gdcmDocEntry* newDocEntry = ReadNextDocEntry();
if ( !newDocEntry )
{
// FIXME Should warn user
{
if ( newDocEntry->IsSequenceDelimitor() )
{
- set->SetSequenceDelimitationItem( newDocEntry );
+ seqEntry->SetSequenceDelimitationItem( newDocEntry );
break;
}
}
break;
}
- gdcmSQItem *itemSQ = new gdcmSQItem(set->GetDepthLevel());
- itemSQ->AddEntry(newDocEntry);
+ gdcmSQItem *itemSQ = new gdcmSQItem( seqEntry->GetDepthLevel() );
+ std::ostringstream newBase;
+ newBase << seqEntry->GetKey()
+ << "/"
+ << SQItemNumber
+ << "#";
+ itemSQ->SetBaseTagKey( newBase.str() );
unsigned int l = newDocEntry->GetReadLength();
if ( l == 0xffffffff )
dlm_mod = false;
}
- int lgr = ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod);
- (void)lgr; //FIXME not used
+ (void)ParseDES(itemSQ, newDocEntry->GetOffset(), l, dlm_mod);
- set->AddEntry(itemSQ, SQItemNumber);
+ seqEntry->AddEntry( itemSQ, SQItemNumber );
SQItemNumber++;
- if ( !delim_mode && (ftell(Fp)-offset) >= l_max)
+ if ( !delim_mode && ( ftell(Fp) - offset ) >= l_max )
{
break;
}
// When we find a BinEntry not very much can be done :
if (gdcmBinEntry* binEntryPtr = dynamic_cast< gdcmBinEntry* >(entry) )
{
-
- LoadEntryVoidArea(binEntryPtr);
s << "gdcm::Loaded (BinEntry)";
binEntryPtr->SetValue(s.str());
+ LoadEntryVoidArea(binEntryPtr); // last one, not to erase length !
return;
}
if ( IsDocEntryAnInteger(entry) )
{
uint32_t NewInt;
- //std::ostringstream s; //shadow previous declaration
int nbInt;
// When short integer(s) are expected, read and convert the following
// n *two characters properly i.e. consider them as short integers as
/**
* \brief Parse pixel data from disk for multi-fragment Jpeg/Rle files
* No other way so 'skip' the Data
- *
*/
void gdcmDocument::Parse7FE0 ()
{
}
}
+/**
+ * \brief Walk recursively the given \ref gdcmDocEntrySet, and feed
+ * the given hash table (\ref TagDocEntryHT) with all the
+ * \ref gdcmDocEntry (Dicom entries) encountered.
+ * This method does the job for \ref BuildFlatHashTable.
+ * @param builtHT Where to collect all the \ref gdcmDocEntry encountered
+ * when recursively walking the given set.
+ * @param set The structure to be traversed (recursively).
+ */
+void gdcmDocument::BuildFlatHashTableRecurse( TagDocEntryHT& builtHT,
+ gdcmDocEntrySet* set )
+{
+ if (gdcmElementSet* elementSet = dynamic_cast< gdcmElementSet* > ( set ) )
+ {
+ TagDocEntryHT* currentHT = elementSet->GetTagHT();
+ for( TagDocEntryHT::const_iterator i = currentHT->begin();
+ i != currentHT->end();
+ ++i)
+ {
+ gdcmDocEntry* entry = i->second;
+ if ( gdcmSeqEntry* seqEntry = dynamic_cast<gdcmSeqEntry*>(entry) )
+ {
+ ListSQItem& items = seqEntry->GetSQItems();
+ for( ListSQItem::const_iterator item = items.begin();
+ item != items.end();
+ ++item)
+ {
+ BuildFlatHashTableRecurse( builtHT, *item );
+ }
+ continue;
+ }
+ builtHT[entry->GetKey()] = entry;
+ }
+ return;
+ }
+
+ if (gdcmSQItem* SQItemSet = dynamic_cast< gdcmSQItem* > ( set ) )
+ {
+ ListDocEntry& currentList = SQItemSet->GetDocEntries();
+ for (ListDocEntry::iterator i = currentList.begin();
+ i != currentList.end();
+ ++i)
+ {
+ gdcmDocEntry* entry = *i;
+ if ( gdcmSeqEntry* seqEntry = dynamic_cast<gdcmSeqEntry*>(entry) )
+ {
+ ListSQItem& items = seqEntry->GetSQItems();
+ for( ListSQItem::const_iterator item = items.begin();
+ item != items.end();
+ ++item)
+ {
+ BuildFlatHashTableRecurse( builtHT, *item );
+ }
+ continue;
+ }
+ builtHT[entry->GetKey()] = entry;
+ }
+
+ }
+}
+
+/**
+ * \brief Build a \ref TagDocEntryHT (i.e. a std::map<>) from the current
+ * gdcmDocument.
+ *
+ * The structure used by a gdcmDocument (through \ref gdcmElementSet),
+ * in order to old the parsed entries of a Dicom header, is a recursive
+ * one. This is due to the fact that the sequences (when present)
+ * can be nested. Additionaly, the sequence items (represented in
+ * gdcm as \ref gdcmSQItem) add an extra complexity to the data
+ * structure. Hence, a gdcm user whishing to visit all the entries of
+ * a Dicom header will need to dig in the gdcm internals (which
+ * implies exposing all the internal data structures to the API).
+ * In order to avoid this burden to the user, \ref BuildFlatHashTable
+ * recursively builds a temporary hash table, which olds all the
+ * Dicom entries in a flat structure (a \ref TagDocEntryHT i.e. a
+ * std::map<>).
+ * \warning Of course there is NO integrity constrain between the
+ * returned \ref TagDocEntryHT and the \ref gdcmElemenSet used
+ * to build it. Hence if the underlying \ref gdcmElemenSet is
+ * altered, then it is the caller responsability to invoke
+ * \ref BuildFlatHashTable again...
+ * @return The flat std::map<> we juste build.
+ */
+TagDocEntryHT* gdcmDocument::BuildFlatHashTable()
+{
+ TagDocEntryHT* FlatHT = new TagDocEntryHT;
+ BuildFlatHashTableRecurse( *FlatHT, this );
+ return FlatHT;
+}
+
/**