1 /*=========================================================================
4 Module: $RCSfile: gdcmDataEntry.cxx,v $
6 Date: $Date: 2005/11/02 10:14:33 $
7 Version: $Revision: 1.16 $
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"
30 //-----------------------------------------------------------------------------
31 #define MAX_SIZE_PRINT_ELEMENT_VALUE 0x7fffffff
32 uint32_t DataEntry::MaxSizePrintEntry = MAX_SIZE_PRINT_ELEMENT_VALUE;
34 //-----------------------------------------------------------------------------
35 // Constructor / Destructor
37 * \brief Constructor for a given DictEntry
38 * @param e Pointer to existing dictionary entry
40 DataEntry::DataEntry(DictEntry *e)
51 * \brief Constructor for a given DocEntry
52 * @param e Pointer to existing Doc entry
54 DataEntry::DataEntry(DocEntry *e)
55 : DocEntry(e->GetDictEntry())
65 * \brief Canonical destructor.
67 DataEntry::~DataEntry ()
72 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
78 * \brief Sets the value (non string) of the current Dicom Header Entry
80 void DataEntry::SetBinArea( uint8_t *area, bool self )
90 * \brief Inserts the value (non string) into the current Dicom Header Entry
92 void DataEntry::CopyBinArea( uint8_t *area, uint32_t length )
96 uint32_t lgh = length + length%2;
99 if( area && length > 0 )
102 memcpy(BinArea,area,length);
106 State = STATE_LOADED;
110 void DataEntry::SetValue(const uint32_t &id, const double &val)
114 State = STATE_LOADED;
116 if( id > GetValueCount() )
118 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
122 const VRKey &vr = GetVR();
123 if( vr == "US" || vr == "SS" )
125 uint16_t *data = (uint16_t *)BinArea;
126 data[id] = (uint16_t)val;
128 else if( vr == "UL" || vr == "SL" )
130 uint32_t *data = (uint32_t *)BinArea;
131 data[id] = (uint32_t)val;
133 else if( vr == "FL" )
135 float *data = (float *)BinArea;
136 data[id] = (float)val;
138 else if( vr == "FD" )
140 double *data = (double *)BinArea;
141 data[id] = (double)val;
143 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
145 gdcmErrorMacro("SetValue on String representable not implemented yet");
149 BinArea[id] = (uint8_t)val;
153 * \brief returns, as a double (?!?) one of the values
154 // (when entry is multivaluated), identified by its index.
155 // Returns 0.0 if index is wrong
156 // FIXME : warn the user there was a problem !
158 double DataEntry::GetValue(const uint32_t &id) const
162 gdcmErrorMacro("BinArea not set. Can't get the value");
166 uint32_t count = GetValueCount();
169 gdcmErrorMacro("Index (" << id << ")is greater than the data size");
173 // FIX the API : user *knows* that entry contains a US
174 // and he receives a double ?!?
176 const VRKey &vr = GetVR();
177 if( vr == "US" || vr == "SS" )
178 return ((uint16_t *)BinArea)[id];
179 else if( vr == "UL" || vr == "SL" )
180 return ((uint32_t *)BinArea)[id];
181 else if( vr == "FL" )
182 return ((float *)BinArea)[id];
183 else if( vr == "FD" )
184 return ((double *)BinArea)[id];
185 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
189 // Don't use std::string to accelerate processing
191 char *tmp = new char[GetLength()+1];
192 memcpy(tmp,BinArea,GetLength());
203 for(uint32_t i=0;i<GetLength();i++)
233 * \brief Checks if the multiplicity of the value follows Dictionary VM
235 bool DataEntry::IsValueCountValid() const
239 const std::string &strVM = GetVM();
240 uint32_t vc = GetValueCount();
242 // FIXME : what shall we do with VM = "2-n", "3-n", etc
246 // make sure it is at least one ??? FIXME
247 valid = vc >= 1 || vc == 0;
251 std::istringstream os;
255 // vm respect the one from the dict
256 // vm is 0 (we need to check if this element is allowed to be empty) FIXME
260 // Entries whose type is 1 are mandatory, with a mandatory value.
261 // Entries whose type is 1c are mandatory-inside-a-Sequence,
262 // with a mandatory value.
263 // Entries whose type is 2 are mandatory, with an optional value.
264 // Entries whose type is 2c are mandatory-inside-a-Sequence,
265 // with an optional value.
266 // Entries whose type is 3 are optional.
268 // case vc == 0 is only applicable for 'type 2' entries.
269 // Problem : entry type may depend on the modality and/or the Sequence
270 // it's embedded in !
271 // (Get the information in the 'Conformance Statements' ...)
272 valid = vc == vm || vc == 0;
278 * \brief returns the number of elementary values
280 uint32_t DataEntry::GetValueCount( ) const
282 const VRKey &vr = GetVR();
283 if( vr == "US" || vr == "SS" )
284 return GetLength()/sizeof(uint16_t);
285 else if( vr == "UL" || vr == "SL" )
286 return GetLength()/sizeof(uint32_t);
287 else if( vr == "FL" )
288 return GetLength()/sizeof(float);
289 else if( vr == "FD" )
290 return GetLength()/sizeof(double);
291 else if( Global::GetVR()->IsVROfStringRepresentable(vr) )
293 // Some element in DICOM are allowed to be empty
294 if( !GetLength() ) return 0;
295 // Don't use std::string to accelerate processing
297 for(uint32_t i=0;i<GetLength();i++)
299 if( BinArea[i] == '\\')
308 * \brief Sets the 'value' of an Entry, passed as a std::string
309 * @param value string representation of the value to be set
311 void DataEntry::SetString(std::string const &value)
315 const VRKey &vr = GetVR();
316 if ( vr == "US" || vr == "SS" )
318 std::vector<std::string> tokens;
319 Util::Tokenize (value, tokens, "\\");
320 SetLength(tokens.size()*sizeof(uint16_t));
323 uint16_t *data = (uint16_t *)BinArea;
324 for (unsigned int i=0; i<tokens.size();i++)
325 data[i] = atoi(tokens[i].c_str());
328 else if ( vr == "UL" || vr == "SL" )
330 std::vector<std::string> tokens;
331 Util::Tokenize (value, tokens, "\\");
332 SetLength(tokens.size()*sizeof(uint32_t));
335 uint32_t *data = (uint32_t *)BinArea;
336 for (unsigned int i=0; i<tokens.size();i++)
337 data[i] = atoi(tokens[i].c_str());
340 else if ( vr == "FL" )
342 std::vector<std::string> tokens;
343 Util::Tokenize (value, tokens, "\\");
344 SetLength(tokens.size()*sizeof(float));
347 float *data = (float *)BinArea;
348 for (unsigned int i=0; i<tokens.size();i++)
349 data[i] = (float)atof(tokens[i].c_str());
352 else if ( vr == "FD" )
354 std::vector<std::string> tokens;
355 Util::Tokenize (value, tokens, "\\");
356 SetLength(tokens.size()*sizeof(double));
359 double *data = (double *)BinArea;
360 for (unsigned int i=0; i<tokens.size();i++)
361 data[i] = atof(tokens[i].c_str());
366 if( value.size() > 0 )
368 // FIXME : should be quicker if we don't create one more std::string
369 // just to make even the length of a char array ...
372 std::string finalVal = Util::DicomString( value.c_str() );
373 SetLength(finalVal.size());
376 memcpy(BinArea, &(finalVal[0]), finalVal.size());
379 size_t l = value.size();
382 //memcpy(BinArea, &(value[0]), l);
383 memcpy(BinArea, value.c_str(), l);
388 State = STATE_LOADED;
391 * \brief returns as a string (when possible) the value of the DataEntry
393 std::string const &DataEntry::GetString() const
395 static std::ostringstream s;
396 const VRKey &vr = GetVR();
404 // When short integer(s) are stored, convert the following (n * 2) characters
405 // as a displayable string, the values being separated by a back-slash
407 if( vr == "US" || vr == "SS" )
409 uint16_t *data=(uint16_t *)BinArea;
411 for (unsigned int i=0; i < GetValueCount(); i++)
419 // See above comment on multiple short integers (mutatis mutandis).
420 else if( vr == "UL" || vr == "SL" )
422 uint32_t *data=(uint32_t *)BinArea;
424 for (unsigned int i=0; i < GetValueCount(); i++)
432 else if( vr == "FL" )
434 float *data=(float *)BinArea;
436 for (unsigned int i=0; i < GetValueCount(); i++)
444 else if( vr == "FD" )
446 double *data=(double *)BinArea;
448 for (unsigned int i=0; i < GetValueCount(); i++)
457 StrArea.append((const char *)BinArea,GetLength());
462 * \brief Copies all the attributes from an other DocEntry
463 * @param doc entry to copy from
465 void DataEntry::Copy(DocEntry *doc)
469 DataEntry *entry = dynamic_cast<DataEntry *>(doc);
472 State = entry->State;
474 CopyBinArea(entry->BinArea,entry->GetLength());
478 * \brief Writes the 'value' area of a DataEntry
479 * @param fp already open ofstream pointer
480 * @param filetype type of the file (ACR, ImplicitVR, ExplicitVR, ...)
482 void DataEntry::WriteContent(std::ofstream *fp, FileType filetype)
484 DocEntry::WriteContent(fp, filetype);
486 if ( GetGroup() == 0xfffe )
488 return; //delimitors have NO value
491 // --> We only deal with Little Endian writting
492 // --> forget Big Endian Transfer Syntax writting!
493 // Next DICOM version will give it up ...
495 // WARNING - For Implicit VR private element,
496 // we have *no choice* but considering them as
497 // something like 'OB' values.
498 // we rewrite them as we found them on disc.
499 // Some trouble will occur if element was
500 // *actually* OW, if image was produced
501 // on Big endian based processor, read and writen
502 // on Little endian based processor
503 // and, later on, somebody needs
504 // this 'OW' Implicit VR private element (?!?)
505 // (Same stuff, mutatis mutandis, for Little/Big)
507 // 8/16 bits Pixels problem should be solved automatiquely,
508 // since we ensure the VR is conform to Pixel size.
510 uint8_t *data = BinArea; //safe notation
511 size_t l = GetLength();
512 gdcmDebugMacro ("in DataEntry::WriteContent " << GetKey()
513 << " : " << Global::GetVR()->GetAtomicElementLength(this->GetVR())
515 if (BinArea) // the binArea was *actually* loaded
517 #if defined(GDCM_WORDS_BIGENDIAN) || defined(GDCM_FORCE_BIGENDIAN_EMULATION)
518 unsigned short vrLgth =
519 Global::GetVR()->GetAtomicElementLength(this->GetVR());
525 binary_write (*fp, data, l );
530 uint16_t *data16 = (uint16_t *)data;
531 for(i=0;i<l/vrLgth;i++)
532 binary_write( *fp, data16[i]);
537 uint32_t *data32 = (uint32_t *)data;
538 for(i=0;i<l/vrLgth;i++)
539 binary_write( *fp, data32[i]);
544 double *data64 = (double *)data;
545 for(i=0;i<l/vrLgth;i++)
546 binary_write( *fp, data64[i]);
551 binary_write (*fp, data, l );
552 #endif //GDCM_WORDS_BIGENDIAN
557 // nothing was loaded, but we need to skip space on disc
559 // --> WARNING : nothing is written;
560 // --> the initial data (on the the source image) is lost
561 // --> user is *not* informed !
563 fp->seekp(l, std::ios::cur);
567 //-----------------------------------------------------------------------------
569 void DataEntry::NewBinArea(void)
572 if( GetLength() > 0 )
573 BinArea = new uint8_t[GetLength()];
577 void DataEntry::DeleteBinArea(void)
579 if (BinArea && SelfArea)
586 //-----------------------------------------------------------------------------
589 //-----------------------------------------------------------------------------
592 * \brief Prints a DataEntry (Dicom entry)
593 * @param os ostream we want to print in
594 * @param indent Indentation string to be prepended during printing
596 void DataEntry::Print(std::ostream &os, std::string const & )
601 uint16_t g = GetGroup();
602 if (g == 0xfffe) // delimiters have NO value
604 return; // just to avoid identing all the remaining code
607 std::ostringstream s;
613 const VRKey &vr = GetVR();
615 if( vr == "US" || vr == "SS" )
616 s << " [" << GetString() << "]";
617 else if( vr == "UL" || vr == "SL" )
618 s << " [" << GetString() << "]";
619 else if ( vr == "FL" )
620 s << " [" << GetString() << "]";
621 else if ( vr == "FD" )
622 s << " [" << GetString() << "]";
625 if(Global::GetVR()->IsVROfStringRepresentable(vr))
627 // replace non printable characters by '.'
628 std::string cleanString = Util::CreateCleanString(v);
629 if ( cleanString.length() <= GetMaxSizePrintEntry()
632 // FIXME : when IsNotLoaded(), you create a Clean String ?!?
633 // FIXME : PrintLevel<2 *does* print the values
634 // (3 is only for extra offsets printing)
635 // What do you wanted to do ? JPR
637 s << " [" << cleanString << "]";
641 s << " [gdcm::too long for print (" << cleanString.length() << ") ]";
646 // A lot of Private elements (with no VR) contain actually
647 // only printable characters;
648 // Let's deal with them as is they were VR std::string representable
650 if ( Util::IsCleanArea( GetBinArea(), GetLength() ) )
652 // FIXME : since the 'Area' *is* clean, just use
653 // a 'CreateString' method, to save CPU time.
654 std::string cleanString =
655 Util::CreateCleanString( BinArea,GetLength() );
656 s << " [" << cleanString << "]";
660 s << " [" << GDCM_BINLOADED << ";"
661 << "length = " << GetLength() << "]";
669 s << " [" << GDCM_NOTLOADED << "]";
670 else if( IsUnfound() )
671 s << " [" << GDCM_UNFOUND << "]";
672 else if( IsUnread() )
673 s << " [" << GDCM_UNREAD << "]";
674 else if ( GetLength() == 0 )
679 s << " (" << GDCM_PIXELDATA << ")";
681 // Display the UID value (instead of displaying only the rough code)
682 // First 'clean' trailing character (space or zero)
685 const uint16_t &gr = GetGroup();
686 const uint16_t &elt = GetElement();
687 TS *ts = Global::GetTS();
691 // Any more to be displayed ?
692 if ( elt == 0x0010 || elt == 0x0002 )
694 if ( v.length() != 0 ) // for brain damaged headers
696 if ( ! isdigit((unsigned char)v[v.length()-1]) )
698 v.erase(v.length()-1, 1);
701 s << " ==>\t[" << ts->GetValue(v) << "]";
704 else if (gr == 0x0008)
706 if ( elt == 0x0016 || elt == 0x1150 )
708 if ( v.length() != 0 ) // for brain damaged headers
710 if ( ! isdigit((unsigned char)v[v.length()-1]) )
712 v.erase(v.length()-1, 1);
715 s << " ==>\t[" << ts->GetValue(v) << "]";
718 else if (gr == 0x0004)
720 if ( elt == 0x1510 || elt == 0x1512 )
722 if ( v.length() != 0 ) // for brain damaged headers
724 if ( ! isdigit((unsigned char)v[v.length()-1]) )
726 v.erase(v.length()-1, 1);
729 s << " ==>\t[" << ts->GetValue(v) << "]";
737 //-----------------------------------------------------------------------------
738 } // end namespace gdcm