Merge pull request #244 from forresti/fix-comment
[platform/upstream/armcl.git] / src / runtime / Pyramid.cpp
1 /*
2  * Copyright (c) 2016, 2017 ARM Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "arm_compute/runtime/Pyramid.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/PyramidInfo.h"
28 #include "arm_compute/core/TensorInfo.h"
29 #include "arm_compute/core/TensorShape.h"
30 #include "support/ToolchainSupport.h"
31
32 #include <cmath>
33
34 using namespace arm_compute;
35
36 void Pyramid::init(const PyramidInfo &info)
37 {
38     internal_init(info, false);
39 }
40
41 void Pyramid::init_auto_padding(const PyramidInfo &info)
42 {
43     internal_init(info, true);
44 }
45
46 void Pyramid::internal_init(const PyramidInfo &info, bool auto_padding)
47 {
48     _info    = info;
49     _pyramid = arm_compute::support::cpp14::make_unique<Tensor[]>(_info.num_levels());
50
51     size_t      w            = _info.width();
52     size_t      h            = _info.height();
53     size_t      ref_w        = w;
54     size_t      ref_h        = h;
55     bool        is_orb_scale = (SCALE_PYRAMID_ORB == _info.scale());
56     TensorShape tensor_shape = _info.tensor_shape();
57
58     // Note: Look-up table used by the OpenVX sample implementation
59     const float c_orbscale[4] = { 0.5f,
60                                   SCALE_PYRAMID_ORB,
61                                   SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB,
62                                   SCALE_PYRAMID_ORB *SCALE_PYRAMID_ORB * SCALE_PYRAMID_ORB
63                                 };
64
65     for(size_t i = 0; i < _info.num_levels(); ++i)
66     {
67         TensorInfo tensor_info(tensor_shape, _info.format());
68
69         if(auto_padding)
70         {
71             tensor_info.auto_padding();
72         }
73
74         (_pyramid.get() + i)->allocator()->init(tensor_info);
75
76         if(is_orb_scale)
77         {
78             float orb_scale = c_orbscale[(i + 1) % 4];
79             w               = static_cast<int>(std::ceil(static_cast<float>(ref_w) * orb_scale));
80             h               = static_cast<int>(std::ceil(static_cast<float>(ref_h) * orb_scale));
81
82             if(0 == ((i + 1) % 4))
83             {
84                 ref_w = w;
85                 ref_h = h;
86             }
87         }
88         else
89         {
90             w = (w + 1) * _info.scale();
91             h = (h + 1) * _info.scale();
92         }
93
94         // Update tensor_shape
95         tensor_shape.set(0, w);
96         tensor_shape.set(1, h);
97     }
98 }
99
100 void Pyramid::allocate()
101 {
102     ARM_COMPUTE_ERROR_ON(_pyramid == nullptr);
103
104     for(size_t i = 0; i < _info.num_levels(); ++i)
105     {
106         (_pyramid.get() + i)->allocator()->allocate();
107     }
108 }
109
110 const PyramidInfo *Pyramid::info() const
111 {
112     return &_info;
113 }
114
115 Tensor *Pyramid::get_pyramid_level(size_t index) const
116 {
117     ARM_COMPUTE_ERROR_ON(index >= _info.num_levels());
118
119     return (_pyramid.get() + index);
120 }