feat: new photon tone curve (2)
This commit is contained in:
1 parent
12d89f94d7
commit
b0373dacda
6 files changed
+638
-143
No files matched your search
@@ -3,7 +3,9 @@ package com.hinnka.mycamera.raw
|
||||
import java.util.Arrays
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.ln
|
||||
import kotlin.math.pow
|
||||
|
||||
/**
|
||||
* Scene-wide PGTM statistics computed from one uniform sample population.
|
||||
@@ -23,10 +25,21 @@ internal data class DngPgtmGlobalStats(
|
||||
val highlightFraction: Float,
|
||||
val linearMean: Float,
|
||||
val logAverage: Float,
|
||||
val shadowEdge: Float,
|
||||
val shadowFoot: Float,
|
||||
val sampleCount: Int,
|
||||
) {
|
||||
companion object {
|
||||
private const val HIGHLIGHT_THRESHOLD = 0.92f
|
||||
private const val LOG_HISTOGRAM_MIN_EV = -16f
|
||||
private const val LOG_HISTOGRAM_MAX_EV = 8f
|
||||
private const val LOG_HISTOGRAM_BIN_EV = 0.125f
|
||||
private const val LOG_HISTOGRAM_BIN_COUNT = 192
|
||||
private const val LOG_HISTOGRAM_SMOOTH_SIGMA_BINS = 2f
|
||||
private const val LOG_HISTOGRAM_SMOOTH_RADIUS = 8
|
||||
private const val SHADOW_SEARCH_MIN_QUANTILE = 0.01f
|
||||
private const val SHADOW_SEARCH_MAX_QUANTILE = 0.40f
|
||||
private const val SHADOW_FOOT_DENSITY_RATIO = 0.15f
|
||||
|
||||
/**
|
||||
* Compacts and sorts [samples] in place. The caller must not reuse the
|
||||
@@ -50,6 +63,7 @@ internal data class DngPgtmGlobalStats(
|
||||
if (validCount <= 0) return null
|
||||
|
||||
Arrays.sort(samples, 0, validCount)
|
||||
val shadowSlope = shadowSlopeFromSortedSamples(samples, validCount)
|
||||
return DngPgtmGlobalStats(
|
||||
p10 = percentileFromSorted(samples, validCount, 0.10f),
|
||||
p50 = percentileFromSorted(samples, validCount, 0.50f),
|
||||
@@ -61,10 +75,144 @@ internal data class DngPgtmGlobalStats(
|
||||
highlightFraction = highlightCount.toFloat() / validCount.toFloat(),
|
||||
linearMean = (linearSum / validCount.toDouble()).toFloat(),
|
||||
logAverage = exp(logSum / validCount.toDouble()).toFloat(),
|
||||
shadowEdge = shadowSlope.edge,
|
||||
shadowFoot = shadowSlope.foot,
|
||||
sampleCount = validCount
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates the main shadow-content onset in an EV histogram. The strongest
|
||||
* rising edge is searched only in the lower 40% of positive samples, then
|
||||
* its outer foot is taken where density reaches 15% of the following peak.
|
||||
* This keeps the black reference tied to image structure instead of to a
|
||||
* fixed population count such as p10.
|
||||
*/
|
||||
private fun shadowSlopeFromSortedSamples(
|
||||
sortedSamples: FloatArray,
|
||||
sampleCount: Int,
|
||||
): ShadowSlope {
|
||||
val histogram = FloatArray(LOG_HISTOGRAM_BIN_COUNT)
|
||||
var positiveCount = 0
|
||||
for (index in 0 until sampleCount) {
|
||||
val value = sortedSamples[index]
|
||||
if (value <= 0f) continue
|
||||
val ev = log2(value).coerceIn(
|
||||
LOG_HISTOGRAM_MIN_EV,
|
||||
LOG_HISTOGRAM_MAX_EV - LOG_HISTOGRAM_BIN_EV
|
||||
)
|
||||
val bin = floor(
|
||||
(ev - LOG_HISTOGRAM_MIN_EV) / LOG_HISTOGRAM_BIN_EV
|
||||
).toInt().coerceIn(0, histogram.lastIndex)
|
||||
histogram[bin] += 1f
|
||||
positiveCount += 1
|
||||
}
|
||||
if (positiveCount <= 0) {
|
||||
val minimum = 2.0f.pow(LOG_HISTOGRAM_MIN_EV)
|
||||
return ShadowSlope(edge = minimum, foot = minimum)
|
||||
}
|
||||
|
||||
val smoothed = smoothHistogram(histogram)
|
||||
val searchStart = percentileBin(
|
||||
histogram,
|
||||
positiveCount,
|
||||
SHADOW_SEARCH_MIN_QUANTILE
|
||||
).coerceIn(1, histogram.lastIndex - 1)
|
||||
val searchEnd = percentileBin(
|
||||
histogram,
|
||||
positiveCount,
|
||||
SHADOW_SEARCH_MAX_QUANTILE
|
||||
).coerceIn(searchStart, histogram.lastIndex - 1)
|
||||
|
||||
var edgeBin = searchStart
|
||||
var strongestRise = Float.NEGATIVE_INFINITY
|
||||
for (bin in searchStart..searchEnd) {
|
||||
val rise = smoothed[(bin + 1).coerceAtMost(smoothed.lastIndex)] -
|
||||
smoothed[bin - 1]
|
||||
if (rise > strongestRise) {
|
||||
strongestRise = rise
|
||||
edgeBin = bin
|
||||
}
|
||||
}
|
||||
|
||||
var peakBin = edgeBin
|
||||
for (bin in edgeBin..searchEnd) {
|
||||
if (smoothed[bin] > smoothed[peakBin]) peakBin = bin
|
||||
}
|
||||
val footDensity = smoothed[peakBin] * SHADOW_FOOT_DENSITY_RATIO
|
||||
var footBin = edgeBin
|
||||
while (footBin > 0 && smoothed[footBin] > footDensity) {
|
||||
footBin -= 1
|
||||
}
|
||||
|
||||
val edgeEv = binCenterEv(edgeBin)
|
||||
val footEv = if (footBin < edgeBin) {
|
||||
val lowerDensity = smoothed[footBin]
|
||||
val upperDensity = smoothed[footBin + 1]
|
||||
val fraction = if (upperDensity > lowerDensity) {
|
||||
((footDensity - lowerDensity) / (upperDensity - lowerDensity))
|
||||
.coerceIn(0f, 1f)
|
||||
} else {
|
||||
0f
|
||||
}
|
||||
binCenterEv(footBin) + fraction * LOG_HISTOGRAM_BIN_EV
|
||||
} else {
|
||||
edgeEv
|
||||
}
|
||||
return ShadowSlope(
|
||||
edge = 2.0f.pow(edgeEv),
|
||||
foot = 2.0f.pow(footEv.coerceAtMost(edgeEv))
|
||||
)
|
||||
}
|
||||
|
||||
private fun smoothHistogram(histogram: FloatArray): FloatArray {
|
||||
val kernel = FloatArray(LOG_HISTOGRAM_SMOOTH_RADIUS * 2 + 1)
|
||||
var kernelSum = 0.0
|
||||
for (offset in -LOG_HISTOGRAM_SMOOTH_RADIUS..LOG_HISTOGRAM_SMOOTH_RADIUS) {
|
||||
val normalized = offset.toDouble() / LOG_HISTOGRAM_SMOOTH_SIGMA_BINS.toDouble()
|
||||
val weight = exp(-0.5 * normalized * normalized)
|
||||
kernel[offset + LOG_HISTOGRAM_SMOOTH_RADIUS] = weight.toFloat()
|
||||
kernelSum += weight
|
||||
}
|
||||
for (index in kernel.indices) {
|
||||
kernel[index] = (kernel[index] / kernelSum).toFloat()
|
||||
}
|
||||
|
||||
return FloatArray(histogram.size) { bin ->
|
||||
var value = 0f
|
||||
for (offset in -LOG_HISTOGRAM_SMOOTH_RADIUS..LOG_HISTOGRAM_SMOOTH_RADIUS) {
|
||||
val sourceBin = bin + offset
|
||||
if (sourceBin !in histogram.indices) continue
|
||||
value += histogram[sourceBin] *
|
||||
kernel[offset + LOG_HISTOGRAM_SMOOTH_RADIUS]
|
||||
}
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
private fun percentileBin(
|
||||
histogram: FloatArray,
|
||||
sampleCount: Int,
|
||||
percentile: Float,
|
||||
): Int {
|
||||
val target = ceil(sampleCount * percentile.coerceIn(0f, 1f)).toInt()
|
||||
.coerceAtLeast(1)
|
||||
var cumulative = 0
|
||||
for (bin in histogram.indices) {
|
||||
cumulative += histogram[bin].toInt()
|
||||
if (cumulative >= target) return bin
|
||||
}
|
||||
return histogram.lastIndex
|
||||
}
|
||||
|
||||
private fun binCenterEv(bin: Int): Float {
|
||||
return LOG_HISTOGRAM_MIN_EV + (bin.toFloat() + 0.5f) * LOG_HISTOGRAM_BIN_EV
|
||||
}
|
||||
|
||||
private fun log2(value: Float): Float {
|
||||
return (ln(value.toDouble()) / ln(2.0)).toFloat()
|
||||
}
|
||||
|
||||
private fun percentileFromSorted(
|
||||
sortedSamples: FloatArray,
|
||||
sampleCount: Int,
|
||||
@@ -74,5 +222,10 @@ internal data class DngPgtmGlobalStats(
|
||||
.coerceIn(1, sampleCount) - 1
|
||||
return sortedSamples[index]
|
||||
}
|
||||
|
||||
private data class ShadowSlope(
|
||||
val edge: Float,
|
||||
val foot: Float,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -23,27 +23,28 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
// 257 points; a linear table would place its first sample only 8 EV down.
|
||||
private const val TABLE_INPUT_GAMMA = 0.5f
|
||||
private const val MAX_SCENE_MIDDLE_GRAY = 0.18f
|
||||
private const val SOURCE_SHADOW_HEADROOM_EV = 4f
|
||||
internal const val SHADOW_FOOT_HEADROOM_EV = 1.2f
|
||||
private const val MIN_SOURCE_COMPRESSION_EV = 0.75f
|
||||
private const val MAX_SOURCE_DYNAMIC_RANGE_EV = 12.5f
|
||||
private const val STANDARD_DYNAMIC_RANGE_EV = 8f
|
||||
private const val WIDE_DYNAMIC_RANGE_EV = 8.5f
|
||||
private const val HDR_DYNAMIC_RANGE_EV = 9f
|
||||
private const val EXTENDED_HDR_DYNAMIC_RANGE_EV = 9.5f
|
||||
private const val WIDE_RANGE_THRESHOLD_EV = 5.5f
|
||||
private const val HDR_RANGE_THRESHOLD_EV = 6f
|
||||
private const val EXTENDED_HDR_RANGE_THRESHOLD_EV = 6.5f
|
||||
private const val WIDE_HIGHLIGHT_RANGE_THRESHOLD_EV = 2.5f
|
||||
private const val HDR_HIGHLIGHT_RANGE_THRESHOLD_EV = 3.5f
|
||||
private const val EXTENDED_HDR_HIGHLIGHT_RANGE_THRESHOLD_EV = 4f
|
||||
private const val MIN_DYNAMIC_RANGE_EV = STANDARD_DYNAMIC_RANGE_EV
|
||||
private const val MAX_DYNAMIC_RANGE_EV = EXTENDED_HDR_DYNAMIC_RANGE_EV
|
||||
private const val MIN_STATS_BLACK_POINT = 0.002f
|
||||
private const val STANDARD_DISPLAY_MIDDLE_GRAY = 0.26f
|
||||
private const val WIDE_DISPLAY_MIDDLE_GRAY = 0.28f
|
||||
private const val HDR_DISPLAY_MIDDLE_GRAY = 0.30f
|
||||
private const val EXTENDED_HDR_DISPLAY_MIDDLE_GRAY = 0.32f
|
||||
private const val MIN_DISPLAY_EXPOSURE_LIFT_EV = 0.27f
|
||||
private const val MAX_DISPLAY_EXPOSURE_LIFT_EV = 2.24f
|
||||
private const val DISPLAY_EXPOSURE_TRANSITION_START_EV = 4.6f
|
||||
private const val DISPLAY_EXPOSURE_TRANSITION_END_EV = 6.3f
|
||||
private const val DISPLAY_EXPOSURE_TRANSITION_START_EV = 1.7f
|
||||
private const val DISPLAY_EXPOSURE_TRANSITION_END_EV = 3.45f
|
||||
private const val SPARSE_HIGHLIGHT_GAP_START_EV = 0.45f
|
||||
private const val SPARSE_HIGHLIGHT_GAP_FULL_EV = 0.90f
|
||||
private const val MIN_HIGHLIGHT_ENDPOINT_SLOPE_RATIO = 0.65f
|
||||
private const val MAX_HIGHLIGHT_ENDPOINT_SLOPE_RATIO = 0.88f
|
||||
private const val MAX_LOCAL_MICRO_CONTRAST_EV = 0.22f
|
||||
@@ -98,6 +99,7 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
val toneAnchors = buildPhotonToneAnchors(
|
||||
global = global,
|
||||
exposureAnchor = globalStats.logAverage,
|
||||
shadowFoot = globalStats.shadowFoot,
|
||||
baselineExposureEv = safeBaselineEv
|
||||
)
|
||||
val inputScale = sanitizeInputScale(1f / toneAnchors.whitePoint)
|
||||
@@ -140,21 +142,24 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
val appliedP50Output = p50Input * appliedP50Gain
|
||||
val finalDisplayP50 = photonProfileOutputForInput(appliedP50Output)
|
||||
val anchorMiddleGain = toneAnchors.middleGrayOutput / toneAnchors.middleGrayPoint
|
||||
val exposureLiftEv = displayExposureLiftEv(toneAnchors.observedDynamicRangeEv)
|
||||
val exposureLiftEv = displayExposureLiftEv(toneAnchors.exposureHighlightRangeEv)
|
||||
val domainOverflowEv = log2(
|
||||
max(global.p999, toneAnchors.whitePoint) / toneAnchors.whitePoint
|
||||
).coerceAtLeast(0f)
|
||||
val observedShadowRangeEv = log2(
|
||||
max(global.p50, MIN_CURVE_INPUT) / max(global.p10, MIN_STATS_BLACK_POINT)
|
||||
)
|
||||
val observedHighlightRangeEv = log2(
|
||||
max(global.p999, global.p50) / max(global.p50, MIN_CURVE_INPUT)
|
||||
max(globalStats.logAverage, globalStats.shadowFoot) /
|
||||
max(globalStats.shadowFoot, MIN_CURVE_INPUT)
|
||||
)
|
||||
PLog.d(
|
||||
TAG,
|
||||
"Built Photon PGTM: grid=${gridWidth}x${gridHeight}x$safePointCount " +
|
||||
"statsSource=global-samples globalSampleCount=${globalStats.sampleCount} " +
|
||||
"baselineEv=$safeBaselineEv observedRangeEv=${toneAnchors.observedDynamicRangeEv} " +
|
||||
"baselineEv=$safeBaselineEv " +
|
||||
"tailRangeEv=${toneAnchors.tailHighlightRangeEv} " +
|
||||
"exposureRangeEv=${toneAnchors.exposureHighlightRangeEv} " +
|
||||
"curveRangeEv=${toneAnchors.curveDynamicRangeEv} " +
|
||||
"targetRangeEv=${toneAnchors.targetDynamicRangeEv} " +
|
||||
"shadowRangeEv=$observedShadowRangeEv highlightRangeEv=$observedHighlightRangeEv " +
|
||||
"shadowRangeEv=$observedShadowRangeEv " +
|
||||
"relativeEv=${toneAnchors.blackRelativeExposure}.." +
|
||||
"${toneAnchors.whiteRelativeExposure} " +
|
||||
"source=${toneAnchors.blackPoint},${toneAnchors.middleGrayPoint}," +
|
||||
@@ -163,10 +168,15 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
"${toneAnchors.displayMiddleGray},1.0 " +
|
||||
"tierDisplayMiddle=${toneAnchors.tierDisplayMiddleGray} " +
|
||||
"rawRight=${toneAnchors.rawWhitePoint} inputScale=$inputScale " +
|
||||
"domainOverflowEv=$domainOverflowEv " +
|
||||
"p10=${global.p10} p50=${global.p50} p90=${global.p90} " +
|
||||
"p98=${global.p98} p995=${global.p995} p999=${global.p999} " +
|
||||
"maxInput=${global.maxInput} " +
|
||||
"linearMean=${globalStats.linearMean} logAverage=${globalStats.logAverage} " +
|
||||
"shadowEdge=${globalStats.shadowEdge} shadowFoot=${globalStats.shadowFoot} " +
|
||||
"exposureHighlight=${toneAnchors.exposureHighlightReference} " +
|
||||
"highlightGapEv=${toneAnchors.highlightGapEv} " +
|
||||
"sparseHighlightStrength=${toneAnchors.sparseHighlightStrength} " +
|
||||
"highlightFraction=${global.highlightFraction}"
|
||||
)
|
||||
PLog.d(
|
||||
@@ -178,8 +188,10 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
"appliedP50Gain=$appliedP50Gain pgtmP50=$appliedP50Output " +
|
||||
"displayP50=$finalDisplayP50 targetDisplayMiddle=${toneAnchors.displayMiddleGray} " +
|
||||
"tierDisplayMiddle=${toneAnchors.tierDisplayMiddleGray} " +
|
||||
"tailRangeEv=${toneAnchors.tailHighlightRangeEv} " +
|
||||
"exposureRangeEv=${toneAnchors.exposureHighlightRangeEv} " +
|
||||
"exposureLiftEv=$exposureLiftEv " +
|
||||
"whiteAdjustment=${toneAnchors.whitePoint / BASE_WHITE_POINT} " +
|
||||
"whiteAdjustment=${toneAnchors.rawWhitePoint / BASE_WHITE_POINT} " +
|
||||
"maxGain=$MAX_GAIN_VALUE"
|
||||
)
|
||||
}
|
||||
@@ -187,10 +199,10 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
}
|
||||
|
||||
private fun packedStatsAt(stats: FloatArray, offset: Int, sampleWeight: Float): PhotonPgtmCellStats {
|
||||
val p10 = safe01(stats[offset])
|
||||
val p50 = max(p10, safe01(stats[offset + 1]))
|
||||
val p90 = max(p50, safe01(stats[offset + 2]))
|
||||
val p98 = max(p90, safe01(stats[offset + 3]))
|
||||
val p10 = safePositive(stats[offset], 0f)
|
||||
val p50 = max(p10, safePositive(stats[offset + 1], p10))
|
||||
val p90 = max(p50, safePositive(stats[offset + 2], p50))
|
||||
val p98 = max(p90, safePositive(stats[offset + 3], p90))
|
||||
val p995 = max(p98, safePositive(stats[offset + 6], p98))
|
||||
val p999 = max(p995, safePositive(stats[offset + 7], p995))
|
||||
return PhotonPgtmCellStats(
|
||||
@@ -208,10 +220,10 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
|
||||
private fun DngPgtmGlobalStats.toPhotonCellStats(): PhotonPgtmCellStats? {
|
||||
if (sampleCount <= 0) return null
|
||||
val safeP10 = safe01(p10)
|
||||
val safeP50 = max(safeP10, safe01(p50))
|
||||
val safeP90 = max(safeP50, safe01(p90))
|
||||
val safeP98 = max(safeP90, safe01(p98))
|
||||
val safeP10 = safePositive(p10, 0f)
|
||||
val safeP50 = max(safeP10, safePositive(p50, safeP10))
|
||||
val safeP90 = max(safeP50, safePositive(p90, safeP50))
|
||||
val safeP98 = max(safeP90, safePositive(p98, safeP90))
|
||||
val safeP995 = max(safeP98, safePositive(p995, safeP98))
|
||||
val safeP999 = max(safeP995, safePositive(p999, safeP995))
|
||||
return PhotonPgtmCellStats(
|
||||
@@ -230,15 +242,22 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
private fun buildPhotonToneAnchors(
|
||||
global: PhotonPgtmCellStats,
|
||||
exposureAnchor: Float,
|
||||
shadowFoot: Float,
|
||||
baselineExposureEv: Float,
|
||||
): PhotonToneAnchors {
|
||||
val observedDynamicRangeEv = photonObservedDynamicRangeEv(global)
|
||||
val targetDynamicRangeEv = photonDynamicRangeTier(observedDynamicRangeEv)
|
||||
val highlightRanges = photonHighlightRanges(
|
||||
p90 = global.p90,
|
||||
p98 = global.p98,
|
||||
p995 = global.p995,
|
||||
p999 = global.p999,
|
||||
sceneMiddle = exposureAnchor
|
||||
)
|
||||
val targetDynamicRangeEv = photonDynamicRangeTier(highlightRanges.tailRangeEv)
|
||||
val displayBlackPoint = 1f / 2.0f.pow(targetDynamicRangeEv)
|
||||
val tierDisplayMiddleGray = displayMiddleGrayForTier(targetDynamicRangeEv)
|
||||
val displayMiddleGray = displayMiddleGrayForDynamicRange(
|
||||
val displayMiddleGray = displayMiddleGrayForHighlightRange(
|
||||
exposureAnchor = exposureAnchor,
|
||||
observedRangeEv = observedDynamicRangeEv,
|
||||
highlightRangeEv = highlightRanges.exposureRangeEv,
|
||||
displayBlackPoint = displayBlackPoint,
|
||||
tierDisplayMiddleGray = tierDisplayMiddleGray
|
||||
)
|
||||
@@ -247,31 +266,35 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
val whitePointAdjustment = photonWhitePointAdjustment(global, targetDynamicRangeEv)
|
||||
val baselineGain = 2.0f.pow(baselineExposureEv.coerceIn(MIN_BASELINE_EV, MAX_BASELINE_EV))
|
||||
|
||||
// ProfileGainTableMap is evaluated with baseline gain applied to its
|
||||
// input. A scene-domain white of 1 therefore corresponds to raw-linear
|
||||
// 1 / baselineGain. Highlight statistics may extend that endpoint, but
|
||||
// the extension is deliberately capped at 30%.
|
||||
val rawWhitePoint = (BASE_WHITE_POINT / baselineGain) * whitePointAdjustment
|
||||
// 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.
|
||||
val blackRangeWhiteReference = BASE_WHITE_POINT * whitePointAdjustment
|
||||
val rawWhitePoint = blackRangeWhiteReference
|
||||
val whitePoint = rawWhitePoint * baselineGain
|
||||
|
||||
// p10 is a shadow percentile, not the scene black. Reserve up to four
|
||||
// stops below it without extending the curve domain beyond the selected
|
||||
// output range plus the deliberate compression headroom.
|
||||
val percentileBlackPoint = max(global.p10, MIN_STATS_BLACK_POINT) /
|
||||
2.0f.pow(SOURCE_SHADOW_HEADROOM_EV)
|
||||
val compressionBlackPoint = whitePoint /
|
||||
// The EV-histogram foot identifies the perceptual shadow onset. Keep a
|
||||
// short toe reserve below it; unlike p10, this point is based on image
|
||||
// structure and never participates in exposure lift. Black placement is
|
||||
// normalized to the raw-white reference, not to the expanded Baseline-
|
||||
// Exposure lookup domain, so extending highlights cannot move exposure.
|
||||
val slopeBlackPoint = max(shadowFoot, MIN_CURVE_INPUT) /
|
||||
2.0f.pow(SHADOW_FOOT_HEADROOM_EV)
|
||||
val compressionBlackPoint = blackRangeWhiteReference /
|
||||
2.0f.pow(targetDynamicRangeEv + MIN_SOURCE_COMPRESSION_EV)
|
||||
val gainLimitedBlackPoint = blackOutput / MAX_GAIN_VALUE
|
||||
val minimumBlackPoint = max(
|
||||
whitePoint / 2.0f.pow(MAX_SOURCE_DYNAMIC_RANGE_EV),
|
||||
blackRangeWhiteReference / 2.0f.pow(MAX_SOURCE_DYNAMIC_RANGE_EV),
|
||||
gainLimitedBlackPoint
|
||||
)
|
||||
val blackPoint = max(percentileBlackPoint, compressionBlackPoint)
|
||||
.coerceIn(minimumBlackPoint, whitePoint * 0.25f)
|
||||
val blackPoint = max(slopeBlackPoint, compressionBlackPoint)
|
||||
.coerceIn(minimumBlackPoint, blackRangeWhiteReference * 0.25f)
|
||||
|
||||
// Exposure normalization is global: map the perceptual log-average
|
||||
// luminance (never a value above canonical 18% gray) through the scene-range exposure
|
||||
// curve, within the selected tier's perceptual middle-gray ceiling.
|
||||
// luminance (never a value above canonical 18% gray) through the
|
||||
// highlight-range exposure curve, within the selected tier's ceiling.
|
||||
val minimumMiddleGrayPoint = max(
|
||||
blackPoint * 4f,
|
||||
middleGrayOutput / MAX_GAIN_VALUE
|
||||
@@ -285,7 +308,11 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
val blackRelativeExposure = log2(blackPoint / middleGrayPoint)
|
||||
val curveDynamicRangeEv = whiteRelativeExposure - blackRelativeExposure
|
||||
return PhotonToneAnchors(
|
||||
observedDynamicRangeEv = observedDynamicRangeEv,
|
||||
tailHighlightRangeEv = highlightRanges.tailRangeEv,
|
||||
exposureHighlightRangeEv = highlightRanges.exposureRangeEv,
|
||||
exposureHighlightReference = highlightRanges.exposureReference,
|
||||
highlightGapEv = highlightRanges.highlightGapEv,
|
||||
sparseHighlightStrength = highlightRanges.sparseStrength,
|
||||
targetDynamicRangeEv = targetDynamicRangeEv,
|
||||
curveDynamicRangeEv = curveDynamicRangeEv,
|
||||
blackRelativeExposure = blackRelativeExposure,
|
||||
@@ -311,24 +338,24 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
private fun displayMiddleGrayForDynamicRange(
|
||||
private fun displayMiddleGrayForHighlightRange(
|
||||
exposureAnchor: Float,
|
||||
observedRangeEv: Float,
|
||||
highlightRangeEv: Float,
|
||||
displayBlackPoint: Float,
|
||||
tierDisplayMiddleGray: Float,
|
||||
): Float {
|
||||
val exposureLiftEv = displayExposureLiftEv(observedRangeEv)
|
||||
val exposureLiftEv = displayExposureLiftEv(highlightRangeEv)
|
||||
return (exposureAnchor * 2.0f.pow(exposureLiftEv)).coerceIn(
|
||||
displayBlackPoint * 4f,
|
||||
tierDisplayMiddleGray
|
||||
)
|
||||
}
|
||||
|
||||
internal fun displayExposureLiftEv(observedRangeEv: Float): Float {
|
||||
internal fun displayExposureLiftEv(highlightRangeEv: Float): Float {
|
||||
val rangeStrength = smoothStep(
|
||||
DISPLAY_EXPOSURE_TRANSITION_START_EV,
|
||||
DISPLAY_EXPOSURE_TRANSITION_END_EV,
|
||||
observedRangeEv
|
||||
highlightRangeEv
|
||||
)
|
||||
return lerp(
|
||||
MIN_DISPLAY_EXPOSURE_LIFT_EV,
|
||||
@@ -337,20 +364,52 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
)
|
||||
}
|
||||
|
||||
private fun photonObservedDynamicRangeEv(global: PhotonPgtmCellStats): Float {
|
||||
val shadowReference = max(global.p10, MIN_STATS_BLACK_POINT)
|
||||
val percentileWhite = max(global.p98, global.p90)
|
||||
val tailWhite = max(global.p999, max(global.p995, percentileWhite))
|
||||
val percentileRangeEv = log2(max(percentileWhite, shadowReference) / shadowReference)
|
||||
val tailRangeEv = log2(max(tailWhite, shadowReference) / shadowReference)
|
||||
return max(percentileRangeEv, tailRangeEv)
|
||||
internal fun photonHighlightRanges(
|
||||
p90: Float,
|
||||
p98: Float,
|
||||
p995: Float,
|
||||
p999: Float,
|
||||
sceneMiddle: Float,
|
||||
): PhotonHighlightRanges {
|
||||
val safeMiddle = max(sceneMiddle, MIN_CURVE_INPUT)
|
||||
val safeP90 = max(p90, safeMiddle)
|
||||
val safeP98 = max(p98, safeP90)
|
||||
val safeP995 = max(p995, safeP98)
|
||||
val safeP999 = max(p999, safeP995)
|
||||
val highlightGapEv = log2(safeP98 / safeP90).coerceAtLeast(0f)
|
||||
val sparseStrength = smoothStep(
|
||||
SPARSE_HIGHLIGHT_GAP_START_EV,
|
||||
SPARSE_HIGHLIGHT_GAP_FULL_EV,
|
||||
highlightGapEv
|
||||
)
|
||||
|
||||
// p999 remains the compression endpoint. When the upper 2% is
|
||||
// separated from the main highlight population by a large EV gap,
|
||||
// drive whole-frame exposure from the log-center across that gap
|
||||
// instead of from percentiles that are both inside the sparse bright
|
||||
// population. This preserves the tail without letting a window or
|
||||
// specular region brighten the rest of the image.
|
||||
val denseHighlightReference = 2.0f.pow(
|
||||
0.5f * (log2(safeP90) + log2(safeP98))
|
||||
)
|
||||
val exposureReference = 2.0f.pow(
|
||||
lerp(log2(safeP999), log2(denseHighlightReference), sparseStrength)
|
||||
)
|
||||
return PhotonHighlightRanges(
|
||||
tailRangeEv = log2(safeP999 / safeMiddle).coerceAtLeast(0f),
|
||||
exposureRangeEv = log2(exposureReference / safeMiddle).coerceAtLeast(0f),
|
||||
exposureReference = exposureReference,
|
||||
highlightGapEv = highlightGapEv,
|
||||
sparseStrength = sparseStrength
|
||||
)
|
||||
}
|
||||
|
||||
private fun photonDynamicRangeTier(observedRangeEv: Float): Float {
|
||||
private fun photonDynamicRangeTier(highlightRangeEv: Float): Float {
|
||||
val tier = when {
|
||||
observedRangeEv >= EXTENDED_HDR_RANGE_THRESHOLD_EV -> EXTENDED_HDR_DYNAMIC_RANGE_EV
|
||||
observedRangeEv >= HDR_RANGE_THRESHOLD_EV -> HDR_DYNAMIC_RANGE_EV
|
||||
observedRangeEv >= WIDE_RANGE_THRESHOLD_EV -> WIDE_DYNAMIC_RANGE_EV
|
||||
highlightRangeEv >= EXTENDED_HDR_HIGHLIGHT_RANGE_THRESHOLD_EV ->
|
||||
EXTENDED_HDR_DYNAMIC_RANGE_EV
|
||||
highlightRangeEv >= HDR_HIGHLIGHT_RANGE_THRESHOLD_EV -> HDR_DYNAMIC_RANGE_EV
|
||||
highlightRangeEv >= WIDE_HIGHLIGHT_RANGE_THRESHOLD_EV -> WIDE_DYNAMIC_RANGE_EV
|
||||
else -> STANDARD_DYNAMIC_RANGE_EV
|
||||
}
|
||||
return tier.coerceIn(MIN_DYNAMIC_RANGE_EV, MAX_DYNAMIC_RANGE_EV)
|
||||
@@ -562,7 +621,7 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
|
||||
private fun sanitizeInputScale(inputScale: Float): Float {
|
||||
return inputScale.takeIf { it.isFinite() }
|
||||
?.coerceIn(1f / MAX_WHITE_POINT_ADJUSTMENT, 1.0f)
|
||||
?.coerceIn(MIN_CURVE_INPUT, 1.0f)
|
||||
?: 1.0f
|
||||
}
|
||||
|
||||
@@ -638,7 +697,11 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
)
|
||||
|
||||
private data class PhotonToneAnchors(
|
||||
val observedDynamicRangeEv: Float,
|
||||
val tailHighlightRangeEv: Float,
|
||||
val exposureHighlightRangeEv: Float,
|
||||
val exposureHighlightReference: Float,
|
||||
val highlightGapEv: Float,
|
||||
val sparseHighlightStrength: Float,
|
||||
val targetDynamicRangeEv: Float,
|
||||
val curveDynamicRangeEv: Float,
|
||||
val blackRelativeExposure: Float,
|
||||
@@ -653,4 +716,12 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
val whitePoint: Float,
|
||||
val rawWhitePoint: Float,
|
||||
)
|
||||
|
||||
internal data class PhotonHighlightRanges(
|
||||
val tailRangeEv: Float,
|
||||
val exposureRangeEv: Float,
|
||||
val exposureReference: Float,
|
||||
val highlightGapEv: Float,
|
||||
val sparseStrength: Float,
|
||||
)
|
||||
}
|
||||
@@ -80,7 +80,9 @@ internal object DngProfileToneCurve {
|
||||
0.97638f, 0.99517f, 0.98425f, 0.99686f, 0.99213f, 0.99845f, 1f, 1f
|
||||
)
|
||||
|
||||
// Fixed neutral base curve. Scene-dependent contrast and exposure belong to PGTM.
|
||||
// Shape-preserving resample of the original curve, pinned at (0.25, 0.15) and (0.75, 0.8).
|
||||
// The original toe, midpoint, and shoulder remain fixed; scene-dependent contrast and
|
||||
// exposure belong to PGTM.
|
||||
private val PHOTON_PGTM_TONE_CURVE_POINTS = floatArrayOf(
|
||||
0.00000000f, 0.000000000f,
|
||||
0.00390625f, 0.000012401f,
|
||||
@@ -91,16 +93,25 @@ internal object DngProfileToneCurve {
|
||||
0.02343750f, 0.002622200f,
|
||||
0.02734375f, 0.004144829f,
|
||||
0.03125000f, 0.005831674f,
|
||||
0.04687500f, 0.013821328f,
|
||||
0.06250000f, 0.023228984f,
|
||||
0.09375000f, 0.045079457f,
|
||||
0.12500000f, 0.069945026f,
|
||||
0.17968750f, 0.118763626f,
|
||||
0.25000000f, 0.188884780f,
|
||||
0.37500000f, 0.327727005f,
|
||||
0.04687500f, 0.011777433f,
|
||||
0.06250000f, 0.018572280f,
|
||||
0.09375000f, 0.034529686f,
|
||||
0.12500000f, 0.053344787f,
|
||||
0.17968750f, 0.092067414f,
|
||||
0.21875000f, 0.123345188f,
|
||||
0.25000000f, 0.150000000f,
|
||||
0.28125000f, 0.180555931f,
|
||||
0.31250000f, 0.216832463f,
|
||||
0.37500000f, 0.301148691f,
|
||||
0.43750000f, 0.392151404f,
|
||||
0.50000000f, 0.479043320f,
|
||||
0.62500000f, 0.636591673f,
|
||||
0.75000000f, 0.779041231f,
|
||||
0.56250000f, 0.563864078f,
|
||||
0.62500000f, 0.650740975f,
|
||||
0.68750000f, 0.732008214f,
|
||||
0.71875000f, 0.768142651f,
|
||||
0.75000000f, 0.800000000f,
|
||||
0.78125000f, 0.826992743f,
|
||||
0.81250000f, 0.850655138f,
|
||||
0.87500000f, 0.896886647f,
|
||||
1.00000000f, 1.000000000f
|
||||
)
|
||||
|
||||
@@ -10,7 +10,6 @@ import kotlin.math.max
|
||||
internal object RawProfileGainTableMapBuilder {
|
||||
private const val TAG = "RawProfileGainTableMapBuilder"
|
||||
private const val SAMPLE_GRID = 16
|
||||
private const val HIST_BINS = 256
|
||||
|
||||
private data class BuiltPgtmStats(
|
||||
val packedCellStats: FloatArray,
|
||||
@@ -98,7 +97,6 @@ internal object RawProfileGainTableMapBuilder {
|
||||
|
||||
val baselineGain = DngBaselineExposure.exactGain(metadata.baselineExposure)
|
||||
val stats = FloatArray(gridWidth * gridHeight * DngHdrProfileGainTableGenerator.CELL_STATS_FLOAT_STRIDE)
|
||||
val hist = IntArray(HIST_BINS)
|
||||
val inputSamples = FloatArray(SAMPLE_GRID * SAMPLE_GRID)
|
||||
val globalSamples = if (collectGlobalStats) {
|
||||
FloatArray(gridWidth * gridHeight * SAMPLE_GRID * SAMPLE_GRID)
|
||||
@@ -110,7 +108,6 @@ internal object RawProfileGainTableMapBuilder {
|
||||
val statsHeight = safeStatsBounds.height()
|
||||
for (cellY in 0 until gridHeight) {
|
||||
for (cellX in 0 until gridWidth) {
|
||||
java.util.Arrays.fill(hist, 0)
|
||||
var sampleCount = 0
|
||||
var highlightCount = 0
|
||||
var startX = safeStatsBounds.left + (cellX * statsWidth) / gridWidth
|
||||
@@ -153,11 +150,6 @@ internal object RawProfileGainTableMapBuilder {
|
||||
globalSamples[globalSampleCount] = inputValue
|
||||
globalSampleCount += 1
|
||||
}
|
||||
val clampedInput = inputValue.coerceIn(0f, 1f)
|
||||
val bin = (clampedInput * (HIST_BINS - 1).toFloat() + 0.5f)
|
||||
.toInt()
|
||||
.coerceIn(0, HIST_BINS - 1)
|
||||
hist[bin] += 1
|
||||
sampleCount += 1
|
||||
if (inputValue >= 0.92f) {
|
||||
highlightCount += 1
|
||||
@@ -165,18 +157,19 @@ internal object RawProfileGainTableMapBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
stats[offset] = percentileFromHist(hist, sampleCount, 0.10f)
|
||||
stats[offset + 1] = percentileFromHist(hist, sampleCount, 0.50f)
|
||||
stats[offset + 2] = percentileFromHist(hist, sampleCount, 0.90f)
|
||||
stats[offset + 3] = percentileFromHist(hist, sampleCount, 0.98f)
|
||||
java.util.Arrays.sort(inputSamples, 0, sampleCount)
|
||||
stats[offset] = percentileFromSortedSamples(inputSamples, sampleCount, 0.10f)
|
||||
stats[offset + 1] = percentileFromSortedSamples(inputSamples, sampleCount, 0.50f)
|
||||
stats[offset + 2] = percentileFromSortedSamples(inputSamples, sampleCount, 0.90f)
|
||||
stats[offset + 3] = percentileFromSortedSamples(inputSamples, sampleCount, 0.98f)
|
||||
stats[offset + 4] = if (sampleCount > 0) {
|
||||
highlightCount.toFloat() / sampleCount.toFloat()
|
||||
} else {
|
||||
0f
|
||||
}
|
||||
stats[offset + 5] = sampleCount.toFloat()
|
||||
stats[offset + 6] = percentileFromSamples(inputSamples, sampleCount, 0.995f)
|
||||
stats[offset + 7] = percentileFromSamples(inputSamples, sampleCount, 0.999f)
|
||||
stats[offset + 6] = percentileFromSortedSamples(inputSamples, sampleCount, 0.995f)
|
||||
stats[offset + 7] = percentileFromSortedSamples(inputSamples, sampleCount, 0.999f)
|
||||
}
|
||||
}
|
||||
val globalStats = globalSamples?.let { samples ->
|
||||
@@ -351,23 +344,12 @@ internal object RawProfileGainTableMapBuilder {
|
||||
return ((raw.toFloat() - black) / range).coerceIn(0f, 1f)
|
||||
}
|
||||
|
||||
private fun percentileFromHist(hist: IntArray, sampleCount: Int, percentile: Float): Float {
|
||||
private fun percentileFromSortedSamples(
|
||||
samples: FloatArray,
|
||||
sampleCount: Int,
|
||||
percentile: Float,
|
||||
): Float {
|
||||
if (sampleCount <= 0) return 0f
|
||||
val target = ceil(sampleCount.toFloat() * percentile.coerceIn(0f, 1f)).toInt()
|
||||
.coerceAtLeast(1)
|
||||
var cumulative = 0
|
||||
hist.forEachIndexed { index, count ->
|
||||
cumulative += count
|
||||
if (cumulative >= target) {
|
||||
return index.toFloat() / (hist.size - 1).toFloat()
|
||||
}
|
||||
}
|
||||
return 1f
|
||||
}
|
||||
|
||||
private fun percentileFromSamples(samples: FloatArray, sampleCount: Int, percentile: Float): Float {
|
||||
if (sampleCount <= 0) return 0f
|
||||
java.util.Arrays.sort(samples, 0, sampleCount)
|
||||
val index = ceil(sampleCount.toFloat() * percentile.coerceIn(0f, 1f)).toInt()
|
||||
.coerceIn(1, sampleCount) - 1
|
||||
return samples[index].takeIf { it.isFinite() && it > 0f } ?: 0f
|
||||
|
||||
@@ -2,9 +2,11 @@ package com.hinnka.mycamera.raw
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.ln
|
||||
import kotlin.math.pow
|
||||
|
||||
class DngPgtmGlobalStatsTest {
|
||||
@Test
|
||||
@@ -28,6 +30,43 @@ class DngPgtmGlobalStatsTest {
|
||||
(1..1_000).sumOf { value -> ln(value.toDouble() / 1_000.0) } / 1_000.0
|
||||
).toFloat()
|
||||
assertEquals(expectedLogAverage, stats.logAverage, 1e-6f)
|
||||
assertTrue("shadowEdge=${stats.shadowEdge}", stats.shadowEdge > 0f)
|
||||
assertTrue(
|
||||
"shadowFoot=${stats.shadowFoot} shadowEdge=${stats.shadowEdge}",
|
||||
stats.shadowFoot in 0f..stats.shadowEdge
|
||||
)
|
||||
assertEquals(1_000, stats.sampleCount)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shadowSlopeFootIgnoresMovementInsideSparseDarkTail() {
|
||||
fun samples(tailStartEv: Float): FloatArray {
|
||||
return FloatArray(1_000) { index ->
|
||||
val ev = if (index < 150) {
|
||||
tailStartEv + 3f * index.toFloat() / 149f
|
||||
} else {
|
||||
-4f + 3f * (index - 150).toFloat() / 849f
|
||||
}
|
||||
2.0f.pow(ev)
|
||||
}
|
||||
}
|
||||
|
||||
val first = DngPgtmGlobalStats.fromMutableSamples(samples(-9f), 1_000)
|
||||
?: error("Expected first stats")
|
||||
val second = DngPgtmGlobalStats.fromMutableSamples(samples(-10f), 1_000)
|
||||
?: error("Expected second stats")
|
||||
val p10DeltaEv = kotlin.math.abs(log2(second.p10 / first.p10))
|
||||
val footDeltaEv = kotlin.math.abs(log2(second.shadowFoot / first.shadowFoot))
|
||||
|
||||
assertTrue("p10DeltaEv=$p10DeltaEv", p10DeltaEv > 0.8f)
|
||||
assertTrue(
|
||||
"firstFoot=${first.shadowFoot} secondFoot=${second.shadowFoot} " +
|
||||
"footDeltaEv=$footDeltaEv",
|
||||
footDeltaEv < 0.2f
|
||||
)
|
||||
}
|
||||
|
||||
private fun log2(value: Float): Float {
|
||||
return (ln(value.toDouble()) / ln(2.0)).toFloat()
|
||||
}
|
||||
}
|
||||
+280
-41
@@ -10,26 +10,9 @@ import kotlin.math.pow
|
||||
|
||||
class DngPhotonProfileGainTableGeneratorTest {
|
||||
@Test
|
||||
fun compatibleFieldCalibrationScenesStayWithinPointTwelveEv() {
|
||||
fun compatibleWideRangeFieldCalibrationStaysWithinPointTwelveEv() {
|
||||
assertFieldCalibrationCases(
|
||||
listOf(
|
||||
ExposureCalibrationCase(
|
||||
name = "14:18 baseline-0 low-range",
|
||||
baselineExposureEv = 0f,
|
||||
p10 = 0.035187688f,
|
||||
p50 = 0.08524494f,
|
||||
p90 = 0.14404307f,
|
||||
p98 = 0.16417652f,
|
||||
p995 = 0.17360601f,
|
||||
p999 = 0.18164334f,
|
||||
maxInput = 0.20776238f,
|
||||
linearMean = null,
|
||||
logAverage = null,
|
||||
highlightFraction = 0f,
|
||||
reportedObservedRangeEv = 2.3679657f,
|
||||
reportedExposureLiftEv = 0.66f,
|
||||
reportedBrightnessErrorEv = 0.39f
|
||||
),
|
||||
ExposureCalibrationCase(
|
||||
name = "14:22 baseline-0 wide-range",
|
||||
baselineExposureEv = 0f,
|
||||
@@ -51,6 +34,227 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
)
|
||||
}
|
||||
|
||||
@Ignore(
|
||||
"This persisted field case predates log-average diagnostics. The new " +
|
||||
"exposure model requires the measured log average and must not " +
|
||||
"reconstruct it from p50."
|
||||
)
|
||||
@Test
|
||||
fun lowRangeFieldCalibrationWithoutLogAverage() {
|
||||
assertFieldCalibrationCases(
|
||||
listOf(
|
||||
ExposureCalibrationCase(
|
||||
name = "14:18 baseline-0 low-range",
|
||||
baselineExposureEv = 0f,
|
||||
p10 = 0.035187688f,
|
||||
p50 = 0.08524494f,
|
||||
p90 = 0.14404307f,
|
||||
p98 = 0.16417652f,
|
||||
p995 = 0.17360601f,
|
||||
p999 = 0.18164334f,
|
||||
maxInput = 0.20776238f,
|
||||
linearMean = null,
|
||||
logAverage = null,
|
||||
highlightFraction = 0f,
|
||||
reportedObservedRangeEv = 2.3679657f,
|
||||
reportedExposureLiftEv = 0.66f,
|
||||
reportedBrightnessErrorEv = 0.39f
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun highlightRangeSeparatesOldScenesAndStabilizesNearIdenticalDngs() {
|
||||
val oldWideRange = log2(0.5752382f / 0.09985464f)
|
||||
val oldNarrowRange = log2(0.2313348f / 0.07818398f)
|
||||
assertTrue(
|
||||
"oldWide=$oldWideRange oldNarrow=$oldNarrowRange",
|
||||
oldWideRange - oldNarrowRange > 0.90f
|
||||
)
|
||||
|
||||
val dng2840LogAverage = 0.0932018935f
|
||||
val dng2840P999 = 0.3981607637f
|
||||
val dng2842LogAverage = 0.07615876645f
|
||||
val dng2842P999 = 0.3566421426f
|
||||
val dng2840Range = log2(dng2840P999 / dng2840LogAverage)
|
||||
val dng2842Range = log2(dng2842P999 / dng2842LogAverage)
|
||||
assertTrue(
|
||||
"dng2840=$dng2840Range dng2842=$dng2842Range",
|
||||
kotlin.math.abs(dng2842Range - dng2840Range) < 0.15f
|
||||
)
|
||||
|
||||
val dng2840DisplayEv = log2(dng2840LogAverage) +
|
||||
DngPhotonProfileGainTableGenerator.displayExposureLiftEv(dng2840Range)
|
||||
val dng2842DisplayEv = log2(dng2842LogAverage) +
|
||||
DngPhotonProfileGainTableGenerator.displayExposureLiftEv(dng2842Range)
|
||||
assertTrue(
|
||||
"dng2840DisplayEv=$dng2840DisplayEv dng2842DisplayEv=$dng2842DisplayEv",
|
||||
kotlin.math.abs(dng2842DisplayEv - dng2840DisplayEv) <= 0.12f
|
||||
)
|
||||
|
||||
val normalHighlightCases = listOf(
|
||||
DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = 0.263932749f,
|
||||
p98 = 0.3304134763f,
|
||||
p995 = 0.3685588609f,
|
||||
p999 = 0.3981607637f,
|
||||
sceneMiddle = 0.0932018935f
|
||||
),
|
||||
DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = 0.2486532208f,
|
||||
p98 = 0.2975876967f,
|
||||
p995 = 0.3287509693f,
|
||||
p999 = 0.3566421426f,
|
||||
sceneMiddle = 0.07615876645f
|
||||
),
|
||||
DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = 0.35547075f,
|
||||
p98 = 0.4203099f,
|
||||
p995 = 0.44679728f,
|
||||
p999 = 0.46304873f,
|
||||
sceneMiddle = 0.039605092f
|
||||
),
|
||||
DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = 0.21021318f,
|
||||
p98 = 0.24911182f,
|
||||
p995 = 0.27169982f,
|
||||
p999 = 0.5752382f,
|
||||
sceneMiddle = 0.09985464f
|
||||
),
|
||||
DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = 0.13890508f,
|
||||
p98 = 0.18092623f,
|
||||
p995 = 0.2126976f,
|
||||
p999 = 0.2313348f,
|
||||
sceneMiddle = 0.07818398f
|
||||
)
|
||||
)
|
||||
normalHighlightCases.forEach { ranges ->
|
||||
assertTrue("sparseStrength=${ranges.sparseStrength}", ranges.sparseStrength == 0f)
|
||||
assertTrue(
|
||||
"tail=${ranges.tailRangeEv} exposure=${ranges.exposureRangeEv}",
|
||||
kotlin.math.abs(ranges.exposureRangeEv - ranges.tailRangeEv) < 1e-5f
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun separatedSparseHighlightsUseLogCenterAcrossHighlightGap() {
|
||||
val ranges = DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = 0.20524885f,
|
||||
p98 = 0.45892385f,
|
||||
p995 = 0.5724387f,
|
||||
p999 = 0.6364626f,
|
||||
sceneMiddle = 0.09018845f
|
||||
)
|
||||
val tailLift = DngPhotonProfileGainTableGenerator.displayExposureLiftEv(
|
||||
ranges.tailRangeEv
|
||||
)
|
||||
val exposureLift = DngPhotonProfileGainTableGenerator.displayExposureLiftEv(
|
||||
ranges.exposureRangeEv
|
||||
)
|
||||
|
||||
assertTrue("tailRange=${ranges.tailRangeEv}", ranges.tailRangeEv in 2.81f..2.83f)
|
||||
assertTrue(
|
||||
"exposureRange=${ranges.exposureRangeEv}",
|
||||
ranges.exposureRangeEv in 1.76f..1.78f
|
||||
)
|
||||
assertTrue("sparseStrength=${ranges.sparseStrength}", ranges.sparseStrength == 1f)
|
||||
assertTrue(
|
||||
"tailLift=$tailLift exposureLift=$exposureLift",
|
||||
exposureLift - tailLift in -1.39f..-1.37f
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stronglySeparatedNearWhiteHighlightsDoNotForceMaximumExposureLift() {
|
||||
val ranges = DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = 0.21023509f,
|
||||
p98 = 0.8875543f,
|
||||
p995 = 1.0032426f,
|
||||
p999 = 1.0724883f,
|
||||
sceneMiddle = 0.06121027f
|
||||
)
|
||||
val exposureLift = DngPhotonProfileGainTableGenerator.displayExposureLiftEv(
|
||||
ranges.exposureRangeEv
|
||||
)
|
||||
val displayMiddle = 0.06121027f * 2.0f.pow(exposureLift)
|
||||
|
||||
assertTrue("gap=${ranges.highlightGapEv}", ranges.highlightGapEv in 2.07f..2.09f)
|
||||
assertTrue("sparseStrength=${ranges.sparseStrength}", ranges.sparseStrength == 1f)
|
||||
assertTrue(
|
||||
"exposureReference=${ranges.exposureReference}",
|
||||
ranges.exposureReference in 0.431f..0.433f
|
||||
)
|
||||
assertTrue(
|
||||
"exposureRange=${ranges.exposureRangeEv}",
|
||||
ranges.exposureRangeEv in 2.81f..2.83f
|
||||
)
|
||||
assertTrue("exposureLift=$exposureLift", exposureLift in 1.65f..1.67f)
|
||||
assertTrue("displayMiddle=$displayMiddle", displayMiddle in 0.192f..0.194f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun baselineExposureHeadroomRemainsInsideProfileGainTableDomain() {
|
||||
val width = 1280
|
||||
val height = 960
|
||||
val grid = DngHdrProfileGainTableGenerator.gridSizeFor(width, height)
|
||||
val cellCount = grid[0] * grid[1]
|
||||
val stats = FloatArray(
|
||||
cellCount * DngHdrProfileGainTableGenerator.CELL_STATS_FLOAT_STRIDE
|
||||
)
|
||||
repeat(cellCount) { cell ->
|
||||
val offset = cell * DngHdrProfileGainTableGenerator.CELL_STATS_FLOAT_STRIDE
|
||||
stats[offset] = 0.008017952f
|
||||
stats[offset + 1] = 0.06326936f
|
||||
stats[offset + 2] = 1f
|
||||
stats[offset + 3] = 1f
|
||||
stats[offset + 4] = 0.13241959f
|
||||
stats[offset + 5] = 256f
|
||||
stats[offset + 6] = 12.278543f
|
||||
stats[offset + 7] = 14.262961f
|
||||
}
|
||||
val baselineGain = 2.0f.pow(3.000001f)
|
||||
val map = DngPhotonProfileGainTableGenerator.forCellStats(
|
||||
width = width,
|
||||
height = height,
|
||||
baselineExposureEv = 3.000001f,
|
||||
packedCellStats = stats,
|
||||
globalStats = testGlobalStats(
|
||||
p10 = 0.008017952f,
|
||||
p50 = 0.06326936f,
|
||||
p90 = 1f,
|
||||
p98 = 1f,
|
||||
p995 = 12.278543f,
|
||||
p999 = 14.262961f,
|
||||
maxInput = 14.262961f,
|
||||
highlightFraction = 0.13241959f,
|
||||
linearMean = 0.51709765f,
|
||||
logAverage = 0.07712941f,
|
||||
shadowEdge = 0.025163911f,
|
||||
shadowFoot = 0.0035361373f
|
||||
),
|
||||
emitDiagnostics = false
|
||||
) ?: error("Expected Photon PGTM")
|
||||
|
||||
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 displayAtDomainRight = finalOutputForScene(map, inputScale, sceneWhite)
|
||||
|
||||
assertTrue("sceneWhite=$sceneWhite", sceneWhite in 10.39f..10.41f)
|
||||
assertTrue("rawRight=$rawRight", rawRight in 1.299f..1.301f)
|
||||
assertTrue(
|
||||
"nominal=$displayAtNominalWhite rawWhite=$displayAtRawWhite",
|
||||
displayAtRawWhite > displayAtNominalWhite + 0.05f
|
||||
)
|
||||
assertTrue("domainRight=$displayAtDomainRight", displayAtDomainRight in 0.99f..1.01f)
|
||||
assertCurvesAreFiniteAndMonotonic(map, sceneWhite)
|
||||
}
|
||||
|
||||
@Ignore(
|
||||
"Conflicting field feedback: the baseline-3 low-range case has nearly " +
|
||||
"the same observed range and p50 as the active 14:18 case but " +
|
||||
@@ -100,7 +304,7 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun moderateRangeTailCompressesToEightEvAndRaisesSceneMedian() {
|
||||
fun sparseHighlightTailSelectsNineEvAndRaisesSceneMedian() {
|
||||
val width = 2800
|
||||
val height = 2102
|
||||
val grid = DngHdrProfileGainTableGenerator.gridSizeFor(width, height)
|
||||
@@ -138,11 +342,13 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
|
||||
val inputScale = map.mapInputWeights.sum()
|
||||
val sceneWhite = 1f / inputScale
|
||||
val nominalRawRight = 1f / 2.0f.pow(0.64f)
|
||||
val rawRight = sceneWhite * nominalRawRight
|
||||
val baselineGain = 2.0f.pow(0.64f)
|
||||
val rawRight = sceneWhite / baselineGain
|
||||
val sourceBlackPoint = max(
|
||||
0.045098f / 2.0f.pow(4f),
|
||||
sceneWhite / 2.0f.pow(8.75f)
|
||||
0.045098f / 2.0f.pow(
|
||||
DngPhotonProfileGainTableGenerator.SHADOW_FOOT_HEADROOM_EV
|
||||
),
|
||||
rawRight / 2.0f.pow(9.75f)
|
||||
)
|
||||
val blackOutput = sourceBlackPoint * medianGain(
|
||||
map,
|
||||
@@ -160,13 +366,16 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
val outputDynamicRangeEv = log2(finalWhiteOutput / finalBlackOutput)
|
||||
|
||||
assertTrue("gamma=${map.gamma}", map.gamma == 0.5f)
|
||||
assertTrue("sceneWhite=$sceneWhite", sceneWhite in 1f..1.3001f)
|
||||
assertTrue(
|
||||
"rawRight=$rawRight nominal=$nominalRawRight",
|
||||
rawRight / nominalRawRight in 1f..1.3001f
|
||||
"sceneWhite=$sceneWhite baselineGain=$baselineGain",
|
||||
sceneWhite / baselineGain in 1f..1.3001f
|
||||
)
|
||||
assertTrue("outputDynamicRangeEv=$outputDynamicRangeEv", outputDynamicRangeEv in 7.8f..8.2f)
|
||||
assertTrue("finalMiddleOutput=$finalMiddleOutput", finalMiddleOutput in 0.165f..0.185f)
|
||||
assertTrue(
|
||||
"rawRight=$rawRight",
|
||||
rawRight in 1f..1.3001f
|
||||
)
|
||||
assertTrue("outputDynamicRangeEv=$outputDynamicRangeEv", outputDynamicRangeEv in 8.8f..9.2f)
|
||||
assertTrue("finalMiddleOutput=$finalMiddleOutput", finalMiddleOutput in 0.285f..0.305f)
|
||||
assertTrue(
|
||||
"sceneMedian=$sceneMedian finalMiddleOutput=$finalMiddleOutput",
|
||||
finalMiddleOutput > sceneMedian * 1.45f
|
||||
@@ -233,9 +442,9 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun everyTierCompressesToItsDisplayRangeAndRaisesTheSceneMedian() {
|
||||
fun everyTierCompressesToItsDisplayRangeAndUsesHighlightAwareMiddle() {
|
||||
val cases = listOf(
|
||||
TierCase(0.10f, 0.15f, 0.40f, 0.60f, 0.75f, 8f, 0.18087f),
|
||||
TierCase(0.10f, 0.15f, 0.40f, 0.60f, 0.75f, 8f, 0.26f),
|
||||
TierCase(0.020f, 0.10f, 0.50f, 0.75f, 1.05f, 8.5f, 0.28f),
|
||||
TierCase(0.015f, 0.08f, 0.55f, 0.80f, 1.15f, 9f, 0.30f),
|
||||
TierCase(0.012f, 0.07f, 0.55f, 0.85f, 1.20f, 9.5f, 0.32f)
|
||||
@@ -245,7 +454,9 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
val inputScale = map.mapInputWeights.sum()
|
||||
val sceneWhite = 1f / inputScale
|
||||
val sourceBlack = max(
|
||||
tier.p10 / 2.0f.pow(4f),
|
||||
(tier.p10 * 0.25f) / 2.0f.pow(
|
||||
DngPhotonProfileGainTableGenerator.SHADOW_FOOT_HEADROOM_EV
|
||||
),
|
||||
sceneWhite / 2.0f.pow(tier.dynamicRangeEv + 0.75f)
|
||||
)
|
||||
val pgtmBlack = sourceBlack * medianGain(
|
||||
@@ -261,14 +472,28 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
val finalMiddle = samplePhotonProfileCurve(pgtmMiddle)
|
||||
val finalWhite = samplePhotonProfileCurve(pgtmWhite)
|
||||
val outputRangeEv = log2(finalWhite / finalBlack)
|
||||
val highlightRanges = DngPhotonProfileGainTableGenerator.photonHighlightRanges(
|
||||
p90 = tier.p90,
|
||||
p98 = tier.p98,
|
||||
p995 = tier.tailP99 * 0.92f,
|
||||
p999 = tier.tailP99,
|
||||
sceneMiddle = tier.p50
|
||||
)
|
||||
val expectedLift = DngPhotonProfileGainTableGenerator.displayExposureLiftEv(
|
||||
highlightRanges.exposureRangeEv
|
||||
)
|
||||
val expectedMiddle = (tier.p50 * 2.0f.pow(expectedLift)).coerceIn(
|
||||
4f / 2.0f.pow(tier.dynamicRangeEv),
|
||||
tier.displayMiddleGray
|
||||
)
|
||||
|
||||
assertTrue(
|
||||
"tier=${tier.dynamicRangeEv} outputRangeEv=$outputRangeEv",
|
||||
outputRangeEv in (tier.dynamicRangeEv - 0.20f)..(tier.dynamicRangeEv + 0.20f)
|
||||
)
|
||||
assertTrue(
|
||||
"tier=${tier.dynamicRangeEv} finalMiddle=$finalMiddle",
|
||||
finalMiddle in (tier.displayMiddleGray - 0.015f)..(tier.displayMiddleGray + 0.015f)
|
||||
"tier=${tier.dynamicRangeEv} finalMiddle=$finalMiddle expectedMiddle=$expectedMiddle",
|
||||
finalMiddle in (expectedMiddle - 0.015f)..(expectedMiddle + 0.015f)
|
||||
)
|
||||
assertTrue(
|
||||
"tier=${tier.dynamicRangeEv} p50=${tier.p50} finalMiddle=$finalMiddle",
|
||||
@@ -329,7 +554,9 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
p995 = tier.tailP99 * 0.92f,
|
||||
p999 = tier.tailP99,
|
||||
maxInput = tier.tailP99,
|
||||
highlightFraction = 0f
|
||||
highlightFraction = 0f,
|
||||
shadowEdge = tier.p10 * 0.5f,
|
||||
shadowFoot = tier.p10 * 0.25f
|
||||
),
|
||||
emitDiagnostics = false
|
||||
) ?: error("Expected Photon PGTM for tier ${tier.dynamicRangeEv}")
|
||||
@@ -344,6 +571,10 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
p999: Float,
|
||||
maxInput: Float,
|
||||
highlightFraction: Float = 0f,
|
||||
linearMean: Float = p50,
|
||||
logAverage: Float = p50,
|
||||
shadowEdge: Float = p10,
|
||||
shadowFoot: Float = p10,
|
||||
): DngPgtmGlobalStats {
|
||||
return DngPgtmGlobalStats(
|
||||
p10 = p10,
|
||||
@@ -354,8 +585,10 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
p999 = p999,
|
||||
maxInput = maxInput,
|
||||
highlightFraction = highlightFraction,
|
||||
linearMean = p50,
|
||||
logAverage = p50,
|
||||
linearMean = linearMean,
|
||||
logAverage = logAverage,
|
||||
shadowEdge = shadowEdge,
|
||||
shadowFoot = shadowFoot,
|
||||
sampleCount = 256
|
||||
)
|
||||
}
|
||||
@@ -446,25 +679,31 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
|
||||
private fun assertFieldCalibrationCases(cases: List<ExposureCalibrationCase>) {
|
||||
cases.forEach { case ->
|
||||
val calculatedObservedRangeEv = log2(
|
||||
val calculatedLegacyRangeEv = log2(
|
||||
max(case.p999, max(case.p995, case.p98)) /
|
||||
max(case.p10, 0.002f)
|
||||
)
|
||||
val logAverage = case.logAverage
|
||||
?: error("${case.name}: logAverage is required by the highlight-range model")
|
||||
val calculatedHighlightRangeEv = log2(
|
||||
max(case.p999, logAverage) / logAverage
|
||||
)
|
||||
val actualExposureLiftEv =
|
||||
DngPhotonProfileGainTableGenerator.displayExposureLiftEv(calculatedObservedRangeEv)
|
||||
DngPhotonProfileGainTableGenerator.displayExposureLiftEv(calculatedHighlightRangeEv)
|
||||
val expectedExposureLiftEv =
|
||||
case.reportedExposureLiftEv - case.reportedBrightnessErrorEv
|
||||
val residualEv = actualExposureLiftEv - expectedExposureLiftEv
|
||||
|
||||
assertTrue(
|
||||
"${case.name}: observed=$calculatedObservedRangeEv " +
|
||||
"${case.name}: legacyObserved=$calculatedLegacyRangeEv " +
|
||||
"reported=${case.reportedObservedRangeEv}",
|
||||
kotlin.math.abs(
|
||||
calculatedObservedRangeEv - case.reportedObservedRangeEv
|
||||
calculatedLegacyRangeEv - case.reportedObservedRangeEv
|
||||
) <= 0.002f
|
||||
)
|
||||
assertTrue(
|
||||
"${case.name}: actualLift=$actualExposureLiftEv " +
|
||||
"${case.name}: highlightRange=$calculatedHighlightRangeEv " +
|
||||
"actualLift=$actualExposureLiftEv " +
|
||||
"expectedLift=$expectedExposureLiftEv residual=$residualEv",
|
||||
residualEv in -0.12f..0.12f
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user