1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2006/04/14 08:10:02 $
7 Version: $Revision: 1.37 $
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(uint16_t group,uint16_t elem,
48 : DocEntry(group,elem,vr)
58 * \brief Constructor for a given DocEntry
59 * @param e Pointer to existing Doc entry
61 DataEntry::DataEntry(DocEntry *e)
62 //: DocEntry(e->GetDictEntry())
63 : DocEntry(e->GetGroup(),e->GetElement(), e->GetVR() )
73 * \brief Canonical destructor.
75 DataEntry::~DataEntry ()
80 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
86 * \brief Sets the value (non string) of the current DataEntry
90 void DataEntry::SetBinArea( uint8_t *area, bool self )
100 * \brief Inserts the value (non string) into the current DataEntry
102 * @param length length
104 void DataEntry::CopyBinArea( uint8_t *area, uint32_t length )
108 uint32_t lgh = length + length%2;
111 if( area && length > 0 )
114 memcpy(BinArea,area,length);
118 State = STATE_LOADED;
123 * \brief Inserts the elementary (non string) value into the current DataEntry
124 * @param id index of the elementary value to be set
125 * @param val value, passed as a double
127 void DataEntry::SetValue(const uint32_t &id, const double &val)
131 State = STATE_LOADED;
133 if( id > GetValueCount() )
135 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
139 const VRKey &vr = GetVR();
140 if( vr == "US" || vr == "SS" )
142 uint16_t *data = (uint16_t *)BinArea;
143 data[id] = (uint16_t)val;
145 else if( vr == "UL" || vr == "SL" )
147 uint32_t *data = (uint32_t *)BinArea;
148 data[id] = (uint32_t)val;
150 else if( vr == "FL" )
152 float *data = (float *)BinArea;
153 data[id] = (float)val;
155 else if( vr == "FD" )
157 double *data = (double *)BinArea;
158 data[id] = (double)val;
160 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
162 gdcmErrorMacro("SetValue on String representable not implemented yet");
166 BinArea[id] = (uint8_t)val;
170 * \brief returns, as a double (?!?) one of the values
171 * (when entry is multivaluated), identified by its index.
172 * Returns 0.0 if index is wrong
175 double DataEntry::GetValue(const uint32_t &id) const
179 if (GetLength() != 0) // avoid stupid messages
180 /// \todo warn the user there was a problem !
181 gdcmErrorMacro("BinArea not set " << std::hex
182 << GetGroup() << " " << GetElement()
183 << " Can't get the value");
187 uint32_t count = GetValueCount();
190 gdcmErrorMacro("Index (" << id << ") is greater than the data size");
194 /// \todo FIX the API : user *knows* that entry contains a US
195 /// and he receives a double ?!?
197 const VRKey &vr = GetVR();
198 /// \todo FIX the API : user *knows* that entry contains a US,
199 /// the method is supposed to return a double
200 /// but sends a US ?!?
201 if( vr == "US" || vr == "SS" )
202 return ((uint16_t *)BinArea)[id];
203 else if( vr == "UL" || vr == "SL" )
204 return ((uint32_t *)BinArea)[id];
205 else if( vr == "FL" )
206 return ((float *)BinArea)[id];
207 else if( vr == "FD" )
208 return ((double *)BinArea)[id];
209 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
211 // this is for VR = "DS", ...
214 // Don't use std::string to accelerate processing
216 char *tmp = new char[GetLength()+1];
217 memcpy(tmp,BinArea,GetLength());
228 for(uint32_t i=0;i<GetLength();i++)
258 * \brief Checks if the multiplicity of the value follows Dictionary VM
260 bool DataEntry::IsValueCountValid() /*const*/
263 const std::string &strVM = GetVM();
264 uint32_t vc = GetValueCount();
265 bool valid = vc == 0;
269 // FIXME : what shall we do with VM = "2-n", "3-n", etc
273 // make sure there is at least one ??? FIXME
278 std::istringstream os;
282 // vm respects the one from the dict
283 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
287 // Entries whose type is 1 are mandatory, with a mandatory value.
288 // Entries whose type is 1c are mandatory-inside-a-Sequence,
289 // with a mandatory value.
290 // Entries whose type is 2 are mandatory, with an optional value.
291 // Entries whose type is 2c are mandatory-inside-a-Sequence,
292 // with an optional value.
293 // Entries whose type is 3 are optional.
295 // case vc == 0 is only applicable for 'type 2' entries.
296 // Problem : entry type may depend on the modality and/or the Sequence
297 // it's embedded in !
298 // (Get the information in the 'Conformance Statements' ...)
305 * \brief returns the number of elementary values
307 uint32_t DataEntry::GetValueCount( ) const
309 const VRKey &vr = GetVR();
310 if( vr == "US" || vr == "SS" )
311 return GetLength()/sizeof(uint16_t);
312 else if( vr == "UL" || vr == "SL" )
313 return GetLength()/sizeof(uint32_t);
314 else if( vr == "FL" || vr == "OF" )
315 return GetLength()/4 ; // FL has a *4* length! sizeof(float);
316 else if( vr == "FD" )
317 return GetLength()/8; // FD has a *8* length! sizeof(double);
318 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
320 // Some element in DICOM are allowed to be empty
323 // Don't use std::string to accelerate processing
325 for(uint32_t i=0;i<GetLength();i++)
327 if( BinArea[i] == '\\')
336 * \brief Gets a std::vector <double> holding the value(s) of a DS DataEntry
337 * @param valueVector std::vector <double> of value(s)
338 * \return false if VR not "DS" or DataEntry empty
340 bool DataEntry::GetDSValue(std::vector <double> &valueVector)
342 /// \todo rewrite the whole method, in order *not to use* std::string !
343 std::vector<std::string> tokens;
345 if (GetVR() != "DS") // never trust a user !
348 Util::Tokenize ( GetString().c_str(), tokens, "\\" );
350 int nbValues= tokens.size();
354 for (int loop=0; loop<nbValues; loop++)
355 valueVector.push_back(atof(tokens[loop].c_str()));
361 * \brief Sets the 'value' of a DataEntry, passed as a std::string
362 * @param value string representation of the value to be set
364 void DataEntry::SetString(std::string const &value)
368 const VRKey &vr = GetVR();
369 if ( vr == "US" || vr == "SS" )
371 std::vector<std::string> tokens;
372 Util::Tokenize (value, tokens, "\\");
373 SetLength(tokens.size()*sizeof(uint16_t));
376 uint16_t *data = (uint16_t *)BinArea;
377 for (unsigned int i=0; i<tokens.size();i++)
378 data[i] = atoi(tokens[i].c_str());
381 else if ( vr == "UL" || vr == "SL" )
383 std::vector<std::string> tokens;
384 Util::Tokenize (value, tokens, "\\");
385 SetLength(tokens.size()*sizeof(uint32_t));
388 uint32_t *data = (uint32_t *)BinArea;
389 for (unsigned int i=0; i<tokens.size();i++)
390 data[i] = atoi(tokens[i].c_str());
393 else if ( vr == "FL" )
395 std::vector<std::string> tokens;
396 Util::Tokenize (value, tokens, "\\");
397 SetLength(tokens.size()*sizeof(float));
400 float *data = (float *)BinArea;
401 for (unsigned int i=0; i<tokens.size();i++)
402 data[i] = (float)atof(tokens[i].c_str());
405 else if ( vr == "FD" )
407 std::vector<std::string> tokens;
408 Util::Tokenize (value, tokens, "\\");
409 SetLength(tokens.size()*sizeof(double));
412 double *data = (double *)BinArea;
413 for (unsigned int i=0; i<tokens.size();i++)
414 data[i] = atof(tokens[i].c_str());
419 //if( value.size() > 0 ) // when user sets a string to 0, *do* the job.
421 size_t l = value.size();
424 memcpy(BinArea, value.c_str(), l);
429 State = STATE_LOADED;
432 * \brief returns as a string (when possible) the value of the DataEntry
434 std::string const &DataEntry::GetString() const
436 static std::ostringstream s;
437 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
447 uint16_t *data=(uint16_t *)BinArea;
448 for (unsigned int i=0; i < GetValueCount(); i++)
456 else if (vr == "SS" )
458 int16_t *data=(int16_t *)BinArea;
459 for (unsigned int i=0; i < GetValueCount(); i++)
466 } // See above comment on multiple short integers (mutatis mutandis).
467 else if( vr == "UL" )
469 uint32_t *data=(uint32_t *)BinArea;
470 for (unsigned int i=0; i < GetValueCount(); i++)
478 else if( vr == "SL" )
480 int32_t *data=(int32_t *)BinArea;
481 for (unsigned int i=0; i < GetValueCount(); i++)
488 } else if( vr == "FL" )
490 float *data=(float *)BinArea;
491 for (unsigned int i=0; i < GetValueCount(); i++)
499 else if( vr == "FD" )
501 double *data=(double *)BinArea;
502 for (unsigned int i=0; i < GetValueCount(); i++)
512 StrArea.append((const char *)BinArea,GetLength());
513 // to avoid gdcm to propagate oddities in lengthes
515 StrArea.append(" ",1); }
521 * \brief Copies all the attributes from an other DocEntry
522 * @param doc entry to copy from
523 * @remarks The content BinArea is copied too
525 void DataEntry::Copy(DocEntry *doc)
529 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
532 State = entry->State;
534 CopyBinArea(entry->BinArea,entry->GetLength());
539 * \brief Writes the 'value' area of a DataEntry
540 * @param fp already open ofstream pointer
541 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
543 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
545 DocEntry::WriteContent(fp, filetype);
547 if ( GetGroup() == 0xfffe )
549 return; //delimitors have NO value
552 // --> We only deal with Little Endian writting.
553 // --> forget Big Endian Transfer Syntax writting!
554 // Next DICOM version will give it up ...
556 // WARNING - For Implicit VR private element,
557 // we have *no choice* but considering them as
558 // something like 'OB' values.
559 // we rewrite them as we found them on disc.
560 // Some trouble will occur if element was
561 // *actually* OW, if image was produced
562 // on Big endian based processor, read and writen
563 // on Little endian based processor
564 // and, later on, somebody needs
565 // this 'OW' Implicit VR private element (?!?)
566 // (Same stuff, mutatis mutandis, for Little/Big)
568 // 8/16 bits Pixels problem should be solved automatiquely,
569 // since we ensure the VR (OB vs OW) is conform to Pixel size.
571 uint8_t *data = BinArea; //safe notation
572 size_t l = GetLength();
573 // gdcmDebugMacro("in DataEntry::WriteContent " << GetKey() << " AtomicLength: "
574 // << Global::GetVR()->GetAtomicElementLength(this->GetVR() ) // << " BinArea in :" << &BinArea
576 if (BinArea) // the binArea was *actually* loaded
578 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
579 unsigned short vrLgth =
580 Global::GetVR()->GetAtomicElementLength(this->GetVR());
586 binary_write (*fp, data, l );
591 uint16_t *data16 = (uint16_t *)data;
592 for(i=0;i<l/vrLgth;i++)
593 binary_write( *fp, data16[i]);
598 uint32_t *data32 = (uint32_t *)data;
599 for(i=0;i<l/vrLgth;i++)
600 binary_write( *fp, data32[i]);
605 double *data64 = (double *)data;
606 for(i=0;i<l/vrLgth;i++)
607 binary_write( *fp, data64[i]);
612 binary_write (*fp, data, l );
613 #endif //GDCM_WORDS_BIGENDIAN
618 // nothing was loaded, but we need to skip space on disc
621 // --> WARNING : nothing is written;
622 // --> the initial data (on the the source image) is lost
623 // --> user is *not* informed !
624 gdcmDebugMacro ("Nothing was loaded, but we need to skip space on disc. "
625 << "Length =" << l << " for " << GetKey() );
626 fp->seekp(l, std::ios::cur); // At Write time, for unloaded elems
629 // to avoid gdcm to propagate oddities
630 // (length was already modified)
632 fp->seekp(1, std::ios::cur); // At Write time, for non even length elems
636 * \brief Compute the full length of the elementary DataEntry (not only value
637 * length) depending on the VR.
639 uint32_t DataEntry::ComputeFullLength()
641 return GetFullLength();
644 //-----------------------------------------------------------------------------
646 /// \brief Creates a DataEntry owned BinArea (remove previous one if any)
647 void DataEntry::NewBinArea( )
650 if( GetLength() > 0 )
651 BinArea = new uint8_t[GetLength()];
654 /// \brief Removes the BinArea, if owned by the DataEntry
655 void DataEntry::DeleteBinArea(void)
657 if (BinArea && SelfArea)
664 //-----------------------------------------------------------------------------
667 //-----------------------------------------------------------------------------
670 * \brief Prints a DataEntry (Dicom entry)
671 * @param os ostream we want to print in
672 * @param indent Indentation string to be prepended during printing
674 void DataEntry::Print(std::ostream &os, std::string const & )
679 uint16_t g = GetGroup();
680 if (g == 0xfffe) // delimiters have NO value
682 return; // just to avoid identing all the remaining code
685 std::ostringstream s;
691 const VRKey &vr = GetVR();
693 if( vr == "US" || vr == "SS" || vr == "UL" || vr == "SL"
694 || vr == "FL" || vr == "FD")
695 s << " [" << GetString() << "]";
698 if(Global::GetVR()->IsVROfStringRepresentable(vr))
700 // replace non printable characters by '.'
701 std::string cleanString = Util::CreateCleanString(v);
702 if ( cleanString.length() <= GetMaxSizePrintEntry()
705 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
706 // FIXME : PrintLevel<2 *does* print the values
707 // (3 is only for extra offsets printing)
708 // What do you wanted to do ? JPR
710 s << " [" << cleanString << "]";
714 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
719 // A lot of Private elements (with no VR) contain actually
720 // only printable characters;
721 // Let's deal with them as is they were VR std::string representable
723 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
725 // FIXME : since the 'Area' *is* clean, just use
726 // a 'CreateString' method, to save CPU time.
727 std::string cleanString =
728 Util::CreateCleanString( BinArea,GetLength() );
729 s << " [" << cleanString << "]";
733 s << " [" << GDCM_BINLOADED << ";"
734 << "length = " << GetLength() << "]";
742 s << " [" << GDCM_NOTLOADED << "]";
743 else if( IsUnfound() )
744 s << " [" << GDCM_UNFOUND << "]";
745 else if( IsUnread() )
746 s << " [" << GDCM_UNREAD << "]";
747 else if ( GetLength() == 0 )
752 s << " (" << GDCM_PIXELDATA << ")";
754 // Display the UID value (instead of displaying only the rough code)
755 // First 'clean' trailing character (space or zero)
758 const uint16_t &gr = GetGroup();
759 const uint16_t &elt = GetElement();
760 TS *ts = Global::GetTS();
764 // Any more to be displayed ?
765 if ( elt == 0x0010 || elt == 0x0002 )
767 if ( v.length() != 0 ) // for brain damaged headers
769 if ( ! isdigit((unsigned char)v[v.length()-1]) )
771 v.erase(v.length()-1, 1);
774 s << " ==>\t[" << ts->GetValue(v) << "]";
777 else if (gr == 0x0008)
779 if ( elt == 0x0016 || elt == 0x1150 )
781 if ( v.length() != 0 ) // for brain damaged headers
783 if ( ! isdigit((unsigned char)v[v.length()-1]) )
785 v.erase(v.length()-1, 1);
788 s << " ==>\t[" << ts->GetValue(v) << "]";
791 else if (gr == 0x0004)
793 if ( elt == 0x1510 || elt == 0x1512 )
795 if ( v.length() != 0 ) // for brain damaged headers
797 if ( ! isdigit((unsigned char)v[v.length()-1]) )
799 v.erase(v.length()-1, 1);
802 s << " ==>\t[" << ts->GetValue(v) << "]";
810 //-----------------------------------------------------------------------------
811 } // end namespace gdcm