Tries to open the file, returning true on success and false on failure. On success, m_inputFile will be valid. If throwOnFailure is true then a descriptive Exception is thrown rather than false being returned.
| 443 | // m_inputFile will be valid. If throwOnFailure is true then a descriptive |
| 444 | // Exception is thrown rather than false being returned. |
| 445 | bool open( bool throwOnFailure = false ) |
| 446 | { |
| 447 | if( m_cache && m_reader->fileName() == m_inputFileName ) |
| 448 | { |
| 449 | // we already opened the right file successfully |
| 450 | return true; |
| 451 | } |
| 452 | |
| 453 | m_inputFileName = ""; |
| 454 | #if OIIO_VERSION >= 30000 |
| 455 | m_cache = ImageCache::create( /* shared */ false ); |
| 456 | #else |
| 457 | m_cache.reset( ImageCache::create( /* shared */ false ), destroyImageCache ); |
| 458 | #endif |
| 459 | // Autompip ensures that if a miplevel is requested that the file |
| 460 | // doesn't contain, OIIO creates the respective level on the fly. |
| 461 | m_cache->attribute( "automip", 1 ); |
| 462 | |
| 463 | // a non-null spec indicates the image was opened successfully |
| 464 | const ImageSpec *spec = m_cache->imagespec( ustring( m_reader->fileName() ), 0, miplevel() ); |
| 465 | if( spec ) |
| 466 | { |
| 467 | m_inputFileName = m_reader->fileName(); |
| 468 | |
| 469 | // Get the fileFormat and store the current and linear color spaces |
| 470 | // We do this so we can perform color conversion on the image after |
| 471 | // loading the data. |
| 472 | const char *fileFormat = nullptr; |
| 473 | m_cache->get_image_info( |
| 474 | m_inputFileName, |
| 475 | 0, miplevel(), // subimage, miplevel |
| 476 | ustring( "fileformat" ), |
| 477 | OIIO::TypeString, &fileFormat |
| 478 | ); |
| 479 | |
| 480 | if( strcmp( fileFormat, "png" ) == 0 ) |
| 481 | { |
| 482 | // The most common use for loading PNGs via Cortex is for icons in Gaffer. |
| 483 | // If we were to use the OCIO config to guess the colorspaces as below, we |
| 484 | // would get it spectacularly wrong. For instance, with an ACES config the |
| 485 | // resulting icons are so washed out as to be illegible. Instead, we hardcode |
| 486 | // the rudimentary colour spaces much more likely to be associated with a PNG. |
| 487 | // These are supported by OIIO regardless of what OCIO config is in use. |
| 488 | /// \todo Should this apply to other formats too? Can we somehow fix |
| 489 | /// `OpenImageIOAlgo::colorSpace` instead? |
| 490 | m_linearColorSpace = "linear"; |
| 491 | m_currentColorSpace = "sRGB"; |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | m_linearColorSpace = OpenImageIOAlgo::colorSpace( "", *spec ); |
| 496 | m_currentColorSpace = OpenImageIOAlgo::colorSpace( fileFormat, *spec ); |
| 497 | } |
| 498 | |
| 499 | return true; |
| 500 | } |
| 501 | |
| 502 | if( !throwOnFailure ) |
no test coverage detected