fix: photon curve highlight color error

This commit is contained in:
Hinnka committed 2026-07-26 11:28:31 +08:00
1 parent 6c09a4da72
commit 87b40ab2fb
7 files changed
+116 -34

No files matched your search

@@ -35,6 +35,8 @@ internal object DngHdrProfileGainTableGpuShaders {
uniform int uHueSatEnabled;
uniform ivec3 uHueSatDivisions;
uniform int uHueSatEncoding;
uniform int uHueSatSupportOverrange;
uniform int uUseMaxRgbInput;
uniform int uWriteCellSamples;
uniform int uWarpCount;
@@ -135,12 +137,13 @@ internal object DngHdrProfileGainTableGpuShaders {
uHueSatMap,
uHueSatDivisions,
uHueSatEncoding,
false
uHueSatSupportOverrange != 0
);
}
profileRgb = clamp(profileRgb, 0.0, 1.0) * uBaselineGain;
float rgbMin = min(profileRgb.r, min(profileRgb.g, profileRgb.b));
float rgbMax = max(profileRgb.r, max(profileRgb.g, profileRgb.b));
if (uUseMaxRgbInput != 0) return rgbMax;
return max(
0.1495 * profileRgb.r +
0.2935 * profileRgb.g +
@@ -5,7 +5,7 @@ import kotlin.math.ln
import kotlin.math.min
/**
* GLES 3.1 compute passes for the Photon Local Laplacian + scalar Google BGU pipeline.
* GLES 3.1 compute passes for the Photon max-RGB Local Laplacian + scalar Google BGU pipeline.
*
* The equations and boundary handling intentionally mirror [DngPhotonLocalToneMapper]. Each
* pyramid level occupies a disjoint range in one SSBO so a pass can read one level and write the
@@ -472,8 +472,8 @@ internal object DngPhotonLocalToneMapGpuShaders {
);
float filteredLog = minimumLog +
reconstructed[uSourceOffset + index] * (maximumLog - minimumLog);
// Local Laplacian operates on the already-exposed input. Its reconstructed
// linear radiance is the definitive SDR target; no second global curve follows it.
// Photon PGTM has a normalized SDR output contract. Keep max RGB inside that
// contract before later nonlinear profile stages can change channel ratios.
target[index] = clamp(exp(filteredLog) - SOURCE_EPS, 0.0, 1.0);
}
""".trimIndent()
@@ -27,7 +27,7 @@ import kotlin.math.roundToInt
* Photon local tone mapping for DNG ProfileGainTableMap.
*
* Algorithm lineage:
* 1. Paris/Hasinoff/Kautz Local Laplacian HDR remapping in log luminance.
* 1. Paris/Hasinoff/Kautz Local Laplacian HDR remapping on Photon max-RGB intensity.
* 2. Aubry et al. intensity-discretized Local Laplacian evaluation.
* 3. Google's fit-and-slice affine bilateral grid (BGU).
*
@@ -126,9 +126,10 @@ internal object DngPhotonLocalToneMapper {
}
/**
* Fast general Local Laplacian evaluation from llf_general.m. [exposureGain] already contains
* BaselineExposure and the fixed ACR3 +1.1 EV placement. The middle 99% determines only the
* Local Laplacian large-edge slope; reconstructed linear radiance is the final SDR target.
* Fast general Local Laplacian evaluation from llf_general.m. The caller supplies max-RGB
* intensity, and [exposureGain] already contains BaselineExposure and the fixed ACR3 +1.25 EV
* placement. The middle 99% determines the Local Laplacian large-edge slope. Reconstructed
* max-RGB intensity is bounded to Photon PGTM's normalized SDR output contract.
*/
internal fun localLaplacianToneMap(
source: FloatArray,
@@ -13,6 +13,8 @@ internal object DngPhotonProfileGainTableGenerator {
private const val MIN_TABLE_GAIN = 1f / 4096f
private const val MAX_TABLE_GAIN = 4096f
internal val MAX_RGB_INPUT_WEIGHTS = floatArrayOf(0f, 0f, 0f, 0f, 1f)
fun plan(
grid: HdrPgtmGrid,
pointCount: Int,
@@ -35,8 +37,8 @@ internal object DngPhotonProfileGainTableGenerator {
// DNG renderers include BaselineExposure before calculating the table coordinate.
// Dividing the weights by that exact gain makes the BGU guide the sampled source-linear
// scene input, while the gain itself still acts on the exposed signal.
val mapWeights = FloatArray(DngHdrProfileGainTableGenerator.BASE_INPUT_WEIGHTS.size) {
DngHdrProfileGainTableGenerator.BASE_INPUT_WEIGHTS[it] / exposureGain
val mapWeights = FloatArray(MAX_RGB_INPUT_WEIGHTS.size) {
MAX_RGB_INPUT_WEIGHTS[it] / exposureGain
}
return HdrProfileGainTablePlan(
curveModel = HdrPgtmCurveModel.PHOTON,
@@ -60,9 +62,13 @@ internal object DngPhotonProfileGainTableGenerator {
* Parameters copied from the published Local Laplacian and Google BGU reference code.
*
* The Local Laplacian edge slope is the compression ratio required to reach the reference target
* dynamic range. ACR3's fixed +1.1 EV gray placement is applied to scene-linear input before Local
* dynamic range. ACR3's fixed +1.25 EV gray placement is applied to scene-linear input before Local
* Laplacian; scene highlights therefore do not determine exposure. The resulting SDR target is
* then projected directly into ProfileGainTableMap's RGB-uniform multiplicative representation.
* Photon uses max RGB as both the Local Laplacian intensity and the DNG table coordinate, so the
* gain follows and bounds the brightest channel without changing RGB ratios. A luma-like guide
* can reach one while a saturated channel is still far above one after gain, which turns
* downstream profile rendering into a color-changing operation.
* The MATLAB example's final display gamma is omitted because output encoding is performed by the
* renderer after the DNG profile pipeline.
*/
@@ -449,6 +449,7 @@ class RawDemosaicProcessor {
useProfileExposureRamp = true,
applyProfileDcpBaselineExposureOffset = false,
supportProfileOverrange = false,
hueSatMapSupportsOverrange = false,
) != null
val warmedProfileMap = when (profileToneMapMode) {
RawProfileToneMapMode.Photon,
@@ -464,6 +465,7 @@ class RawDemosaicProcessor {
colorCorrectionMatrix = identity,
cameraWhite = metadata.cameraWhite,
hueSatMap = null,
hueSatMapSupportsOverrange = false,
)
else -> null
@@ -492,6 +494,7 @@ class RawDemosaicProcessor {
profileBaselineExposureOffsetEv = 0f,
clampProfileRgb = true,
supportProfileOverrange = false,
hueSatMapSupportsOverrange = false,
label = "CaptureWarmupLinearRcdPass",
)
PLog.d(
@@ -854,6 +857,7 @@ class RawDemosaicProcessor {
val applyDcpBaselineExposureOffset: Boolean,
val clampProfileRgb: Boolean,
val supportProfileOverrange: Boolean,
val hueSatMapSupportsOverrange: Boolean,
val hncsCameraDomainGains: FloatArray?,
val colorEngine: RawRenderingEngine,
val activeDcpRenderPlan: DcpRenderPlan?,
@@ -1879,6 +1883,9 @@ class RawDemosaicProcessor {
?: actualMetadata.colorCorrectionMatrix,
cameraWhite = resolvedDcpRenderPlan?.cameraWhite ?: actualMetadata.cameraWhite,
hueSatMap = resolvedDcpRenderPlan?.hueSatMap,
hueSatMapSupportsOverrange =
requestedProfileToneMapMode != RawProfileToneMapMode.Photon &&
resolvedDcpRenderPlan?.supportsOverrange == true,
warpRectilinear = dngWarpRectilinear,
)?.takeIf { it.isValid }
if (generated != null) {
@@ -1998,8 +2005,23 @@ class RawDemosaicProcessor {
val applyDcpBaselineExposureOffset =
shouldApplyDcpBaselineExposureOffset(activeDcpRenderPlan)
val useProfileExposureRamp = useAdobeProfilePipeline
val photonPgtmNormalizedSdr = profileGainToneMapActive &&
requestedProfileToneMapMode == RawProfileToneMapMode.Photon
val supportProfileOverrange =
useAdobeProfilePipeline && activeDcpRenderPlan?.supportsOverrange == true
useAdobeProfilePipeline &&
!photonPgtmNormalizedSdr &&
activeDcpRenderPlan?.supportsOverrange == true
val hueSatMapSupportsOverrange = useAdobeProfilePipeline && if (
photonPgtmNormalizedSdr
) {
false
} else if (
profileGainToneMapActive
) {
profileBaseDcpRenderPlan?.supportsOverrange == true
} else {
activeDcpRenderPlan?.supportsOverrange == true
}
val clampProfileRgb = useAdobeProfilePipeline
val engineWorkingColorSpace = colorEngine.workingColorSpace
val profileToEngineTransform = computeWorkingToOutputTransform(
@@ -2057,7 +2079,8 @@ class RawDemosaicProcessor {
profileToEngineTransform = profileToEngineTransform,
useAdobeProfilePipeline = useAdobeProfilePipeline,
useProfileExposureRamp = useProfileExposureRamp,
applyDcpBaselineExposureOffset = applyDcpBaselineExposureOffset
applyDcpBaselineExposureOffset = applyDcpBaselineExposureOffset,
hueSatMapSupportsOverrange = hueSatMapSupportsOverrange,
)
PLog.d(
TAG,
@@ -2183,6 +2206,7 @@ class RawDemosaicProcessor {
useProfileExposureRamp = useProfileExposureRamp,
applyProfileDcpBaselineExposureOffset = applyDcpBaselineExposureOffset,
supportProfileOverrange = supportProfileOverrange,
hueSatMapSupportsOverrange = hueSatMapSupportsOverrange,
hncsCameraDomainGains = hncsCameraDomainGains,
)
}
@@ -2211,6 +2235,7 @@ class RawDemosaicProcessor {
colorCorrectionMatrix = linearColorCorrectionMatrix,
cameraWhite = linearCameraWhite,
hueSatMap = activeDcpRenderPlan?.hueSatMap,
hueSatMapSupportsOverrange = hueSatMapSupportsOverrange,
warpRectilinear = dngWarpRectilinear,
)
else -> null
@@ -2296,6 +2321,7 @@ class RawDemosaicProcessor {
applyDcpBaselineExposureOffset = applyDcpBaselineExposureOffset,
clampProfileRgb = clampProfileRgb,
supportProfileOverrange = supportProfileOverrange,
hueSatMapSupportsOverrange = hueSatMapSupportsOverrange,
hncsCameraDomainGains = hncsCameraDomainGains,
colorEngine = colorEngine,
activeDcpRenderPlan = activeDcpRenderPlan,
@@ -2401,6 +2427,7 @@ class RawDemosaicProcessor {
},
clampProfileRgb = clampProfileRgb,
supportProfileOverrange = supportProfileOverrange,
hueSatMapSupportsOverrange = hueSatMapSupportsOverrange,
hncsCameraDomainGains = hncsCameraDomainGains,
label = "LinearRcdPass"
)
@@ -2797,6 +2824,7 @@ class RawDemosaicProcessor {
},
clampProfileRgb = config.clampProfileRgb,
supportProfileOverrange = config.supportProfileOverrange,
hueSatMapSupportsOverrange = config.hueSatMapSupportsOverrange,
hncsCameraDomainGains = config.hncsCameraDomainGains,
globalOriginX = working.left,
globalOriginY = working.top,
@@ -3107,6 +3135,7 @@ class RawDemosaicProcessor {
colorCorrectionMatrix: FloatArray,
cameraWhite: FloatArray,
hueSatMap: DcpHueSatMap?,
hueSatMapSupportsOverrange: Boolean,
warpRectilinear: FloatArray? = null,
): DngProfileGainTableMap? {
if (rawTextureId == 0 || width <= 0 || height <= 0 ||
@@ -3255,6 +3284,10 @@ class RawDemosaicProcessor {
GLES31.glGetUniformLocation(pgtmCellStatsProgram, "uHueSatEncoding"),
activeHueSatMap?.encoding ?: DcpHueSatMap.ENCODING_LINEAR,
)
GLES31.glUniform1i(
GLES31.glGetUniformLocation(pgtmCellStatsProgram, "uHueSatSupportOverrange"),
if (hueSatMapSupportsOverrange) 1 else 0,
)
GLES31.glUniform2i(
GLES31.glGetUniformLocation(pgtmCellStatsProgram, "uImageSize"),
rawTextureWidth,
@@ -3299,6 +3332,10 @@ class RawDemosaicProcessor {
GLES31.glGetUniformLocation(pgtmCellStatsProgram, "uBaselineGain"),
if (curveModel == HdrPgtmCurveModel.PHOTON) 1f else exposureGain,
)
GLES31.glUniform1i(
GLES31.glGetUniformLocation(pgtmCellStatsProgram, "uUseMaxRgbInput"),
if (curveModel == HdrPgtmCurveModel.PHOTON) 1 else 0,
)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(pgtmCellStatsProgram, "uHighlightThreshold"),
if (curveModel == HdrPgtmCurveModel.PHOTON) 1f / exposureGain else 0.92f,
@@ -3388,6 +3425,8 @@ class RawDemosaicProcessor {
"grid=${gridWidth}x$gridHeight samplesPerPixel=$samplesPerPixel " +
"lsc=${lensShadingLogString(metadata)} " +
"warpCount=${activeWarpParameters.size / 8} " +
"photonGuide=maxRgb " +
"hueSatOverrange=$hueSatMapSupportsOverrange " +
"photonBaselineGain=${photonPlan.exposureGain} " +
"photonPreToneMapGain=$photonPreToneMapGain " +
"photonLlfLevels=${photonPlan.parameters.localLaplacianIntensityLevels} " +
@@ -4893,7 +4932,7 @@ class RawDemosaicProcessor {
uniform float uHncsHrMax;
uniform int uClampProfileRgb;
uniform int uClampProfileGainOutput;
uniform int uProfileSupportOverrange;
uniform int uHueSatSupportOverrange;
uniform int uProfileGainEnabled;
uniform ivec3 uProfileGainTableSize;
uniform vec4 uProfileGainGrid;
@@ -4994,7 +5033,7 @@ class RawDemosaicProcessor {
uLinearDcpHueSatMap,
uLinearDcpHueSatDivisions,
uLinearDcpHueSatEncoding,
uProfileSupportOverrange != 0
uHueSatSupportOverrange != 0
);
}
vec3 profileInputRgb = rgb * uProfileGainBaselineGain;
@@ -10483,9 +10522,9 @@ class RawDemosaicProcessor {
workingColorSpace = basePlan?.workingColorSpace ?: workingColorSpace,
baselineExposureOffset = basePlan?.baselineExposureOffset ?: 0f,
defaultBlackRender = basePlan?.defaultBlackRender ?: DcpDefaultBlackRender.None,
// Photon endpoint normalization may require a value above 1 before a negative
// BaselineExposure gain. Preserve it until exposure and the tone curve are applied.
supportsOverrange = mode == RawProfileToneMapMode.Photon ||
// Photon PGTM has a normalized SDR contract. Do not inherit profile overrange:
// later nonlinear profile stages would otherwise change highlight channel ratios.
supportsOverrange = mode != RawProfileToneMapMode.Photon &&
basePlan?.supportsOverrange == true,
colorCorrectionMatrix = basePlan?.colorCorrectionMatrix ?: metadata.colorCorrectionMatrix,
cameraWhite = basePlan?.cameraWhite ?: metadata.cameraWhite,
@@ -10566,7 +10605,8 @@ class RawDemosaicProcessor {
profileToEngineTransform: FloatArray,
useAdobeProfilePipeline: Boolean,
useProfileExposureRamp: Boolean,
applyDcpBaselineExposureOffset: Boolean
applyDcpBaselineExposureOffset: Boolean,
hueSatMapSupportsOverrange: Boolean,
) {
if (profilePlanSource == null) return
@@ -10607,6 +10647,7 @@ class RawDemosaicProcessor {
"profileToneCurve=$profileToneCurveEnabled " +
"profileExposureRamp=$useProfileExposureRamp " +
"profileSupportsOverrange=${dcpRenderPlan?.supportsOverrange == true} " +
"hueSatSupportsOverrange=$hueSatMapSupportsOverrange " +
"defaultBlackRender=$defaultBlackRender " +
"baselineExposureOffset=$dcpBaselineExposureOffset " +
"cameraWhite=${cameraWhite.contentToString()} " +
@@ -10730,6 +10771,7 @@ class RawDemosaicProcessor {
profileBaselineExposureOffsetEv: Float,
clampProfileRgb: Boolean,
supportProfileOverrange: Boolean,
hueSatMapSupportsOverrange: Boolean,
hncsCameraDomainGains: FloatArray? = null,
globalOriginX: Int = 0,
globalOriginY: Int = 0,
@@ -10829,8 +10871,8 @@ class RawDemosaicProcessor {
if (clampProfileRgb && !supportProfileOverrange) 1 else 0
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(linearRcdProgram, "uProfileSupportOverrange"),
if (supportProfileOverrange) 1 else 0
GLES30.glGetUniformLocation(linearRcdProgram, "uHueSatSupportOverrange"),
if (hueSatMapSupportsOverrange) 1 else 0
)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(linearRcdProgram, "uGlobalUvOrigin"),
@@ -11053,6 +11095,7 @@ class RawDemosaicProcessor {
useProfileExposureRamp: Boolean,
applyProfileDcpBaselineExposureOffset: Boolean,
supportProfileOverrange: Boolean,
hueSatMapSupportsOverrange: Boolean,
hncsCameraDomainGains: FloatArray? = null,
): Float? {
val previewSize = resolveLongEdgePreviewSize(
@@ -11088,6 +11131,7 @@ class RawDemosaicProcessor {
profileBaselineExposureOffsetEv = profileBaselineExposureOffsetEv,
clampProfileRgb = clampProfileRgb,
supportProfileOverrange = supportProfileOverrange,
hueSatMapSupportsOverrange = hueSatMapSupportsOverrange,
hncsCameraDomainGains = hncsCameraDomainGains,
label = "AdobeExposurePreviewLinearPass"
)
@@ -11163,6 +11207,7 @@ class RawDemosaicProcessor {
profileBaselineExposureOffsetEv = profileBaselineExposureOffsetEv,
clampProfileRgb = clampProfileRgb,
supportProfileOverrange = supportProfileOverrange,
hueSatMapSupportsOverrange = hueSatMapSupportsOverrange,
hncsCameraDomainGains = hncsCameraDomainGains,
label = "LinearExposurePreviewPass"
)
@@ -30,7 +30,7 @@ class DngPhotonProfileGainTableGeneratorTest {
}
@Test
fun dngCoordinatesUseSourceLinearGuideAtTileCenters() {
fun dngCoordinatesUseMaxRgbGuideAtTileCenters() {
val plan = plan(
gridWidth = 4,
gridHeight = 2,
@@ -48,7 +48,7 @@ class DngPhotonProfileGainTableGeneratorTest {
assertEquals(0.375, plan.grid.mapSpacingV, 0.0)
assertEquals(0.3125, plan.grid.mapOriginH, 0.0)
assertEquals(0.3125, plan.grid.mapOriginV, 0.0)
DngHdrProfileGainTableGenerator.BASE_INPUT_WEIGHTS.forEachIndexed { index, weight ->
DngPhotonProfileGainTableGenerator.MAX_RGB_INPUT_WEIGHTS.forEachIndexed { index, weight ->
assertEquals(weight / exposureGain, plan.mapInputWeights[index], 1e-7f)
}
val map = requireNotNull(
@@ -90,6 +90,28 @@ class DngPhotonProfileGainTableGeneratorTest {
}
}
@Test
fun localLaplacianClampsSparsePeakBeforeNonlinearProfileStages() {
val width = 16
val height = 16
val source = FloatArray(width * height) { 0.1f }
source[source.lastIndex] = 1f
val result = DngPhotonLocalToneMapper.localLaplacianToneMap(
source = source,
width = width,
height = height,
exposureGain = 2f,
parameters = PhotonLocalToneMappingParameters(
targetDynamicRange = 1_000_000f,
),
)
assertEquals(0.2f, result.target.first(), 2e-4f)
assertEquals(1f, result.target.last(), 0f)
assertEquals(0, result.target.count { it > 1f })
}
private fun plan(
gridWidth: Int,
gridHeight: Int,