Skip to content

Commit

Permalink
apply correct normalization factor during decode
Browse files Browse the repository at this point in the history
Test: ./ultrahdr_unit_test
  • Loading branch information
ram-mohan authored and DichenZhang1 committed Oct 31, 2024
1 parent 613fee1 commit 990f103
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/src/gpu/applygainmap_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ static const std::string applyGainMapShader = R"__SHADER__(
uniform float logMinBoost;
uniform float logMaxBoost;
uniform float weight;
uniform float displayBoost;
uniform float normalize;
float applyGainMapSample(const float channel, float gain) {
gain = pow(gain, 1.0f / gamma);
float logBoost = logMinBoost * (1.0f - gain) + logMaxBoost * gain;
logBoost = exp2(logBoost * weight);
return channel * logBoost / displayBoost;
return channel * logBoost / normalize;
}
vec3 applyGain(const vec3 color, const vec3 gain) {
Expand Down Expand Up @@ -317,7 +317,7 @@ uhdr_error_info_t applyGainMapGLES(uhdr_raw_image_t* sdr_intent, uhdr_raw_image_
GLint logMinBoostLocation = glGetUniformLocation(shaderProgram, "logMinBoost");
GLint logMaxBoostLocation = glGetUniformLocation(shaderProgram, "logMaxBoost");
GLint weightLocation = glGetUniformLocation(shaderProgram, "weight");
GLint displayBoostLocation = glGetUniformLocation(shaderProgram, "displayBoost");
GLint normalizeLocation = glGetUniformLocation(shaderProgram, "normalize");

glUniform1i(pWidthLocation, sdr_intent->w);
glUniform1i(pHeightLocation, sdr_intent->h);
Expand All @@ -335,7 +335,10 @@ uhdr_error_info_t applyGainMapGLES(uhdr_raw_image_t* sdr_intent, uhdr_raw_image_
gainmap_weight = 1.0f;
}
glUniform1f(weightLocation, gainmap_weight);
glUniform1f(displayBoostLocation, display_boost);
float normalize = 1.0f;
if (output_ct == UHDR_CT_HLG) normalize = kHlgMaxNits / kSdrWhiteNits;
else if (output_ct == UHDR_CT_PQ) normalize = kPqMaxNits / kSdrWhiteNits;
glUniform1f(normalizeLocation, normalize);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, yuvTexture);
Expand Down
5 changes: 3 additions & 2 deletions lib/src/jpegr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima

JobQueue jobQueue;
std::function<void()> applyRecMap = [sdr_intent, gainmap_img, dest, &jobQueue, &idwTable,
output_ct, &gainLUT, display_boost,
output_ct, &gainLUT,
#if !USE_APPLY_GAIN_LUT
gainmap_metadata, gainmap_weight,
#endif
Expand Down Expand Up @@ -1530,7 +1530,6 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima
#endif
}

rgb_hdr = rgb_hdr / display_boost;
size_t pixel_idx = x + y * dest->stride[UHDR_PLANE_PACKED];

switch (output_ct) {
Expand All @@ -1545,6 +1544,7 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima
#else
ColorTransformFn hdrOetf = hlgOetf;
#endif
rgb_hdr = rgb_hdr * kSdrWhiteNits / kHlgMaxNits;
rgb_hdr = hlgInverseOotfApprox(rgb_hdr);
Color rgb_gamma_hdr = hdrOetf(rgb_hdr);
uint32_t rgba_1010102 = colorToRgba1010102(rgb_gamma_hdr);
Expand All @@ -1558,6 +1558,7 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima
#else
ColorTransformFn hdrOetf = pqOetf;
#endif
rgb_hdr = rgb_hdr * kSdrWhiteNits / kPqMaxNits;
Color rgb_gamma_hdr = hdrOetf(rgb_hdr);
uint32_t rgba_1010102 = colorToRgba1010102(rgb_gamma_hdr);
reinterpret_cast<uint32_t*>(dest->planes[UHDR_PLANE_PACKED])[pixel_idx] =
Expand Down

0 comments on commit 990f103

Please sign in to comment.