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