1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2005/11/21 12:15:06 $
7 Version: $Revision: 1.23 $
9 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10 l'Image). All rights reserved. See Doc/License.txt or
11 http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
13 This software is distributed WITHOUT ANY WARRANTY; without even
14 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 PURPOSE. See the above copyright notices for more information.
17 =========================================================================*/
19 #include "gdcmDataEntry.h"
22 #include "gdcmGlobal.h"
24 #include "gdcmDebug.h"
30 //-----------------------------------------------------------------------------
31 #define MAX_SIZE_PRINT_ELEMENT_VALUE 0x7fffffff
32 uint32_t DataEntry::MaxSizePrintEntry = MAX_SIZE_PRINT_ELEMENT_VALUE;
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
37 * \brief Constructor for a given DictEntry
38 * @param e Pointer to existing dictionary entry
40 DataEntry::DataEntry(DictEntry *e)
51 * \brief Constructor for a given DocEntry
52 * @param e Pointer to existing Doc entry
54 DataEntry::DataEntry(DocEntry *e)
55 : DocEntry(e->GetDictEntry())
65 * \brief Canonical destructor.
67 DataEntry::~DataEntry ()
72 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
78 * \brief Sets the value (non string) of the current Dicom Header Entry
80 void DataEntry::SetBinArea( uint8_t *area, bool self )
90 * \brief Inserts the value (non string) into the current Dicom Header Entry
92 void DataEntry::CopyBinArea( uint8_t *area, uint32_t length )
96 uint32_t lgh = length + length%2;
99 if( area && length > 0 )
102 memcpy(BinArea,area,length);
106 State = STATE_LOADED;
110 void DataEntry::SetValue(const uint32_t &id, const double &val)
114 State = STATE_LOADED;
116 if( id > GetValueCount() )
118 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
122 const VRKey &vr = GetVR();
123 if( vr == "US" || vr == "SS" )
125 uint16_t *data = (uint16_t *)BinArea;
126 data[id] = (uint16_t)val;
128 else if( vr == "UL" || vr == "SL" )
130 uint32_t *data = (uint32_t *)BinArea;
131 data[id] = (uint32_t)val;
133 else if( vr == "FL" )
135 float *data = (float *)BinArea;
136 data[id] = (float)val;
138 else if( vr == "FD" )
140 double *data = (double *)BinArea;
141 data[id] = (double)val;
143 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
145 gdcmErrorMacro("SetValue on String representable not implemented yet");
149 BinArea[id] = (uint8_t)val;
153 * \brief returns, as a double (?!?) one of the values
154 // (when entry is multivaluated), identified by its index.
155 // Returns 0.0 if index is wrong
156 // FIXME : warn the user there was a problem !
158 double DataEntry::GetValue(const uint32_t &id) const
162 gdcmErrorMacro("BinArea not set. Can't get the value");
166 uint32_t count = GetValueCount();
169 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
173 // FIX the API : user *knows* that entry contains a US
174 // and he receives a double ?!?
176 const VRKey &vr = GetVR();
177 if( vr == "US" || vr == "SS" )
178 return ((uint16_t *)BinArea)[id];
179 else if( vr == "UL" || vr == "SL" )
180 return ((uint32_t *)BinArea)[id];
181 else if( vr == "FL" )
182 return ((float *)BinArea)[id];
183 else if( vr == "FD" )
184 return ((double *)BinArea)[id];
185 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
189 // Don't use std::string to accelerate processing
191 char *tmp = new char[GetLength()+1];
192 memcpy(tmp,BinArea,GetLength());
203 for(uint32_t i=0;i<GetLength();i++)
233 * \brief Checks if the multiplicity of the value follows Dictionary VM
235 bool DataEntry::IsValueCountValid() const
238 const std::string &strVM = GetVM();
239 uint32_t vc = GetValueCount();
240 bool valid = vc == 0;
244 // FIXME : what shall we do with VM = "2-n", "3-n", etc
248 // make sure there is at least one ??? FIXME
253 std::istringstream os;
257 // vm respects the one from the dict
258 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
262 // Entries whose type is 1 are mandatory, with a mandatory value.
263 // Entries whose type is 1c are mandatory-inside-a-Sequence,
264 // with a mandatory value.
265 // Entries whose type is 2 are mandatory, with an optional value.
266 // Entries whose type is 2c are mandatory-inside-a-Sequence,
267 // with an optional value.
268 // Entries whose type is 3 are optional.
270 // case vc == 0 is only applicable for 'type 2' entries.
271 // Problem : entry type may depend on the modality and/or the Sequence
272 // it's embedded in !
273 // (Get the information in the 'Conformance Statements' ...)
280 * \brief returns the number of elementary values
282 uint32_t DataEntry::GetValueCount( ) const
284 const VRKey &vr = GetVR();
285 if( vr == "US" || vr == "SS" )
286 return GetLength()/sizeof(uint16_t);
287 else if( vr == "UL" || vr == "SL" )
288 return GetLength()/sizeof(uint32_t);
289 else if( vr == "FL" || vr == "OF" )
290 return GetLength()/4 ; // FL has a *4* length! sizeof(float);
291 else if( vr == "FD" )
292 return GetLength()/8; // FD has a *8* length! sizeof(double);
293 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
295 // Some element in DICOM are allowed to be empty
298 // Don't use std::string to accelerate processing
300 for(uint32_t i=0;i<GetLength();i++)
302 if( BinArea[i] == '\\')
310 * \brief Sets the 'value' of an Entry, passed as a std::string
311 * @param value string representation of the value to be set
313 void DataEntry::SetString(std::string const &value)
317 const VRKey &vr = GetVR();
318 if ( vr == "US" || vr == "SS" )
320 std::vector<std::string> tokens;
321 Util::Tokenize (value, tokens, "\\");
322 SetLength(tokens.size()*sizeof(uint16_t));
325 uint16_t *data = (uint16_t *)BinArea;
326 for (unsigned int i=0; i<tokens.size();i++)
327 data[i] = atoi(tokens[i].c_str());
330 else if ( vr == "UL" || vr == "SL" )
332 std::vector<std::string> tokens;
333 Util::Tokenize (value, tokens, "\\");
334 SetLength(tokens.size()*sizeof(uint32_t));
337 uint32_t *data = (uint32_t *)BinArea;
338 for (unsigned int i=0; i<tokens.size();i++)
339 data[i] = atoi(tokens[i].c_str());
342 else if ( vr == "FL" )
344 std::vector<std::string> tokens;
345 Util::Tokenize (value, tokens, "\\");
346 SetLength(tokens.size()*sizeof(float));
349 float *data = (float *)BinArea;
350 for (unsigned int i=0; i<tokens.size();i++)
351 data[i] = (float)atof(tokens[i].c_str());
354 else if ( vr == "FD" )
356 std::vector<std::string> tokens;
357 Util::Tokenize (value, tokens, "\\");
358 SetLength(tokens.size()*sizeof(double));
361 double *data = (double *)BinArea;
362 for (unsigned int i=0; i<tokens.size();i++)
363 data[i] = atof(tokens[i].c_str());
368 if( value.size() > 0 )
370 size_t l = value.size();
373 memcpy(BinArea, value.c_str(), l);
378 State = STATE_LOADED;
381 * \brief returns as a string (when possible) the value of the DataEntry
383 std::string const &DataEntry::GetString() const
385 static std::ostringstream s;
386 const VRKey &vr = GetVR();
394 // When short integer(s) are stored, convert the following (n * 2) characters
395 // as a displayable string, the values being separated by a back-slash
397 if( vr == "US" || vr == "SS" )
399 uint16_t *data=(uint16_t *)BinArea;
401 for (unsigned int i=0; i < GetValueCount(); i++)
409 // See above comment on multiple short integers (mutatis mutandis).
410 else if( vr == "UL" || vr == "SL" )
412 uint32_t *data=(uint32_t *)BinArea;
414 for (unsigned int i=0; i < GetValueCount(); i++)
422 else if( vr == "FL" )
424 float *data=(float *)BinArea;
426 for (unsigned int i=0; i < GetValueCount(); i++)
434 else if( vr == "FD" )
436 double *data=(double *)BinArea;
438 for (unsigned int i=0; i < GetValueCount(); i++)
448 StrArea.append((const char *)BinArea,GetLength());
449 // to avoid gdcm propagate oddities in lengthes
451 StrArea.append(" ",1);
456 * \brief Copies all the attributes from an other DocEntry
457 * @param doc entry to copy from
459 void DataEntry::Copy(DocEntry *doc)
463 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
466 State = entry->State;
468 CopyBinArea(entry->BinArea,entry->GetLength());
472 * \brief Writes the 'value' area of a DataEntry
473 * @param fp already open ofstream pointer
474 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
476 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
478 DocEntry::WriteContent(fp, filetype);
480 if ( GetGroup() == 0xfffe )
482 return; //delimitors have NO value
485 // --> We only deal with Little Endian writting
486 // --> forget Big Endian Transfer Syntax writting!
487 // Next DICOM version will give it up ...
489 // WARNING - For Implicit VR private element,
490 // we have *no choice* but considering them as
491 // something like 'OB' values.
492 // we rewrite them as we found them on disc.
493 // Some trouble will occur if element was
494 // *actually* OW, if image was produced
495 // on Big endian based processor, read and writen
496 // on Little endian based processor
497 // and, later on, somebody needs
498 // this 'OW' Implicit VR private element (?!?)
499 // (Same stuff, mutatis mutandis, for Little/Big)
501 // 8/16 bits Pixels problem should be solved automatiquely,
502 // since we ensure the VR (OB vs OW) is conform to Pixel size.
504 uint8_t *data = BinArea; //safe notation
505 size_t l = GetLength();
506 gdcmDebugMacro ("in DataEntry::WriteContent " << GetKey()
507 << " : " << Global::GetVR()->GetAtomicElementLength(this->GetVR())
509 if (BinArea) // the binArea was *actually* loaded
511 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
512 unsigned short vrLgth =
513 Global::GetVR()->GetAtomicElementLength(this->GetVR());
519 binary_write (*fp, data, l );
524 gdcmDebugMacro ("AtomicLength = 2 found; lgt =" << l);
525 uint16_t *data16 = (uint16_t *)data;
526 for(i=0;i<l/vrLgth;i++)
527 binary_write( *fp, data16[i]);
532 uint32_t *data32 = (uint32_t *)data;
533 for(i=0;i<l/vrLgth;i++)
534 binary_write( *fp, data32[i]);
539 double *data64 = (double *)data;
540 for(i=0;i<l/vrLgth;i++)
541 binary_write( *fp, data64[i]);
546 binary_write (*fp, data, l );
547 #endif //GDCM_WORDS_BIGENDIAN
552 // nothing was loaded, but we need to skip space on disc
554 // --> WARNING : nothing is written;
555 // --> the initial data (on the the source image) is lost
556 // --> user is *not* informed !
558 fp->seekp(l, std::ios::cur);
560 // to avoid gdcm to propagate oddities
561 // (length was already modified)
563 fp->seekp(1, std::ios::cur);
567 * \brief Compute the full length of the elementary DataEntry (not only value
568 * length) depending on the VR.
570 uint32_t DataEntry::ComputeFullLength()
572 return GetFullLength();
575 //-----------------------------------------------------------------------------
577 /// \brief Creates a DataEntry owned BinArea (remove previous one if any)
578 void DataEntry::NewBinArea(void)
581 if( GetLength() > 0 )
582 BinArea = new uint8_t[GetLength()];
585 /// \brief Removes the BinArea, if owned by the DataEntry
586 void DataEntry::DeleteBinArea(void)
588 if (BinArea && SelfArea)
595 //-----------------------------------------------------------------------------
598 //-----------------------------------------------------------------------------
601 * \brief Prints a DataEntry (Dicom entry)
602 * @param os ostream we want to print in
603 * @param indent Indentation string to be prepended during printing
605 void DataEntry::Print(std::ostream &os, std::string const & )
610 uint16_t g = GetGroup();
611 if (g == 0xfffe) // delimiters have NO value
613 return; // just to avoid identing all the remaining code
616 std::ostringstream s;
622 const VRKey &vr = GetVR();
624 if( vr == "US" || vr == "SS" )
625 s << " [" << GetString() << "]";
626 else if( vr == "UL" || vr == "SL" )
627 s << " [" << GetString() << "]";
628 else if ( vr == "FL" )
629 s << " [" << GetString() << "]";
630 else if ( vr == "FD" )
631 s << " [" << GetString() << "]";
634 if(Global::GetVR()->IsVROfStringRepresentable(vr))
636 // replace non printable characters by '.'
637 std::string cleanString = Util::CreateCleanString(v);
638 if ( cleanString.length() <= GetMaxSizePrintEntry()
641 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
642 // FIXME : PrintLevel<2 *does* print the values
643 // (3 is only for extra offsets printing)
644 // What do you wanted to do ? JPR
646 s << " [" << cleanString << "]";
650 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
655 // A lot of Private elements (with no VR) contain actually
656 // only printable characters;
657 // Let's deal with them as is they were VR std::string representable
659 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
661 // FIXME : since the 'Area' *is* clean, just use
662 // a 'CreateString' method, to save CPU time.
663 std::string cleanString =
664 Util::CreateCleanString( BinArea,GetLength() );
665 s << " [" << cleanString << "]";
669 s << " [" << GDCM_BINLOADED << ";"
670 << "length = " << GetLength() << "]";
678 s << " [" << GDCM_NOTLOADED << "]";
679 else if( IsUnfound() )
680 s << " [" << GDCM_UNFOUND << "]";
681 else if( IsUnread() )
682 s << " [" << GDCM_UNREAD << "]";
683 else if ( GetLength() == 0 )
688 s << " (" << GDCM_PIXELDATA << ")";
690 // Display the UID value (instead of displaying only the rough code)
691 // First 'clean' trailing character (space or zero)
694 const uint16_t &gr = GetGroup();
695 const uint16_t &elt = GetElement();
696 TS *ts = Global::GetTS();
700 // Any more to be displayed ?
701 if ( elt == 0x0010 || elt == 0x0002 )
703 if ( v.length() != 0 ) // for brain damaged headers
705 if ( ! isdigit((unsigned char)v[v.length()-1]) )
707 v.erase(v.length()-1, 1);
710 s << " ==>\t[" << ts->GetValue(v) << "]";
713 else if (gr == 0x0008)
715 if ( elt == 0x0016 || elt == 0x1150 )
717 if ( v.length() != 0 ) // for brain damaged headers
719 if ( ! isdigit((unsigned char)v[v.length()-1]) )
721 v.erase(v.length()-1, 1);
724 s << " ==>\t[" << ts->GetValue(v) << "]";
727 else if (gr == 0x0004)
729 if ( elt == 0x1510 || elt == 0x1512 )
731 if ( v.length() != 0 ) // for brain damaged headers
733 if ( ! isdigit((unsigned char)v[v.length()-1]) )
735 v.erase(v.length()-1, 1);
738 s << " ==>\t[" << ts->GetValue(v) << "]";
746 //-----------------------------------------------------------------------------
747 } // end namespace gdcm