1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2006/03/31 07:52:23 $
7 Version: $Revision: 1.35 $
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"
28 #if defined(__BORLANDC__)
29 #include <mem.h> // for memcpy
30 #include <stdlib.h> // for atof
31 #include <ctype.h> // for isdigit
36 //-----------------------------------------------------------------------------
37 #define MAX_SIZE_PRINT_ELEMENT_VALUE 0x7fffffff
38 uint32_t DataEntry::MaxSizePrintEntry = MAX_SIZE_PRINT_ELEMENT_VALUE;
40 //-----------------------------------------------------------------------------
41 // Constructor / Destructor
43 * \brief Constructor for a given DictEntry
44 * @param e Pointer to existing dictionary entry
46 DataEntry::DataEntry(DictEntry *e)
57 * \brief Constructor for a given DocEntry
58 * @param e Pointer to existing Doc entry
60 DataEntry::DataEntry(DocEntry *e)
61 : DocEntry(e->GetDictEntry())
71 * \brief Canonical destructor.
73 DataEntry::~DataEntry ()
78 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
84 * \brief Sets the value (non string) of the current DataEntry
88 void DataEntry::SetBinArea( uint8_t *area, bool self )
98 * \brief Inserts the value (non string) into the current DataEntry
100 * @param length length
102 void DataEntry::CopyBinArea( uint8_t *area, uint32_t length )
106 uint32_t lgh = length + length%2;
109 if( area && length > 0 )
112 memcpy(BinArea,area,length);
116 State = STATE_LOADED;
121 * \brief Inserts the elementary (non string) value into the current DataEntry
122 * @param id index of the elementary value to be set
123 * @param val value, passed as a double
125 void DataEntry::SetValue(const uint32_t &id, const double &val)
129 State = STATE_LOADED;
131 if( id > GetValueCount() )
133 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
137 const VRKey &vr = GetVR();
138 if( vr == "US" || vr == "SS" )
140 uint16_t *data = (uint16_t *)BinArea;
141 data[id] = (uint16_t)val;
143 else if( vr == "UL" || vr == "SL" )
145 uint32_t *data = (uint32_t *)BinArea;
146 data[id] = (uint32_t)val;
148 else if( vr == "FL" )
150 float *data = (float *)BinArea;
151 data[id] = (float)val;
153 else if( vr == "FD" )
155 double *data = (double *)BinArea;
156 data[id] = (double)val;
158 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
160 gdcmErrorMacro("SetValue on String representable not implemented yet");
164 BinArea[id] = (uint8_t)val;
168 * \brief returns, as a double (?!?) one of the values
169 * (when entry is multivaluated), identified by its index.
170 * Returns 0.0 if index is wrong
173 double DataEntry::GetValue(const uint32_t &id) const
177 if (GetLength() != 0) // avoid stupid messages
178 /// \todo warn the user there was a problem !
179 gdcmErrorMacro("BinArea not set " << std::hex
180 << GetGroup() << " " << GetElement()
181 << " Can't get the value");
185 uint32_t count = GetValueCount();
188 gdcmErrorMacro("Index (" << id << ") is greater than the data size");
192 /// \todo FIX the API : user *knows* that entry contains a US
193 /// and he receives a double ?!?
195 const VRKey &vr = GetVR();
196 /// \todo FIX the API : user *knows* that entry contains a US,
197 /// the method is supposed to return a double
198 /// but sends a US ?!?
199 if( vr == "US" || vr == "SS" )
200 return ((uint16_t *)BinArea)[id];
201 else if( vr == "UL" || vr == "SL" )
202 return ((uint32_t *)BinArea)[id];
203 else if( vr == "FL" )
204 return ((float *)BinArea)[id];
205 else if( vr == "FD" )
206 return ((double *)BinArea)[id];
207 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
209 // this is for VR = "DS", ...
212 // Don't use std::string to accelerate processing
214 char *tmp = new char[GetLength()+1];
215 memcpy(tmp,BinArea,GetLength());
226 for(uint32_t i=0;i<GetLength();i++)
256 * \brief Checks if the multiplicity of the value follows Dictionary VM
258 bool DataEntry::IsValueCountValid() const
261 const std::string &strVM = GetVM();
262 uint32_t vc = GetValueCount();
263 bool valid = vc == 0;
267 // FIXME : what shall we do with VM = "2-n", "3-n", etc
271 // make sure there is at least one ??? FIXME
276 std::istringstream os;
280 // vm respects the one from the dict
281 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
285 // Entries whose type is 1 are mandatory, with a mandatory value.
286 // Entries whose type is 1c are mandatory-inside-a-Sequence,
287 // with a mandatory value.
288 // Entries whose type is 2 are mandatory, with an optional value.
289 // Entries whose type is 2c are mandatory-inside-a-Sequence,
290 // with an optional value.
291 // Entries whose type is 3 are optional.
293 // case vc == 0 is only applicable for 'type 2' entries.
294 // Problem : entry type may depend on the modality and/or the Sequence
295 // it's embedded in !
296 // (Get the information in the 'Conformance Statements' ...)
303 * \brief returns the number of elementary values
305 uint32_t DataEntry::GetValueCount( ) const
307 const VRKey &vr = GetVR();
308 if( vr == "US" || vr == "SS" )
309 return GetLength()/sizeof(uint16_t);
310 else if( vr == "UL" || vr == "SL" )
311 return GetLength()/sizeof(uint32_t);
312 else if( vr == "FL" || vr == "OF" )
313 return GetLength()/4 ; // FL has a *4* length! sizeof(float);
314 else if( vr == "FD" )
315 return GetLength()/8; // FD has a *8* length! sizeof(double);
316 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
318 // Some element in DICOM are allowed to be empty
321 // Don't use std::string to accelerate processing
323 for(uint32_t i=0;i<GetLength();i++)
325 if( BinArea[i] == '\\')
334 * \brief Gets a std::vector <double> holding the value(s) of a DS DataEntry
335 * @param valueVector std::vector <double> of value(s)
336 * \return false if VR not "DS" or DataEntry empty
338 bool DataEntry::GetDSValue(std::vector <double> &valueVector)
340 /// \todo rewrite the whole method, in order *not to use* std::string !
341 std::vector<std::string> tokens;
343 if (GetVR() != "DS") // never trust a user !
346 Util::Tokenize ( GetString().c_str(), tokens, "\\" );
348 int nbValues= tokens.size();
352 for (int loop=0; loop<nbValues; loop++)
353 valueVector.push_back(atof(tokens[loop].c_str()));
359 * \brief Sets the 'value' of a DataEntry, passed as a std::string
360 * @param value string representation of the value to be set
362 void DataEntry::SetString(std::string const &value)
366 const VRKey &vr = GetVR();
367 if ( vr == "US" || vr == "SS" )
369 std::vector<std::string> tokens;
370 Util::Tokenize (value, tokens, "\\");
371 SetLength(tokens.size()*sizeof(uint16_t));
374 uint16_t *data = (uint16_t *)BinArea;
375 for (unsigned int i=0; i<tokens.size();i++)
376 data[i] = atoi(tokens[i].c_str());
379 else if ( vr == "UL" || vr == "SL" )
381 std::vector<std::string> tokens;
382 Util::Tokenize (value, tokens, "\\");
383 SetLength(tokens.size()*sizeof(uint32_t));
386 uint32_t *data = (uint32_t *)BinArea;
387 for (unsigned int i=0; i<tokens.size();i++)
388 data[i] = atoi(tokens[i].c_str());
391 else if ( vr == "FL" )
393 std::vector<std::string> tokens;
394 Util::Tokenize (value, tokens, "\\");
395 SetLength(tokens.size()*sizeof(float));
398 float *data = (float *)BinArea;
399 for (unsigned int i=0; i<tokens.size();i++)
400 data[i] = (float)atof(tokens[i].c_str());
403 else if ( vr == "FD" )
405 std::vector<std::string> tokens;
406 Util::Tokenize (value, tokens, "\\");
407 SetLength(tokens.size()*sizeof(double));
410 double *data = (double *)BinArea;
411 for (unsigned int i=0; i<tokens.size();i++)
412 data[i] = atof(tokens[i].c_str());
417 if( value.size() > 0 )
419 size_t l = value.size();
422 memcpy(BinArea, value.c_str(), l);
427 State = STATE_LOADED;
430 * \brief returns as a string (when possible) the value of the DataEntry
432 std::string const &DataEntry::GetString() const
434 static std::ostringstream s;
435 const VRKey &vr = GetVR();
443 // When short integer(s) are stored, convert the following (n * 2) characters
444 // as a displayable string, the values being separated by a back-slash
446 if( vr == "US" || vr == "SS" )
448 uint16_t *data=(uint16_t *)BinArea;
450 for (unsigned int i=0; i < GetValueCount(); i++)
458 // See above comment on multiple short integers (mutatis mutandis).
459 else if( vr == "UL" || vr == "SL" )
461 uint32_t *data=(uint32_t *)BinArea;
463 for (unsigned int i=0; i < GetValueCount(); i++)
471 else if( vr == "FL" )
473 float *data=(float *)BinArea;
475 for (unsigned int i=0; i < GetValueCount(); i++)
483 else if( vr == "FD" )
485 double *data=(double *)BinArea;
487 for (unsigned int i=0; i < GetValueCount(); i++)
497 StrArea.append((const char *)BinArea,GetLength());
498 // to avoid gdcm propagate oddities in lengthes
500 StrArea.append(" ",1);
506 * \brief Copies all the attributes from an other DocEntry
507 * @param doc entry to copy from
508 * @remarks The content BinArea is copied too
510 void DataEntry::Copy(DocEntry *doc)
514 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
517 State = entry->State;
519 CopyBinArea(entry->BinArea,entry->GetLength());
524 * \brief Writes the 'value' area of a DataEntry
525 * @param fp already open ofstream pointer
526 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
528 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
530 DocEntry::WriteContent(fp, filetype);
532 if ( GetGroup() == 0xfffe )
534 return; //delimitors have NO value
537 // --> We only deal with Little Endian writting.
538 // --> forget Big Endian Transfer Syntax writting!
539 // Next DICOM version will give it up ...
541 // WARNING - For Implicit VR private element,
542 // we have *no choice* but considering them as
543 // something like 'OB' values.
544 // we rewrite them as we found them on disc.
545 // Some trouble will occur if element was
546 // *actually* OW, if image was produced
547 // on Big endian based processor, read and writen
548 // on Little endian based processor
549 // and, later on, somebody needs
550 // this 'OW' Implicit VR private element (?!?)
551 // (Same stuff, mutatis mutandis, for Little/Big)
553 // 8/16 bits Pixels problem should be solved automatiquely,
554 // since we ensure the VR (OB vs OW) is conform to Pixel size.
556 uint8_t *data = BinArea; //safe notation
557 size_t l = GetLength();
558 gdcmDebugMacro("in DataEntry::WriteContent " << GetKey() << " AtomicLength: "
559 << Global::GetVR()->GetAtomicElementLength(this->GetVR() ) // << " BinArea in :" << &BinArea
561 if (BinArea) // the binArea was *actually* loaded
563 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
564 unsigned short vrLgth =
565 Global::GetVR()->GetAtomicElementLength(this->GetVR());
571 binary_write (*fp, data, l );
576 uint16_t *data16 = (uint16_t *)data;
577 for(i=0;i<l/vrLgth;i++)
578 binary_write( *fp, data16[i]);
583 uint32_t *data32 = (uint32_t *)data;
584 for(i=0;i<l/vrLgth;i++)
585 binary_write( *fp, data32[i]);
590 double *data64 = (double *)data;
591 for(i=0;i<l/vrLgth;i++)
592 binary_write( *fp, data64[i]);
597 binary_write (*fp, data, l );
598 #endif //GDCM_WORDS_BIGENDIAN
603 // nothing was loaded, but we need to skip space on disc
606 // --> WARNING : nothing is written;
607 // --> the initial data (on the the source image) is lost
608 // --> user is *not* informed !
609 gdcmDebugMacro ("Nothing was loaded, but we need to skip space on disc. "
610 << "Length =" << l << " for " << GetKey() );
611 fp->seekp(l, std::ios::cur); // At Write time, for unloaded elems
614 // to avoid gdcm to propagate oddities
615 // (length was already modified)
617 fp->seekp(1, std::ios::cur); // At Write time, for non even length elems
621 * \brief Compute the full length of the elementary DataEntry (not only value
622 * length) depending on the VR.
624 uint32_t DataEntry::ComputeFullLength()
626 return GetFullLength();
629 //-----------------------------------------------------------------------------
631 /// \brief Creates a DataEntry owned BinArea (remove previous one if any)
632 void DataEntry::NewBinArea( )
635 if( GetLength() > 0 )
636 BinArea = new uint8_t[GetLength()];
639 /// \brief Removes the BinArea, if owned by the DataEntry
640 void DataEntry::DeleteBinArea(void)
642 if (BinArea && SelfArea)
649 //-----------------------------------------------------------------------------
652 //-----------------------------------------------------------------------------
655 * \brief Prints a DataEntry (Dicom entry)
656 * @param os ostream we want to print in
657 * @param indent Indentation string to be prepended during printing
659 void DataEntry::Print(std::ostream &os, std::string const & )
664 uint16_t g = GetGroup();
665 if (g == 0xfffe) // delimiters have NO value
667 return; // just to avoid identing all the remaining code
670 std::ostringstream s;
676 const VRKey &vr = GetVR();
678 if( vr == "US" || vr == "SS" || vr == "UL" || vr == "SL"
679 || vr == "FL" || vr == "FD")
680 s << " [" << GetString() << "]";
683 if(Global::GetVR()->IsVROfStringRepresentable(vr))
685 // replace non printable characters by '.'
686 std::string cleanString = Util::CreateCleanString(v);
687 if ( cleanString.length() <= GetMaxSizePrintEntry()
690 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
691 // FIXME : PrintLevel<2 *does* print the values
692 // (3 is only for extra offsets printing)
693 // What do you wanted to do ? JPR
695 s << " [" << cleanString << "]";
699 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
704 // A lot of Private elements (with no VR) contain actually
705 // only printable characters;
706 // Let's deal with them as is they were VR std::string representable
708 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
710 // FIXME : since the 'Area' *is* clean, just use
711 // a 'CreateString' method, to save CPU time.
712 std::string cleanString =
713 Util::CreateCleanString( BinArea,GetLength() );
714 s << " [" << cleanString << "]";
718 s << " [" << GDCM_BINLOADED << ";"
719 << "length = " << GetLength() << "]";
727 s << " [" << GDCM_NOTLOADED << "]";
728 else if( IsUnfound() )
729 s << " [" << GDCM_UNFOUND << "]";
730 else if( IsUnread() )
731 s << " [" << GDCM_UNREAD << "]";
732 else if ( GetLength() == 0 )
737 s << " (" << GDCM_PIXELDATA << ")";
739 // Display the UID value (instead of displaying only the rough code)
740 // First 'clean' trailing character (space or zero)
743 const uint16_t &gr = GetGroup();
744 const uint16_t &elt = GetElement();
745 TS *ts = Global::GetTS();
749 // Any more to be displayed ?
750 if ( elt == 0x0010 || elt == 0x0002 )
752 if ( v.length() != 0 ) // for brain damaged headers
754 if ( ! isdigit((unsigned char)v[v.length()-1]) )
756 v.erase(v.length()-1, 1);
759 s << " ==>\t[" << ts->GetValue(v) << "]";
762 else if (gr == 0x0008)
764 if ( elt == 0x0016 || elt == 0x1150 )
766 if ( v.length() != 0 ) // for brain damaged headers
768 if ( ! isdigit((unsigned char)v[v.length()-1]) )
770 v.erase(v.length()-1, 1);
773 s << " ==>\t[" << ts->GetValue(v) << "]";
776 else if (gr == 0x0004)
778 if ( elt == 0x1510 || elt == 0x1512 )
780 if ( v.length() != 0 ) // for brain damaged headers
782 if ( ! isdigit((unsigned char)v[v.length()-1]) )
784 v.erase(v.length()-1, 1);
787 s << " ==>\t[" << ts->GetValue(v) << "]";
795 //-----------------------------------------------------------------------------
796 } // end namespace gdcm