CLAHE Python bindings
[profile/ivi/opencv.git] / modules / ocl / perf / perf_blend.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // @Authors
18 //    Fangfang Bai, fangfang@multicorewareinc.com
19 //    Jin Ma,       jin@multicorewareinc.com
20 //
21 // Redistribution and use in source and binary forms, with or without modification,
22 // are permitted provided that the following conditions are met:
23 //
24 //   * Redistribution's of source code must retain the above copyright notice,
25 //     this list of conditions and the following disclaimer.
26 //
27 //   * Redistribution's in binary form must reproduce the above copyright notice,
28 //     this list of conditions and the following disclaimer in the documentation
29 //     and/or other oclMaterials provided with the distribution.
30 //
31 //   * The name of the copyright holders may not be used to endorse or promote products
32 //     derived from this software without specific prior written permission.
33 //
34 // This software is provided by the copyright holders and contributors as is and
35 // any express or implied warranties, including, but not limited to, the implied
36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
37 // In no event shall the Intel Corporation or contributors be liable for any direct,
38 // indirect, incidental, special, exemplary, or consequential damages
39 // (including, but not limited to, procurement of substitute goods or services;
40 // loss of use, data, or profits; or business interruption) however caused
41 // and on any theory of liability, whether in contract, strict liability,
42 // or tort (including negligence or otherwise) arising in any way out of
43 // the use of this software, even if advised of the possibility of such damage.
44 //
45 //M*/
46
47 #include "precomp.hpp"
48 ///////////// blend ////////////////////////
49 template <typename T>
50 void blendLinearGold(const cv::Mat &img1, const cv::Mat &img2, const cv::Mat &weights1, const cv::Mat &weights2, cv::Mat &result_gold)
51 {
52     result_gold.create(img1.size(), img1.type());
53
54     int cn = img1.channels();
55
56     for (int y = 0; y < img1.rows; ++y)
57     {
58         const float *weights1_row = weights1.ptr<float>(y);
59         const float *weights2_row = weights2.ptr<float>(y);
60         const T *img1_row = img1.ptr<T>(y);
61         const T *img2_row = img2.ptr<T>(y);
62         T *result_gold_row = result_gold.ptr<T>(y);
63
64         for (int x = 0; x < img1.cols * cn; ++x)
65         {
66             float w1 = weights1_row[x / cn];
67             float w2 = weights2_row[x / cn];
68             result_gold_row[x] = static_cast<T>((img1_row[x] * w1 + img2_row[x] * w2) / (w1 + w2 + 1e-5f));
69         }
70     }
71 }
72 PERFTEST(blend)
73 {
74     Mat src1, src2, weights1, weights2, dst, ocl_dst;
75     ocl::oclMat d_src1, d_src2, d_weights1, d_weights2, d_dst;
76
77     int all_type[] = {CV_8UC1, CV_8UC4};
78     std::string type_name[] = {"CV_8UC1", "CV_8UC4"};
79
80     for (int size = Min_Size; size <= Max_Size; size *= Multiple)
81     {
82         for (size_t j = 0; j < sizeof(all_type) / sizeof(int); j++)
83         {
84             SUBTEST << size << 'x' << size << "; " << type_name[j] << " and CV_32FC1";
85
86             gen(src1, size, size, all_type[j], 0, 256);
87             gen(src2, size, size, all_type[j], 0, 256);
88             gen(weights1, size, size, CV_32FC1, 0, 1);
89             gen(weights2, size, size, CV_32FC1, 0, 1);
90
91             blendLinearGold<uchar>(src1, src2, weights1, weights2, dst);
92
93             CPU_ON;
94             blendLinearGold<uchar>(src1, src2, weights1, weights2, dst);
95             CPU_OFF;
96
97             d_src1.upload(src1);
98             d_src2.upload(src2);
99             d_weights1.upload(weights1);
100             d_weights2.upload(weights2);
101
102             WARMUP_ON;
103             ocl::blendLinear(d_src1, d_src2, d_weights1, d_weights2, d_dst);
104             WARMUP_OFF;
105
106             GPU_ON;
107             ocl::blendLinear(d_src1, d_src2, d_weights1, d_weights2, d_dst);
108             GPU_OFF;
109
110             GPU_FULL_ON;
111             d_src1.upload(src1);
112             d_src2.upload(src2);
113             d_weights1.upload(weights1);
114             d_weights2.upload(weights2);
115             ocl::blendLinear(d_src1, d_src2, d_weights1, d_weights2, d_dst);
116             d_dst.download(ocl_dst);
117             GPU_FULL_OFF;
118
119             TestSystem::instance().ExpectedMatNear(dst, ocl_dst, 1.f);
120         }
121     }
122 }