MCPcopy Create free account
hub / github.com/RenderKit/oidn / saveImagePPM

Function saveImagePPM

apps/utils/image_io.cpp:263–297  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

261 }
262
263 void saveImagePPM(const std::string& filename, const ImageBuffer& image)
264 {
265 const int H = image.getH();
266 const int W = image.getW();
267 const int C = image.getC();
268
269 std::string id;
270 if (C == 3)
271 id = "P6";
272 else if (C == 1)
273 id = "P5";
274 else
275 throw std::runtime_error("unsupported number of channels for PPM image");
276
277 // Open the file
278 std::ofstream file(filename, std::ios::binary);
279 if (file.fail())
280 throw std::runtime_error("cannot open image file: '" + filename + "'");
281
282 // Write the header
283 file << id << std::endl;
284 file << W << " " << H << std::endl;
285 file << "255" << std::endl;
286
287 // Write the pixels
288 for (int i = 0; i < W*H; ++i)
289 {
290 for (int c = 0; c < C; ++c)
291 {
292 const float x = image.get(i*C+c);
293 const int ch = std::min(std::max(int(x * 255.f), 0), 255);
294 file.put(char(ch));
295 }
296 }
297 }
298
299 #ifdef OIDN_USE_OPENIMAGEIO
300 std::shared_ptr<ImageBuffer> loadImageOIIO(const DeviceRef& device,

Callers 1

saveImageFunction · 0.85

Calls 7

failMethod · 0.80
minFunction · 0.50
maxFunction · 0.50
getHMethod · 0.45
getWMethod · 0.45
getCMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected