X-Git-Url: https://git.sesse.net/?p=movit;a=blobdiff_plain;f=gamma_compression_effect.cpp;h=079f849e16a62445c6862f6477fa84a511f920be;hp=581542884aa81dab44e398e0818422c53763afcb;hb=849e2cd0910a468c1aa7b11cd855c685421781ae;hpb=193e18b583fde18f2f3b3192d61121f3173e0e27 diff --git a/gamma_compression_effect.cpp b/gamma_compression_effect.cpp index 5815428..079f849 100644 --- a/gamma_compression_effect.cpp +++ b/gamma_compression_effect.cpp @@ -1,25 +1,116 @@ #include +#include "effect_util.h" #include "gamma_compression_effect.h" #include "util.h" +using namespace std; + +namespace movit { + GammaCompressionEffect::GammaCompressionEffect() : destination_curve(GAMMA_LINEAR) { register_int("destination_curve", (int *)&destination_curve); } -std::string GammaCompressionEffect::output_glsl() +string GammaCompressionEffect::output_fragment_shader() +{ + if (destination_curve == GAMMA_LINEAR) { + return read_file("identity.frag"); + } + if (destination_curve == GAMMA_sRGB || + destination_curve == GAMMA_REC_709 || // Also includes Rec. 601, and 10-bit Rec. 2020. + destination_curve == GAMMA_REC_2020_12_BIT) { + return read_file("gamma_compression_effect.frag"); + } + assert(false); +} + +void GammaCompressionEffect::set_gl_state(GLuint glsl_program_num, const string &prefix, unsigned *sampler_num) { - 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 - // Not implemented yet. - assert(false); - default: - assert(false); + Effect::set_gl_state(glsl_program_num, prefix, sampler_num); + + // See GammaExpansionEffect for more details about the approximations in use; + // we will primarily deal with the differences here. + // + // Like in expansion, we have a piecewise curve that for very low values + // (up to some β) are linear. Above β, we have a power curve that looks + // like this: + // + // y = ɑ x^ɣ - (ɑ - 1) + // + // Like in expansion, we want to approximate this by some minimax polynomial + // in the range β..1. However, in this case, ɣ is typically around 0.4, and + // x^0.4 is actually very hard to approximate accurately in this range. + // We do a little trick by instead asking for a polynomial of s=sqrt(x), + // which means we instead need something like s^0.8, which is much easier. + // This warps the input space a bit as seen by the minimax algorithm, + // but since we are optimizing for _maximum_ error and not _average_, + // we should not add any extra weighting factors. + // + // However, since we have problems reaching the desired accuracy (~25% + // of a pixel level), especially for sRGB, we modify w(x) from + // GammaExpansionEffect to remove the special handling of the area + // around β; it is not really as useful when the next step is just a + // dither and round anyway. We keep it around 1, though, since that + // seems to hurt less. + // + // The Maple commands this time around become (again using sRGB as an example): + // + // > alpha := 1.055; + // > beta := 0.0031308; + // > gamma_ := 1.0/2.4; + // > w := x -> piecewise(x > 0.999, 10, 1); + // > numapprox[minimax](alpha * (x^2)^gamma_ - (alpha - 1), x=sqrt(beta)..1, [4,0], w(x^2), 'maxerror'); + // + // Since the error here is possible to interpret on a uniform scale, + // we also show it as a value relative to a 8-, 10- or 12-bit pixel level, + // as appropriate. + + if (destination_curve == GAMMA_sRGB) { + // From the Wikipedia article on sRGB; ɑ (called a+1 there) = 1.055, + // β = 0.0031308, ɣ = 1/2.4. + // maxerror = 0.000785 = 0.200 * 255 + // error at 1.0 = 0.000078 = 0.020 * 255 + set_uniform_float(glsl_program_num, prefix, "linear_scale", 12.92); + set_uniform_float(glsl_program_num, prefix, "c0", -0.03679675939); + set_uniform_float(glsl_program_num, prefix, "c1", 1.443803073); + set_uniform_float(glsl_program_num, prefix, "c2", -0.9239780987); + set_uniform_float(glsl_program_num, prefix, "c3", 0.8060491596); + set_uniform_float(glsl_program_num, prefix, "c4", -0.2891558568); + set_uniform_float(glsl_program_num, prefix, "beta", 0.0031308); + } + if (destination_curve == GAMMA_REC_709) { // Also includes Rec. 601, and 10-bit Rec. 2020. + // Rec. 2020, page 3; ɑ = 1.099, β = 0.018, ɣ = 0.45. + // maxerror = 0.000131 = 0.033 * 255 = 0.134 * 1023 + // error at 1.0 = 0.000013 = 0.003 * 255 = 0.013 * 1023 + set_uniform_float(glsl_program_num, prefix, "linear_scale", 4.5); + set_uniform_float(glsl_program_num, prefix, "c0", -0.08541688528); + set_uniform_float(glsl_program_num, prefix, "c1", 1.292793370); + set_uniform_float(glsl_program_num, prefix, "c2", -0.4070417645); + set_uniform_float(glsl_program_num, prefix, "c3", 0.2923891828); + set_uniform_float(glsl_program_num, prefix, "c4", -0.09273699351); + set_uniform_float(glsl_program_num, prefix, "beta", 0.018); + } + if (destination_curve == GAMMA_REC_2020_12_BIT) { + // Rec. 2020, page 3; ɑ = 1.0993, β = 0.0181, ɣ = 0.45. + // maxerror = 0.000130 = 0.533 * 4095 + // error at 1.0 = 0.000013 = 0.053 * 4095 + // + // Note that this error is above one half of a pixel level, + // which means that a few values will actually be off in the lowest + // bit. (Removing the constraint for x=1 will only take this down + // from 0.553 to 0.501; adding a fifth order can get it down to + // 0.167, although this assumes working in fp64 and not fp32.) + set_uniform_float(glsl_program_num, prefix, "linear_scale", 4.5); + set_uniform_float(glsl_program_num, prefix, "c0", -0.08569685663); + set_uniform_float(glsl_program_num, prefix, "c1", 1.293000900); + set_uniform_float(glsl_program_num, prefix, "c2", -0.4067291321); + set_uniform_float(glsl_program_num, prefix, "c3", 0.2919741179); + set_uniform_float(glsl_program_num, prefix, "c4", -0.09256205770); + set_uniform_float(glsl_program_num, prefix, "beta", 0.0181); } } + +} // namespace movit