feat: MGC standard gamma output curve for HDRNet PGTM
This commit is contained in:
1 parent
9c6fab2332
commit
d584bc9124
5 files changed
+102
-37
No files matched your search
@@ -197,28 +197,11 @@ float EvaluateGuide(float source_luma, const float* shifts,
|
||||
return std::clamp(guide, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
float InputForAcrOutput(float output, const float* curve, int curve_count) {
|
||||
const float target = std::clamp(output, curve[0], curve[curve_count - 1]);
|
||||
if (target <= curve[0]) return 0.0f;
|
||||
if (target >= curve[curve_count - 1]) return 1.0f;
|
||||
|
||||
int lower_index = 0;
|
||||
int upper_index = curve_count - 1;
|
||||
while (lower_index + 1 < upper_index) {
|
||||
const int middle_index = (lower_index + upper_index) >> 1;
|
||||
if (curve[middle_index] < target) {
|
||||
lower_index = middle_index;
|
||||
} else {
|
||||
upper_index = middle_index;
|
||||
}
|
||||
}
|
||||
const float lower_output = curve[lower_index];
|
||||
const float upper_output = curve[upper_index];
|
||||
const float amount = upper_output > lower_output
|
||||
? (target - lower_output) /
|
||||
(upper_output - lower_output)
|
||||
: 0.0f;
|
||||
return (lower_index + amount) / static_cast<float>(curve_count - 1);
|
||||
float SampleOutputCurve(float input, const float* curve, int curve_count) {
|
||||
const float position = std::clamp(input, 0.0f, 1.0f) * (curve_count - 1);
|
||||
const int lower = static_cast<int>(position);
|
||||
const int upper = std::min(lower + 1, curve_count - 1);
|
||||
return Lerp(curve[lower], curve[upper], position - lower);
|
||||
}
|
||||
|
||||
bool IsFiniteArray(const float* values, int count) {
|
||||
@@ -228,12 +211,12 @@ bool IsFiniteArray(const float* values, int count) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IsValidAcrCurve(const float* curve, int count) {
|
||||
bool IsValidOutputCurve(const float* curve, int count) {
|
||||
if (count < 2 || !IsFiniteArray(curve, count)) return false;
|
||||
for (int index = 1; index < count; ++index) {
|
||||
if (curve[index] < curve[index - 1]) return false;
|
||||
}
|
||||
return curve[count - 1] > curve[0];
|
||||
return curve[0] == 0.0f && curve[count - 1] == 1.0f;
|
||||
}
|
||||
|
||||
struct DehazeCurve {
|
||||
@@ -737,12 +720,12 @@ Java_com_hinnka_mycamera_raw_DngHdrNetProfileGainTableNative_nativeGenerateGains
|
||||
jfloat render_max_gain_blend_threshold,
|
||||
jfloat min_table_gain, jfloat max_table_gain,
|
||||
jfloatArray guide_shifts_array,
|
||||
jfloatArray guide_slopes_array, jfloatArray acr_curve_array,
|
||||
jfloatArray guide_slopes_array, jfloatArray output_curve_array,
|
||||
jfloatArray dehaze_curve_array, jfloat post_exposure_gain, jfloatArray map_input_weights_array,
|
||||
jfloatArray output_gains_array) {
|
||||
if (coefficients_array == nullptr || model_input_array == nullptr ||
|
||||
guide_shifts_array == nullptr ||
|
||||
guide_slopes_array == nullptr || acr_curve_array == nullptr ||
|
||||
guide_slopes_array == nullptr || output_curve_array == nullptr ||
|
||||
dehaze_curve_array == nullptr || map_input_weights_array == nullptr || output_gains_array == nullptr ||
|
||||
input_width <= 0 || input_height <= 0 || input_channels < 3 ||
|
||||
source_grid_width <= 0 ||
|
||||
@@ -773,7 +756,7 @@ Java_com_hinnka_mycamera_raw_DngHdrNetProfileGainTableNative_nativeGenerateGains
|
||||
static_cast<int64_t>(output_grid_width) * output_grid_height;
|
||||
const int64_t output_values = cell_count * point_count;
|
||||
const int guide_count = env->GetArrayLength(guide_shifts_array);
|
||||
const int acr_curve_count = env->GetArrayLength(acr_curve_array);
|
||||
const int output_curve_count = env->GetArrayLength(output_curve_array);
|
||||
if (coefficient_values <= 0 || input_values <= 0 || output_values <= 0 ||
|
||||
coefficient_values > std::numeric_limits<jsize>::max() ||
|
||||
input_values > std::numeric_limits<jsize>::max() ||
|
||||
@@ -781,7 +764,7 @@ Java_com_hinnka_mycamera_raw_DngHdrNetProfileGainTableNative_nativeGenerateGains
|
||||
env->GetArrayLength(coefficients_array) != coefficient_values ||
|
||||
env->GetArrayLength(model_input_array) != input_values ||
|
||||
env->GetArrayLength(guide_slopes_array) != guide_count ||
|
||||
acr_curve_count < 2 ||
|
||||
output_curve_count < 2 ||
|
||||
env->GetArrayLength(dehaze_curve_array) != kDehazeCurveValueCount ||
|
||||
env->GetArrayLength(map_input_weights_array) != 5 ||
|
||||
env->GetArrayLength(output_gains_array) != output_values) {
|
||||
@@ -792,13 +775,13 @@ Java_com_hinnka_mycamera_raw_DngHdrNetProfileGainTableNative_nativeGenerateGains
|
||||
ScopedFloatArray model_input(env, model_input_array);
|
||||
ScopedFloatArray guide_shifts(env, guide_shifts_array);
|
||||
ScopedFloatArray guide_slopes(env, guide_slopes_array);
|
||||
ScopedFloatArray acr_curve(env, acr_curve_array);
|
||||
ScopedFloatArray output_curve(env, output_curve_array);
|
||||
ScopedFloatArray dehaze_curve_values(env, dehaze_curve_array);
|
||||
ScopedFloatArray map_input_weights(env, map_input_weights_array);
|
||||
ScopedFloatArray output_gains(env, output_gains_array);
|
||||
if (coefficients.data() == nullptr || model_input.data() == nullptr ||
|
||||
guide_shifts.data() == nullptr ||
|
||||
guide_slopes.data() == nullptr || acr_curve.data() == nullptr ||
|
||||
guide_slopes.data() == nullptr || output_curve.data() == nullptr ||
|
||||
dehaze_curve_values.data() == nullptr || map_input_weights.data() == nullptr || output_gains.data() == nullptr) {
|
||||
LogError("Unable to acquire HDRNet PGTM arrays");
|
||||
return JNI_FALSE;
|
||||
@@ -809,7 +792,7 @@ Java_com_hinnka_mycamera_raw_DngHdrNetProfileGainTableNative_nativeGenerateGains
|
||||
!IsFiniteArray(guide_shifts.data(), guide_count) ||
|
||||
!IsFiniteArray(guide_slopes.data(), guide_count) ||
|
||||
!IsFiniteArray(map_input_weights.data(), 5) ||
|
||||
!IsValidAcrCurve(acr_curve.data(), acr_curve_count) ||
|
||||
!IsValidOutputCurve(output_curve.data(), output_curve_count) ||
|
||||
!ReadDehazeCurve(
|
||||
dehaze_curve_values.data(), kDehazeCurveValueCount, &dehaze_curve)) {
|
||||
LogError("Rejected non-finite HDRNet PGTM input");
|
||||
@@ -911,8 +894,10 @@ Java_com_hinnka_mycamera_raw_DngHdrNetProfileGainTableNative_nativeGenerateGains
|
||||
gain_curve[point] = min_table_gain;
|
||||
continue;
|
||||
}
|
||||
const float pre_curve_target = InputForAcrOutput(
|
||||
target_luma, acr_curve.data(), acr_curve_count);
|
||||
// One forward lookup composes MGC Standard Gamma -> sRGB decode -> inverse
|
||||
// ACR3. The forward ACR3 used by the DNG renderer remains unchanged.
|
||||
const float pre_curve_target = SampleOutputCurve(
|
||||
target_luma, output_curve.data(), output_curve_count);
|
||||
// PGTM is applied before BaselineExposure, so divide by source luma after that
|
||||
// pending exposure. The subsequent exposure ramp then restores pre_curve_target exactly.
|
||||
const float baseline_applied_source_intensity =
|
||||
|
||||
@@ -14,7 +14,7 @@ internal object DngHdrNetProfileGainTableNative {
|
||||
modelInput: FloatArray,
|
||||
guideShifts: FloatArray,
|
||||
guideSlopes: FloatArray,
|
||||
acr3Curve: FloatArray,
|
||||
outputCurve: FloatArray,
|
||||
renderMinGain: Float,
|
||||
renderMaxGain: Float,
|
||||
renderMaxGainBlendThreshold: Float,
|
||||
@@ -50,7 +50,7 @@ internal object DngHdrNetProfileGainTableNative {
|
||||
maxTableGain = maxTableGain,
|
||||
guideShifts = guideShifts,
|
||||
guideSlopes = guideSlopes,
|
||||
acr3Curve = acr3Curve,
|
||||
outputCurve = outputCurve,
|
||||
dehazeCurve = dehazeCurve.toNativeArray(),
|
||||
postExposureGain = postExposureGain,
|
||||
mapInputWeights = plan.mapInputWeights,
|
||||
@@ -195,7 +195,7 @@ internal object DngHdrNetProfileGainTableNative {
|
||||
maxTableGain: Float,
|
||||
guideShifts: FloatArray,
|
||||
guideSlopes: FloatArray,
|
||||
acr3Curve: FloatArray,
|
||||
outputCurve: FloatArray,
|
||||
dehazeCurve: FloatArray,
|
||||
postExposureGain: Float,
|
||||
mapInputWeights: FloatArray,
|
||||
|
||||
@@ -201,6 +201,8 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
* chromaticity so the downstream arithmetic-RGB Dehaze curve is represented by the scalar
|
||||
* table without reverting to a neutral-gray assumption. [postExposureEv] controls the SLM
|
||||
* rolloff/digital response after that composed target, independently of HDRNet inference.
|
||||
* The final scalar target then passes through Standard Gamma, sRGB decode and inverse
|
||||
* ACR3 in one LUT, so the renderer's subsequent ACR3 restores Standard's linear output.
|
||||
*/
|
||||
fun mapFromHdrNetCoefficients(
|
||||
plan: HdrNetProfileGainTablePlan,
|
||||
@@ -224,7 +226,7 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
modelInput = modelInput,
|
||||
guideShifts = HDRNET_GUIDE_SHIFTS,
|
||||
guideSlopes = HDRNET_GUIDE_SLOPES,
|
||||
acr3Curve = ACR3Curve.samples(),
|
||||
outputCurve = MgcHdrNetOutputCurve.samples(),
|
||||
renderMinGain = HDRNET_RENDER_MIN_GAIN,
|
||||
renderMaxGain = HDRNET_RENDER_MAX_GAIN,
|
||||
renderMaxGainBlendThreshold = HDRNET_RENDER_MAX_GAIN_BLEND_THRESHOLD,
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import kotlin.math.pow
|
||||
|
||||
/** HDRNet linear output -> MGC Standard Gamma -> sRGB decode -> inverse ACR3. */
|
||||
internal object MgcHdrNetOutputCurve {
|
||||
// CPU LUT, independent of GL_MAX_TEXTURE_SIZE. Keep both normalized endpoints.
|
||||
private const val SAMPLE_COUNT = 65537
|
||||
|
||||
private val preAcrSamples by lazy {
|
||||
FloatArray(SAMPLE_COUNT) { index ->
|
||||
val encoded = standardGamma(index.toDouble() / (SAMPLE_COUNT - 1))
|
||||
val linear = if (encoded <= 0.04045) {
|
||||
encoded / 12.92
|
||||
} else {
|
||||
((encoded + 0.055) / 1.055).pow(2.4)
|
||||
}
|
||||
ACR3Curve.inputForOutput(linear.toFloat())
|
||||
}
|
||||
}
|
||||
|
||||
fun samples(): FloatArray = preAcrSamples
|
||||
|
||||
/** Full linear-input Standard function (0x4E94060), including its power transform. */
|
||||
internal fun standardGamma(input: Double): Double {
|
||||
val knots = MgcStandardGammaData.KNOTS
|
||||
val position = input.coerceIn(0.0, 1.0).pow(MgcStandardGammaData.EXPONENT) * knots.lastIndex
|
||||
val index = position.toInt().coerceAtMost(knots.lastIndex - 1)
|
||||
val t = position - index
|
||||
val y0 = knots[index]
|
||||
val y1 = knots[index + 1]
|
||||
|
||||
// Original cubic evaluator 0x4E94680: central tangents inside the table;
|
||||
// special first/last segment coefficients, not duplicated endpoint knots.
|
||||
val slope: Double
|
||||
val quadratic: Double
|
||||
val cubic: Double
|
||||
when (index) {
|
||||
0 -> {
|
||||
val nextSlope = (knots[2] - y0) * 0.5
|
||||
cubic = (y0 + nextSlope - y1) * 0.5
|
||||
quadratic = 0.0
|
||||
slope = nextSlope - 3.0 * cubic
|
||||
}
|
||||
knots.lastIndex - 1 -> {
|
||||
slope = (y1 - knots[index - 1]) * 0.5
|
||||
cubic = (y0 + slope - y1) * 0.5
|
||||
quadratic = -3.0 * cubic
|
||||
}
|
||||
else -> {
|
||||
slope = (y1 - knots[index - 1]) * 0.5
|
||||
val nextSlope = (knots[index + 2] - y0) * 0.5
|
||||
cubic = 2.0 * y0 + (slope + (nextSlope - 2.0 * y1))
|
||||
quadratic = (y1 - slope - y0) - cubic
|
||||
}
|
||||
}
|
||||
return y0 + t * (slope + t * (quadratic + t * cubic))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
// Generated by tools/extract_mgc_standard_gamma.py; do not edit.
|
||||
// MGC V25 libgcastartup.so SHA-256: 76966e7c150dfcb2795ea5b2119827a7e90b13d8c03035dbc4de583d9d12eaa6
|
||||
// Google Stock defaults: 33 doubles at VA/file 0xF3F138; exponent at 0x6B8E88.
|
||||
internal object MgcStandardGammaData {
|
||||
const val EXPONENT = 0.45454543828964233
|
||||
val KNOTS = doubleArrayOf(
|
||||
0.0, 0.003959, 0.018405, 0.043836,
|
||||
0.073478, 0.104864, 0.13715, 0.170288,
|
||||
0.204674, 0.240396, 0.277553, 0.31608,
|
||||
0.355398, 0.394413, 0.432555, 0.469181,
|
||||
0.505111, 0.540626, 0.575595, 0.61008,
|
||||
0.644066, 0.677381, 0.710066, 0.742155,
|
||||
0.773639, 0.804525, 0.834824, 0.864424,
|
||||
0.893152, 0.920521, 0.947214, 0.973732,
|
||||
1.0,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user