1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2006/01/27 10:01:33 $
7 Version: $Revision: 1.29 $
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
171 * FIXME : warn the user there was a problem !
174 double DataEntry::GetValue(const uint32_t &id) const
178 gdcmErrorMacro("BinArea not set. Can't get the value");
182 uint32_t count = GetValueCount();
185 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
189 // FIX the API : user *knows* that entry contains a US
190 // and he receives a double ?!?
192 const VRKey &vr = GetVR();
193 if( vr == "US" || vr == "SS" )
194 return ((uint16_t *)BinArea)[id];
195 else if( vr == "UL" || vr == "SL" )
196 return ((uint32_t *)BinArea)[id];
197 else if( vr == "FL" )
198 return ((float *)BinArea)[id];
199 else if( vr == "FD" )
200 return ((double *)BinArea)[id];
201 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
205 // Don't use std::string to accelerate processing
207 char *tmp = new char[GetLength()+1];
208 memcpy(tmp,BinArea,GetLength());
219 for(uint32_t i=0;i<GetLength();i++)
249 * \brief Checks if the multiplicity of the value follows Dictionary VM
251 bool DataEntry::IsValueCountValid() const
254 const std::string &strVM = GetVM();
255 uint32_t vc = GetValueCount();
256 bool valid = vc == 0;
260 // FIXME : what shall we do with VM = "2-n", "3-n", etc
264 // make sure there is at least one ??? FIXME
269 std::istringstream os;
273 // vm respects the one from the dict
274 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
278 // Entries whose type is 1 are mandatory, with a mandatory value.
279 // Entries whose type is 1c are mandatory-inside-a-Sequence,
280 // with a mandatory value.
281 // Entries whose type is 2 are mandatory, with an optional value.
282 // Entries whose type is 2c are mandatory-inside-a-Sequence,
283 // with an optional value.
284 // Entries whose type is 3 are optional.
286 // case vc == 0 is only applicable for 'type 2' entries.
287 // Problem : entry type may depend on the modality and/or the Sequence
288 // it's embedded in !
289 // (Get the information in the 'Conformance Statements' ...)
296 * \brief returns the number of elementary values
298 uint32_t DataEntry::GetValueCount( ) const
300 const VRKey &vr = GetVR();
301 if( vr == "US" || vr == "SS" )
302 return GetLength()/sizeof(uint16_t);
303 else if( vr == "UL" || vr == "SL" )
304 return GetLength()/sizeof(uint32_t);
305 else if( vr == "FL" || vr == "OF" )
306 return GetLength()/4 ; // FL has a *4* length! sizeof(float);
307 else if( vr == "FD" )
308 return GetLength()/8; // FD has a *8* length! sizeof(double);
309 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
311 // Some element in DICOM are allowed to be empty
314 // Don't use std::string to accelerate processing
316 for(uint32_t i=0;i<GetLength();i++)
318 if( BinArea[i] == '\\')
326 * \brief Sets the 'value' of a DataEntry, passed as a std::string
327 * @param value string representation of the value to be set
329 void DataEntry::SetString(std::string const &value)
333 const VRKey &vr = GetVR();
334 if ( vr == "US" || vr == "SS" )
336 std::vector<std::string> tokens;
337 Util::Tokenize (value, tokens, "\\");
338 SetLength(tokens.size()*sizeof(uint16_t));
341 uint16_t *data = (uint16_t *)BinArea;
342 for (unsigned int i=0; i<tokens.size();i++)
343 data[i] = atoi(tokens[i].c_str());
346 else if ( vr == "UL" || vr == "SL" )
348 std::vector<std::string> tokens;
349 Util::Tokenize (value, tokens, "\\");
350 SetLength(tokens.size()*sizeof(uint32_t));
353 uint32_t *data = (uint32_t *)BinArea;
354 for (unsigned int i=0; i<tokens.size();i++)
355 data[i] = atoi(tokens[i].c_str());
358 else if ( vr == "FL" )
360 std::vector<std::string> tokens;
361 Util::Tokenize (value, tokens, "\\");
362 SetLength(tokens.size()*sizeof(float));
365 float *data = (float *)BinArea;
366 for (unsigned int i=0; i<tokens.size();i++)
367 data[i] = (float)atof(tokens[i].c_str());
370 else if ( vr == "FD" )
372 std::vector<std::string> tokens;
373 Util::Tokenize (value, tokens, "\\");
374 SetLength(tokens.size()*sizeof(double));
377 double *data = (double *)BinArea;
378 for (unsigned int i=0; i<tokens.size();i++)
379 data[i] = atof(tokens[i].c_str());
384 if( value.size() > 0 )
386 size_t l = value.size();
389 memcpy(BinArea, value.c_str(), l);
394 State = STATE_LOADED;
397 * \brief returns as a string (when possible) the value of the DataEntry
399 std::string const &DataEntry::GetString() const
401 static std::ostringstream s;
402 const VRKey &vr = GetVR();
410 // When short integer(s) are stored, convert the following (n * 2) characters
411 // as a displayable string, the values being separated by a back-slash
413 if( vr == "US" || vr == "SS" )
415 uint16_t *data=(uint16_t *)BinArea;
417 for (unsigned int i=0; i < GetValueCount(); i++)
425 // See above comment on multiple short integers (mutatis mutandis).
426 else if( vr == "UL" || vr == "SL" )
428 uint32_t *data=(uint32_t *)BinArea;
430 for (unsigned int i=0; i < GetValueCount(); i++)
438 else if( vr == "FL" )
440 float *data=(float *)BinArea;
442 for (unsigned int i=0; i < GetValueCount(); i++)
450 else if( vr == "FD" )
452 double *data=(double *)BinArea;
454 for (unsigned int i=0; i < GetValueCount(); i++)
464 StrArea.append((const char *)BinArea,GetLength());
465 // to avoid gdcm propagate oddities in lengthes
467 StrArea.append(" ",1);
473 * \brief Copies all the attributes from an other DocEntry
474 * @param doc entry to copy from
475 * @remarks The content BinArea is copied too
477 void DataEntry::Copy(DocEntry *doc)
481 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
484 State = entry->State;
486 CopyBinArea(entry->BinArea,entry->GetLength());
491 * \brief Writes the 'value' area of a DataEntry
492 * @param fp already open ofstream pointer
493 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
495 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
497 DocEntry::WriteContent(fp, filetype);
499 if ( GetGroup() == 0xfffe )
501 return; //delimitors have NO value
504 // --> We only deal with Little Endian writting.
505 // --> forget Big Endian Transfer Syntax writting!
506 // Next DICOM version will give it up ...
508 // WARNING - For Implicit VR private element,
509 // we have *no choice* but considering them as
510 // something like 'OB' values.
511 // we rewrite them as we found them on disc.
512 // Some trouble will occur if element was
513 // *actually* OW, if image was produced
514 // on Big endian based processor, read and writen
515 // on Little endian based processor
516 // and, later on, somebody needs
517 // this 'OW' Implicit VR private element (?!?)
518 // (Same stuff, mutatis mutandis, for Little/Big)
520 // 8/16 bits Pixels problem should be solved automatiquely,
521 // since we ensure the VR (OB vs OW) is conform to Pixel size.
523 uint8_t *data = BinArea; //safe notation
524 size_t l = GetLength();
525 gdcmDebugMacro("in DataEntry::WriteContent " << GetKey() << " AtomicLength: "
526 << Global::GetVR()->GetAtomicElementLength(this->GetVR() ) // << " BinArea in :" << &BinArea
528 if (BinArea) // the binArea was *actually* loaded
530 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
531 unsigned short vrLgth =
532 Global::GetVR()->GetAtomicElementLength(this->GetVR());
538 binary_write (*fp, data, l );
543 uint16_t *data16 = (uint16_t *)data;
544 for(i=0;i<l/vrLgth;i++)
545 binary_write( *fp, data16[i]);
550 uint32_t *data32 = (uint32_t *)data;
551 for(i=0;i<l/vrLgth;i++)
552 binary_write( *fp, data32[i]);
557 double *data64 = (double *)data;
558 for(i=0;i<l/vrLgth;i++)
559 binary_write( *fp, data64[i]);
564 binary_write (*fp, data, l );
565 #endif //GDCM_WORDS_BIGENDIAN
570 // nothing was loaded, but we need to skip space on disc
573 // --> WARNING : nothing is written;
574 // --> the initial data (on the the source image) is lost
575 // --> user is *not* informed !
576 gdcmDebugMacro ("Nothing was loaded, but we need to skip space on disc. "
577 << "Length =" << l << " for " << GetKey() );
578 fp->seekp(l, std::ios::cur);
581 // to avoid gdcm to propagate oddities
582 // (length was already modified)
584 fp->seekp(1, std::ios::cur);
588 * \brief Compute the full length of the elementary DataEntry (not only value
589 * length) depending on the VR.
591 uint32_t DataEntry::ComputeFullLength()
593 return GetFullLength();
596 //-----------------------------------------------------------------------------
598 /// \brief Creates a DataEntry owned BinArea (remove previous one if any)
599 void DataEntry::NewBinArea( )
602 if( GetLength() > 0 )
603 BinArea = new uint8_t[GetLength()];
606 /// \brief Removes the BinArea, if owned by the DataEntry
607 void DataEntry::DeleteBinArea(void)
609 if (BinArea && SelfArea)
616 //-----------------------------------------------------------------------------
619 //-----------------------------------------------------------------------------
622 * \brief Prints a DataEntry (Dicom entry)
623 * @param os ostream we want to print in
624 * @param indent Indentation string to be prepended during printing
626 void DataEntry::Print(std::ostream &os, std::string const & )
631 uint16_t g = GetGroup();
632 if (g == 0xfffe) // delimiters have NO value
634 return; // just to avoid identing all the remaining code
637 std::ostringstream s;
643 const VRKey &vr = GetVR();
645 if( vr == "US" || vr == "SS" )
646 s << " [" << GetString() << "]";
647 else if( vr == "UL" || vr == "SL" )
648 s << " [" << GetString() << "]";
649 else if ( vr == "FL" )
650 s << " [" << GetString() << "]";
651 else if ( vr == "FD" )
652 s << " [" << GetString() << "]";
655 if(Global::GetVR()->IsVROfStringRepresentable(vr))
657 // replace non printable characters by '.'
658 std::string cleanString = Util::CreateCleanString(v);
659 if ( cleanString.length() <= GetMaxSizePrintEntry()
662 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
663 // FIXME : PrintLevel<2 *does* print the values
664 // (3 is only for extra offsets printing)
665 // What do you wanted to do ? JPR
667 s << " [" << cleanString << "]";
671 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
676 // A lot of Private elements (with no VR) contain actually
677 // only printable characters;
678 // Let's deal with them as is they were VR std::string representable
680 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
682 // FIXME : since the 'Area' *is* clean, just use
683 // a 'CreateString' method, to save CPU time.
684 std::string cleanString =
685 Util::CreateCleanString( BinArea,GetLength() );
686 s << " [" << cleanString << "]";
690 s << " [" << GDCM_BINLOADED << ";"
691 << "length = " << GetLength() << "]";
699 s << " [" << GDCM_NOTLOADED << "]";
700 else if( IsUnfound() )
701 s << " [" << GDCM_UNFOUND << "]";
702 else if( IsUnread() )
703 s << " [" << GDCM_UNREAD << "]";
704 else if ( GetLength() == 0 )
709 s << " (" << GDCM_PIXELDATA << ")";
711 // Display the UID value (instead of displaying only the rough code)
712 // First 'clean' trailing character (space or zero)
715 const uint16_t &gr = GetGroup();
716 const uint16_t &elt = GetElement();
717 TS *ts = Global::GetTS();
721 // Any more to be displayed ?
722 if ( elt == 0x0010 || elt == 0x0002 )
724 if ( v.length() != 0 ) // for brain damaged headers
726 if ( ! isdigit((unsigned char)v[v.length()-1]) )
728 v.erase(v.length()-1, 1);
731 s << " ==>\t[" << ts->GetValue(v) << "]";
734 else if (gr == 0x0008)
736 if ( elt == 0x0016 || elt == 0x1150 )
738 if ( v.length() != 0 ) // for brain damaged headers
740 if ( ! isdigit((unsigned char)v[v.length()-1]) )
742 v.erase(v.length()-1, 1);
745 s << " ==>\t[" << ts->GetValue(v) << "]";
748 else if (gr == 0x0004)
750 if ( elt == 0x1510 || elt == 0x1512 )
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) << "]";
767 //-----------------------------------------------------------------------------
768 } // end namespace gdcm