mwl4 / ConverterPIX

Conversion tool that is able to convert from the game binary formats (.pmx) to the middle formats (.pix) in Euro Truck Simulator 2 & American Truck Simulator.
https://truckersmp.com/
GNU Lesser General Public License v3.0
67 stars 27 forks source link

Fix to linear RGB to sRGB conversion for color values above 1.0 #38

Closed theHarven closed 4 years ago

theHarven commented 4 years ago

According to 50keda's message here: https://forum.scssoft.com/viewtopic.php?p=1409264#p1409264 when converting linear RGB color values written in .mat files to sRGB values used in .pit files, converter_pix should first normalize all RGB components by dividing them by the highest value (if that value is above 1.0) so that the values are scaled to 0...1 range, then should perform the lin2s conversion and finally scale all values back by multiplying all sRGB components by the same highest value used at the beginning. Currently when doing a conversion loop (.mat -> .pit -> .mat -> .pit -> ...) with converter_pix and scs conversion tools, color values above 1.0 are getting broken with each iteration so they finally get down to 1.0. After implementing my fix I tested it and it looks that all RGB components are now correctly converted - you can see the test result here - https://forum.scssoft.com/viewtopic.php?p=1410835#p1410835

About the implementation: I refactored the static function convertAttribIfNeeded() so that it now accepts Material::Attribute reference (in/out) as the first parameter and I made the function a part of the Material class so that it can access the private member Material::Attribute::m_value. I also changed lin2s and s2lin to use double instead of float values so there is no need anymore to cast passed values to float.