MCPcopy Create free account
hub / github.com/Arm-Examples/ML-examples / ReadPpmP6

Function ReadPpmP6

ethos-u-corstone-1000/app/src/common/InputUtils.cpp:72–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

70}
71
72Image 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
104Image ReadBmp24(const std::string& bmpPath)
105{

Callers 1

ReadImageFunction · 0.85

Calls 1

ReadPpmTokenFunction · 0.85

Tested by

no test coverage detected