Merge pull request #244 from forresti/fix-comment
[platform/upstream/armcl.git] / src / runtime / MultiImage.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/MultiImage.h"
25
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "arm_compute/runtime/TensorAllocator.h"
29
30 using namespace arm_compute;
31
32 MultiImage::MultiImage()
33     : _info(), _plane()
34 {
35 }
36
37 const MultiImageInfo *MultiImage::info() const
38 {
39     return &_info;
40 }
41
42 void MultiImage::init(unsigned int width, unsigned int height, Format format)
43 {
44     internal_init(width, height, format, false);
45 }
46
47 void MultiImage::init_auto_padding(unsigned int width, unsigned int height, Format format)
48 {
49     internal_init(width, height, format, true);
50 }
51
52 void MultiImage::internal_init(unsigned int width, unsigned int height, Format format, bool auto_padding)
53 {
54     TensorInfo info(width, height, Format::U8);
55
56     if(auto_padding)
57     {
58         info.auto_padding();
59     }
60
61     switch(format)
62     {
63         case Format::U8:
64         case Format::S16:
65         case Format::U16:
66         case Format::S32:
67         case Format::F16:
68         case Format::F32:
69         case Format::U32:
70         case Format::RGB888:
71         case Format::RGBA8888:
72         case Format::YUYV422:
73         case Format::UYVY422:
74         {
75             TensorInfo info_full(width, height, format);
76
77             if(auto_padding)
78             {
79                 info_full.auto_padding();
80             }
81
82             std::get<0>(_plane).allocator()->init(info_full);
83             break;
84         }
85         case Format::NV12:
86         case Format::NV21:
87         {
88             TensorInfo info_uv88(width / 2, height / 2, Format::UV88);
89
90             if(auto_padding)
91             {
92                 info_uv88.auto_padding();
93             }
94
95             std::get<0>(_plane).allocator()->init(info);
96             std::get<1>(_plane).allocator()->init(info_uv88);
97             break;
98         }
99         case Format::IYUV:
100         {
101             TensorInfo info_sub2(width / 2, height / 2, Format::U8);
102
103             if(auto_padding)
104             {
105                 info_sub2.auto_padding();
106             }
107
108             std::get<0>(_plane).allocator()->init(info);
109             std::get<1>(_plane).allocator()->init(info_sub2);
110             std::get<2>(_plane).allocator()->init(info_sub2);
111             break;
112         }
113         case Format::YUV444:
114             std::get<0>(_plane).allocator()->init(info);
115             std::get<1>(_plane).allocator()->init(info);
116             std::get<2>(_plane).allocator()->init(info);
117             break;
118         default:
119             ARM_COMPUTE_ERROR("Not supported");
120             break;
121     }
122
123     _info.init(width, height, format);
124 }
125
126 void MultiImage::allocate()
127 {
128     switch(_info.format())
129     {
130         case Format::U8:
131         case Format::S16:
132         case Format::U16:
133         case Format::S32:
134         case Format::F16:
135         case Format::F32:
136         case Format::U32:
137         case Format::RGB888:
138         case Format::RGBA8888:
139         case Format::YUYV422:
140         case Format::UYVY422:
141             std::get<0>(_plane).allocator()->allocate();
142             break;
143         case Format::NV12:
144         case Format::NV21:
145             std::get<0>(_plane).allocator()->allocate();
146             std::get<1>(_plane).allocator()->allocate();
147             break;
148         case Format::IYUV:
149         case Format::YUV444:
150             std::get<0>(_plane).allocator()->allocate();
151             std::get<1>(_plane).allocator()->allocate();
152             std::get<2>(_plane).allocator()->allocate();
153             break;
154         default:
155             ARM_COMPUTE_ERROR("Not supported");
156             break;
157     }
158 }
159
160 void MultiImage::create_subimage(MultiImage *image, const Coordinates &coords, unsigned int width, unsigned int height)
161 {
162     arm_compute::Format format = image->info()->format();
163     const TensorInfo    info(width, height, Format::U8);
164
165     switch(format)
166     {
167         case Format::U8:
168         case Format::S16:
169         case Format::U16:
170         case Format::S32:
171         case Format::F32:
172         case Format::F16:
173         case Format::U32:
174         case Format::RGB888:
175         case Format::RGBA8888:
176         case Format::YUYV422:
177         case Format::UYVY422:
178         {
179             const TensorInfo info_full(width, height, format);
180             std::get<0>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(0))->allocator(), coords, info_full);
181             break;
182         }
183         case Format::NV12:
184         case Format::NV21:
185         {
186             const TensorInfo info_uv88(width / 2, height / 2, Format::UV88);
187             std::get<0>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(0))->allocator(), coords, info);
188             std::get<1>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(1))->allocator(), coords, info_uv88);
189             break;
190         }
191         case Format::IYUV:
192         {
193             const TensorInfo info_sub2(width / 2, height / 2, Format::U8);
194             std::get<0>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(0))->allocator(), coords, info);
195             std::get<1>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(1))->allocator(), coords, info_sub2);
196             std::get<2>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(2))->allocator(), coords, info_sub2);
197             break;
198         }
199         case Format::YUV444:
200             std::get<0>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(0))->allocator(), coords, info);
201             std::get<1>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(0))->allocator(), coords, info);
202             std::get<2>(_plane).allocator()->init(*dynamic_cast<Image *>(image->plane(0))->allocator(), coords, info);
203             break;
204         default:
205             ARM_COMPUTE_ERROR("Not supported");
206             break;
207     }
208
209     _info.init(width, height, format);
210 }
211
212 Image *MultiImage::plane(unsigned int index)
213 {
214     return &_plane[index];
215 }
216
217 const Image *MultiImage::plane(unsigned int index) const
218 {
219     return &_plane[index];
220 }