1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2006/03/13 14:44:07 $
7 Version: $Revision: 1.33 $
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 /// \todo warn the user there was a problem !
178 gdcmErrorMacro("BinArea not set" << std::hex << GetGroup() << " " << GetElement() << " Can't get the value");
182 uint32_t count = GetValueCount();
185 gdcmErrorMacro("Index (" << id << ") is greater than the data size");
189 /// \todo FIX the API : user *knows* that entry contains a US
190 /// and he receives a double ?!?
192 const VRKey &vr = GetVR();
193 /// \todo FIX the API : user *knows* that entry contains a US,
194 /// the method is supposed to return a double
195 /// but sends a US ?!?
196 if( vr == "US" || vr == "SS" )
197 return ((uint16_t *)BinArea)[id];
198 else if( vr == "UL" || vr == "SL" )
199 return ((uint32_t *)BinArea)[id];
200 else if( vr == "FL" )
201 return ((float *)BinArea)[id];
202 else if( vr == "FD" )
203 return ((double *)BinArea)[id];
204 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
206 // this is for VR = "DS", ...
209 // Don't use std::string to accelerate processing
211 char *tmp = new char[GetLength()+1];
212 memcpy(tmp,BinArea,GetLength());
223 for(uint32_t i=0;i<GetLength();i++)
253 * \brief Checks if the multiplicity of the value follows Dictionary VM
255 bool DataEntry::IsValueCountValid() const
258 const std::string &strVM = GetVM();
259 uint32_t vc = GetValueCount();
260 bool valid = vc == 0;
264 // FIXME : what shall we do with VM = "2-n", "3-n", etc
268 // make sure there is at least one ??? FIXME
273 std::istringstream os;
277 // vm respects the one from the dict
278 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
282 // Entries whose type is 1 are mandatory, with a mandatory value.
283 // Entries whose type is 1c are mandatory-inside-a-Sequence,
284 // with a mandatory value.
285 // Entries whose type is 2 are mandatory, with an optional value.
286 // Entries whose type is 2c are mandatory-inside-a-Sequence,
287 // with an optional value.
288 // Entries whose type is 3 are optional.
290 // case vc == 0 is only applicable for 'type 2' entries.
291 // Problem : entry type may depend on the modality and/or the Sequence
292 // it's embedded in !
293 // (Get the information in the 'Conformance Statements' ...)
300 * \brief returns the number of elementary values
302 uint32_t DataEntry::GetValueCount( ) const
304 const VRKey &vr = GetVR();
305 if( vr == "US" || vr == "SS" )
306 return GetLength()/sizeof(uint16_t);
307 else if( vr == "UL" || vr == "SL" )
308 return GetLength()/sizeof(uint32_t);
309 else if( vr == "FL" || vr == "OF" )
310 return GetLength()/4 ; // FL has a *4* length! sizeof(float);
311 else if( vr == "FD" )
312 return GetLength()/8; // FD has a *8* length! sizeof(double);
313 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
315 // Some element in DICOM are allowed to be empty
318 // Don't use std::string to accelerate processing
320 for(uint32_t i=0;i<GetLength();i++)
322 if( BinArea[i] == '\\')
331 * \brief Gets a std::vector <double> holding the value(s) of a DS DataEntry
332 * @param valueVector std::vector <double> of value(s)
333 * \return false if VR not "DS" or DataEntry empty
335 bool DataEntry::GetDSValue(std::vector <double> &valueVector)
337 /// \todo rewrite the whole method, in order *not to use* std::string !
338 std::vector<std::string> tokens;
340 if (GetVR() != "DS") // never trust a user !
343 Util::Tokenize ( GetString().c_str(), tokens, "\\" );
345 int nbValues= tokens.size();
349 for (int loop=0; loop<nbValues; loop++)
350 valueVector.push_back(atof(tokens[loop].c_str()));
356 * \brief Sets the 'value' of a DataEntry, passed as a std::string
357 * @param value string representation of the value to be set
359 void DataEntry::SetString(std::string const &value)
363 const VRKey &vr = GetVR();
364 if ( vr == "US" || vr == "SS" )
366 std::vector<std::string> tokens;
367 Util::Tokenize (value, tokens, "\\");
368 SetLength(tokens.size()*sizeof(uint16_t));
371 uint16_t *data = (uint16_t *)BinArea;
372 for (unsigned int i=0; i<tokens.size();i++)
373 data[i] = atoi(tokens[i].c_str());
376 else if ( vr == "UL" || vr == "SL" )
378 std::vector<std::string> tokens;
379 Util::Tokenize (value, tokens, "\\");
380 SetLength(tokens.size()*sizeof(uint32_t));
383 uint32_t *data = (uint32_t *)BinArea;
384 for (unsigned int i=0; i<tokens.size();i++)
385 data[i] = atoi(tokens[i].c_str());
388 else if ( vr == "FL" )
390 std::vector<std::string> tokens;
391 Util::Tokenize (value, tokens, "\\");
392 SetLength(tokens.size()*sizeof(float));
395 float *data = (float *)BinArea;
396 for (unsigned int i=0; i<tokens.size();i++)
397 data[i] = (float)atof(tokens[i].c_str());
400 else if ( vr == "FD" )
402 std::vector<std::string> tokens;
403 Util::Tokenize (value, tokens, "\\");
404 SetLength(tokens.size()*sizeof(double));
407 double *data = (double *)BinArea;
408 for (unsigned int i=0; i<tokens.size();i++)
409 data[i] = atof(tokens[i].c_str());
414 if( value.size() > 0 )
416 size_t l = value.size();
419 memcpy(BinArea, value.c_str(), l);
424 State = STATE_LOADED;
427 * \brief returns as a string (when possible) the value of the DataEntry
429 std::string const &DataEntry::GetString() const
431 static std::ostringstream s;
432 const VRKey &vr = GetVR();
440 // When short integer(s) are stored, convert the following (n * 2) characters
441 // as a displayable string, the values being separated by a back-slash
443 if( vr == "US" || vr == "SS" )
445 uint16_t *data=(uint16_t *)BinArea;
447 for (unsigned int i=0; i < GetValueCount(); i++)
455 // See above comment on multiple short integers (mutatis mutandis).
456 else if( vr == "UL" || vr == "SL" )
458 uint32_t *data=(uint32_t *)BinArea;
460 for (unsigned int i=0; i < GetValueCount(); i++)
468 else if( vr == "FL" )
470 float *data=(float *)BinArea;
472 for (unsigned int i=0; i < GetValueCount(); i++)
480 else if( vr == "FD" )
482 double *data=(double *)BinArea;
484 for (unsigned int i=0; i < GetValueCount(); i++)
494 StrArea.append((const char *)BinArea,GetLength());
495 // to avoid gdcm propagate oddities in lengthes
497 StrArea.append(" ",1);
503 * \brief Copies all the attributes from an other DocEntry
504 * @param doc entry to copy from
505 * @remarks The content BinArea is copied too
507 void DataEntry::Copy(DocEntry *doc)
511 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
514 State = entry->State;
516 CopyBinArea(entry->BinArea,entry->GetLength());
521 * \brief Writes the 'value' area of a DataEntry
522 * @param fp already open ofstream pointer
523 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
525 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
527 DocEntry::WriteContent(fp, filetype);
529 if ( GetGroup() == 0xfffe )
531 return; //delimitors have NO value
534 // --> We only deal with Little Endian writting.
535 // --> forget Big Endian Transfer Syntax writting!
536 // Next DICOM version will give it up ...
538 // WARNING - For Implicit VR private element,
539 // we have *no choice* but considering them as
540 // something like 'OB' values.
541 // we rewrite them as we found them on disc.
542 // Some trouble will occur if element was
543 // *actually* OW, if image was produced
544 // on Big endian based processor, read and writen
545 // on Little endian based processor
546 // and, later on, somebody needs
547 // this 'OW' Implicit VR private element (?!?)
548 // (Same stuff, mutatis mutandis, for Little/Big)
550 // 8/16 bits Pixels problem should be solved automatiquely,
551 // since we ensure the VR (OB vs OW) is conform to Pixel size.
553 uint8_t *data = BinArea; //safe notation
554 size_t l = GetLength();
555 gdcmDebugMacro("in DataEntry::WriteContent " << GetKey() << " AtomicLength: "
556 << Global::GetVR()->GetAtomicElementLength(this->GetVR() ) // << " BinArea in :" << &BinArea
558 if (BinArea) // the binArea was *actually* loaded
560 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
561 unsigned short vrLgth =
562 Global::GetVR()->GetAtomicElementLength(this->GetVR());
568 binary_write (*fp, data, l );
573 uint16_t *data16 = (uint16_t *)data;
574 for(i=0;i<l/vrLgth;i++)
575 binary_write( *fp, data16[i]);
580 uint32_t *data32 = (uint32_t *)data;
581 for(i=0;i<l/vrLgth;i++)
582 binary_write( *fp, data32[i]);
587 double *data64 = (double *)data;
588 for(i=0;i<l/vrLgth;i++)
589 binary_write( *fp, data64[i]);
594 binary_write (*fp, data, l );
595 #endif //GDCM_WORDS_BIGENDIAN
600 // nothing was loaded, but we need to skip space on disc
603 // --> WARNING : nothing is written;
604 // --> the initial data (on the the source image) is lost
605 // --> user is *not* informed !
606 gdcmDebugMacro ("Nothing was loaded, but we need to skip space on disc. "
607 << "Length =" << l << " for " << GetKey() );
608 fp->seekp(l, std::ios::cur); // At Write time, for unloaded elems
611 // to avoid gdcm to propagate oddities
612 // (length was already modified)
614 fp->seekp(1, std::ios::cur); // At Write time, for non even length elems
618 * \brief Compute the full length of the elementary DataEntry (not only value
619 * length) depending on the VR.
621 uint32_t DataEntry::ComputeFullLength()
623 return GetFullLength();
626 //-----------------------------------------------------------------------------
628 /// \brief Creates a DataEntry owned BinArea (remove previous one if any)
629 void DataEntry::NewBinArea( )
632 if( GetLength() > 0 )
633 BinArea = new uint8_t[GetLength()];
636 /// \brief Removes the BinArea, if owned by the DataEntry
637 void DataEntry::DeleteBinArea(void)
639 if (BinArea && SelfArea)
646 //-----------------------------------------------------------------------------
649 //-----------------------------------------------------------------------------
652 * \brief Prints a DataEntry (Dicom entry)
653 * @param os ostream we want to print in
654 * @param indent Indentation string to be prepended during printing
656 void DataEntry::Print(std::ostream &os, std::string const & )
661 uint16_t g = GetGroup();
662 if (g == 0xfffe) // delimiters have NO value
664 return; // just to avoid identing all the remaining code
667 std::ostringstream s;
673 const VRKey &vr = GetVR();
675 if( vr == "US" || vr == "SS" || vr == "UL" || vr == "SL"
676 || vr == "FL" || vr == "FD")
677 s << " [" << GetString() << "]";
680 if(Global::GetVR()->IsVROfStringRepresentable(vr))
682 // replace non printable characters by '.'
683 std::string cleanString = Util::CreateCleanString(v);
684 if ( cleanString.length() <= GetMaxSizePrintEntry()
687 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
688 // FIXME : PrintLevel<2 *does* print the values
689 // (3 is only for extra offsets printing)
690 // What do you wanted to do ? JPR
692 s << " [" << cleanString << "]";
696 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
701 // A lot of Private elements (with no VR) contain actually
702 // only printable characters;
703 // Let's deal with them as is they were VR std::string representable
705 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
707 // FIXME : since the 'Area' *is* clean, just use
708 // a 'CreateString' method, to save CPU time.
709 std::string cleanString =
710 Util::CreateCleanString( BinArea,GetLength() );
711 s << " [" << cleanString << "]";
715 s << " [" << GDCM_BINLOADED << ";"
716 << "length = " << GetLength() << "]";
724 s << " [" << GDCM_NOTLOADED << "]";
725 else if( IsUnfound() )
726 s << " [" << GDCM_UNFOUND << "]";
727 else if( IsUnread() )
728 s << " [" << GDCM_UNREAD << "]";
729 else if ( GetLength() == 0 )
734 s << " (" << GDCM_PIXELDATA << ")";
736 // Display the UID value (instead of displaying only the rough code)
737 // First 'clean' trailing character (space or zero)
740 const uint16_t &gr = GetGroup();
741 const uint16_t &elt = GetElement();
742 TS *ts = Global::GetTS();
746 // Any more to be displayed ?
747 if ( elt == 0x0010 || elt == 0x0002 )
749 if ( v.length() != 0 ) // for brain damaged headers
751 if ( ! isdigit((unsigned char)v[v.length()-1]) )
753 v.erase(v.length()-1, 1);
756 s << " ==>\t[" << ts->GetValue(v) << "]";
759 else if (gr == 0x0008)
761 if ( elt == 0x0016 || elt == 0x1150 )
763 if ( v.length() != 0 ) // for brain damaged headers
765 if ( ! isdigit((unsigned char)v[v.length()-1]) )
767 v.erase(v.length()-1, 1);
770 s << " ==>\t[" << ts->GetValue(v) << "]";
773 else if (gr == 0x0004)
775 if ( elt == 0x1510 || elt == 0x1512 )
777 if ( v.length() != 0 ) // for brain damaged headers
779 if ( ! isdigit((unsigned char)v[v.length()-1]) )
781 v.erase(v.length()-1, 1);
784 s << " ==>\t[" << ts->GetValue(v) << "]";
792 //-----------------------------------------------------------------------------
793 } // end namespace gdcm