From 6ff3b19ee6120edf015fad8caab2991faa3070af Mon Sep 17 00:00:00 2001 From: Anthony Barbier Date: Mon, 4 Sep 2017 18:44:23 +0100 Subject: COMPMID-344 Updated doxygen Change-Id: I32f7b84daa560e460b77216add529c8fa8b327ae --- utils/Utils.cpp | 171 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 utils/Utils.cpp (limited to 'utils/Utils.cpp') diff --git a/utils/Utils.cpp b/utils/Utils.cpp new file mode 100644 index 0000000000..5316690a3d --- /dev/null +++ b/utils/Utils.cpp @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "Utils.h" + +#include +#include +#include +#include + +namespace arm_compute +{ +namespace utils +{ +namespace +{ +/* Advance the iterator to the first character which is not a comment + * + * @param[in,out] fs Stream to drop comments from + */ +void discard_comments(std::ifstream &fs) +{ + while(fs.peek() == '#') + { + fs.ignore(std::numeric_limits::max(), '\n'); + } +} + +/* Advance the string iterator to the next character which is neither a space or a comment + * + * @param[in,out] fs Stream to drop comments from + */ +void discard_comments_and_spaces(std::ifstream &fs) +{ + while(true) + { + discard_comments(fs); + + if(isspace(fs.peek()) == 0) + { + break; + } + + fs.ignore(1); + } +} +} // namespace + +int run_example(int argc, const char **argv, example &func) +{ + std::cout << "\n" + << argv[0] << "\n\n"; + + try + { + func(argc, argv); + + std::cout << "\nTest passed\n"; + return 0; + } +#ifdef ARM_COMPUTE_CL + catch(cl::Error &err) + { + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + std::cerr << std::endl + << "ERROR " << err.what() << "(" << err.err() << ")" << std::endl; + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + } +#endif /* ARM_COMPUTE_CL */ + catch(std::runtime_error &err) + { + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + std::cerr << std::endl + << "ERROR " << err.what() << " " << (errno ? strerror(errno) : "") << std::endl; + std::cerr << "!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl; + } + + std::cout << "\nTest FAILED\n"; + + return -1; +} + +void draw_detection_rectangle(ITensor *tensor, const DetectionWindow &rect, uint8_t r, uint8_t g, uint8_t b) +{ + ARM_COMPUTE_ERROR_ON_FORMAT_NOT_IN(tensor, Format::RGB888); + + uint8_t *top = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y)) + tensor->buffer(); + uint8_t *bottom = tensor->info()->offset_element_in_bytes(Coordinates(rect.x, rect.y + rect.height)) + tensor->buffer(); + uint8_t *left = top; + uint8_t *right = tensor->info()->offset_element_in_bytes(Coordinates(rect.x + rect.width, rect.y)) + tensor->buffer(); + size_t stride = tensor->info()->strides_in_bytes()[Window::DimY]; + + for(size_t x = 0; x < rect.width; ++x) + { + top[0] = r; + top[1] = g; + top[2] = b; + bottom[0] = r; + bottom[1] = g; + bottom[2] = b; + + top += 3; + bottom += 3; + } + + for(size_t y = 0; y < rect.height; ++y) + { + left[0] = r; + left[1] = g; + left[2] = b; + right[0] = r; + right[1] = g; + right[2] = b; + + left += stride; + right += stride; + } +} + +std::tuple parse_ppm_header(std::ifstream &fs) +{ + // Check the PPM magic number is valid + std::array magic_number{ { 0 } }; + fs >> magic_number[0] >> magic_number[1]; + ARM_COMPUTE_ERROR_ON_MSG(magic_number[0] != 'P' || magic_number[1] != '6', "Invalid file type"); + ARM_COMPUTE_UNUSED(magic_number); + + discard_comments_and_spaces(fs); + + unsigned int width = 0; + fs >> width; + + discard_comments_and_spaces(fs); + + unsigned int height = 0; + fs >> height; + + discard_comments_and_spaces(fs); + + int max_val = 0; + fs >> max_val; + + discard_comments(fs); + + ARM_COMPUTE_ERROR_ON_MSG(isspace(fs.peek()) == 0, "Invalid PPM header"); + fs.ignore(1); + + return std::make_tuple(width, height, max_val); +} +} // namespace utils +} // namespace arm_compute -- cgit v1.2.1