itom  4.1.0
dataObjectFuncs.h
1 /* ********************************************************************
2  itom software
3  URL: http://www.uni-stuttgart.de/ito
4  Copyright (C) 2020, Institut fuer Technische Optik (ITO),
5  Universitaet Stuttgart, Germany
6 
7  This file is part of itom and its software development toolkit (SDK).
8 
9  itom is free software; you can redistribute it and/or modify it
10  under the terms of the GNU Library General Public Licence as published by
11  the Free Software Foundation; either version 2 of the Licence, or (at
12  your option) any later version.
13 
14  In addition, as a special exception, the Institut fuer Technische
15  Optik (ITO) gives you certain additional rights.
16  These rights are described in the ITO LGPL Exception version 1.0,
17  which can be found in the file LGPL_EXCEPTION.txt in this package.
18 
19  itom is distributed in the hope that it will be useful, but
20  WITHOUT ANY WARRANTY; without even the implied warranty of
21  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
22  General Public Licence for more details.
23 
24  You should have received a copy of the GNU Library General Public License
25  along with itom. If not, see <http://www.gnu.org/licenses/>.
26 *********************************************************************** */
27 
28 #ifndef __DATAOBJFUNCH
29 #define __DATAOBJFUNCH
30 
31 #include "../common/typeDefs.h"
32 #include "dataobj.h"
33 #include <assert.h> /* assert */
34 
35 namespace ito
36 {
37 namespace dObjHelper
38 {
40  enum CmplxSelectionFlags
41  {
42  CMPLX_ABS_VALUE = 0,
43  CMPLX_IMAGINARY_VALUE = 1,
44  CMPLX_REAL_VALUE = 2,
45  CMPLX_ARGUMENT_VALUE = 3
46  };
47 
49  DATAOBJ_EXPORT RetVal minValue(const DataObject *dObj, float64 &minValue, uint32 *firstLocation, bool ignoreInf = true);
50 
52  DATAOBJ_EXPORT RetVal maxValue(const DataObject *dObj, float64 &maxValue, uint32 *firstLocation, bool ignoreInf = true);
53 
55  DATAOBJ_EXPORT RetVal minMaxValue(const DataObject *dObj, float64 &minValue, uint32 *firstMinLocation, float64 &maxValue, uint32 *firstMaxLocation, bool ignoreInf = true, const int specialDataTypeFlags = CMPLX_ABS_VALUE);
56 
57  DATAOBJ_EXPORT RetVal meanValue(const DataObject *dObj, float64 &meanResult, bool ignoreNaN = true);
58 
59  DATAOBJ_EXPORT RetVal medianValue(const DataObject *dObj, float64 &medianResult, bool ignoreNaN = true);
60 
61  DATAOBJ_EXPORT RetVal devValue(const DataObject *dObj, const int devTypFlag, float64 &meanResult, float64 &devResult, bool ignoreNaN = true);
62 
63  DATAOBJ_EXPORT RetVal calcCVDFT(DataObject *dObjIO, const bool inverse, const bool inverseAsReal, const bool lineWise);
64 
66  DATAOBJ_EXPORT RetVal verifyDataObjectType(const DataObject* dObj, const char* name, uint8 numberOfAllowedTypes, ...); //append allowed data types, e.g. ito::tUint8, ito::tInt8... (order does not care)
67 
69  DATAOBJ_EXPORT RetVal verify2DDataObject(const DataObject* dObj, const char* name, int sizeYMin, int sizeYMax, int sizeXMin, int sizeXMax, uint8 numberOfAllowedTypes, ...); //append allowed data types, e.g. ito::tUint8, ito::tInt8... (order does not care)
70 
72  //ito::RetVal DATAOBJ_EXPORT verify1PlaneDObject(const ito::DataObject* dObj, const char* name, int &yIdx, int sizeYMin, int sizeYMax, int &xIdx, int sizeXMin, int sizeXMax, uint8 numberOfAllowedTypes, ...); //append allowed data types, e.g. ito::tUint8, ito::tInt8... (order does not care)
73 
75  DATAOBJ_EXPORT RetVal verify3DDataObject(const DataObject* dObj, const char* name, int sizeZMin, int sizeZMax, int sizeYMin, int sizeYMax, int sizeXMin, int sizeXMax, uint8 numberOfAllowedTypes, ...); //append allowed data types, e.g. ito::tUint8, ito::tInt8... (order does not care)
76 
82  DATAOBJ_EXPORT RetVal verifySize(int size, int minSize, int maxSize, const char *axisName, const char* dObjName);
83 
85  DATAOBJ_EXPORT ito::DataObject squeezeConvertCheck2DDataObject(const ito::DataObject *dObj, const char* name, const ito::Range &sizeY, const ito::Range &sizeX, ito::RetVal &retval, int convertToType, uint8 numberOfAllowedTypes, ...);
86 
88  DATAOBJ_EXPORT ito::DataObject* createFromNamedDataObject(const ito::DataObject *dObj, int nrDims, ito::tDataType type, const char *name = NULL, int *sizeLimits = NULL, ito::RetVal *retval = NULL);
89 
90 
91  //-----------------------------------------------------------------------------------------------
104  inline bool dObjareEqualDetail(const ito::DataObject *dObj1, const ito::DataObject *dObj2, bool &typeFlag, bool &dimsFlag, bool &last2DimsFlag)
105  {
106  assert(dObj1 && dObj2);
107 
108  bool sizeEqual;
109 
110  last2DimsFlag = true;
111 
112  int d1 = dObj1->getDims();
113  int d2 = dObj2->getDims();
114  dimsFlag = (d1 == d2);
115  typeFlag = (dObj1->getType() == dObj2->getType());
116 
117  if(d1 != d2)
118  {
119  sizeEqual = false;
120 
121  if (d1 >= 2 && d2 >= 2)
122  {
123  if(dObj1->getSize(dObj1->getDims() - 2) != dObj2->getSize(dObj2->getDims() - 2))
124  {
125  last2DimsFlag = false;
126  }
127  else if(dObj1->getSize(dObj1->getDims() - 1) != dObj2->getSize(dObj2->getDims() - 1))
128  {
129  last2DimsFlag = false;
130  }
131  }
132  else
133  {
134  last2DimsFlag = false;
135  }
136  }
137  else
138  {
139  sizeEqual = (dObj1->getSize() == dObj2->getSize());
140 
141  if (d1 >= 2)
142  {
143  if(dObj1->getSize(dObj1->getDims() - 2) != dObj2->getSize(dObj2->getDims() - 2))
144  {
145  last2DimsFlag = false;
146  }
147  else if(dObj1->getSize(dObj1->getDims() - 1) != dObj2->getSize(dObj2->getDims() - 1))
148  {
149  last2DimsFlag = false;
150  }
151  }
152  else
153  {
154  last2DimsFlag = false;
155  }
156  }
157  return typeFlag && dimsFlag && last2DimsFlag && sizeEqual;
158  }
159 
160  //-----------------------------------------------------------------------------------------------
167  inline bool dObjareEqualShort(const ito::DataObject *obj1, const ito::DataObject *obj2)
168  {
169  assert(obj1 && obj2);
170  return (obj1->getType() == obj2->getType()) && (obj1->getSize() == obj2->getSize());
171  }
172 
173  //--------------------------------------------------------------------------------------------
175 
186  DATAOBJ_EXPORT std::string invertUnit(const std::string &oldUnit);
187 
188  //-----------------------------------------------------------------------------------------------
197  inline bool isIntType(const ito::tDataType type, int *size)
198  {
199  switch (type)
200  {
201  case ito::tInt8:
202  case ito::tUInt8:
203  *size = 1;
204  return true;
205  case ito::tInt16:
206  case ito::tUInt16:
207  *size = 2;
208  return true;
209  case ito::tInt32:
210  case ito::tUInt32:
211  *size = 4;
212  return true;
213  default:
214  *size = -1;
215  return false;
216  }
217  }
218 
219  //-----------------------------------------------------------------------------------------------
228  inline bool isFPType(const ito::tDataType type, int *size)
229  {
230  if((type == ito::tFloat32))
231  {
232  *size = 4;
233  return true;
234  }
235  else if((type == ito::tFloat64))
236  {
237  *size = 8;
238  return true;
239  }
240  else
241  {
242  *size = -1;
243  return false;
244  }
245  }
246 
247  //-----------------------------------------------------------------------------------------------
256  inline bool isCplxType(const ito::tDataType type, int *size)
257  {
258  if((type == ito::tComplex64))
259  {
260  *size = 8;
261  return true;
262  }
263  else if((type == ito::tComplex128))
264  {
265  *size = 16;
266  return true;
267  }
268  else
269  {
270  *size = -1;
271  return false;
272  }
273  }
274 
275 
276 
278  DATAOBJ_EXPORT ito::RetVal dObjCopyLastNAxisTags(const ito::DataObject &DataObjectIn, ito::DataObject &DataObjectOut, const int copyLastNDims, const bool includeValueTags = true, const bool includeRotationMatrix = true);
279 
280  DATAOBJ_EXPORT ito::RetVal dObjSetScaleRectangle(ito::DataObject &DataObjectInOut, const double &x0, const double &x1, const double &y0, const double &y1);
281 
282 
283  //-----------------------------------------------------------------------------------------------
296  template<typename _Tp> ito::RetVal freeRowPointer(_Tp *** &pointer)
297  {
298  if(pointer == NULL)
299  {
300  return ito::RetVal::format(ito::retError, 0, "Delete rowpointer failed, pointer was not allocated freeRowPointer(...)");
301  }
302 
303  int i = 0;
304 
305  while(pointer[i] != 0)
306  {
307  free(pointer[i]);
308  i++;
309  // end of pointer is marked by a NULL-Pointer
310  }
311 
312  free(pointer);
313  pointer = NULL;
314 
315  return ito::retOk;
316  }
317 
318  //--------------------------------------------------------------------------------------------------------
333  template<typename _Tp> ito::RetVal getRowPointer(ito::DataObject* dObj, _Tp *** &pointer)
334  {
335 
336  if(!dObj)
337  {
338  return ito::RetVal::format(ito::retError, 0, "DataObject was a NULL pointer in function getRowPointer(...)");
339  }
340 
341  if(dObj->getType() != ito::getDataType2<_Tp*>())
342  {
343  return ito::RetVal::format(ito::retError, 0, "DataObject and template Type differed in function getRowPointer(...)");
344  }
345 
346  if(dObj->getDims() < 2)
347  {
348  return ito::RetVal::format(ito::retError, 0, "DataObject was not initialized in function getRowPointer(...)");
349  }
350 
351  pointer = (_Tp***)calloc(dObj->getNumPlanes() + 1, sizeof(_Tp**)); //all bytes are allocated to zero
352 
353  if(pointer == NULL)
354  {
355  return ito::RetVal::format(ito::retError, 0, "Allocate plane-pointer failed in function getRowPointer(...)");
356  }
357 
358  ito::RetVal retVal(ito::retOk);
359  int sizeY = dObj->getSize(dObj->getDims() - 2);
360 
361  cv::Mat** mdata = dObj->get_mdata();
362  int numMats = dObj->getNumPlanes();
363  for(int i = 0; i < dObj->getNumPlanes(); i++)
364  {
365  pointer[i] = (_Tp**)calloc(sizeY, sizeof(_Tp*));
366  if(pointer[i])
367  {
368  for(int y = 0; y < sizeY; y++)
369  {
370  pointer[i][y] = mdata[dObj->seekMat(i, numMats)]->ptr<_Tp>(y);
371  }
372  }
373  else
374  {
375  retVal += ito::RetVal::format(ito::retError, 0, "Allocate row-pointer failed in function getRowPointer(...)");
376  break;
377  }
378  }
379 
380  if(retVal.containsError())
381  {
382  freeRowPointer(pointer);
383  }
384  return retVal;
385  }
386 
387 
388  //-------------------------------------------------------------------------------------------------------------------
389  //
390  // LEGACY FUNCTIONS. DON'T USE THEM IN FUTURE CODE
391  //
392  //-------------------------------------------------------------------------------------------------------------------
393 
395  template<typename _Tp> void GetHLineL(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData);
396 
398  template<typename _Tp> void GetHLineD(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData);
399 
401  template<typename _Tp> void GetHLineC(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData);
402 
404  template<typename _Tp> void SetHLineL(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData);
405 
407  template<typename _Tp> void SetHLineD(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData);
408 
409 //-----------------------------------------------------------------------------------------------
421  template<typename _Tp> inline void GetHLineL(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData)
422  {
423  _Tp* resRowPtr;
424  resRowPtr = (_Tp*)(srcPlane->ptr(y));
425 
426  int x = x0;
427  for(int i = 0; i < length; i++, x++)
428  {
429  pData[i] = (int32)(resRowPtr[x]);
430  }
431  }
432 
433  //-----------------------------------------------------------------------------------------------
444  template<> inline void GetHLineL<int32>(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData)
445  {
446  int32* resRowPtr;
447  resRowPtr = (int32*)(srcPlane->ptr(y));
448  memcpy(pData, &resRowPtr[x0], length *sizeof(int32));
449  }
450 
451  //-----------------------------------------------------------------------------------------------
463  template<typename _Tp> inline void GetHLineD(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData)
464  {
465  _Tp* resRowPtr;
466  resRowPtr = (_Tp*)(srcPlane->ptr(y));
467 
468  int x = x0;
469  for(int i = 0; i < length; i++, x++)
470  {
471  pData[i] = cv::saturate_cast<ito::float64>(resRowPtr[x]);
472  }
473  }
474 
475  //-----------------------------------------------------------------------------------------------
486  template<> inline void GetHLineD<ito::float64>(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData)
487  {
488  float64* resRowPtr;
489  resRowPtr = (float64*)(srcPlane->ptr(y));
490  memcpy(pData, &resRowPtr[x0], length *sizeof(float64));
491  }
492 
493  //-----------------------------------------------------------------------------------------------
505  template<typename _Tp> inline void GetHLineC(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
506  {
507  const _Tp* resRowPtr = srcPlane->ptr<_Tp>(y);
508 
509  int x = x0;
510  for(int i = 0; i < length; i++, x++)
511  {
512  pData[i] = complex128(cv::saturate_cast<ito::float64>(resRowPtr[x]), 0.0);
513  }
514  }
515 
516  //-----------------------------------------------------------------------------------------------
527  template<> inline void GetHLineC<complex64>(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
528  {
529  const complex64* resRowPtr = srcPlane->ptr<complex64>(y);
530 
531  int x = x0;
532  for(int i = 0; i < length; i++, x++)
533  {
534  pData[i] = complex128(resRowPtr[x].real(), resRowPtr[x].imag());
535  }
536  }
537 
538  //-----------------------------------------------------------------------------------------------
549  template<> inline void GetHLineC<complex128>(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
550  {
551  const complex128* resRowPtr = srcPlane->ptr<complex128>(y);
552  memcpy(pData, &resRowPtr[x0], length *sizeof(complex128));
553  }
554 
555  //-----------------------------------------------------------------------------------------------
567  template<typename _Tp> inline void SetHLineL(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData)
568  {
569  _Tp* resRowPtr;
570  resRowPtr = (_Tp*)(destPlane->ptr(y));
571 
572  int x = x0;
573  for(int i = 0; i < length; i++, x++)
574  {
575  resRowPtr[x] = (_Tp)(pData[i]);
576  }
577  }
578 
579  //-----------------------------------------------------------------------------------------------
591  template<> inline void SetHLineL<int32>(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData)
592  {
593  int32* resRowPtr;
594  resRowPtr = (int32*)(destPlane->ptr(y));
595  memcpy(&resRowPtr[x0], pData, length *sizeof(int32));
596  }
597 
598  //-----------------------------------------------------------------------------------------------
610  template<typename _Tp> inline void SetHLineD(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData)
611  {
612  _Tp* resRowPtr;
613  resRowPtr = (_Tp*)destPlane->ptr(y);
614 
615  int x = x0;
616  for(int i = 0; i < length; i++, x++)
617  {
618  resRowPtr[x] = cv::saturate_cast<_Tp>(pData[i]);
619  }
620  }
621 
622  //-----------------------------------------------------------------------------------------------
634  template<> inline void SetHLineD<ito::float64>(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData)
635  {
636  float64* resRowPtr;
637  resRowPtr = (float64*)(destPlane->ptr(y));
638  memcpy(&resRowPtr[x0], pData, length *sizeof(float64));
639  }
640 
641  //----------------------------------------------------------------------------------------------------------------------------------
648  inline int itomType2cvType(const int type)
649  {
650  switch(type)
651  {
652  case ito::tUInt8:
653  return CV_8U;
654  case ito::tUInt16:
655  return CV_16U;
656  case ito::tInt8:
657  return CV_8S;
658  case ito::tInt16:
659  return CV_16S;
660  case ito::tInt32:
661  return CV_32S;
662  case ito::tFloat32:
663  return CV_32F;
664  case ito::tFloat64:
665  return CV_64F;
666  default: //ito::tUInt32 and complextype
667  return -1;
668  }
669  }
670  //----------------------------------------------------------------------------------------------------------------------------------
677  inline int cvType2itomType(const int type)
678  {
679  switch(type)
680  {
681  case CV_8U:
682  return ito::tUInt8;
683  case CV_16U:
684  return ito::tUInt16;
685  case CV_8S:
686  return ito::tInt8;
687  case CV_16S:
688  return ito::tInt16;
689  case CV_32S:
690  return ito::tInt32;
691  case CV_32F:
692  return ito::tFloat32;
693  case CV_64F:
694  return ito::tFloat64;
695  default:
696  return -1;
697  }
698  }
699 
700 } //end namespace dObjHelper
701 } //end namespace ito
702 
703 #endif
dataObject contains a n-dimensional matrix
Definition: dataobj.h:511
MSize getSize(void)
returns the size-member. m_size fits to the physical organization of data in memory.
Definition: dataobj.h:885
static RetVal format(ito::tRetValue retValue, int retCode, const char *pRetMessage,...)
create RetVal with message that may contain placeholders using the formalism of the default sprintf m...
Definition: retVal.cpp:69
Definition: typeDefs.h:91
Definition: typeDefs.h:60
Definition: typeDefs.h:96
Class for managing status values (like errors or warning)
Definition: retVal.h:54
Definition: typeDefs.h:98
Definition: typeDefs.h:97
int getType(void) const
Definition: dataobj.h:794
Definition: typeDefs.h:94
Definition: typeDefs.h:58
Definition: typeDefs.h:89
DataObject imag(const DataObject &dObj)
high-level value which calculates the imaginary value of each element of the input source data object...
Definition: dataobj.cpp:10622
each range value has a start and end point. Optionally range can be marked as Range::all(), which indicates a full range
Definition: dataobj.h:252
Definition: apiFunctionsGraph.cpp:39
int getDims(void) const
Definition: dataobj.h:791
int seekMat(const int matNum, const int numMats) const
returns the index vector-index of m_data which corresponds to the given zero-based two-dimensional ma...
Definition: dataobj.cpp:1434
Definition: typeDefs.h:93
Definition: typeDefs.h:95
Definition: typeDefs.h:92
cv::Mat ** get_mdata(void)
returns array of pointers to cv::_Mat-matrices (planes) of the data object.
Definition: dataobj.h:861
DataObject real(const DataObject &dObj)
high-level value which calculates the real value of each element of the input source data object and ...
Definition: dataobj.cpp:10215
tDataType
Definition: typeDefs.h:87
int getNumPlanes(void) const
calculates numbers of single opencv matrices which are part of the ROI which has previously been set...
Definition: dataobj.h:817
Definition: typeDefs.h:90