X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;ds=sidebyside;f=gamma_compression_effect.cpp;h=5581fca32c38c14e15fa7905460761a68d271ddb;hb=3bb4d84f2c3914ac815b1c3fbcdea0439aa30d74;hp=b6a737bf4416315b79f1f8e58449a4440679ef60;hpb=17c083aad45a10df14c38cfe879a87220dfd4fb9;p=movit diff --git a/gamma_compression_effect.cpp b/gamma_compression_effect.cpp index b6a737b..5581fca 100644 --- a/gamma_compression_effect.cpp +++ b/gamma_compression_effect.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include "gamma_compression_effect.h" @@ -7,18 +9,48 @@ GammaCompressionEffect::GammaCompressionEffect() : destination_curve(GAMMA_LINEAR) { register_int("destination_curve", (int *)&destination_curve); + memset(compression_curve, 0, sizeof(compression_curve)); + register_1d_texture("compression_curve_tex", compression_curve, COMPRESSION_CURVE_SIZE); } std::string GammaCompressionEffect::output_fragment_shader() { - switch (destination_curve) { - case GAMMA_LINEAR: - return read_file("identity.glsl"); - case GAMMA_sRGB: - return read_file("gamma_compression_effect_srgb.glsl"); - case GAMMA_REC_709: // and GAMMA_REC_601 - return read_file("gamma_compression_effect_rec709.glsl"); - default: - assert(false); + if (destination_curve == GAMMA_LINEAR) { + return read_file("identity.frag"); } + if (destination_curve == GAMMA_sRGB) { + for (unsigned i = 0; i < COMPRESSION_CURVE_SIZE; ++i) { + float x = i / (float)(COMPRESSION_CURVE_SIZE - 1); + if (x < 0.0031308f) { + compression_curve[i] = 12.92f * x; + } else { + compression_curve[i] = 1.055f * pow(x, 1.0f / 2.4f) - 0.055f; + } + } + invalidate_1d_texture("compression_curve_tex"); + return read_file("gamma_compression_effect.frag"); + } + if (destination_curve == GAMMA_REC_709 || // And Rec. 601, and 10-bit Rec. 2020. + destination_curve == GAMMA_REC_2020_12_BIT) { + // Rec. 2020, page 3. + float alpha, beta; + if (destination_curve == GAMMA_REC_2020_12_BIT) { + alpha = 1.0993f; + beta = 0.0181f; + } else { + alpha = 1.099f; + beta = 0.018f; + } + for (unsigned i = 0; i < COMPRESSION_CURVE_SIZE; ++i) { + float x = i / (float)(COMPRESSION_CURVE_SIZE - 1); + if (x < beta) { + compression_curve[i] = 4.5f * x; + } else { + compression_curve[i] = alpha * pow(x, 0.45f) - (alpha - 1.0f); + } + } + invalidate_1d_texture("compression_curve_tex"); + return read_file("gamma_compression_effect.frag"); + } + assert(false); }