@@ -70,24 +70,51 @@ bool QtOIIOHandler::read(QImage *image)
7070 // libRAW configuration
7171 configSpec.attribute (" raw:auto_bright" , 0 ); // don't want exposure correction
7272 configSpec.attribute (" raw:use_camera_wb" , 1 ); // want white balance correction
73+ #if OIIO_VERSION <= (10000 * 2 + 100 * 0 + 8) // OIIO_VERSION <= 2.0.8
74+ // In these previous versions of oiio, there was no Linear option
7375 configSpec.attribute (" raw:ColorSpace" , " sRGB" ); // want colorspace sRGB
76+ #else
77+ configSpec.attribute (" raw:ColorSpace" , " Linear" ); // want linear colorspace with sRGB primaries
78+ #endif
7479 configSpec.attribute (" raw:use_camera_matrix" , 3 ); // want to use embeded color profile
7580
7681 oiio::ImageBuf inBuf (path, 0 , 0 , NULL , &configSpec);
7782
7883 if (!inBuf.initialized ())
7984 throw std::runtime_error (" Can't find/open image file '" + path + " '." );
8085
86+ #if OIIO_VERSION <= (10000 * 2 + 100 * 0 + 8) // OIIO_VERSION <= 2.0.8
87+ // Workaround for bug in RAW colorspace management in previous versions of OIIO:
88+ // When asking sRGB we got sRGB primaries with linear gamma,
89+ // but oiio::ColorSpace was wrongly set to sRGB.
8190 oiio::ImageSpec inSpec = inBuf.spec ();
91+ if (inSpec.get_string_attribute (" oiio:ColorSpace" , " " ) == " sRGB" )
92+ {
93+ const auto in = oiio::ImageInput::open (path, nullptr );
94+ const std::string formatStr = in->format_name ();
95+ if (formatStr == " raw" )
96+ {
97+ // For the RAW plugin: override colorspace as linear (as the content is linear with sRGB primaries but declared as sRGB)
98+ inSpec.attribute (" oiio:ColorSpace" , " Linear" );
99+ qDebug () << " OIIO workaround: RAW input image " << QString::fromStdString (path) << " is in Linear." ;
100+ }
101+ }
102+ #else
103+ const oiio::ImageSpec& inSpec = inBuf.spec ();
104+ #endif
82105
83106 qDebug () << " [QtOIIO] width:" << inSpec.width << " , height:" << inSpec.height << " , nchannels:" << inSpec.nchannels ;
84107
85108 if (inSpec.nchannels >= 3 )
86109 {
87110 // Color conversion to sRGB
88- const std::string& colorSpace = inSpec.get_string_attribute (" oiio:Colorspace" , " sRGB" );
89- if (colorSpace != " sRGB" )
111+ const std::string& colorSpace = inSpec.get_string_attribute (" oiio:ColorSpace" , " sRGB" ); // default image color space is sRGB
112+
113+ if (colorSpace != " sRGB" ) // color conversion to sRGB
114+ {
90115 oiio::ImageBufAlgo::colorconvert (inBuf, inBuf, colorSpace, " sRGB" );
116+ qDebug () << " Convert image " << QString::fromStdString (path) << " from " << QString::fromStdString (colorSpace) << " to sRGB colorspace" ;
117+ }
91118 }
92119
93120 int nchannels = 0 ;
0 commit comments