| 70 | } |
| 71 | |
| 72 | Image ReadPpmP6(const std::string& ppmPath) |
| 73 | { |
| 74 | std::ifstream ppmStream(ppmPath, std::ios::binary); |
| 75 | if (!ppmStream.is_open()) { |
| 76 | throw std::runtime_error("failed to open PPM file: " + ppmPath); |
| 77 | } |
| 78 | |
| 79 | const std::string magic = ReadPpmToken(ppmStream); |
| 80 | if (magic != "P6") { |
| 81 | throw std::runtime_error("only P6 PPM input is supported: " + ppmPath); |
| 82 | } |
| 83 | |
| 84 | const int width = std::stoi(ReadPpmToken(ppmStream)); |
| 85 | const int height = std::stoi(ReadPpmToken(ppmStream)); |
| 86 | const int maxVal = std::stoi(ReadPpmToken(ppmStream)); |
| 87 | if (width <= 0 || height <= 0 || maxVal != 255) { |
| 88 | throw std::runtime_error("invalid PPM header values in: " + ppmPath); |
| 89 | } |
| 90 | |
| 91 | ppmStream.get(); |
| 92 | |
| 93 | const size_t bytes = static_cast<size_t>(width) * static_cast<size_t>(height) * 3U; |
| 94 | std::vector<uint8_t> rgb(bytes); |
| 95 | ppmStream.read(reinterpret_cast<char*>(rgb.data()), static_cast<std::streamsize>(bytes)); |
| 96 | |
| 97 | if (static_cast<size_t>(ppmStream.gcount()) != bytes) { |
| 98 | throw std::runtime_error("PPM payload size mismatch in: " + ppmPath); |
| 99 | } |
| 100 | |
| 101 | return Image{width, height, std::move(rgb)}; |
| 102 | } |
| 103 | |
| 104 | Image ReadBmp24(const std::string& bmpPath) |
| 105 | { |
no test coverage detected