blob: 8d07e995b57622292aab735817d59c09796d6716 [file] [log] [blame]
John Richardson4268fbe2017-08-29 11:22:35 +01001/*
2 * Copyright (c) 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 "IntegralImage.h"
25
26namespace arm_compute
27{
28namespace test
29{
30namespace validation
31{
32namespace reference
33{
34template <typename T>
35SimpleTensor<uint32_t> integral_image(const SimpleTensor<T> &src)
36{
37 SimpleTensor<uint32_t> dst(src.shape(), DataType::U32);
38
39 // Length of dimensions
40 const size_t width = src.shape().x();
41 const size_t height = src.shape().y();
42 const size_t depth = src.shape().total_size_upper(2);
43
44 const size_t image_size = width * height;
45
46 for(size_t z = 0; z < depth; ++z)
47 {
48 size_t current_image = z * image_size;
49
50 //First element of each image
51 dst[current_image] = src[current_image];
52
53 // First row of each image (add only pixel on the left)
54 for(size_t x = 1; x < width; ++x)
55 {
56 dst[current_image + x] = static_cast<uint32_t>(src[current_image + x]) + dst[current_image + x - 1];
57 }
58
59 // Subsequent rows
60 for(size_t y = 1; y < height; ++y)
61 {
62 size_t current_row = current_image + (width * y);
63
64 // First element of each row (add only pixel up)
65 dst[current_row] = static_cast<uint32_t>(src[current_row]) + dst[current_row - width];
66
67 // Following row elements
68 for(size_t x = 1; x < width; ++x)
69 {
70 size_t current_pixel = current_row + x;
71
72 // out = in + up(out) + left(out) - up_left(out)
73 dst[current_pixel] = static_cast<uint32_t>(src[current_pixel]) + dst[current_pixel - 1]
74 + dst[current_pixel - width] - dst[current_pixel - width - 1];
75 }
76 }
77 }
78
79 return dst;
80}
81
82template SimpleTensor<uint32_t> integral_image(const SimpleTensor<uint8_t> &src);
83} // namespace reference
84} // namespace validation
85} // namespace test
86} // namespace arm_compute