1 /*
2 * cv_base_class.cpp - base class for all OpenCV related features
3 *
4 * Copyright (c) 2016-2017 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Andrey Parfenov <a1994ndrey@gmail.com>
19 * Author: Wind Yuan <feng.yuan@intel.com>
20 */
21
22 #include "cv_base_class.h"
23
24 namespace XCam {
25
CVBaseClass()26 CVBaseClass::CVBaseClass ()
27 {
28 _cv_context = CVContext::instance ();
29 XCAM_ASSERT (_cv_context.ptr ());
30 _use_ocl = _cv_context->is_ocl_enabled ();
31 }
32
33 bool
set_ocl(bool use_ocl)34 CVBaseClass::set_ocl (bool use_ocl)
35 {
36 if (use_ocl && !_cv_context->is_ocl_enabled ()) {
37 return false;
38 }
39 _use_ocl = use_ocl;
40 return true;
41 }
42
43 bool
convert_to_mat(SmartPtr<VideoBuffer> buffer,cv::Mat & image)44 CVBaseClass::convert_to_mat (SmartPtr<VideoBuffer> buffer, cv::Mat &image)
45 {
46
47 VideoBufferInfo info = buffer->get_video_info ();
48 XCAM_FAIL_RETURN (WARNING, info.format == V4L2_PIX_FMT_NV12, false, "convert_to_mat only support NV12 format");
49
50 uint8_t *ptr = buffer->map ();
51 XCAM_FAIL_RETURN (WARNING, ptr, false, "convert_to_mat buffer map failed");
52
53 cv::Mat mat = cv::Mat (info.aligned_height * 3 / 2, info.width, CV_8UC1, ptr, info.strides[0]);
54 cv::cvtColor (mat, image, cv::COLOR_YUV2BGR_NV12);
55 //buffer->unmap ();
56
57 return true;
58 }
59
60 bool
convert_to_mat(SmartPtr<VideoBuffer> buffer,cv::Mat & image)61 convert_to_mat (SmartPtr<VideoBuffer> buffer, cv::Mat &image)
62 {
63 CVBaseClass cv_obj;
64 return cv_obj.convert_to_mat (buffer, image);
65 }
66
67 }
68