itom
Loading...
Searching...
No Matches
dataObjectFuncs.h
1/* ********************************************************************
2 itom software
3 URL: http://www.uni-stuttgart.de/ito
4 Copyright (C) 2020, Institut für Technische Optik (ITO),
5 Universität 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 für 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
35namespace ito
36{
37namespace 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 &dataObjIn, ito::DataObject &dataObjOut, const int copyLastNDims, const bool includeValueTags = true, const bool includeRotationMatrix = true);
279
281 /* The values are adjusted, such that the minimum physical x-coordinate is equal
282 to x0 and the maximum one to x1. The same holds for the y-direction and the
283 boundaries y0 and y1.
284
285 \param dataObj is the 2dim dataObject to be adjusted.
286 \param x0 is the minimum physical x-coordinate
287 \param x0 is the maximum physical x-coordinate
288 \param x0 is the minimum physical y-coordinate
289 \param x0 is the maximum physical y-coordinate
290 */
291 DATAOBJ_EXPORT ito::RetVal dObjSetScaleRectangle(ito::DataObject &dataObj, const double &x0, const double &x1, const double &y0, const double &y1);
292
293
294 //-----------------------------------------------------------------------------------------------
307 template<typename _Tp> ito::RetVal freeRowPointer(_Tp *** &pointer)
308 {
309 if(pointer == NULL)
310 {
311 return ito::RetVal::format(ito::retError, 0, "Delete rowpointer failed, pointer was not allocated freeRowPointer(...)");
312 }
313
314 int i = 0;
315
316 while(pointer[i] != 0)
317 {
318 free(pointer[i]);
319 i++;
320 // end of pointer is marked by a NULL-Pointer
321 }
322
323 free(pointer);
324 pointer = NULL;
325
326 return ito::retOk;
327 }
328
329 //--------------------------------------------------------------------------------------------------------
344 template<typename _Tp> ito::RetVal getRowPointer(ito::DataObject* dObj, _Tp *** &pointer)
345 {
346
347 if(!dObj)
348 {
349 return ito::RetVal::format(ito::retError, 0, "DataObject was a NULL pointer in function getRowPointer(...)");
350 }
351
352 if(dObj->getType() != ito::getDataType2<_Tp*>())
353 {
354 return ito::RetVal::format(ito::retError, 0, "DataObject and template Type differed in function getRowPointer(...)");
355 }
356
357 if(dObj->getDims() < 2)
358 {
359 return ito::RetVal::format(ito::retError, 0, "DataObject was not initialized in function getRowPointer(...)");
360 }
361
362 pointer = (_Tp***)calloc(dObj->getNumPlanes() + 1, sizeof(_Tp**)); //all bytes are allocated to zero
363
364 if(pointer == NULL)
365 {
366 return ito::RetVal::format(ito::retError, 0, "Allocate plane-pointer failed in function getRowPointer(...)");
367 }
368
369 ito::RetVal retVal(ito::retOk);
370 int sizeY = dObj->getSize(dObj->getDims() - 2);
371
372 cv::Mat** mdata = dObj->get_mdata();
373 int numMats = dObj->getNumPlanes();
374 for(int i = 0; i < dObj->getNumPlanes(); i++)
375 {
376 pointer[i] = (_Tp**)calloc(sizeY, sizeof(_Tp*));
377 if(pointer[i])
378 {
379 for(int y = 0; y < sizeY; y++)
380 {
381 pointer[i][y] = mdata[dObj->seekMat(i, numMats)]->ptr<_Tp>(y);
382 }
383 }
384 else
385 {
386 retVal += ito::RetVal::format(ito::retError, 0, "Allocate row-pointer failed in function getRowPointer(...)");
387 break;
388 }
389 }
390
391 if(retVal.containsError())
392 {
393 freeRowPointer(pointer);
394 }
395 return retVal;
396 }
397
398
399 //-------------------------------------------------------------------------------------------------------------------
400 //
401 // LEGACY FUNCTIONS. DON'T USE THEM IN FUTURE CODE
402 //
403 //-------------------------------------------------------------------------------------------------------------------
404
406 template<typename _Tp> void GetHLineL(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData);
407
409 template<typename _Tp> void GetHLineD(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData);
410
412 template<typename _Tp> void GetHLineC(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData);
413
415 template<typename _Tp> void SetHLineL(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData);
416
418 template<typename _Tp> void SetHLineD(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData);
419
420//-----------------------------------------------------------------------------------------------
432 template<typename _Tp> inline void GetHLineL(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData)
433 {
434 _Tp* resRowPtr;
435 resRowPtr = (_Tp*)(srcPlane->ptr(y));
436
437 int x = x0;
438 for(int i = 0; i < length; i++, x++)
439 {
440 pData[i] = (int32)(resRowPtr[x]);
441 }
442 }
443
444 //-----------------------------------------------------------------------------------------------
455 template<> inline void GetHLineL<int32>(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData)
456 {
457 int32* resRowPtr;
458 resRowPtr = (int32*)(srcPlane->ptr(y));
459 memcpy(pData, &resRowPtr[x0], length *sizeof(int32));
460 }
461
462 //-----------------------------------------------------------------------------------------------
474 template<typename _Tp> inline void GetHLineD(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData)
475 {
476 _Tp* resRowPtr;
477 resRowPtr = (_Tp*)(srcPlane->ptr(y));
478
479 int x = x0;
480 for(int i = 0; i < length; i++, x++)
481 {
482 pData[i] = cv::saturate_cast<ito::float64>(resRowPtr[x]);
483 }
484 }
485
486 //-----------------------------------------------------------------------------------------------
497 template<> inline void GetHLineD<ito::float64>(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData)
498 {
499 float64* resRowPtr;
500 resRowPtr = (float64*)(srcPlane->ptr(y));
501 memcpy(pData, &resRowPtr[x0], length *sizeof(float64));
502 }
503
504 //-----------------------------------------------------------------------------------------------
516 template<typename _Tp> inline void GetHLineC(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
517 {
518 const _Tp* resRowPtr = srcPlane->ptr<_Tp>(y);
519
520 int x = x0;
521 for(int i = 0; i < length; i++, x++)
522 {
523 pData[i] = complex128(cv::saturate_cast<ito::float64>(resRowPtr[x]), 0.0);
524 }
525 }
526
527 //-----------------------------------------------------------------------------------------------
538 template<> inline void GetHLineC<complex64>(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
539 {
540 const complex64* resRowPtr = srcPlane->ptr<complex64>(y);
541
542 int x = x0;
543 for(int i = 0; i < length; i++, x++)
544 {
545 pData[i] = complex128(resRowPtr[x].real(), resRowPtr[x].imag());
546 }
547 }
548
549 //-----------------------------------------------------------------------------------------------
560 template<> inline void GetHLineC<complex128>(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
561 {
562 const complex128* resRowPtr = srcPlane->ptr<complex128>(y);
563 memcpy(pData, &resRowPtr[x0], length *sizeof(complex128));
564 }
565
566 //-----------------------------------------------------------------------------------------------
578 template<typename _Tp> inline void SetHLineL(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData)
579 {
580 _Tp* resRowPtr;
581 resRowPtr = (_Tp*)(destPlane->ptr(y));
582
583 int x = x0;
584 for(int i = 0; i < length; i++, x++)
585 {
586 resRowPtr[x] = (_Tp)(pData[i]);
587 }
588 }
589
590 //-----------------------------------------------------------------------------------------------
602 template<> inline void SetHLineL<int32>(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData)
603 {
604 int32* resRowPtr;
605 resRowPtr = (int32*)(destPlane->ptr(y));
606 memcpy(&resRowPtr[x0], pData, length *sizeof(int32));
607 }
608
609 //-----------------------------------------------------------------------------------------------
621 template<typename _Tp> inline void SetHLineD(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData)
622 {
623 _Tp* resRowPtr;
624 resRowPtr = (_Tp*)destPlane->ptr(y);
625
626 int x = x0;
627 for(int i = 0; i < length; i++, x++)
628 {
629 resRowPtr[x] = cv::saturate_cast<_Tp>(pData[i]);
630 }
631 }
632
633 //-----------------------------------------------------------------------------------------------
645 template<> inline void SetHLineD<ito::float64>(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData)
646 {
647 float64* resRowPtr;
648 resRowPtr = (float64*)(destPlane->ptr(y));
649 memcpy(&resRowPtr[x0], pData, length *sizeof(float64));
650 }
651
652 //----------------------------------------------------------------------------------------------------------------------------------
659 inline int itomType2cvType(const int type)
660 {
661 switch(type)
662 {
663 case ito::tUInt8:
664 return CV_8U;
665 case ito::tUInt16:
666 return CV_16U;
667 case ito::tInt8:
668 return CV_8S;
669 case ito::tInt16:
670 return CV_16S;
671 case ito::tInt32:
672 return CV_32S;
673 case ito::tFloat32:
674 return CV_32F;
675 case ito::tFloat64:
676 return CV_64F;
677 default: //ito::tUInt32 and complextype
678 return -1;
679 }
680 }
681 //----------------------------------------------------------------------------------------------------------------------------------
688 inline int cvType2itomType(const int type)
689 {
690 switch(type)
691 {
692 case CV_8U:
693 return ito::tUInt8;
694 case CV_16U:
695 return ito::tUInt16;
696 case CV_8S:
697 return ito::tInt8;
698 case CV_16S:
699 return ito::tInt16;
700 case CV_32S:
701 return ito::tInt32;
702 case CV_32F:
703 return ito::tFloat32;
704 case CV_64F:
705 return ito::tFloat64;
706 default:
707 return -1;
708 }
709 }
710
711} //end namespace dObjHelper
712} //end namespace ito
713
714#endif
dataObject contains a n-dimensional matrix
Definition dataobj.h:591
int getDims(void) const
Definition dataobj.h:882
int seekMat(const int matNum, const int numMats) const
Definition dataobj.cpp:1557
MSize getSize(void)
returns the size-member. m_size fits to the physical organization of data in memory.
Definition dataobj.h:976
int getNumPlanes(void) const
calculates numbers of single opencv matrices which are part of the ROI which has previously been set.
Definition dataobj.h:908
cv::Mat ** get_mdata(void)
returns array of pointers to cv::_Mat-matrices (planes) of the data object.
Definition dataobj.h:952
int getType(void) const
Definition dataobj.h:885
each range value has a start and end point. Optionally range can be marked as Range::all(),...
Definition dataobj.h:332
Class for managing status values (like errors or warning)
Definition retVal.h:54
static RetVal format(ito::tRetValue retValue, int retCode, const char *pRetMessage,...)
Definition retVal.cpp:69
Definition apiFunctionsGraph.cpp:40
ito::tDataType getDataType2()
method which returns the value of enumeration ito::tDataType, which corresponds to the template param...
Definition dataobj.h:1703
DataObject imag(const DataObject &dObj)
Definition dataobj.cpp:13456
tDataType
Definition typeDefs.h:88
@ tInt16
Definition typeDefs.h:91
@ tFloat32
Definition typeDefs.h:95
@ tInt8
Definition typeDefs.h:89
@ tComplex128
Definition typeDefs.h:98
@ tInt32
Definition typeDefs.h:93
@ tUInt16
Definition typeDefs.h:92
@ tFloat64
Definition typeDefs.h:96
@ tComplex64
Definition typeDefs.h:97
@ tUInt8
Definition typeDefs.h:90
@ tUInt32
Definition typeDefs.h:94
DataObject real(const DataObject &dObj)
Definition dataobj.cpp:13020
@ retError
Definition typeDefs.h:60
@ retOk
Definition typeDefs.h:58