1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2018-2019 Intel Corporation
7 #include "test_precomp.hpp"
10 #include "gapi_fluid_test_kernels.hpp"
11 #include <opencv2/gapi/core.hpp>
15 namespace gapi_test_kernels
18 GAPI_FLUID_KERNEL(FAddSimple, TAddSimple, false)
20 static const int Window = 1;
22 static void run(const cv::gapi::fluid::View &a,
23 const cv::gapi::fluid::View &b,
24 cv::gapi::fluid::Buffer &o)
26 // std::cout << "AddSimple {{{\n";
27 // std::cout << " a - "; a.debug(std::cout);
28 // std::cout << " b - "; b.debug(std::cout);
29 // std::cout << " o - "; o.debug(std::cout);
31 const uint8_t* in1 = a.InLine<uint8_t>(0);
32 const uint8_t* in2 = b.InLine<uint8_t>(0);
33 uint8_t* out = o.OutLine<uint8_t>();
35 // std::cout << "a: ";
36 // for (int i = 0, w = a.length(); i < w; i++)
38 // std::cout << std::setw(4) << int(in1[i]);
42 // std::cout << "b: ";
43 // for (int i = 0, w = a.length(); i < w; i++)
45 // std::cout << std::setw(4) << int(in2[i]);
49 for (int i = 0, w = a.length(); i < w; i++)
51 out[i] = in1[i] + in2[i];
54 // std::cout << "}}} " << std::endl;;
58 GAPI_FLUID_KERNEL(FAddCSimple, TAddCSimple, false)
60 static const int Window = 1;
61 static const int LPI = 2;
63 static void run(const cv::gapi::fluid::View &in,
65 cv::gapi::fluid::Buffer &out)
67 for (int l = 0, lpi = out.lpi(); l < lpi; l++)
69 const uint8_t* in_row = in .InLine <uint8_t>(l);
70 uint8_t* out_row = out.OutLine<uint8_t>(l);
71 //std::cout << "l=" << l << ": ";
72 for (int i = 0, w = in.length(); i < w; i++)
74 //std::cout << std::setw(4) << int(in_row[i]);
75 out_row[i] = static_cast<uint8_t>(in_row[i] + cval);
77 //std::cout << std::endl;
82 GAPI_FLUID_KERNEL(FAddScalar, TAddScalar, false)
84 static const int Window = 1;
85 static const int LPI = 2;
87 static void run(const cv::gapi::fluid::View &in,
88 const cv::Scalar &cval,
89 cv::gapi::fluid::Buffer &out)
91 for (int l = 0, lpi = out.lpi(); l < lpi; l++)
93 const uint8_t* in_row = in .InLine <uint8_t>(l);
94 uint8_t* out_row = out.OutLine<uint8_t>(l);
95 std::cout << "l=" << l << ": ";
96 for (int i = 0, w = in.length(); i < w; i++)
98 std::cout << std::setw(4) << int(in_row[i]);
99 out_row[i] = static_cast<uint8_t>(in_row[i] + cval[0]);
101 std::cout << std::endl;
106 GAPI_FLUID_KERNEL(FAddScalarToMat, TAddScalarToMat, false)
108 static const int Window = 1;
109 static const int LPI = 2;
111 static void run(const cv::Scalar &cval,
112 const cv::gapi::fluid::View &in,
113 cv::gapi::fluid::Buffer &out)
115 for (int l = 0, lpi = out.lpi(); l < lpi; l++)
117 const uint8_t* in_row = in .InLine <uint8_t>(l);
118 uint8_t* out_row = out.OutLine<uint8_t>(l);
119 std::cout << "l=" << l << ": ";
120 for (int i = 0, w = in.length(); i < w; i++)
122 std::cout << std::setw(4) << int(in_row[i]);
123 out_row[i] = static_cast<uint8_t>(in_row[i] + cval[0]);
125 std::cout << std::endl;
130 template<int kernelSize, int lpi = 1>
131 static void runBlur(const cv::gapi::fluid::View& src, cv::gapi::fluid::Buffer& dst)
133 const auto borderSize = (kernelSize - 1) / 2;
134 const unsigned char* ins[kernelSize];
136 for (int l = 0; l < lpi; l++)
138 for (int i = 0; i < kernelSize; i++)
140 ins[i] = src.InLine<unsigned char>(i - borderSize + l);
143 auto out = dst.OutLine<unsigned char>(l);
144 const auto width = dst.length();
146 for (int w = 0; w < width; w++)
149 for (int i = 0; i < kernelSize; i++)
151 for (int j = -borderSize; j < borderSize + 1; j++)
156 out[w] = static_cast<unsigned char>(std::rint(res / (kernelSize * kernelSize)));
161 GAPI_FLUID_KERNEL(FBlur1x1, TBlur1x1, false)
163 static const int Window = 1;
165 static void run(const cv::gapi::fluid::View &src, int /*borderType*/,
166 cv::Scalar /*borderValue*/, cv::gapi::fluid::Buffer &dst)
168 runBlur<Window>(src, dst);
172 GAPI_FLUID_KERNEL(FBlur3x3, TBlur3x3, false)
174 static const int Window = 3;
176 static void run(const cv::gapi::fluid::View &src, int /*borderType*/,
177 cv::Scalar /*borderValue*/, cv::gapi::fluid::Buffer &dst)
179 runBlur<Window>(src, dst);
182 static cv::gapi::fluid::Border getBorder(const cv::GMatDesc &/*src*/, int borderType, cv::Scalar borderValue)
184 return { borderType, to_own(borderValue)};
188 GAPI_FLUID_KERNEL(FBlur5x5, TBlur5x5, false)
190 static const int Window = 5;
192 static void run(const cv::gapi::fluid::View &src, int /*borderType*/,
193 cv::Scalar /*borderValue*/, cv::gapi::fluid::Buffer &dst)
195 runBlur<Window>(src, dst);
198 static cv::gapi::fluid::Border getBorder(const cv::GMatDesc &/*src*/, int borderType, cv::Scalar borderValue)
200 return { borderType, to_own(borderValue)};
204 GAPI_FLUID_KERNEL(FBlur3x3_2lpi, TBlur3x3_2lpi, false)
206 static const int Window = 3;
207 static const int LPI = 2;
209 static void run(const cv::gapi::fluid::View &src, int /*borderType*/,
210 cv::Scalar /*borderValue*/, cv::gapi::fluid::Buffer &dst)
212 runBlur<Window, LPI>(src, dst);
215 static cv::gapi::fluid::Border getBorder(const cv::GMatDesc &/*src*/, int borderType, cv::Scalar borderValue)
217 return { borderType, to_own(borderValue)};
221 GAPI_FLUID_KERNEL(FBlur5x5_2lpi, TBlur5x5_2lpi, false)
223 static const int Window = 5;
224 static const int LPI = 2;
226 static void run(const cv::gapi::fluid::View &src, int /*borderType*/,
227 cv::Scalar /*borderValue*/, cv::gapi::fluid::Buffer &dst)
229 runBlur<Window, LPI>(src, dst);
232 static cv::gapi::fluid::Border getBorder(const cv::GMatDesc &/*src*/, int borderType, cv::Scalar borderValue)
234 return { borderType, to_own(borderValue )};
238 GAPI_FLUID_KERNEL(FIdentity, TId, false)
240 static const int Window = 3;
242 static void run(const cv::gapi::fluid::View &a,
243 cv::gapi::fluid::Buffer &o)
245 const uint8_t* in[3] = {
246 a.InLine<uint8_t>(-1),
247 a.InLine<uint8_t>( 0),
248 a.InLine<uint8_t>(+1)
250 uint8_t* out = o.OutLine<uint8_t>();
252 // ReadFunction3x3(in, a.length());
253 for (int i = 0, w = a.length(); i < w; i++)
259 static gapi::fluid::Border getBorder(const cv::GMatDesc &)
261 return { cv::BORDER_REPLICATE, cv::gapi::own::Scalar{} };
265 GAPI_FLUID_KERNEL(FId7x7, TId7x7, false)
267 static const int Window = 7;
268 static const int LPI = 2;
270 static void run(const cv::gapi::fluid::View &a,
271 cv::gapi::fluid::Buffer &o)
273 for (int l = 0, lpi = o.lpi(); l < lpi; l++)
275 const uint8_t* in[Window] = {
276 a.InLine<uint8_t>(-3 + l),
277 a.InLine<uint8_t>(-2 + l),
278 a.InLine<uint8_t>(-1 + l),
279 a.InLine<uint8_t>( 0 + l),
280 a.InLine<uint8_t>(+1 + l),
281 a.InLine<uint8_t>(+2 + l),
282 a.InLine<uint8_t>(+3 + l),
284 uint8_t* out = o.OutLine<uint8_t>(l);
286 // std::cout << "Id7x7 " << l << " of " << lpi << " {{{\n";
287 // std::cout << " a - "; a.debug(std::cout);
288 // std::cout << " o - "; o.debug(std::cout);
289 // std::cout << "}}} " << std::endl;;
291 // // std::cout << "Id7x7 at " << a.y() << "/L" << l << " {{{" << std::endl;
292 // for (int j = 0; j < Window; j++)
294 // // std::cout << std::setw(2) << j-(Window-1)/2 << ": ";
295 // for (int i = 0, w = a.length(); i < w; i++)
296 // std::cout << std::setw(4) << int(in[j][i]);
297 // std::cout << std::endl;
299 // std::cout << "}}}" << std::endl;
301 for (int i = 0, w = a.length(); i < w; i++)
302 out[i] = in[(Window-1)/2][i];
306 static cv::gapi::fluid::Border getBorder(const cv::GMatDesc&/* src*/)
308 return { cv::BORDER_REPLICATE, cv::gapi::own::Scalar{} };
312 GAPI_FLUID_KERNEL(FPlusRow0, TPlusRow0, true)
314 static const int Window = 1;
316 static void initScratch(const cv::GMatDesc &in,
317 cv::gapi::fluid::Buffer &scratch)
319 cv::Size scratch_size{in.size.width, 1};
320 cv::gapi::fluid::Buffer buffer(in.withSize(scratch_size));
321 scratch = std::move(buffer);
324 static void resetScratch(cv::gapi::fluid::Buffer &scratch)
326 // FIXME: only 1 line can be used!
327 uint8_t* out_row = scratch.OutLine<uint8_t>();
328 for (int i = 0, w = scratch.length(); i < w; i++)
334 static void run(const cv::gapi::fluid::View &in,
335 cv::gapi::fluid::Buffer &out,
336 cv::gapi::fluid::Buffer &scratch)
338 const uint8_t* in_row = in .InLine <uint8_t>(0);
339 uint8_t* out_row = out .OutLine<uint8_t>();
340 uint8_t* tmp_row = scratch.OutLine<uint8_t>();
344 // Copy 1st row to scratch buffer
345 for (int i = 0, w = in.length(); i < w; i++)
347 out_row[i] = in_row[i];
348 tmp_row[i] = in_row[i];
353 // Output is 1st row + in
354 for (int i = 0, w = in.length(); i < w; i++)
356 out_row[i] = in_row[i] + tmp_row[i];
362 GAPI_FLUID_KERNEL(FTestSplit3, cv::gapi::core::GSplit3, false)
364 static const int Window = 1;
366 static void run(const cv::gapi::fluid::View &in,
367 cv::gapi::fluid::Buffer &o1,
368 cv::gapi::fluid::Buffer &o2,
369 cv::gapi::fluid::Buffer &o3)
371 // std::cout << "Split3 {{{\n";
372 // std::cout << " a - "; in.debug(std::cout);
373 // std::cout << " 1 - "; o1.debug(std::cout);
374 // std::cout << " 2 - "; o2.debug(std::cout);
375 // std::cout << " 3 - "; o3.debug(std::cout);
376 // std::cout << "}}} " << std::endl;;
378 const uint8_t* in_rgb = in.InLine<uint8_t>(0);
379 uint8_t* out_r = o1.OutLine<uint8_t>();
380 uint8_t* out_g = o2.OutLine<uint8_t>();
381 uint8_t* out_b = o3.OutLine<uint8_t>();
383 for (int i = 0, w = in.length(); i < w; i++)
385 out_r[i] = in_rgb[3*i];
386 out_g[i] = in_rgb[3*i+1];
387 out_b[i] = in_rgb[3*i+2];
392 GAPI_FLUID_KERNEL(FSum2MatsAndScalar, TSum2MatsAndScalar, false)
394 static const int Window = 1;
395 static const int LPI = 2;
397 static void run(const cv::gapi::fluid::View &a,
398 const cv::Scalar &cval,
399 const cv::gapi::fluid::View &b,
400 cv::gapi::fluid::Buffer &out)
402 for (int l = 0, lpi = out.lpi(); l < lpi; l++)
404 const uint8_t* in_row1 = a .InLine <uint8_t>(l);
405 const uint8_t* in_row2 = b .InLine <uint8_t>(l);
406 uint8_t* out_row = out.OutLine<uint8_t>(l);
407 std::cout << "l=" << l << ": ";
408 for (int i = 0, w = a.length(); i < w; i++)
410 std::cout << std::setw(4) << int(in_row1[i]);
411 std::cout << std::setw(4) << int(in_row2[i]);
412 out_row[i] = static_cast<uint8_t>(in_row1[i] + in_row2[i] + cval[0]);
414 std::cout << std::endl;
419 cv::gapi::GKernelPackage fluidTestPackage = cv::gapi::kernels
435 } // namespace gapi_test_kernels