| 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, |