Added SharedMatrix
[profile/ivi/opencv.git] / modules / core / include / opencv2 / core / ippasync.hpp
1 #ifndef __OPENCV_CORE_IPPASYNC_HPP__
2 #define __OPENCV_CORE_IPPASYNC_HPP__
3
4 #ifdef HAVE_IPP_A
5
6 #include "opencv2/core.hpp"
7 #include <ipp_async_op.h>
8 #include <ipp_async_accel.h>
9
10 namespace cv
11 {
12
13 namespace hpp
14 {
15     //convert OpenCV data type to hppDataType
16     inline int toHppType(const int cvType)
17     {
18         int depth = CV_MAT_DEPTH(cvType);
19         int hppType = depth == CV_8U ? HPP_DATA_TYPE_8U :
20                      depth == CV_16U ? HPP_DATA_TYPE_16U :
21                      depth == CV_16S ? HPP_DATA_TYPE_16S :
22                      depth == CV_32S ? HPP_DATA_TYPE_32S :
23                      depth == CV_32F ? HPP_DATA_TYPE_32F :
24                      depth == CV_64F ? HPP_DATA_TYPE_64F : -1;
25         CV_Assert( hppType >= 0 );
26         return hppType;
27     }
28
29     //convert hppDataType to OpenCV data type
30     inline int toCvType(const int hppType)
31     {
32         int cvType = hppType == HPP_DATA_TYPE_8U ? CV_8U :
33                     hppType == HPP_DATA_TYPE_16U ? CV_16U :
34                     hppType == HPP_DATA_TYPE_16S ? CV_16S :
35                     hppType == HPP_DATA_TYPE_32S ? CV_32S :
36                     hppType == HPP_DATA_TYPE_32F ? CV_32F :
37                     hppType == HPP_DATA_TYPE_64F ? CV_64F : -1;
38         CV_Assert( cvType >= 0 );
39         return cvType;
40     }
41
42     inline void copyHppToMat(hppiMatrix* src, Mat& dst, hppAccel accel, int cn)
43     {
44         hppDataType type;
45         hpp32u width, height;
46         hppStatus sts;
47
48         if (src == NULL)
49             return dst.release();
50
51         sts = hppiInquireMatrix(src, &type, &width, &height);
52
53         CV_Assert( sts == HPP_STATUS_NO_ERROR);
54
55         int matType = CV_MAKETYPE(toCvType(type), cn);
56
57         CV_Assert(width%cn == 0);
58
59         width /= cn;
60
61         dst.create((int)height, (int)width, (int)matType);
62
63         size_t newSize = (size_t)(height*(hpp32u)(dst.step));
64
65         sts = hppiGetMatrixData(accel,src,(hpp32u)(dst.step),dst.data,&newSize);
66
67         CV_Assert( sts == HPP_STATUS_NO_ERROR);
68     }
69
70     //create cv::Mat from hppiMatrix
71     inline Mat getMat(hppiMatrix* src, hppAccel accel, int cn)
72     {
73         Mat dst;
74         copyHppToMat(src, dst, accel, cn);
75         return dst;
76     }
77
78     //create hppiMatrix from cv::Mat
79     inline hppiMatrix* getHpp(const Mat& src, hppAccel accel)
80     {
81         int htype = toHppType(src.type());
82         int cn = src.channels();
83
84         CV_Assert(src.data);
85         hppAccelType accelType = hppQueryAccelType(accel);
86
87         if (accelType!=HPP_ACCEL_TYPE_CPU)
88         {
89             hpp32u pitch, size;
90             hppQueryMatrixAllocParams(accel, src.cols*cn, src.rows, htype, &pitch, &size);
91             if (pitch!=0 && size!=0)
92                 if ((int)(src.data)%4096==0 && pitch==(hpp32u)(src.step))
93                 {
94                     return hppiCreateSharedMatrix(htype, src.cols*cn, src.rows, src.data, pitch, size);
95                 }
96         }
97
98         return hppiCreateMatrix(htype, src.cols*cn, src.rows, src.data, (hpp32s)(src.step));;
99     }
100
101 }}
102
103 #endif
104
105 #endif