1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2006/06/29 13:26:08 $
7 Version: $Revision: 1.39 $
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 DataEntry
44 * @param group group number of the Data Entry to be created
45 * @param elem element number of the Data Entry to be created
46 * @param vr Value Representation of the Data Entry to be created
48 DataEntry::DataEntry(uint16_t group,uint16_t elem,
50 : DocEntry(group,elem,vr)
60 * \brief Constructor for a given DocEntry
61 * @param e Pointer to existing Doc entry
63 DataEntry::DataEntry(DocEntry *e)
64 //: DocEntry(e->GetDictEntry())
65 : DocEntry(e->GetGroup(),e->GetElement(), e->GetVR() )
75 * \brief Canonical destructor.
77 DataEntry::~DataEntry ()
82 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
88 * \brief Sets the value (non string) of the current DataEntry
92 void DataEntry::SetBinArea( uint8_t *area, bool self )
102 * \brief Inserts the value (non string) into the current DataEntry
104 * @param length length
106 void DataEntry::CopyBinArea( uint8_t *area, uint32_t length )
110 uint32_t lgh = length + length%2;
113 if( area && length > 0 )
116 memcpy(BinArea,area,length);
120 State = STATE_LOADED;
125 * \brief Inserts the elementary (non string) value into the current DataEntry
126 * @param id index of the elementary value to be set
127 * @param val value, passed as a double
129 void DataEntry::SetValue(const uint32_t &id, const double &val)
133 State = STATE_LOADED;
135 if( id > GetValueCount() )
137 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
141 const VRKey &vr = GetVR();
142 if( vr == "US" || vr == "SS" )
144 uint16_t *data = (uint16_t *)BinArea;
145 data[id] = (uint16_t)val;
147 else if( vr == "UL" || vr == "SL" )
149 uint32_t *data = (uint32_t *)BinArea;
150 data[id] = (uint32_t)val;
152 else if( vr == "FL" )
154 float *data = (float *)BinArea;
155 data[id] = (float)val;
157 else if( vr == "FD" )
159 double *data = (double *)BinArea;
160 data[id] = (double)val;
162 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
164 gdcmErrorMacro("SetValue on String representable not implemented yet");
168 BinArea[id] = (uint8_t)val;
172 * \brief returns, as a double (?!?) one of the values
173 * (when entry is multivaluated), identified by its index.
174 * Returns 0.0 if index is wrong
177 double DataEntry::GetValue(const uint32_t &id) const
181 if (GetLength() != 0) // avoid stupid messages
182 /// \todo warn the user there was a problem !
183 gdcmErrorMacro("BinArea not set " << std::hex
184 << GetGroup() << " " << GetElement()
185 << " Can't get the value");
189 uint32_t count = GetValueCount();
192 gdcmErrorMacro("Index (" << id << ") is greater than the data size");
196 /// \todo FIX the API : user *knows* that entry contains a US
197 /// and he receives a double ?!?
199 const VRKey &vr = GetVR();
200 /// \todo FIX the API : user *knows* that entry contains a US,
201 /// the method is supposed to return a double
202 /// but sends a US ?!?
203 if( vr == "US" || vr == "SS" )
204 return ((uint16_t *)BinArea)[id];
205 else if( vr == "UL" || vr == "SL" )
206 return ((uint32_t *)BinArea)[id];
207 else if( vr == "FL" )
208 return ((float *)BinArea)[id];
209 else if( vr == "FD" )
210 return ((double *)BinArea)[id];
211 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
213 // this is for VR = "DS", ...
216 // Don't use std::string to accelerate processing
218 char *tmp = new char[GetLength()+1];
219 memcpy(tmp,BinArea,GetLength());
230 for(uint32_t i=0;i<GetLength();i++)
260 * \brief Checks if the multiplicity of the value follows Dictionary VM
262 bool DataEntry::IsValueCountValid() /*const*/
265 const std::string &strVM = GetVM();
266 uint32_t vc = GetValueCount();
267 bool valid = vc == 0;
271 // FIXME : what shall we do with VM = "2-n", "3-n", etc
275 // make sure there is at least one ??? FIXME
280 std::istringstream os;
284 // vm respects the one from the dict
285 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
289 // Entries whose type is 1 are mandatory, with a mandatory value.
290 // Entries whose type is 1c are mandatory-inside-a-Sequence,
291 // with a mandatory value.
292 // Entries whose type is 2 are mandatory, with an optional value.
293 // Entries whose type is 2c are mandatory-inside-a-Sequence,
294 // with an optional value.
295 // Entries whose type is 3 are optional.
297 // case vc == 0 is only applicable for 'type 2' entries.
298 // Problem : entry type may depend on the modality and/or the Sequence
299 // it's embedded in !
300 // (Get the information in the 'Conformance Statements' ...)
307 * \brief returns the number of elementary values
309 uint32_t DataEntry::GetValueCount( ) const
311 const VRKey &vr = GetVR();
312 if( vr == "US" || vr == "SS" )
313 return GetLength()/sizeof(uint16_t);
314 else if( vr == "UL" || vr == "SL" )
315 return GetLength()/sizeof(uint32_t);
316 else if( vr == "FL" || vr == "OF" )
317 return GetLength()/4 ; // FL has a *4* length! sizeof(float);
318 else if( vr == "FD" )
319 return GetLength()/8; // FD has a *8* length! sizeof(double);
320 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
322 // Some element in DICOM are allowed to be empty
325 // Don't use std::string to accelerate processing
327 for(uint32_t i=0;i<GetLength();i++)
329 if( BinArea[i] == '\\')
338 * \brief Gets a std::vector <double> holding the value(s) of a DS DataEntry
339 * @param valueVector std::vector double of value(s)
340 * \return false if VR not "DS" or DataEntry empty
342 bool DataEntry::GetDSValue(std::vector <double> &valueVector)
344 /// \todo rewrite the whole method, in order *not to use* std::string !
345 std::vector<std::string> tokens;
347 if (GetVR() != "DS") // never trust a user !
350 Util::Tokenize ( GetString().c_str(), tokens, "\\" );
352 int nbValues= tokens.size();
356 for (int loop=0; loop<nbValues; loop++)
357 valueVector.push_back(atof(tokens[loop].c_str()));
363 * \brief Sets the 'value' of a DataEntry, passed as a std::string
364 * @param value string representation of the value to be set
366 void DataEntry::SetString(std::string const &value)
370 const VRKey &vr = GetVR();
371 if ( vr == "US" || vr == "SS" )
373 std::vector<std::string> tokens;
374 Util::Tokenize (value, tokens, "\\");
375 SetLength(tokens.size()*sizeof(uint16_t));
378 uint16_t *data = (uint16_t *)BinArea;
379 for (unsigned int i=0; i<tokens.size();i++)
380 data[i] = atoi(tokens[i].c_str());
383 else if ( vr == "UL" || vr == "SL" )
385 std::vector<std::string> tokens;
386 Util::Tokenize (value, tokens, "\\");
387 SetLength(tokens.size()*sizeof(uint32_t));
390 uint32_t *data = (uint32_t *)BinArea;
391 for (unsigned int i=0; i<tokens.size();i++)
392 data[i] = atoi(tokens[i].c_str());
395 else if ( vr == "FL" )
397 std::vector<std::string> tokens;
398 Util::Tokenize (value, tokens, "\\");
399 SetLength(tokens.size()*sizeof(float));
402 float *data = (float *)BinArea;
403 for (unsigned int i=0; i<tokens.size();i++)
404 data[i] = (float)atof(tokens[i].c_str());
407 else if ( vr == "FD" )
409 std::vector<std::string> tokens;
410 Util::Tokenize (value, tokens, "\\");
411 SetLength(tokens.size()*sizeof(double));
414 double *data = (double *)BinArea;
415 for (unsigned int i=0; i<tokens.size();i++)
416 data[i] = atof(tokens[i].c_str());
421 //if( value.size() > 0 ) // when user sets a string to 0, *do* the job.
423 size_t l = value.size();
426 memcpy(BinArea, value.c_str(), l);
431 State = STATE_LOADED;
434 * \brief returns as a string (when possible) the value of the DataEntry
436 std::string const &DataEntry::GetString() const
438 static std::ostringstream s;
439 const VRKey &vr = GetVR();
445 // When short integer(s) are stored, convert the following (n * 2) characters
446 // as a displayable string, the values being separated by a back-slash
449 uint16_t *data=(uint16_t *)BinArea;
450 for (unsigned int i=0; i < GetValueCount(); i++)
458 else if (vr == "SS" )
460 int16_t *data=(int16_t *)BinArea;
461 for (unsigned int i=0; i < GetValueCount(); i++)
468 } // See above comment on multiple short integers (mutatis mutandis).
469 else if( vr == "UL" )
471 uint32_t *data=(uint32_t *)BinArea;
472 for (unsigned int i=0; i < GetValueCount(); i++)
480 else if( vr == "SL" )
482 int32_t *data=(int32_t *)BinArea;
483 for (unsigned int i=0; i < GetValueCount(); i++)
490 } else if( vr == "FL" )
492 float *data=(float *)BinArea;
493 for (unsigned int i=0; i < GetValueCount(); i++)
501 else if( vr == "FD" )
503 double *data=(double *)BinArea;
504 for (unsigned int i=0; i < GetValueCount(); i++)
514 StrArea.append((const char *)BinArea,GetLength());
515 // to avoid gdcm to propagate oddities in lengthes
517 StrArea.append(" ",1); }
523 * \brief Copies all the attributes from an other DocEntry
524 * @param doc entry to copy from
525 * @remarks The content BinArea is copied too
527 void DataEntry::Copy(DocEntry *doc)
531 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
534 State = entry->State;
536 CopyBinArea(entry->BinArea,entry->GetLength());
541 * \brief Writes the 'value' area of a DataEntry
542 * @param fp already open ofstream pointer
543 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
545 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
547 DocEntry::WriteContent(fp, filetype);
549 if ( GetGroup() == 0xfffe )
551 return; //delimitors have NO value
554 // --> We only deal with Little Endian writting.
555 // --> forget Big Endian Transfer Syntax writting!
556 // Next DICOM version will give it up ...
558 // WARNING - For Implicit VR private element,
559 // we have *no choice* but considering them as
560 // something like 'OB' values.
561 // we rewrite them as we found them on disc.
562 // Some trouble will occur if element was
563 // *actually* OW, if image was produced
564 // on Big endian based processor, read and writen
565 // on Little endian based processor
566 // and, later on, somebody needs
567 // this 'OW' Implicit VR private element (?!?)
568 // (Same stuff, mutatis mutandis, for Little/Big)
570 // 8/16 bits Pixels problem should be solved automatiquely,
571 // since we ensure the VR (OB vs OW) is conform to Pixel size.
573 uint8_t *data = BinArea; //safe notation
574 size_t l = GetLength();
575 // gdcmDebugMacro("in DataEntry::WriteContent " << GetKey() << " AtomicLength: "
576 // << Global::GetVR()->GetAtomicElementLength(this->GetVR() ) // << " BinArea in :" << &BinArea
578 if (BinArea) // the binArea was *actually* loaded
580 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
581 unsigned short vrLgth =
582 Global::GetVR()->GetAtomicElementLength(this->GetVR());
588 binary_write (*fp, data, l );
593 uint16_t *data16 = (uint16_t *)data;
594 for(i=0;i<l/vrLgth;i++)
595 binary_write( *fp, data16[i]);
600 uint32_t *data32 = (uint32_t *)data;
601 for(i=0;i<l/vrLgth;i++)
602 binary_write( *fp, data32[i]);
607 double *data64 = (double *)data;
608 for(i=0;i<l/vrLgth;i++)
609 binary_write( *fp, data64[i]);
614 binary_write (*fp, data, l );
615 #endif //GDCM_WORDS_BIGENDIAN
620 // nothing was loaded, but we need to skip space on disc
623 // --> WARNING : nothing is written;
624 // --> the initial data (on the the source image) is lost
625 // --> user is *not* informed !
626 gdcmDebugMacro ("Nothing was loaded, but we need to skip space on disc. "
627 << "Length =" << l << " for " << GetKey() );
628 fp->seekp(l, std::ios::cur); // At Write time, for unloaded elems
631 // to avoid gdcm to propagate oddities
632 // (length was already modified)
634 fp->seekp(1, std::ios::cur); // At Write time, for non even length elems
638 * \brief Compute the full length of the elementary DataEntry (not only value
639 * length) depending on the VR.
641 uint32_t DataEntry::ComputeFullLength()
643 return GetFullLength();
646 //-----------------------------------------------------------------------------
648 /// \brief Creates a DataEntry owned BinArea (remove previous one if any)
649 void DataEntry::NewBinArea( )
652 if( GetLength() > 0 )
653 BinArea = new uint8_t[GetLength()];
656 /// \brief Removes the BinArea, if owned by the DataEntry
657 void DataEntry::DeleteBinArea(void)
659 if (BinArea && SelfArea)
666 //-----------------------------------------------------------------------------
669 //-----------------------------------------------------------------------------
672 * \brief Prints a DataEntry (Dicom entry)
673 * @param os ostream we want to print in
674 * @param indent Indentation string to be prepended during printing
676 void DataEntry::Print(std::ostream &os, std::string const & )
681 uint16_t g = GetGroup();
682 if (g == 0xfffe) // delimiters have NO value
684 return; // just to avoid identing all the remaining code
687 std::ostringstream s;
693 const VRKey &vr = GetVR();
695 if( vr == "US" || vr == "SS" || vr == "UL" || vr == "SL"
696 || vr == "FL" || vr == "FD")
697 s << " [" << GetString() << "]";
700 if(Global::GetVR()->IsVROfStringRepresentable(vr))
702 // replace non printable characters by '.'
703 std::string cleanString = Util::CreateCleanString(v);
704 if ( cleanString.length() <= GetMaxSizePrintEntry()
707 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
708 // FIXME : PrintLevel<2 *does* print the values
709 // (3 is only for extra offsets printing)
710 // What do you wanted to do ? JPR
712 s << " [" << cleanString << "]";
716 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
721 // A lot of Private elements (with no VR) contain actually
722 // only printable characters;
723 // Let's deal with them as is they were VR std::string representable
725 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
727 // FIXME : since the 'Area' *is* clean, just use
728 // a 'CreateString' method, to save CPU time.
729 std::string cleanString =
730 Util::CreateCleanString( BinArea,GetLength() );
731 s << " [" << cleanString << "]";
735 s << " [" << GDCM_BINLOADED << ";"
736 << "length = " << GetLength() << "]";
744 s << " [" << GDCM_NOTLOADED << "]";
745 else if( IsUnfound() )
746 s << " [" << GDCM_UNFOUND << "]";
747 else if( IsUnread() )
748 s << " [" << GDCM_UNREAD << "]";
749 else if ( GetLength() == 0 )
754 s << " (" << GDCM_PIXELDATA << ")";
756 // Display the UID value (instead of displaying only the rough code)
757 // First 'clean' trailing character (space or zero)
760 const uint16_t &gr = GetGroup();
761 const uint16_t &elt = GetElement();
762 TS *ts = Global::GetTS();
766 // Any more to be displayed ?
767 if ( elt == 0x0010 || elt == 0x0002 )
769 if ( v.length() != 0 ) // for brain damaged headers
771 if ( ! isdigit((unsigned char)v[v.length()-1]) )
773 v.erase(v.length()-1, 1);
776 s << " ==>\t[" << ts->GetValue(v) << "]";
779 else if (gr == 0x0008)
781 if ( elt == 0x0016 || elt == 0x1150 )
783 if ( v.length() != 0 ) // for brain damaged headers
785 if ( ! isdigit((unsigned char)v[v.length()-1]) )
787 v.erase(v.length()-1, 1);
790 s << " ==>\t[" << ts->GetValue(v) << "]";
793 else if (gr == 0x0004)
795 if ( elt == 0x1510 || elt == 0x1512 )
797 if ( v.length() != 0 ) // for brain damaged headers
799 if ( ! isdigit((unsigned char)v[v.length()-1]) )
801 v.erase(v.length()-1, 1);
804 s << " ==>\t[" << ts->GetValue(v) << "]";
812 //-----------------------------------------------------------------------------
813 } // end namespace gdcm