| 298 | |
| 299 | #ifdef OIDN_USE_OPENIMAGEIO |
| 300 | std::shared_ptr<ImageBuffer> loadImageOIIO(const DeviceRef& device, |
| 301 | const std::string& filename, |
| 302 | DataType dataType, |
| 303 | Storage storage) |
| 304 | { |
| 305 | auto in = OIIO::ImageInput::open(filename); |
| 306 | if (!in) |
| 307 | throw std::runtime_error("cannot open image file: '" + filename + "'"); |
| 308 | |
| 309 | const OIIO::ImageSpec& spec = in->spec(); |
| 310 | const int numChannels = std::min(spec.nchannels, 3); |
| 311 | |
| 312 | if (dataType == DataType::Undefined) |
| 313 | dataType = (spec.channelformat(0) == OIIO::TypeDesc::HALF) ? DataType::Float16 : DataType::Float32; |
| 314 | |
| 315 | auto image = std::make_shared<ImageBuffer>(device, spec.width, spec.height, numChannels, |
| 316 | dataType, storage); |
| 317 | bool success = in->read_image(0, 0, 0, numChannels, |
| 318 | dataType == DataType::Float16 ? OIIO::TypeDesc::HALF : OIIO::TypeDesc::FLOAT, image->getHostData()); |
| 319 | |
| 320 | in->close(); |
| 321 | |
| 322 | #if OIIO_VERSION < 10903 |
| 323 | OIIO::ImageInput::destroy(in); |
| 324 | #endif |
| 325 | |
| 326 | if (!success) |
| 327 | throw std::runtime_error("failed to read image data"); |
| 328 | |
| 329 | return image; |
| 330 | } |
| 331 | |
| 332 | void saveImageOIIO(const std::string& filename, const ImageBuffer& image) |
| 333 | { |