blob: 4d87d5305ab3ebb7036358a3601a53543ea11247 [file] [log] [blame]
Anthony Barbier871448e2017-03-24 14:54:29 +00001/*
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 "Utils.h"
25
26#include <cctype>
27#include <iomanip>
28#include <string>
Yuxin Wub3640812017-03-31 14:54:34 -070029#include <cerrno>
Anthony Barbier871448e2017-03-24 14:54:29 +000030namespace
31{
32/* Advance the iterator to the first character which is not a comment
33 *
34 * @param[in,out] fs Stream to drop comments from
35 */
36void discard_comments(std::ifstream &fs)
37{
38 while(fs.peek() == '#')
39 {
40 fs.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
41 }
42}
43
44/* Advance the string iterator to the next character which is neither a space or a comment
45 *
46 * @param[in,out] fs Stream to drop comments from
47 */
48void discard_comments_and_spaces(std::ifstream &fs)
49{
50 while(true)
51 {
52 discard_comments(fs);
53
54 if(isspace(fs.peek()) == 0)
55 {
56 break;
57 }
58
59 fs.ignore(1);
60 }
61}
62} // namespace
63
64int test_helpers::run_example(int argc, const char **argv, example &func)
65{
66 std::cout << "\n"
67 << argv[0] << "\n\n";
68
69 try
70 {
71 func(argc, argv);
72
73 std::cout << "\nTest passed\n";
74 return 0;
75 }
76#ifdef ARM_COMPUTE_CL
77 catch(cl::Error &err)
78 {
79 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
80 std::cerr << std::endl
81 << "ERROR " << err.what() << "(" << err.err() << ")" << std::endl;
82 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
83 }
84#endif /* ARM_COMPUTE_CL */
85 catch(std::runtime_error &err)
86 {
87 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
88 std::cerr << std::endl
89 << "ERROR " << err.what() << " " << (errno ? strerror(errno) : "") << std::endl;
90 std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
91 }
92
93 std::cout << "\nTest FAILED\n";
94
95 return -1;
96}
97
98void test_helpers::draw_detection_rectangle(ITensor *tensor, const DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b)
99{
100 ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(tensor, Format::RGB888);
101
102 uint8_t *top = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y)) + tensor->buffer();
103 uint8_t *bottom = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y + rect.height)) + tensor->buffer();
104 uint8_t *left = top;
105 uint8_t *right = tensor->info()->offset_element_in_bytes(Coordinates(rect.x + rect.width, rect.y)) + tensor->buffer();
106 size_t stride = tensor->info()->strides_in_bytes()[Window::DimY];
107
108 for(size_t x = 0; x < rect.width; ++x)
109 {
110 top[0] = r;
111 top[1] = g;
112 top[2] = b;
113 bottom[0] = r;
114 bottom[1] = g;
115 bottom[2] = b;
116
117 top += 3;
118 bottom += 3;
119 }
120
121 for(size_t y = 0; y < rect.height; ++y)
122 {
123 left[0] = r;
124 left[1] = g;
125 left[2] = b;
126 right[0] = r;
127 right[1] = g;
128 right[2] = b;
129
130 left += stride;
131 right += stride;
132 }
133}
134
135std::tuple<unsigned int, unsigned int, int> test_helpers::parse_ppm_header(std::ifstream &fs)
136{
137 // Check the PPM magic number is valid
138 std::array<char, 2> magic_number{ { 0 } };
139 fs >> magic_number[0] >> magic_number[1];
140 ARM_COMPUTE_ERROR_ON_MSG(magic_number[0] != 'P' || magic_number[1] != '6', "Invalid file type");
141 ARM_COMPUTE_UNUSED(magic_number);
142
143 discard_comments_and_spaces(fs);
144
145 unsigned int width = 0;
146 fs >> width;
147
148 discard_comments_and_spaces(fs);
149
150 unsigned int height = 0;
151 fs >> height;
152
153 discard_comments_and_spaces(fs);
154
155 int max_val = 0;
156 fs >> max_val;
157
158 discard_comments(fs);
159
160 ARM_COMPUTE_ERROR_ON_MSG(isspace(fs.peek()) == 0, "Invalid PPM header");
161 fs.ignore(1);
162
163 return std::make_tuple(width, height, max_val);
Yuxin Wub3640812017-03-31 14:54:34 -0700164}