From 8de92619e223225aabdca873c02f231d8e941fd1 Mon Sep 17 00:00:00 2001 From: John Richardson Date: Thu, 22 Feb 2018 14:09:31 +0000 Subject: COMPMID-585: Port OpticalFlow to new validation Change-Id: Ia36bd11ca27420d3059eea15df81b237900149ec Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/125175 Tested-by: Jenkins Reviewed-by: John Richardson Reviewed-by: Anthony Barbier --- tests/AssetsLibrary.cpp | 75 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 17 deletions(-) (limited to 'tests/AssetsLibrary.cpp') diff --git a/tests/AssetsLibrary.cpp b/tests/AssetsLibrary.cpp index 1cbd3b4e3d..ee876f91e3 100644 --- a/tests/AssetsLibrary.cpp +++ b/tests/AssetsLibrary.cpp @@ -120,15 +120,15 @@ void discard_comments_and_spaces(std::ifstream &fs) } } -std::tuple parse_ppm_header(std::ifstream &fs) +std::tuple parse_netpbm_format_header(std::ifstream &fs, char number) { - // Check the PPM magic number is valid + // check file type magic number is valid std::array magic_number{ { 0 } }; fs >> magic_number[0] >> magic_number[1]; - if(magic_number[0] != 'P' || magic_number[1] != '6') + if(magic_number[0] != 'P' || magic_number[1] != number) { - throw std::runtime_error("Only raw PPM format is suported"); + throw std::runtime_error("File type magic number not supported"); } discard_comments_and_spaces(fs); @@ -160,7 +160,7 @@ std::tuple parse_ppm_header(std::ifstream &fs) if(isspace(fs.peek()) == 0) { - throw std::runtime_error("Invalid PPM header"); + throw std::runtime_error("Invalid image header"); } fs.ignore(1); @@ -168,6 +168,39 @@ std::tuple parse_ppm_header(std::ifstream &fs) return std::make_tuple(width, height, max_value); } +std::tuple parse_ppm_header(std::ifstream &fs) +{ + return parse_netpbm_format_header(fs, '6'); +} + +std::tuple parse_pgm_header(std::ifstream &fs) +{ + return parse_netpbm_format_header(fs, '5'); +} + +void check_image_size(std::ifstream &fs, size_t raw_size) +{ + const size_t current_position = fs.tellg(); + fs.seekg(0, std::ios_base::end); + const size_t end_position = fs.tellg(); + fs.seekg(current_position, std::ios_base::beg); + + if((end_position - current_position) < raw_size) + { + throw std::runtime_error("Not enough data in file"); + } +} + +void read_image_buffer(std::ifstream &fs, RawTensor &raw) +{ + fs.read(reinterpret_cast(raw.data()), raw.size()); + + if(!fs.good()) + { + throw std::runtime_error("Failure while reading image buffer"); + } +} + RawTensor load_ppm(const std::string &path) { std::ifstream file(path, std::ios::in | std::ios::binary); @@ -184,24 +217,31 @@ RawTensor load_ppm(const std::string &path) RawTensor raw(TensorShape(width, height), Format::RGB888); - // Check if the file is large enough to fill the image - const size_t current_position = file.tellg(); - file.seekg(0, std::ios_base::end); - const size_t end_position = file.tellg(); - file.seekg(current_position, std::ios_base::beg); + check_image_size(file, raw.size()); + read_image_buffer(file, raw); - if((end_position - current_position) < raw.size()) - { - throw std::runtime_error("Not enough data in file"); - } + return raw; +} - file.read(reinterpret_cast(raw.data()), raw.size()); +RawTensor load_pgm(const std::string &path) +{ + std::ifstream file(path, std::ios::in | std::ios::binary); if(!file.good()) { - throw std::runtime_error("Failure while reading image buffer"); + throw framework::FileNotFound("Could not load PGM image: " + path); } + unsigned int width = 0; + unsigned int height = 0; + + std::tie(width, height, std::ignore) = parse_pgm_header(file); + + RawTensor raw(TensorShape(width, height), Format::U8); + + check_image_size(file, raw.size()); + read_image_buffer(file, raw); + return raw; } } // namespace @@ -244,7 +284,8 @@ const AssetsLibrary::Loader &AssetsLibrary::get_loader(const std::string &extens { static std::unordered_map loaders = { - { "ppm", load_ppm } + { "ppm", load_ppm }, + { "pgm", load_pgm } }; const auto it = loaders.find(extension); -- cgit v1.2.1