1/*
2 * stitcher.h - stitcher interface
3 *
4 *  Copyright (c) 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: Wind Yuan <feng.yuan@intel.com>
19 * Author: Yinhang Liu <yinhangx.liu@intel.com>
20 */
21
22#ifndef XCAM_INTERFACE_STITCHER_H
23#define XCAM_INTERFACE_STITCHER_H
24
25#include <xcam_std.h>
26#include <interface/data_types.h>
27#include <vector>
28#include <video_buffer.h>
29
30#define XCAM_STITCH_FISHEYE_MAX_NUM    6
31#define XCAM_STITCH_MAX_CAMERAS XCAM_STITCH_FISHEYE_MAX_NUM
32#define XCAM_STITCH_MIN_SEAM_WIDTH 56
33
34#define INVALID_INDEX (uint32_t)(-1)
35
36namespace XCam {
37
38enum StitchResMode {
39    StitchRes1080P,
40    StitchRes1080P4,
41    StitchRes4K
42};
43
44struct StitchInfo {
45    uint32_t merge_width[XCAM_STITCH_FISHEYE_MAX_NUM];
46
47    ImageCropInfo crop[XCAM_STITCH_FISHEYE_MAX_NUM];
48    FisheyeInfo fisheye_info[XCAM_STITCH_FISHEYE_MAX_NUM];
49
50    StitchInfo () {
51        xcam_mem_clear (merge_width);
52    }
53};
54
55struct ImageMergeInfo {
56    Rect left;
57    Rect right;
58};
59
60class Stitcher;
61
62struct CalibrationInfo {
63    ExtrinsicParameter extrinsic;
64    IntrinsicParameter intrinsic;
65};
66
67struct CameraInfo {
68    CalibrationInfo   calibration;
69    float             round_angle_start;
70    float             angle_range;;
71};
72
73class Stitcher
74{
75public:
76    struct RoundViewSlice {
77        float          hori_angle_start;
78        float          hori_angle_range;
79        uint32_t       width;
80        uint32_t       height;
81
82        RoundViewSlice ()
83            : hori_angle_start (0.0f), hori_angle_range (0.0f)
84            , width (0), height (0)
85        {}
86    };
87
88    struct CenterMark {
89        uint32_t slice_center_x;
90        uint32_t out_center_x;
91        CenterMark ()
92            : slice_center_x (0)
93            , out_center_x (0)
94        {}
95    };
96
97    struct ScaleFactor {
98        float left_scale;
99        float right_scale;
100
101        ScaleFactor ()
102            : left_scale (1.0f)
103            , right_scale (1.0f)
104        {}
105    };
106
107    struct ImageOverlapInfo {
108        Rect left;
109        Rect right;
110        Rect out_area;
111    };
112
113    struct CopyArea {
114        uint32_t in_idx;
115        Rect     in_area;
116        Rect     out_area;
117
118        CopyArea ()
119            : in_idx (INVALID_INDEX)
120        {}
121    };
122    typedef std::vector<CopyArea>  CopyAreaArray;
123
124public:
125    explicit Stitcher (uint32_t align_x, uint32_t align_y = 1);
126    virtual ~Stitcher ();
127    static SmartPtr<Stitcher> create_ocl_stitcher ();
128    static SmartPtr<Stitcher> create_soft_stitcher ();
129
130    bool set_bowl_config (const BowlDataConfig &config);
131    const BowlDataConfig &get_bowl_config () {
132        return _bowl_config;
133    }
134    bool set_camera_num (uint32_t num);
135    uint32_t get_camera_num () const {
136        return _camera_num;
137    }
138    bool set_camera_info (uint32_t index, const CameraInfo &info);
139    bool get_camera_info (uint32_t index, CameraInfo &info) const;
140
141    bool set_crop_info (uint32_t index, const ImageCropInfo &info);
142    bool get_crop_info (uint32_t index, ImageCropInfo &info) const;
143    bool is_crop_info_set () const {
144        return _is_crop_set;
145    }
146    //bool set_overlap_info (uint32_t index, const ImageOverlapInfo &info);
147    bool is_overlap_info_set () const {
148        return _is_overlap_set;
149    }
150
151    //bool set_stitch_info (const StitchInfo &stitch_info);
152    void set_output_size (uint32_t width, uint32_t height) {
153        _output_width = width; //XCAM_ALIGN_UP (width, XCAM_BLENDER_ALIGNED_WIDTH);
154        _output_height = height;
155    }
156
157    void get_output_size (uint32_t &width, uint32_t &height) const {
158        width = _output_width;
159        height = _output_height;
160    }
161    virtual XCamReturn stitch_buffers (const VideoBufferList &in_bufs, SmartPtr<VideoBuffer> &out_buf) = 0;
162
163protected:
164    XCamReturn estimate_round_slices ();
165    virtual XCamReturn estimate_coarse_crops ();
166    XCamReturn mark_centers ();
167    XCamReturn estimate_overlap ();
168    XCamReturn update_copy_areas ();
169
170    const CenterMark &get_center (uint32_t idx) const {
171        return _center_marks[idx];
172    }
173    const RoundViewSlice &get_round_view_slice (uint32_t idx) const {
174        return _round_view_slices[idx];
175    }
176    const ImageOverlapInfo &get_overlap (uint32_t idx) const {
177        return _overlap_info[idx];
178    }
179    const ImageCropInfo &get_crop (uint32_t idx) const {
180        return _crop_info[idx];
181    }
182    const CopyAreaArray &get_copy_area () const {
183        return _copy_areas;
184    }
185
186private:
187    XCAM_DEAD_COPY (Stitcher);
188
189protected:
190    ImageCropInfo               _crop_info[XCAM_STITCH_MAX_CAMERAS];
191    bool                        _is_crop_set;
192    //update after each feature match
193    ScaleFactor                 _scale_factors[XCAM_STITCH_MAX_CAMERAS];
194
195private:
196    uint32_t                    _alignment_x, _alignment_y;
197    uint32_t                    _output_width, _output_height;
198    float                       _out_start_angle;
199    uint32_t                    _camera_num;
200    CameraInfo                  _camera_info[XCAM_STITCH_MAX_CAMERAS];
201    RoundViewSlice              _round_view_slices[XCAM_STITCH_MAX_CAMERAS];
202    bool                        _is_round_view_set;
203
204    ImageOverlapInfo            _overlap_info[XCAM_STITCH_MAX_CAMERAS];
205    BowlDataConfig              _bowl_config;
206    bool                        _is_overlap_set;
207
208    //auto calculation
209    CenterMark                  _center_marks[XCAM_STITCH_MAX_CAMERAS];
210    bool                        _is_center_marked;
211    CopyAreaArray               _copy_areas;
212};
213
214class BowlModel {
215public:
216    typedef std::vector<PointFloat3> VertexMap;
217    typedef std::vector<PointFloat2> PointMap;
218    typedef std::vector<int32_t> IndexVector;
219
220public:
221    BowlModel (const BowlDataConfig &config, const uint32_t image_width, const uint32_t image_height);
222    bool get_max_topview_area_mm (float &length_mm, float &width_mm);
223    bool get_topview_rect_map (
224        PointMap &texture_points,
225        uint32_t res_width, uint32_t res_height,
226        float length_mm = 0.0f, float width_mm = 0.0f);
227
228    bool get_stitch_image_vertex_model (
229        VertexMap &vertices, PointMap &texture_points, IndexVector &indeices,
230        uint32_t res_width, uint32_t res_height, float vertex_height);
231
232    bool get_bowlview_vertex_model (
233        VertexMap &vertices, PointMap &texture_points, IndexVector &indeices,
234        uint32_t res_width, uint32_t res_height);
235
236    bool get_topview_vertex_model (
237        VertexMap &vertices, PointMap &texture_points, IndexVector &indeices,
238        uint32_t res_width, uint32_t res_height);
239
240private:
241    BowlDataConfig    _config;
242    uint32_t          _bowl_img_width, _bowl_img_height;
243    float             _max_topview_width_mm;
244    float             _max_topview_length_mm;
245};
246
247}
248
249#endif //XCAM_INTERFACE_STITCHER_H
250