fix: PGTM highlight

This commit is contained in:
zhoubinjia committed 2026-07-15 18:35:32 +08:00
1 parent 415daccdbd
commit 875d3e794a
5 files changed
+78 -23

No files matched your search

@@ -43,6 +43,12 @@ internal object DngHdrProfileGainTableGenerator {
private const val AUTO_EXPOSURE_TARGET = 0.278f
private const val WELL_EXPOSED_KEY = 0.525f
private const val WELL_EXPOSED_SIGMA_EV = 0.750f
// Official Pixel maps broaden the exposure fusion only in the shoulder:
// the last trustworthy highlights stay close to white while the toe and
// midtones retain the narrower local-exposure weighting.
private const val HIGHLIGHT_WELL_EXPOSED_SIGMA_EV = 1.200f
private const val HIGHLIGHT_FUSION_START = 0.45f
private const val HIGHLIGHT_FUSION_END = 0.65f
private const val EXPOSURE_PLAN_STEPS_PER_EV = 128f
private const val WEIGHT_LUT_STEPS_PER_EV = 256f
private const val WEIGHT_LUT_MAX_DISTANCE_EV = 12f
@@ -67,6 +73,14 @@ internal object DngHdrProfileGainTableGenerator {
val distanceEv = index.toFloat() / WEIGHT_LUT_STEPS_PER_EV
exp((-0.5f * (distanceEv / WELL_EXPOSED_SIGMA_EV).pow(2f)).toDouble()).toFloat()
}
private val HIGHLIGHT_WELL_EXPOSED_WEIGHT_LUT = FloatArray(
(WEIGHT_LUT_MAX_DISTANCE_EV * WEIGHT_LUT_STEPS_PER_EV).toInt() + 1
) { index ->
val distanceEv = index.toFloat() / WEIGHT_LUT_STEPS_PER_EV
exp(
(-0.5f * (distanceEv / HIGHLIGHT_WELL_EXPOSED_SIGMA_EV).pow(2f)).toDouble()
).toFloat()
}
fun gridSizeFor(width: Int, height: Int): IntArray {
val grid = chooseLtmGrid(width, height)
@@ -507,6 +521,13 @@ internal object DngHdrProfileGainTableGenerator {
exposureGainEvs: FloatArray,
): Float {
val sceneFromKeyEv = log2(max(sceneInput, CURVE_EPS) / WELL_EXPOSED_KEY)
val darkestExposureGain = exposureGains.lastOrNull() ?: return 0f
val tableInput = sceneInput * darkestExposureGain
val highlightFusionStrength = smoothStep(
HIGHLIGHT_FUSION_START,
HIGHLIGHT_FUSION_END,
tableInput
)
var weightSum = 0f
var outputSum = 0f
var nearestDistanceEv = Float.POSITIVE_INFINITY
@@ -515,7 +536,11 @@ internal object DngHdrProfileGainTableGenerator {
val exposureGain = exposureGains[exposure]
val exposed = max(sceneInput * exposureGain, CURVE_EPS)
val distanceEv = sceneFromKeyEv + exposureGainEvs[exposure]
val weight = wellExposedWeight(distanceEv)
val weight = lerp(
wellExposedWeight(distanceEv, WELL_EXPOSED_WEIGHT_LUT),
wellExposedWeight(distanceEv, HIGHLIGHT_WELL_EXPOSED_WEIGHT_LUT),
highlightFusionStrength
)
weightSum += weight
outputSum += weight * min(exposed, 1f)
if (abs(distanceEv) < nearestDistanceEv) {
@@ -527,16 +552,16 @@ internal object DngHdrProfileGainTableGenerator {
return (outputSum / weightSum).coerceIn(0f, 1f)
}
private fun wellExposedWeight(distanceEv: Float): Float {
private fun wellExposedWeight(distanceEv: Float, lut: FloatArray): Float {
// Linear interpolation at 1/256 EV removes millions of exp() calls without changing the
// exposure-fusion equation or its normalization.
val scaled = abs(distanceEv) * WEIGHT_LUT_STEPS_PER_EV
if (scaled >= WELL_EXPOSED_WEIGHT_LUT.lastIndex.toFloat()) return 0f
if (scaled >= lut.lastIndex.toFloat()) return 0f
val index = scaled.toInt()
val fraction = scaled - index
return lerp(
WELL_EXPOSED_WEIGHT_LUT[index],
WELL_EXPOSED_WEIGHT_LUT[index + 1],
lut[index],
lut[index + 1],
fraction
)
}
@@ -196,7 +196,7 @@ internal object DngPhotonProfileGainTableGenerator {
"tailRangeEv=${toneAnchors.tailHighlightRangeEv} " +
"exposureRangeEv=${toneAnchors.exposureHighlightRangeEv} " +
"exposureLiftEv=$exposureLiftEv " +
"whiteAdjustment=${toneAnchors.rawWhitePoint / BASE_WHITE_POINT} " +
"whiteAdjustment=${toneAnchors.whitePoint / BASE_WHITE_POINT} " +
"maxGain=$MAX_GAIN_VALUE"
)
}
@@ -272,13 +272,15 @@ internal object DngPhotonProfileGainTableGenerator {
val baselineGain = 2.0f.pow(baselineExposureEv.coerceIn(MIN_BASELINE_EV, MAX_BASELINE_EV))
// ProfileGainTableMap lookup is evaluated after BaselineExposure even
// when the gain itself is applied before it. Cover the raw-linear white
// domain, including at most 30% profile-RGB overrange, so positive
// BaselineExposure does not collapse its recoverable highlight headroom
// into the final table entry.
// when the gain itself is applied before it. Its right edge belongs to
// that post-baseline profile domain and must not be expanded by the
// baseline gain a second time. Doing so would map sensor saturation to
// an output below white and make clipped channel ratios visible. Keep a
// small, statistics-driven profile overrange, while values beyond it
// reuse the terminal gain and naturally remain above display white.
val blackRangeWhiteReference = BASE_WHITE_POINT * whitePointAdjustment
val rawWhitePoint = blackRangeWhiteReference
val whitePoint = rawWhitePoint * baselineGain
val whitePoint = blackRangeWhiteReference
val rawWhitePoint = whitePoint / baselineGain
// The EV-histogram foot identifies the perceptual shadow onset. Keep a
// short toe reserve below it; unlike p10, this point is based on image
@@ -1350,7 +1350,7 @@ class RawDemosaicProcessor {
)
}
actualRotation = if (dngRawData.rotation != 0) dngRawData.rotation else rotation
embeddedDngRenderPlan = if (false) {
embeddedDngRenderPlan = if (hasClassicTiffHeader) {
DngEmbeddedProfile.resolveRenderPlan(
file = dngFile,
metadata = actualMetadata,
@@ -73,6 +73,34 @@ class DngHdrProfileGainTableGeneratorTest {
}
}
@Test
fun official184953TrustedHighlightTailStaysNearWhite() {
val fixture = loadFixture(
"/pgtm/skyyking/Skyyking_20260711_184953.RAW-02.ORIGINAL.pgtfixture"
)
val map = generateMap(fixture)
val expectedSpecular = anchorGains(
source = fixture.expectedGains,
cellCount = fixture.cellCount,
anchor = ToneAnchor.SPECULAR
)
val actualSpecular = actualAnchorGains(fixture, map, ToneAnchor.SPECULAR)
val trustedShoulderCells = actualSpecular.indices.filter { cell ->
fixture.packedStats[cell * CELL_STATS_STRIDE + STAT_P999] in 2f..3f
}
assertTrue("Expected trusted shoulder cells", trustedShoulderCells.isNotEmpty())
trustedShoulderCells.forEach { cell ->
val p999 = fixture.packedStats[cell * CELL_STATS_STRIDE + STAT_P999]
val expectedOutput = p999 * expectedSpecular[cell]
val actualOutput = p999 * actualSpecular[cell]
assertTrue(
"cell=$cell p999=$p999 expectedOutput=$expectedOutput actualOutput=$actualOutput",
actualOutput + 0.025f >= expectedOutput
)
}
}
@Test
fun generatedMapsMatchEmbeddedSemanticToneDistributions() {
val failures = mutableListOf<String>()
@@ -196,7 +196,7 @@ class DngPhotonProfileGainTableGeneratorTest {
}
@Test
fun baselineExposureHeadroomRemainsInsideProfileGainTableDomain() {
fun baselineExposureDoesNotExpandProfileGainTablePastTrustedWhite() {
val width = 1280
val height = 960
val grid = DngHdrProfileGainTableGenerator.gridSizeFor(width, height)
@@ -241,15 +241,15 @@ class DngPhotonProfileGainTableGeneratorTest {
val inputScale = map.mapInputWeights.sum()
val sceneWhite = 1f / inputScale
val rawRight = sceneWhite / baselineGain
val displayAtNominalWhite = finalOutputForScene(map, inputScale, 1.3f)
val displayAtRawWhite = finalOutputForScene(map, inputScale, baselineGain)
val terminalGain = medianGain(map, tableInput = 1f)
val pgtmAtRawWhite = baselineGain * terminalGain
val displayAtDomainRight = finalOutputForScene(map, inputScale, sceneWhite)
assertTrue("sceneWhite=$sceneWhite", sceneWhite in 10.39f..10.41f)
assertTrue("rawRight=$rawRight", rawRight in 1.299f..1.301f)
assertTrue("sceneWhite=$sceneWhite", sceneWhite in 1.299f..1.301f)
assertTrue("rawRight=$rawRight", rawRight in 0.1623f..0.1627f)
assertTrue(
"nominal=$displayAtNominalWhite rawWhite=$displayAtRawWhite",
displayAtRawWhite > displayAtNominalWhite + 0.05f
"terminalGain=$terminalGain pgtmAtRawWhite=$pgtmAtRawWhite",
pgtmAtRawWhite > 1.5f
)
assertTrue("domainRight=$displayAtDomainRight", displayAtDomainRight in 0.99f..1.01f)
assertCurvesAreFiniteAndMonotonic(map, sceneWhite)
@@ -348,7 +348,7 @@ class DngPhotonProfileGainTableGeneratorTest {
0.045098f / 2.0f.pow(
DngPhotonProfileGainTableGenerator.SHADOW_FOOT_HEADROOM_EV
),
rawRight / 2.0f.pow(9.75f)
sceneWhite / 2.0f.pow(9.75f)
)
val blackOutput = sourceBlackPoint * medianGain(
map,
@@ -368,11 +368,11 @@ class DngPhotonProfileGainTableGeneratorTest {
assertTrue("gamma=${map.gamma}", map.gamma == 0.5f)
assertTrue(
"sceneWhite=$sceneWhite baselineGain=$baselineGain",
sceneWhite / baselineGain in 1f..1.3001f
sceneWhite in 1f..1.3001f
)
assertTrue(
"rawRight=$rawRight",
rawRight in 1f..1.3001f
rawRight in (1f / baselineGain)..(1.3001f / baselineGain)
)
assertTrue("outputDynamicRangeEv=$outputDynamicRangeEv", outputDynamicRangeEv in 8.8f..9.2f)
assertTrue("finalMiddleOutput=$finalMiddleOutput", finalMiddleOutput in 0.285f..0.305f)