| 129 | } |
| 130 | |
| 131 | std::tuple<unsigned int, unsigned int, int> parse_netpbm_format_header(std::ifstream &fs, char number) |
| 132 | { |
| 133 | // check file type magic number is valid |
| 134 | std::array<char, 2> magic_number{{0}}; |
| 135 | fs >> magic_number[0] >> magic_number[1]; |
| 136 | |
| 137 | if (magic_number[0] != 'P' || magic_number[1] != number) |
| 138 | { |
| 139 | throw std::runtime_error("File type magic number not supported"); |
| 140 | } |
| 141 | |
| 142 | discard_comments_and_spaces(fs); |
| 143 | |
| 144 | unsigned int width = 0; |
| 145 | fs >> width; |
| 146 | |
| 147 | discard_comments_and_spaces(fs); |
| 148 | |
| 149 | unsigned int height = 0; |
| 150 | fs >> height; |
| 151 | |
| 152 | discard_comments_and_spaces(fs); |
| 153 | |
| 154 | int max_value = 0; |
| 155 | fs >> max_value; |
| 156 | |
| 157 | if (!fs.good()) |
| 158 | { |
| 159 | throw std::runtime_error("Cannot read image dimensions"); |
| 160 | } |
| 161 | |
| 162 | if (max_value != 255) |
| 163 | { |
| 164 | throw std::runtime_error("RawTensor doesn't have 8-bit values"); |
| 165 | } |
| 166 | |
| 167 | discard_comments(fs); |
| 168 | |
| 169 | if (isspace(fs.peek()) == 0) |
| 170 | { |
| 171 | throw std::runtime_error("Invalid image header"); |
| 172 | } |
| 173 | |
| 174 | fs.ignore(1); |
| 175 | |
| 176 | return std::make_tuple(width, height, max_value); |
| 177 | } |
| 178 | |
| 179 | std::tuple<unsigned int, unsigned int, int> parse_ppm_header(std::ifstream &fs) |
| 180 | { |
no test coverage detected