|
| 1 | + |
| 2 | +#include "ocio_display.h" |
| 3 | + |
| 4 | +#if ( USE_OCIO == 1 ) |
| 5 | +#include <OpenColorIO/OpenColorIO.h> |
| 6 | +#include <OpenColorIO/OpenColorTypes.h> |
| 7 | +namespace OCIO = OCIO_NAMESPACE; |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <Math/float3.h> |
| 11 | +#include <Math/mathutils.h> |
| 12 | + |
| 13 | +#define STB_IMAGE_WRITE_IMPLEMENTATION |
| 14 | +#include "../3rdParty/stbi/stbi.h" |
| 15 | + |
| 16 | +unsigned char FloatToByte(float f) |
| 17 | +{ |
| 18 | + return (unsigned char)RadeonProRender::clamp( (int)(f*255.0f) , (int)0, (int)255); |
| 19 | +} |
| 20 | + |
| 21 | + |
| 22 | +rpr_status OcioDisplay::Display( |
| 23 | + rpr_framebuffer framebuffer, |
| 24 | + const std::string& ocioFile, |
| 25 | + const std::string& src, |
| 26 | + |
| 27 | + const std::string& display, |
| 28 | + const std::string& view, |
| 29 | + |
| 30 | + float exposure, |
| 31 | + float gamma , |
| 32 | + const std::string& file_path, |
| 33 | + |
| 34 | + bool useOCIO |
| 35 | + ) |
| 36 | +{ |
| 37 | + rpr_status status = RPR_SUCCESS; |
| 38 | + |
| 39 | + rpr_framebuffer_desc fbInfoDesc; |
| 40 | + status = rprFrameBufferGetInfo(framebuffer,RPR_FRAMEBUFFER_DESC,sizeof(fbInfoDesc),&fbInfoDesc,0); |
| 41 | + if ( status != RPR_SUCCESS ) |
| 42 | + return status; |
| 43 | + |
| 44 | + const int64_t pxlCount = fbInfoDesc.fb_height * fbInfoDesc.fb_width; |
| 45 | + const int fltCount = pxlCount * 4; |
| 46 | + auto frame_buffer_data = std::make_unique<float[]>(fltCount); |
| 47 | + status = rprFrameBufferGetInfo(framebuffer, RPR_FRAMEBUFFER_DATA, fltCount*4 , frame_buffer_data.get() , NULL ); |
| 48 | + if ( status != RPR_SUCCESS ) |
| 49 | + return status; |
| 50 | + |
| 51 | + const int channelCountLDR = 3; |
| 52 | + auto framebufferLDR = std::make_unique<unsigned char[]>(pxlCount * channelCountLDR); |
| 53 | + |
| 54 | + |
| 55 | + #if ( USE_OCIO == 1 ) |
| 56 | + |
| 57 | + OCIO::ConstProcessorRcPtr processor = nullptr; |
| 58 | + OCIO::ConstCPUProcessorRcPtr cpuProcessor = nullptr;; |
| 59 | + OCIO::ConstConfigRcPtr config = nullptr; |
| 60 | + |
| 61 | + // Create a basic Display View Transform. |
| 62 | + try |
| 63 | + { |
| 64 | + config = OCIO::Config::CreateFromFile( ocioFile.c_str() ); |
| 65 | + |
| 66 | + OCIO::DisplayViewTransformRcPtr transform = OCIO::DisplayViewTransform::Create(); |
| 67 | + transform->setSrc( src.c_str() ); |
| 68 | + transform->setDisplay( display.c_str() ); |
| 69 | + transform->setView( view.c_str() ); |
| 70 | + |
| 71 | + OCIO::LegacyViewingPipelineRcPtr vp = OCIO::LegacyViewingPipeline::Create(); |
| 72 | + vp->setDisplayViewTransform(transform); |
| 73 | + vp->setLooksOverrideEnabled(true); |
| 74 | + vp->setLooksOverride(""); |
| 75 | + |
| 76 | + // exposure |
| 77 | + { |
| 78 | + double gain = exposure; |
| 79 | + const double slope4f[] = { gain, gain, gain, gain }; |
| 80 | + double m44[16]; |
| 81 | + double offset4[4]; |
| 82 | + OCIO::MatrixTransform::Scale(m44, offset4, slope4f); |
| 83 | + OCIO::MatrixTransformRcPtr mtx = OCIO::MatrixTransform::Create(); |
| 84 | + mtx->setMatrix(m44); |
| 85 | + mtx->setOffset(offset4); |
| 86 | + vp->setLinearCC(mtx); |
| 87 | + } |
| 88 | + |
| 89 | + // Post-display transform gamma |
| 90 | + if ( gamma != 0.0 ) |
| 91 | + { |
| 92 | + double exponent = 1.0/std::max(1e-6, (double)gamma); |
| 93 | + const double exponent4f[4] = { exponent, exponent, exponent, exponent }; |
| 94 | + OCIO::ExponentTransformRcPtr expTransform = OCIO::ExponentTransform::Create(); |
| 95 | + expTransform->setValue(exponent4f); |
| 96 | + vp->setDisplayCC(expTransform); |
| 97 | + } |
| 98 | + |
| 99 | + processor = vp->getProcessor(config); |
| 100 | + cpuProcessor = processor->getDefaultCPUProcessor(); |
| 101 | + |
| 102 | + } |
| 103 | + catch( std::exception& e ) |
| 104 | + { |
| 105 | + const char* what = e.what(); |
| 106 | + processor = nullptr; |
| 107 | + cpuProcessor = nullptr; |
| 108 | + config = nullptr; |
| 109 | + std::cout<<"Error during OCIO config: "<< what << std::endl; |
| 110 | + return RPR_ERROR_INTERNAL_ERROR; |
| 111 | + } |
| 112 | + |
| 113 | + #endif |
| 114 | + |
| 115 | + // run the post process on each pixels |
| 116 | + for(int64_t i=0; i<pxlCount; i++) // <-- can be multithreaded for perf improve |
| 117 | + { |
| 118 | + RadeonProRender::float3 rgb = RadeonProRender::float3( |
| 119 | + frame_buffer_data[i*4+0], |
| 120 | + frame_buffer_data[i*4+1], |
| 121 | + frame_buffer_data[i*4+2]); |
| 122 | + |
| 123 | + |
| 124 | + #if ( USE_OCIO == 1 ) |
| 125 | + if ( useOCIO ) // if using OCIO processor |
| 126 | + { |
| 127 | + cpuProcessor->applyRGB(&(rgb.x)); |
| 128 | + } |
| 129 | + else |
| 130 | + #endif |
| 131 | + { |
| 132 | + rgb.x = powf(rgb.x * exposure, 1.0f/gamma ); |
| 133 | + rgb.y = powf(rgb.y * exposure, 1.0f/gamma ); |
| 134 | + rgb.z = powf(rgb.z * exposure, 1.0f/gamma ); |
| 135 | + } |
| 136 | + |
| 137 | + framebufferLDR[i*3+0] = FloatToByte(rgb[0]); |
| 138 | + framebufferLDR[i*3+1] = FloatToByte(rgb[1]); |
| 139 | + framebufferLDR[i*3+2] = FloatToByte(rgb[2]); |
| 140 | + } |
| 141 | + |
| 142 | + // export to png file. |
| 143 | + stbi_write_png( |
| 144 | + file_path.c_str(), |
| 145 | + fbInfoDesc.fb_width, |
| 146 | + fbInfoDesc.fb_height, |
| 147 | + channelCountLDR, |
| 148 | + framebufferLDR.get(), |
| 149 | + fbInfoDesc.fb_width*channelCountLDR |
| 150 | + ); |
| 151 | + |
| 152 | + return RPR_SUCCESS; |
| 153 | +} |
0 commit comments