1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2005/12/09 12:23:38 $
7 Version: $Revision: 1.25 $
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
82 void DataEntry::SetBinArea( uint8_t *area, bool self )
92 * \brief Inserts the value (non string) into the current Dicom Header Entry
94 * @param length length
96 void DataEntry::CopyBinArea( uint8_t *area, uint32_t length )
100 uint32_t lgh = length + length%2;
103 if( area && length > 0 )
106 memcpy(BinArea,area,length);
110 State = STATE_LOADED;
115 * \brief Inserts the value (non string) into the current Dicom Header Entry
119 void DataEntry::SetValue(const uint32_t &id, const double &val)
123 State = STATE_LOADED;
125 if( id > GetValueCount() )
127 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
131 const VRKey &vr = GetVR();
132 if( vr == "US" || vr == "SS" )
134 uint16_t *data = (uint16_t *)BinArea;
135 data[id] = (uint16_t)val;
137 else if( vr == "UL" || vr == "SL" )
139 uint32_t *data = (uint32_t *)BinArea;
140 data[id] = (uint32_t)val;
142 else if( vr == "FL" )
144 float *data = (float *)BinArea;
145 data[id] = (float)val;
147 else if( vr == "FD" )
149 double *data = (double *)BinArea;
150 data[id] = (double)val;
152 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
154 gdcmErrorMacro("SetValue on String representable not implemented yet");
158 BinArea[id] = (uint8_t)val;
162 * \brief returns, as a double (?!?) one of the values
163 * (when entry is multivaluated), identified by its index.
164 * Returns 0.0 if index is wrong
165 * FIXME : warn the user there was a problem !
168 double DataEntry::GetValue(const uint32_t &id) const
172 gdcmErrorMacro("BinArea not set. Can't get the value");
176 uint32_t count = GetValueCount();
179 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
183 // FIX the API : user *knows* that entry contains a US
184 // and he receives a double ?!?
186 const VRKey &vr = GetVR();
187 if( vr == "US" || vr == "SS" )
188 return ((uint16_t *)BinArea)[id];
189 else if( vr == "UL" || vr == "SL" )
190 return ((uint32_t *)BinArea)[id];
191 else if( vr == "FL" )
192 return ((float *)BinArea)[id];
193 else if( vr == "FD" )
194 return ((double *)BinArea)[id];
195 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
199 // Don't use std::string to accelerate processing
201 char *tmp = new char[GetLength()+1];
202 memcpy(tmp,BinArea,GetLength());
213 for(uint32_t i=0;i<GetLength();i++)
243 * \brief Checks if the multiplicity of the value follows Dictionary VM
245 bool DataEntry::IsValueCountValid() const
248 const std::string &strVM = GetVM();
249 uint32_t vc = GetValueCount();
250 bool valid = vc == 0;
254 // FIXME : what shall we do with VM = "2-n", "3-n", etc
258 // make sure there is at least one ??? FIXME
263 std::istringstream os;
267 // vm respects the one from the dict
268 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
272 // Entries whose type is 1 are mandatory, with a mandatory value.
273 // Entries whose type is 1c are mandatory-inside-a-Sequence,
274 // with a mandatory value.
275 // Entries whose type is 2 are mandatory, with an optional value.
276 // Entries whose type is 2c are mandatory-inside-a-Sequence,
277 // with an optional value.
278 // Entries whose type is 3 are optional.
280 // case vc == 0 is only applicable for 'type 2' entries.
281 // Problem : entry type may depend on the modality and/or the Sequence
282 // it's embedded in !
283 // (Get the information in the 'Conformance Statements' ...)
290 * \brief returns the number of elementary values
292 uint32_t DataEntry::GetValueCount( ) const
294 const VRKey &vr = GetVR();
295 if( vr == "US" || vr == "SS" )
296 return GetLength()/sizeof(uint16_t);
297 else if( vr == "UL" || vr == "SL" )
298 return GetLength()/sizeof(uint32_t);
299 else if( vr == "FL" || vr == "OF" )
300 return GetLength()/4 ; // FL has a *4* length! sizeof(float);
301 else if( vr == "FD" )
302 return GetLength()/8; // FD has a *8* length! sizeof(double);
303 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
305 // Some element in DICOM are allowed to be empty
308 // Don't use std::string to accelerate processing
310 for(uint32_t i=0;i<GetLength();i++)
312 if( BinArea[i] == '\\')
320 * \brief Sets the 'value' of an Entry, passed as a std::string
321 * @param value string representation of the value to be set
323 void DataEntry::SetString(std::string const &value)
327 const VRKey &vr = GetVR();
328 if ( vr == "US" || vr == "SS" )
330 std::vector<std::string> tokens;
331 Util::Tokenize (value, tokens, "\\");
332 SetLength(tokens.size()*sizeof(uint16_t));
335 uint16_t *data = (uint16_t *)BinArea;
336 for (unsigned int i=0; i<tokens.size();i++)
337 data[i] = atoi(tokens[i].c_str());
340 else if ( vr == "UL" || vr == "SL" )
342 std::vector<std::string> tokens;
343 Util::Tokenize (value, tokens, "\\");
344 SetLength(tokens.size()*sizeof(uint32_t));
347 uint32_t *data = (uint32_t *)BinArea;
348 for (unsigned int i=0; i<tokens.size();i++)
349 data[i] = atoi(tokens[i].c_str());
352 else if ( vr == "FL" )
354 std::vector<std::string> tokens;
355 Util::Tokenize (value, tokens, "\\");
356 SetLength(tokens.size()*sizeof(float));
359 float *data = (float *)BinArea;
360 for (unsigned int i=0; i<tokens.size();i++)
361 data[i] = (float)atof(tokens[i].c_str());
364 else if ( vr == "FD" )
366 std::vector<std::string> tokens;
367 Util::Tokenize (value, tokens, "\\");
368 SetLength(tokens.size()*sizeof(double));
371 double *data = (double *)BinArea;
372 for (unsigned int i=0; i<tokens.size();i++)
373 data[i] = atof(tokens[i].c_str());
378 if( value.size() > 0 )
380 size_t l = value.size();
383 memcpy(BinArea, value.c_str(), l);
388 State = STATE_LOADED;
391 * \brief returns as a string (when possible) the value of the DataEntry
393 std::string const &DataEntry::GetString() const
395 static std::ostringstream s;
396 const VRKey &vr = GetVR();
404 // When short integer(s) are stored, convert the following (n * 2) characters
405 // as a displayable string, the values being separated by a back-slash
407 if( vr == "US" || vr == "SS" )
409 uint16_t *data=(uint16_t *)BinArea;
411 for (unsigned int i=0; i < GetValueCount(); i++)
419 // See above comment on multiple short integers (mutatis mutandis).
420 else if( vr == "UL" || vr == "SL" )
422 uint32_t *data=(uint32_t *)BinArea;
424 for (unsigned int i=0; i < GetValueCount(); i++)
432 else if( vr == "FL" )
434 float *data=(float *)BinArea;
436 for (unsigned int i=0; i < GetValueCount(); i++)
444 else if( vr == "FD" )
446 double *data=(double *)BinArea;
448 for (unsigned int i=0; i < GetValueCount(); i++)
458 StrArea.append((const char *)BinArea,GetLength());
459 // to avoid gdcm propagate oddities in lengthes
461 StrArea.append(" ",1);
467 * \brief Copies all the attributes from an other DocEntry
468 * @param doc entry to copy from
469 * @remarks The content BinArea is copied too
471 void DataEntry::Copy(DocEntry *doc)
475 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
478 State = entry->State;
480 CopyBinArea(entry->BinArea,entry->GetLength());
485 * \brief Writes the 'value' area of a DataEntry
486 * @param fp already open ofstream pointer
487 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
489 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
491 DocEntry::WriteContent(fp, filetype);
493 if ( GetGroup() == 0xfffe )
495 return; //delimitors have NO value
498 // --> We only deal with Little Endian writting
499 // --> forget Big Endian Transfer Syntax writting!
500 // Next DICOM version will give it up ...
502 // WARNING - For Implicit VR private element,
503 // we have *no choice* but considering them as
504 // something like 'OB' values.
505 // we rewrite them as we found them on disc.
506 // Some trouble will occur if element was
507 // *actually* OW, if image was produced
508 // on Big endian based processor, read and writen
509 // on Little endian based processor
510 // and, later on, somebody needs
511 // this 'OW' Implicit VR private element (?!?)
512 // (Same stuff, mutatis mutandis, for Little/Big)
514 // 8/16 bits Pixels problem should be solved automatiquely,
515 // since we ensure the VR (OB vs OW) is conform to Pixel size.
517 uint8_t *data = BinArea; //safe notation
518 size_t l = GetLength();
519 gdcmDebugMacro ("in DataEntry::WriteContent " << GetKey()
520 << " : " << Global::GetVR()->GetAtomicElementLength(this->GetVR())
522 if (BinArea) // the binArea was *actually* loaded
524 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
525 unsigned short vrLgth =
526 Global::GetVR()->GetAtomicElementLength(this->GetVR());
532 binary_write (*fp, data, l );
537 gdcmDebugMacro ("AtomicLength = 2 found; lgt =" << l);
538 uint16_t *data16 = (uint16_t *)data;
539 for(i=0;i<l/vrLgth;i++)
540 binary_write( *fp, data16[i]);
545 uint32_t *data32 = (uint32_t *)data;
546 for(i=0;i<l/vrLgth;i++)
547 binary_write( *fp, data32[i]);
552 double *data64 = (double *)data;
553 for(i=0;i<l/vrLgth;i++)
554 binary_write( *fp, data64[i]);
559 binary_write (*fp, data, l );
560 #endif //GDCM_WORDS_BIGENDIAN
565 // nothing was loaded, but we need to skip space on disc
567 // --> WARNING : nothing is written;
568 // --> the initial data (on the the source image) is lost
569 // --> user is *not* informed !
571 fp->seekp(l, std::ios::cur);
573 // to avoid gdcm to propagate oddities
574 // (length was already modified)
576 fp->seekp(1, std::ios::cur);
580 * \brief Compute the full length of the elementary DataEntry (not only value
581 * length) depending on the VR.
583 uint32_t DataEntry::ComputeFullLength()
585 return GetFullLength();
588 //-----------------------------------------------------------------------------
590 /// \brief Creates a DataEntry owned BinArea (remove previous one if any)
591 void DataEntry::NewBinArea( )
594 if( GetLength() > 0 )
595 BinArea = new uint8_t[GetLength()];
598 /// \brief Removes the BinArea, if owned by the DataEntry
599 void DataEntry::DeleteBinArea(void)
601 if (BinArea && SelfArea)
608 //-----------------------------------------------------------------------------
611 //-----------------------------------------------------------------------------
614 * \brief Prints a DataEntry (Dicom entry)
615 * @param os ostream we want to print in
616 * @param indent Indentation string to be prepended during printing
618 void DataEntry::Print(std::ostream &os, std::string const & )
623 uint16_t g = GetGroup();
624 if (g == 0xfffe) // delimiters have NO value
626 return; // just to avoid identing all the remaining code
629 std::ostringstream s;
635 const VRKey &vr = GetVR();
637 if( vr == "US" || vr == "SS" )
638 s << " [" << GetString() << "]";
639 else if( vr == "UL" || vr == "SL" )
640 s << " [" << GetString() << "]";
641 else if ( vr == "FL" )
642 s << " [" << GetString() << "]";
643 else if ( vr == "FD" )
644 s << " [" << GetString() << "]";
647 if(Global::GetVR()->IsVROfStringRepresentable(vr))
649 // replace non printable characters by '.'
650 std::string cleanString = Util::CreateCleanString(v);
651 if ( cleanString.length() <= GetMaxSizePrintEntry()
654 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
655 // FIXME : PrintLevel<2 *does* print the values
656 // (3 is only for extra offsets printing)
657 // What do you wanted to do ? JPR
659 s << " [" << cleanString << "]";
663 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
668 // A lot of Private elements (with no VR) contain actually
669 // only printable characters;
670 // Let's deal with them as is they were VR std::string representable
672 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
674 // FIXME : since the 'Area' *is* clean, just use
675 // a 'CreateString' method, to save CPU time.
676 std::string cleanString =
677 Util::CreateCleanString( BinArea,GetLength() );
678 s << " [" << cleanString << "]";
682 s << " [" << GDCM_BINLOADED << ";"
683 << "length = " << GetLength() << "]";
691 s << " [" << GDCM_NOTLOADED << "]";
692 else if( IsUnfound() )
693 s << " [" << GDCM_UNFOUND << "]";
694 else if( IsUnread() )
695 s << " [" << GDCM_UNREAD << "]";
696 else if ( GetLength() == 0 )
701 s << " (" << GDCM_PIXELDATA << ")";
703 // Display the UID value (instead of displaying only the rough code)
704 // First 'clean' trailing character (space or zero)
707 const uint16_t &gr = GetGroup();
708 const uint16_t &elt = GetElement();
709 TS *ts = Global::GetTS();
713 // Any more to be displayed ?
714 if ( elt == 0x0010 || elt == 0x0002 )
716 if ( v.length() != 0 ) // for brain damaged headers
718 if ( ! isdigit((unsigned char)v[v.length()-1]) )
720 v.erase(v.length()-1, 1);
723 s << " ==>\t[" << ts->GetValue(v) << "]";
726 else if (gr == 0x0008)
728 if ( elt == 0x0016 || elt == 0x1150 )
730 if ( v.length() != 0 ) // for brain damaged headers
732 if ( ! isdigit((unsigned char)v[v.length()-1]) )
734 v.erase(v.length()-1, 1);
737 s << " ==>\t[" << ts->GetValue(v) << "]";
740 else if (gr == 0x0004)
742 if ( elt == 0x1510 || elt == 0x1512 )
744 if ( v.length() != 0 ) // for brain damaged headers
746 if ( ! isdigit((unsigned char)v[v.length()-1]) )
748 v.erase(v.length()-1, 1);
751 s << " ==>\t[" << ts->GetValue(v) << "]";
759 //-----------------------------------------------------------------------------
760 } // end namespace gdcm