Log in

View Full Version : Convert to Linear Gamma Expr


MysteryX
17th June 2021, 03:09
I'm trying to convert this Avisynth function to convert Gamma into Linear

ColorYUV(gamma_y = ((1.0 / 2.2) - 1.0) * 256.0)

VapourSynth equivalence doc says to use Expr and "Do the adjustment yourself"

How do I achieve this in VapourSynth?

feisty2
17th June 2021, 06:42
fmtc.transfer (https://forum.doom9.org/showthread.php?t=166504)

MysteryX
17th June 2021, 18:28
In this case, I don't need maximum precision, and it's to put in a plugin DLL so I'd rather avoid unnecessary extra dependency.

feisty2
17th June 2021, 19:28
then you're on your own to write an RPN expression that undoes a gamma curve.

MysteryX
17th June 2021, 20:57
fmtc.transfer
As input, the function accepts only RGB and grayscale colorspaces.

Won't help me here. Unless converting to RGB, to linear, and back to YUV. Where Gamma should only be adjusting Luma, easier in YUV format.

feisty2
17th June 2021, 21:24
Y (of YUV) is a grayscale colorspace. you simply extract the Y plane, do whatever you want, then merge the result with the untouched UV planes

feisty2
17th June 2021, 21:55
Unless converting to RGB, to linear, and back to YUV. Where Gamma should only be adjusting Luma, easier in YUV format.


nonsense, the gamma curve is defined on the RGB colorspace and undoing gamma compression on Y is mathematically incorrect. note that the exponent function (the formula of a gamma curve) is not invariant to linear operators ((e^x + e^y + e^z) / 3 != e^((x + y + z) / 3)) and therefore your manipulations on the Y plane are not isomorphic to the correct definition of gamma compression, even tho no one's stopping you from applying such "subjective" processing.

MysteryX
18th June 2021, 01:41
This is doing a quite good job for my needs.

video = core.resize.Point(video, format=vs.GRAY16)
video = core.fmtc.transfer(video, transs="709", transd="linear", fulls=0)
video = core.resize.Point(video, format=vs.GRAY8)

The addition of "fulls=0" to clips with TV range does make it better, but this argument must be set manually.

Are clip properties such as "_ColorRange" generally set, or do I need to request it as an input parameter?

poisondeathray
18th June 2021, 15:09
as another option core.resize has transfer_in_s="", transfer_s="" and some limited choices, so you necessarily need an external .dll plugin if you're dealing with common situations (fmtc has more options for transfer)

"_ColorRange" depends on the source filter, and the specific video metadata. LSmash and FFMS2 will pass that info if it's in the source. But sometimes you have improperly flagged video (not just for ColorRange, other parameters too). Not sure how the best way to handle it for a plugin/script. Maybe use it unless overriden by user.

As a user, you always having to double check things manually and use SetFrameProp for various parameters - it the pros/cons of using metadata or props to guide functions. It's not always a great way to handle things IMO