itom 2.2.1
K:/git-itom/sources/itom/DataObject/dataObjectFuncs.h
00001 /* ********************************************************************
00002     itom software
00003     URL: http://www.uni-stuttgart.de/ito
00004     Copyright (C) 2016, Institut fuer Technische Optik (ITO),
00005     Universitaet Stuttgart, Germany
00006 
00007     This file is part of itom and its software development toolkit (SDK).
00008 
00009     itom is free software; you can redistribute it and/or modify it
00010     under the terms of the GNU Library General Public Licence as published by
00011     the Free Software Foundation; either version 2 of the Licence, or (at
00012     your option) any later version.
00013    
00014     In addition, as a special exception, the Institut fuer Technische
00015     Optik (ITO) gives you certain additional rights.
00016     These rights are described in the ITO LGPL Exception version 1.0,
00017     which can be found in the file LGPL_EXCEPTION.txt in this package.
00018 
00019     itom is distributed in the hope that it will be useful, but
00020     WITHOUT ANY WARRANTY; without even the implied warranty of
00021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library
00022     General Public Licence for more details.
00023 
00024     You should have received a copy of the GNU Library General Public License
00025     along with itom. If not, see <http://www.gnu.org/licenses/>.
00026 *********************************************************************** */
00027 
00028 #ifndef __DATAOBJFUNCH
00029 #define __DATAOBJFUNCH
00030 
00031 #include "../common/typeDefs.h"
00032 #include "dataobj.h"
00033 #include <assert.h>     /* assert */
00034 
00035 namespace ito 
00036 {
00037 namespace dObjHelper
00038 {
00039     enum CmplxSelectionFlags
00040     {
00041         CMPLX_ABS_VALUE         = 0,
00042         CMPLX_IMAGINARY_VALUE   = 1,
00043         CMPLX_REAL_VALUE        = 2,
00044         CMPLX_ARGUMENT_VALUE    = 3
00045     };
00046 
00048 
00059     std::string DATAOBJ_EXPORT invertUnit(const std::string &oldUnit);
00060 
00061     //----------------------------------------------------------------------------------------------------------------------------------
00068     inline int itomType2cvType(const int type)
00069     {
00070         switch(type)
00071         {
00072         case ito::tUInt8:
00073             return CV_8U;
00074         case ito::tUInt16:
00075             return CV_16U;
00076         case ito::tInt8:
00077             return CV_8S;
00078         case ito::tInt16:
00079             return CV_16S;
00080         case ito::tInt32:
00081             return CV_32S;
00082         case ito::tFloat32:
00083             return CV_32F;
00084         case ito::tFloat64:
00085             return CV_64F;
00086         default:    //ito::tUInt32 and complextype
00087             return -1;
00088         }
00089     }
00090     //----------------------------------------------------------------------------------------------------------------------------------
00097     inline int cvType2itomType(const int type)
00098     {
00099         switch(type)
00100         {
00101         case CV_8U:
00102             return ito::tUInt8;
00103         case CV_16U:
00104             return ito::tUInt16;
00105         case CV_8S:
00106             return ito::tInt8;
00107         case CV_16S:
00108             return ito::tInt16;
00109         case CV_32S:
00110             return ito::tInt32;
00111         case CV_32F:
00112             return ito::tFloat32;
00113         case CV_64F:
00114             return ito::tFloat64;
00115         default:
00116             return -1;
00117         }
00118     }
00119 
00121     template<typename _Tp> void GetHLineL(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData);
00122 
00124     template<typename _Tp> void GetHLineD(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData);
00125 
00127     template<typename _Tp> void GetHLineC(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData);
00128 
00130     template<typename _Tp> void SetHLineL(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData);
00131 
00133     template<typename _Tp> void SetHLineD(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData);
00134 
00135 //-----------------------------------------------------------------------------------------------
00147     template<typename _Tp> inline void GetHLineL(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData)
00148     {
00149         _Tp* resRowPtr;
00150         resRowPtr = (_Tp*)(srcPlane->ptr(y));
00151 
00152         int x = x0;
00153         for(int i = 0; i < length; i++, x++)
00154         {
00155             pData[i] = (int32)(resRowPtr[x]);
00156         }
00157     }
00158 
00159     //-----------------------------------------------------------------------------------------------
00170     template<> inline void GetHLineL<int32>(const cv::Mat *srcPlane, const int x0, const int y, const int length, int32 * pData)
00171     {
00172         int32* resRowPtr;
00173         resRowPtr = (int32*)(srcPlane->ptr(y));
00174         memcpy(pData, &resRowPtr[x0], length *sizeof(int32));
00175     }
00176 
00177     //-----------------------------------------------------------------------------------------------
00189     template<typename _Tp> inline void GetHLineD(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData)
00190     {
00191         _Tp* resRowPtr;
00192         resRowPtr = (_Tp*)(srcPlane->ptr(y));
00193 
00194         int x = x0;
00195         for(int i = 0; i < length; i++, x++)
00196         {
00197             pData[i] = cv::saturate_cast<ito::float64>(resRowPtr[x]);
00198         }
00199     }
00200 
00201     //-----------------------------------------------------------------------------------------------
00212     template<> inline void GetHLineD<ito::float64>(const cv::Mat *srcPlane, const int x0, const int y, const int length, float64 * pData)
00213     {
00214         float64* resRowPtr;
00215         resRowPtr = (float64*)(srcPlane->ptr(y));
00216         memcpy(pData, &resRowPtr[x0], length *sizeof(float64));
00217     }
00218 
00219         //-----------------------------------------------------------------------------------------------
00231     template<typename _Tp> inline void GetHLineC(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
00232     {
00233         const _Tp* resRowPtr = srcPlane->ptr<_Tp>(y);
00234 
00235         int x = x0;
00236         for(int i = 0; i < length; i++, x++)
00237         {
00238             pData[i] = complex128(cv::saturate_cast<ito::float64>(resRowPtr[x]), 0.0);
00239         }
00240     }
00241 
00242     //-----------------------------------------------------------------------------------------------
00253     template<> inline void GetHLineC<complex64>(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
00254     {
00255         const complex64* resRowPtr = srcPlane->ptr<complex64>(y);
00256 
00257         int x = x0;
00258         for(int i = 0; i < length; i++, x++)
00259         {
00260             pData[i] = complex128(resRowPtr[x].real(), resRowPtr[x].imag());
00261         }
00262     }
00263 
00264     //-----------------------------------------------------------------------------------------------
00275     template<> inline void GetHLineC<complex128>(const cv::Mat *srcPlane, const int x0, const int y, const int length, complex128 * pData)
00276     {
00277         const complex128* resRowPtr = srcPlane->ptr<complex128>(y);
00278         memcpy(pData, &resRowPtr[x0], length *sizeof(complex128));
00279     }
00280 
00281     //-----------------------------------------------------------------------------------------------
00293     template<typename _Tp> inline void SetHLineL(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData)
00294     {
00295         _Tp* resRowPtr;
00296         resRowPtr = (_Tp*)(destPlane->ptr(y));
00297 
00298         int x = x0;
00299         for(int i = 0; i < length; i++, x++)
00300         {
00301             resRowPtr[x] = (_Tp)(pData[i]);
00302         }
00303     }
00304 
00305     //-----------------------------------------------------------------------------------------------
00317     template<> inline void SetHLineL<int32>(cv::Mat *destPlane, const int x0, const int y, const int length, const int32 * pData)
00318     {
00319         int32* resRowPtr;
00320         resRowPtr = (int32*)(destPlane->ptr(y));
00321         memcpy(&resRowPtr[x0], pData, length *sizeof(int32));
00322     }
00323 
00324     //-----------------------------------------------------------------------------------------------
00336     template<typename _Tp> inline void SetHLineD(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData)
00337     {
00338         _Tp* resRowPtr;
00339         resRowPtr = (_Tp*)destPlane->ptr(y);
00340 
00341         int x = x0;
00342         for(int i = 0; i < length; i++, x++)
00343         {
00344             resRowPtr[x] = cv::saturate_cast<_Tp>(pData[i]);
00345         }
00346     }
00347 
00348     //-----------------------------------------------------------------------------------------------
00360     template<> inline void SetHLineD<ito::float64>(cv::Mat *destPlane, const int x0, const int y, const int length, const float64 * pData)
00361     {
00362         float64* resRowPtr;
00363         resRowPtr = (float64*)(destPlane->ptr(y));
00364         memcpy(&resRowPtr[x0], pData, length *sizeof(float64));
00365     }
00366 
00368     template<typename _Tp> RetVal minValueFunc(const DataObject *dObj, float64 &minValue, uint32 *firstLocation, bool ignoreInf = true);
00369     
00371     RetVal DATAOBJ_EXPORT minValue(const DataObject *dObj, float64 &minValue, uint32 *firstLocation, bool ignoreInf = true);
00372        
00374     template<typename _Tp> RetVal maxValueFunc(const DataObject *dObj, float64 &maxValue, uint32 *firstLocation, bool ignoreInf = true);
00375 
00377     RetVal DATAOBJ_EXPORT maxValue(const DataObject *dObj, float64 &maxValue, uint32 *firstLocation, bool ignoreInf = true);
00378         
00380     template<typename _Tp> RetVal minMaxValueFunc(const DataObject *dObj, float64 &minValue, uint32 *firstMinLocation, float64 &maxValue, uint32 *firstMaxLocation, bool ignoreInf = true, const int specialDataTypeFlags = CMPLX_ABS_VALUE);
00381     
00383     RetVal DATAOBJ_EXPORT minMaxValue(const DataObject *dObj, float64 &minValue, uint32 *firstMinLocation, float64 &maxValue, uint32 *firstMaxLocation, bool ignoreInf = true, const int specialDataTypeFlags = CMPLX_ABS_VALUE);
00384 
00385     template<typename _Tp, typename _BufTp> RetVal meanValueFunc(const DataObject *dObj, float64 &meanResult, bool ignoreNaN = true);
00386     RetVal DATAOBJ_EXPORT meanValue(const DataObject *dObj, float64 &meanResult, bool ignoreNaN = true);
00387 
00388     template<typename _Tp> RetVal medianValueFunc(const DataObject *dObj, float64 &meanResult, bool ignoreNaN = true);
00389     RetVal DATAOBJ_EXPORT medianValue(const DataObject *dObj, float64 &medianResult, bool ignoreNaN = true);
00390 
00391     template<typename _Tp, typename _BufTp> RetVal devValueFunc(const ito::DataObject *dObj, const int devTypFlag, float64 &meanResult, float64 &devResult, bool ignoreNaN = true);
00392     RetVal DATAOBJ_EXPORT devValue(const DataObject *dObj, const int devTypFlag, float64 &meanResult, float64 &devResult, bool ignoreNaN = true);
00393 
00394     RetVal DATAOBJ_EXPORT calcCVDFT(DataObject *dObjIO, const bool inverse, const bool inverseAsReal, const bool lineWise);
00395 
00397     ito::RetVal DATAOBJ_EXPORT verifyDataObjectType(const ito::DataObject* dObj, const char* name, uint8 numberOfAllowedTypes, ...); //append allowed data types, e.g. ito::tUint8, ito::tInt8... (order does not care)
00398     
00400     ito::RetVal DATAOBJ_EXPORT verify2DDataObject(const ito::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)
00401     
00403     //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)
00404     
00406     ito::RetVal DATAOBJ_EXPORT verify3DDataObject(const ito::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)
00407     
00408     ito::RetVal DATAOBJ_EXPORT verifySize(int size, int minSize, int maxSize, const char *axisName, const char* dObjName);
00409 
00411     ito::DataObject DATAOBJ_EXPORT squeezeConvertCheck2DDataObject(const ito::DataObject *dObj, const char* name, const ito::Range &sizeY, const ito::Range &sizeX, ito::RetVal &retval, int convertToType, uint8 numberOfAllowedTypes, ...);
00412     
00413     //-----------------------------------------------------------------------------------------------
00426     template<typename _Tp> ito::RetVal freeRowPointer(_Tp *** &pointer)
00427     {
00428         if(pointer == NULL)
00429         {
00430             return ito::RetVal::format(ito::retError, 0, "Delete rowpointer failed, pointer was not allocated freeRowPointer(...)");
00431         }
00432 
00433         int i = 0;
00434 
00435         while(pointer[i] != 0)
00436         {
00437             free(pointer[i]);
00438             i++;
00439             // end of pointer is marked by a NULL-Pointer
00440         }
00441 
00442         free(pointer);
00443         pointer = NULL;
00444 
00445         return ito::retOk;
00446     }
00447 
00463     template<typename _Tp> ito::RetVal getRowPointer(ito::DataObject* dObj, _Tp *** &pointer)  
00464     {
00465 
00466         if(!dObj)
00467         {
00468             return ito::RetVal::format(ito::retError, 0, "DataObject was a NULL pointer in function getRowPointer(...)");               
00469         }
00470 
00471         if(dObj->getType() != ito::getDataType2<_Tp*>())
00472         {
00473             return ito::RetVal::format(ito::retError, 0, "DataObject and template Type differed in function getRowPointer(...)");               
00474         }
00475 
00476         if(dObj->getDims() < 2)
00477         {
00478             return ito::RetVal::format(ito::retError, 0, "DataObject was not initialized in function getRowPointer(...)");   
00479         }
00480 
00481         pointer = (_Tp***)calloc(dObj->getNumPlanes() + 1, sizeof(_Tp**));
00482 
00483         if(pointer == NULL)
00484         {
00485             return ito::RetVal::format(ito::retError, 0, "Allocate plane-pointer failed in function getRowPointer(...)");
00486         }
00487 
00488         ito::RetVal retVal(ito::retOk);
00489         int sizeY = dObj->getSize(dObj->getDims() - 2);
00490 
00491         cv::Mat** mdata = dObj->get_mdata();
00492         int numMats = dObj->getNumPlanes();
00493         for(int i = 0; i < dObj->getNumPlanes(); i++)
00494         {
00495             pointer[i] = (_Tp**)calloc(sizeY, sizeof(_Tp*));
00496             if(pointer[i])
00497             {
00498                 for(int y = 0; y < sizeY; y++)
00499                 {
00500                     pointer[i][y] = mdata[dObj->seekMat(i, numMats)]->ptr<_Tp>(y);
00501                 }
00502             }
00503             else
00504             {
00505                 retVal += ito::RetVal::format(ito::retError, 0, "Allocate row-pointer failed in function getRowPointer(...)");
00506                 break;
00507             }
00508         }
00509 
00510         if(retVal.containsError())
00511         {
00512             freeRowPointer(pointer);
00513         }
00514         return retVal;
00515     } 
00516  
00517     //-----------------------------------------------------------------------------------------------
00526     inline bool isIntType(const int type, int *size)
00527     {
00528         if((type == ito::tInt8)||(type == ito::tUInt8))
00529         {
00530             *size = 1;
00531             return true;
00532         }
00533         else if((type == ito::tInt16)||(type == ito::tUInt16))
00534         {
00535             *size = 2;
00536             return true;
00537         }
00538         else if((type == ito::tInt32)||(type == ito::tUInt32))
00539         {
00540             *size = 4;
00541             return true;
00542         }
00543         else
00544         {
00545             *size = -1;
00546             return false;
00547         }
00548     }
00549 
00550     //-----------------------------------------------------------------------------------------------
00559     inline bool isFPType(const int type, int *size)
00560     {
00561         if((type == ito::tFloat32))
00562         {
00563             *size = 4;
00564             return true;
00565         }
00566         else if((type == ito::tFloat64))
00567         {
00568             *size = 8;
00569             return true;
00570         }
00571         else
00572         {
00573             *size = -1;
00574             return false;
00575         }
00576     }
00577 
00578     //-----------------------------------------------------------------------------------------------
00587     inline bool isCplxType(const int type, int *size)
00588     {
00589         if((type == ito::tComplex64))
00590         {
00591             *size = 8;
00592             return true;
00593         }
00594         else if((type == ito::tComplex128))
00595         {
00596             *size = 16;
00597             return true;
00598         }
00599         else
00600         {
00601             *size = -1;
00602             return false;
00603         }
00604     }
00605 
00606     //-----------------------------------------------------------------------------------------------
00619     inline bool dObjareEqualDetail(const ito::DataObject *dObj1, const ito::DataObject *dObj2, bool &typeFlag, bool &dimsFlag, bool &last2DimsFlag)
00620     {
00621         assert(dObj1 && dObj2);
00622 
00623         bool sizeEqual;
00624 
00625         last2DimsFlag = true;
00626 
00627         int d1 = dObj1->getDims();
00628         int d2 = dObj2->getDims();
00629         dimsFlag = (d1 == d2);
00630         typeFlag = (dObj1->getType() == dObj2->getType());
00631 
00632         if(d1 != d2)
00633         {
00634             sizeEqual = false;
00635 
00636             if (d1 >= 2 && d2 >= 2)
00637             {
00638                 if(dObj1->getSize(dObj1->getDims() - 2) != dObj2->getSize(dObj2->getDims() - 2))
00639                 {
00640                     last2DimsFlag = false;
00641                 }
00642                 else if(dObj1->getSize(dObj1->getDims() - 1) != dObj2->getSize(dObj2->getDims() - 1))
00643                 {
00644                     last2DimsFlag = false;
00645                 }
00646             }
00647             else
00648             {
00649                 last2DimsFlag = false;
00650             }
00651         }
00652         else
00653         {
00654             sizeEqual = (dObj1->getSize() == dObj2->getSize());
00655 
00656             if (d1 >= 2)
00657             {
00658                 if(dObj1->getSize(dObj1->getDims() - 2) != dObj2->getSize(dObj2->getDims() - 2))
00659                 {
00660                     last2DimsFlag = false;
00661                 }
00662                 else if(dObj1->getSize(dObj1->getDims() - 1) != dObj2->getSize(dObj2->getDims() - 1))
00663                 {
00664                     last2DimsFlag = false;
00665                 }
00666             }
00667             else
00668             {
00669                 last2DimsFlag = false;
00670             }
00671         }
00672         return typeFlag && dimsFlag && last2DimsFlag && sizeEqual;
00673     }
00674 
00675     //-----------------------------------------------------------------------------------------------
00685     inline bool dObjareEqualShort(const ito::DataObject *obj1, const ito::DataObject *obj2)
00686     {
00687         assert(obj1 && obj2);
00688 
00689         return (obj1->getType() == obj2->getType()) && (obj1->getSize() == obj2->getSize());
00690     }
00691 
00693     ito::RetVal DATAOBJ_EXPORT dObjCopyLastNAxisTags(const ito::DataObject &DataObjectIn, ito::DataObject &DataObjectOut, const int copyLastNDims, const bool includeValueTags = true, const bool includeRotationMatrix = true);
00694 
00695     ito::RetVal DATAOBJ_EXPORT dObjSetScaleRectangle(ito::DataObject &DataObjectInOut, const double &x0, const double &x1, const double &y0, const double &y1);
00696 } //end namespace dObjHelper
00697 } //end namespace ito
00698 
00699 #endif
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Properties Friends