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