MeVisLab Toolbox Reference
mlImageFormatTag.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_IMAGE_FORMAT_TAG_H
14 #define ML_IMAGE_FORMAT_TAG_H
15 
18 
19 // Resolve platform independencies.
21 
22 #include "mlModuleIncludes.h"
23 
24 // Implement everything in ML namespace.
25 ML_START_NAMESPACE
26 
27  //----------------------------------------------------------------------
29  //----------------------------------------------------------------------
31  public:
32 
33  //------------------------------------------------------------------
36  //------------------------------------------------------------------
38  inline MLImageFormatTag() :
39  _name (""),
40  _value("")
41  {}
42 
44  inline MLImageFormatTag(const MLImageFormatTag &tag) :
45  _name (tag._name),
46  _value(tag._value)
47  {}
48 
50  inline MLImageFormatTag(const std::string &name,
51  const std::string &value) :
52  _name (name),
53  _value(value)
54  {}
55 
57  inline MLImageFormatTag(const std::string &name, MLint value) :
58  _name (name)
59  {
60  setValue(value);
61  }
62 
64  inline MLImageFormatTag(const std::string &name, MLdouble value) :
65  _name (name)
66  {
67  setValue(value);
68  }
69 
71  inline MLImageFormatTag(const std::string &name, MLldouble value) :
72  _name (name)
73  {
74  setValue(value);
75  }
76 
79  {
80  // Assign only if different.
81  if (&tag != this){
82  _name = tag._name ;
83  _value = tag._value;
84  }
85  return *this;
86  }
88 
89 
90  //------------------------------------------------------------------
93  //------------------------------------------------------------------
95  inline const std::string &getName() const { return _name; }
96 
98  inline const std::string &getValue() const { return _value; }
99 
101  inline void setName(const std::string &newName){ _name = newName; }
103 
104 
105  //------------------------------------------------------------------
108  //------------------------------------------------------------------
110  inline void setValue(const std::string &newVal)
111  {
112  _value = newVal;
113  }
114 
116  inline void setValue(MLint newVal)
117  {
118  char strBuf[128]="";
119  snprintf(strBuf, 127, "%lld", newVal);
120  _value = strBuf;
121  }
122 
124  inline void setValue(MLdouble newVal)
125  {
126  char frmt[256]="";
127  char bufferStr[512]="";
128 
129  // Create the format string with full number of digits.
130  snprintf(frmt, 255, "%%.%dg", DBL_DIG);
131 
132  // Print the long double value with full number of digits.
133  snprintf(bufferStr, 511, frmt, newVal);
134  _value = bufferStr;
135  }
136 
138  inline void setValue(MLldouble newVal)
139  {
140  char frmt[256]="";
141  char bufferStr[512]="";
142 
143  // Create the format string with full number of digits.
144  snprintf(frmt, 255, "%%.%dLg", LDBL_DIG);
145 
146  // Print the long double value with full number of digits.
147  snprintf(bufferStr, 511, frmt, newVal);
148  _value = bufferStr;
149  }
150 
152  inline size_t getTagSize() const
153  {
154  return _name.size() + _value.size() + 2;
155  }
156 
160  inline void writeNameAndValueIntoMemory(char *memPos) const
161  {
162  const size_t nameSize = _name .size();
163  const size_t valueSize = _value.size();
164  memcpy(memPos , _name .c_str(), nameSize ); memPos[nameSize ] = '\0';
165  memcpy(memPos+nameSize+1, _value.c_str(), valueSize); memPos[nameSize+1+valueSize] = '\0';
166  }
167 
186  inline const char * setNameAndValueFromMemory(const char* mem, const char* lastValidMemByte)
187  {
188  // Get start and end of buffers to parse content safely.
189  const char * bufSeek = mem;
190 
191  // Search first terminating character.
192  int numTerms = 0;
193  const char *firstTerm = nullptr;
194  const char *secondTerm = nullptr;
195  for (; (bufSeek <= lastValidMemByte) && (numTerms < 2); ++bufSeek){
196  if (*bufSeek == 0){
197  // Found character is a null-terminating character. Save it
198  // as firstTerm if firstTerm is still NULL, otherwise save
199  // it pointer to the second terminating character. Also
200  // increase the number of found terminating characters.
201  // We need 2.
202  numTerms++;
203  if (firstTerm && !secondTerm){ secondTerm = bufSeek; }
204  if (!firstTerm ){ firstTerm = bufSeek; }
205  }
206  } // for
207 
208  // Do we have two terminators found? If yes, then assign both values
209  // which is safe then.
210  if ((2 == numTerms) && firstTerm && secondTerm){
211  _name = mem;
212  _value = firstTerm+1;
213  }
214 
215  // Return pointer to second termination null or NULL on failure.
216  return secondTerm;
217  }
219 
220 
221 
222  //------------------------------------------------------------------
225  //------------------------------------------------------------------
227  inline const std::string &getStringValue() const
228  {
229  return _value;
230  }
231 
233  inline MLint getMLintValue() const
234  {
235  // Print the long double value with full number of digits.
236  MLint intVal=0;
237  int numRead = sscanf(_value.c_str(), "%lld", &intVal);
238  return (numRead == 1) ? intVal : 0;
239  }
240 
242  inline MLdouble getDoubleValue() const
243  {
244  // Scan for a double value.
245  MLdouble dblVal=0;
246  int numRead = sscanf(_value.c_str(), "%lf", &dblVal);
247  return (numRead == 1) ? dblVal : 0;
248  }
249 
252  {
253  // Scan for a long double value.
254  MLldouble ldblVal=0;
255  int numRead = sscanf(_value.c_str(), "%Lf", &ldblVal);
256  return (numRead == 1) ? ldblVal : 0;
257  }
259 
260  protected:
262  std::string _name;
263 
265  std::string _value;
266  };
267 
268 
269 
270  //----------------------------------------------------------------------
272  //----------------------------------------------------------------------
273  inline bool operator==(const MLImageFormatTag &tag1, const MLImageFormatTag &tag2)
274  {
275  // Assign only if different.
276  return (tag1.getName() == tag2.getName() ) &&
277  (tag1.getValue() == tag2.getValue());
278  }
279 
280  //----------------------------------------------------------------------
282  //----------------------------------------------------------------------
283  inline bool operator!=(const MLImageFormatTag &tag1, const MLImageFormatTag &tag2)
284  {
285  // Assign only if different.
286  return (tag1.getName() != tag2.getName() ) ||
287  (tag1.getValue() != tag2.getValue());
288  }
289 
290 
291 ML_END_NAMESPACE
292 
293 #endif // of __mlImageFormat_H
294 
#define MLIMAGEFORMATBASE_EXPORT
Global and OS specific declarations for the MLImageFormatBase project.
Class defining a tag used in the MLImageFormat class.
MLldouble getLongDoubleValue() const
Method to get the value as a long double or 0 on failure.
const std::string & getValue() const
Returns the value as string.
MLdouble getDoubleValue() const
Method to get the value as a double or 0 on failure.
std::string _name
The name of the tag to be stored in a file; default is "".
MLint getMLintValue() const
Method to get the value as an MLint or 0 on failure.
MLImageFormatTag(const MLImageFormatTag &tag)
Copy constructor.
void setValue(MLldouble newVal)
Method to set the value from a long double.
size_t getTagSize() const
Return the size of name and value strings + 2 for the two terminating nulls.
const char * setNameAndValueFromMemory(const char *mem, const char *lastValidMemByte)
Set tag and value from a memory buffer which is assumed to contain two null-terminated strings direct...
void setValue(const std::string &newVal)
Method to set the value from a string.
const std::string & getName() const
Returns the tag name as string.
void setValue(MLdouble newVal)
Method to set the value from a double.
void setName(const std::string &newName)
Method to set the value from an MLint64.
MLImageFormatTag(const std::string &name, MLint value)
Constructor from name and MLint.
MLImageFormatTag(const std::string &name, MLldouble value)
Constructor from name and MLldouble.
MLImageFormatTag(const std::string &name, const std::string &value)
Constructor from name and value.
std::string _value
The value of the tag to be stored in a file; default is "".
const std::string & getStringValue() const
Method to set the value from an MLint64.
void writeNameAndValueIntoMemory(char *memPos) const
Write name and tag terminated with null characters to the memory position memPos.
MLImageFormatTag(const std::string &name, MLdouble value)
Constructor from name and MLdouble.
MLImageFormatTag()
Default constructor, setting name and value to empty strings.
MLImageFormatTag & operator=(const MLImageFormatTag &tag)
Assignment operator.
void setValue(MLint newVal)
Method to set the value from an MLint64.
long double MLldouble
Definition: mlTypeDefs.h:238
double MLdouble
Definition: mlTypeDefs.h:223
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition: mlTypeDefs.h:578
bool operator==(const MLImageFormatTag &tag1, const MLImageFormatTag &tag2)
Equality operator.
bool operator!=(const MLImageFormatTag &tag1, const MLImageFormatTag &tag2)
Inequality operator.