feat: new metering system (2)
This commit is contained in:
1 parent
702b5def01
commit
d706ddc569
5 files changed
+552
-762
No files matched your search
@@ -35,6 +35,11 @@ import kotlin.math.hypot
|
||||
* 2. 应用 3D LUT 颜色变换
|
||||
* 3. 渲染到屏幕
|
||||
*/
|
||||
enum class PreviewCaptureSource {
|
||||
FinalDisplay,
|
||||
Original
|
||||
}
|
||||
|
||||
class LutRenderer : GLSurfaceView.Renderer {
|
||||
companion object {
|
||||
private const val TAG = "LutRenderer"
|
||||
@@ -463,6 +468,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
private var captureHeight = 512
|
||||
private var captureAspectRatio = 0f
|
||||
private var captureMaxLongEdge = DEFAULT_PREVIEW_CAPTURE_MAX_LONG_EDGE
|
||||
private var pendingPreviewCaptureSource = PreviewCaptureSource.FinalDisplay
|
||||
private var lastCaptureWidth = 0
|
||||
private var lastCaptureHeight = 0
|
||||
|
||||
@@ -1508,12 +1514,16 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
val needsHdfCompositeForSampling = postProcessEffectEnabled
|
||||
if (shouldCapturePreview) {
|
||||
shouldCapturePreview = false
|
||||
capturePreviewFrameInternal(
|
||||
sourceTextureId = finalDisplayTextureId,
|
||||
sourceWidth = finalDisplayWidth,
|
||||
sourceHeight = finalDisplayHeight,
|
||||
compositeWithHdf = needsHdfCompositeForSampling
|
||||
)
|
||||
when (consumePendingPreviewCaptureSource()) {
|
||||
PreviewCaptureSource.Original -> captureOriginalPreviewFrameInternal(rawPreviewSource)
|
||||
|
||||
PreviewCaptureSource.FinalDisplay -> capturePreviewFrameInternal(
|
||||
sourceTextureId = finalDisplayTextureId,
|
||||
sourceWidth = finalDisplayWidth,
|
||||
sourceHeight = finalDisplayHeight,
|
||||
compositeWithHdf = needsHdfCompositeForSampling
|
||||
)
|
||||
}
|
||||
}
|
||||
if (meteringEnabled && activeVideoRecorder == null) {
|
||||
runMeteringInternal(
|
||||
@@ -1592,10 +1602,11 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
targetMvpMatrix: FloatArray = mvpMatrix,
|
||||
preferBaselineLayer: Boolean = false,
|
||||
suppressBaselineLayer: Boolean = false,
|
||||
suppressCreativeLayer: Boolean = false,
|
||||
sourceOverride: PreviewSourceOverride? = null
|
||||
) {
|
||||
val baselineLayerAvailable = hasBaselineLayer() && !suppressBaselineLayer
|
||||
val creativeLayerAvailable = hasCreativeLayer()
|
||||
val creativeLayerAvailable = hasCreativeLayer() && !suppressCreativeLayer
|
||||
val useCreativeLayer = creativeLayerAvailable && (!preferBaselineLayer || !baselineLayerAvailable)
|
||||
val useBaselineLayer = !useCreativeLayer && baselineLayerAvailable
|
||||
val layerLutConfig = when {
|
||||
@@ -1669,7 +1680,11 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
// 捕获预览帧(如果需要)
|
||||
if (fboId == 0 && shouldCapturePreview) {
|
||||
shouldCapturePreview = false
|
||||
capturePreviewFrameInternal()
|
||||
when (consumePendingPreviewCaptureSource()) {
|
||||
PreviewCaptureSource.Original -> captureOriginalPreviewFrameInternal(rawPreviewSource = null)
|
||||
|
||||
PreviewCaptureSource.FinalDisplay -> capturePreviewFrameInternal()
|
||||
}
|
||||
}
|
||||
|
||||
// 测光和直方图(按需)
|
||||
@@ -2836,12 +2851,22 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
* 请求捕获预览帧
|
||||
* 会在下一帧渲染后捕获并通过回调返回
|
||||
*/
|
||||
fun capturePreviewFrame(maxLongEdge: Int = DEFAULT_PREVIEW_CAPTURE_MAX_LONG_EDGE) {
|
||||
fun capturePreviewFrame(
|
||||
maxLongEdge: Int = DEFAULT_PREVIEW_CAPTURE_MAX_LONG_EDGE,
|
||||
source: PreviewCaptureSource = PreviewCaptureSource.FinalDisplay
|
||||
) {
|
||||
captureMaxLongEdge = maxLongEdge.coerceAtLeast(1)
|
||||
pendingPreviewCaptureSource = source
|
||||
updateCaptureSize()
|
||||
shouldCapturePreview = true
|
||||
}
|
||||
|
||||
private fun consumePendingPreviewCaptureSource(): PreviewCaptureSource {
|
||||
val source = pendingPreviewCaptureSource
|
||||
pendingPreviewCaptureSource = PreviewCaptureSource.FinalDisplay
|
||||
return source
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部方法:捕获当前帧为小尺寸 Bitmap
|
||||
* 必须在 GL 线程中调用
|
||||
@@ -2850,7 +2875,8 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
sourceTextureId: Int? = null,
|
||||
sourceWidth: Int = viewportWidth,
|
||||
sourceHeight: Int = viewportHeight,
|
||||
compositeWithHdf: Boolean = false
|
||||
compositeWithHdf: Boolean = false,
|
||||
suppressColorLayers: Boolean = false
|
||||
) {
|
||||
try {
|
||||
if (captureWidth != lastCaptureWidth || captureHeight != lastCaptureHeight) {
|
||||
@@ -2881,7 +2907,9 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
fboId = captureFboId,
|
||||
width = captureWidth,
|
||||
height = captureHeight,
|
||||
targetMvpMatrix = buildMvpMatrix(captureWidth, captureHeight)
|
||||
targetMvpMatrix = buildMvpMatrix(captureWidth, captureHeight),
|
||||
suppressBaselineLayer = suppressColorLayers,
|
||||
suppressCreativeLayer = suppressColorLayers
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2927,6 +2955,20 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
private fun captureOriginalPreviewFrameInternal(rawPreviewSource: PreviewSourceOverride?) {
|
||||
if (rawPreviewSource != null) {
|
||||
capturePreviewFrameInternal(
|
||||
sourceTextureId = rawPreviewSource.textureId,
|
||||
sourceWidth = viewportWidth,
|
||||
sourceHeight = viewportHeight,
|
||||
compositeWithHdf = false
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
capturePreviewFrameInternal(suppressColorLayers = true)
|
||||
}
|
||||
|
||||
private var lastRunMeteringTime = 0L
|
||||
|
||||
private fun runMeteringInternal(
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.ln
|
||||
@@ -10,14 +9,10 @@ import kotlin.math.pow
|
||||
* 评价测光与场景分析系统
|
||||
*/
|
||||
object MeteringSystem {
|
||||
private const val TAG = "MeteringSystem"
|
||||
private const val DISPLAY_TARGET_LUMA = 0.18f
|
||||
private const val MAX_METERING_COMPENSATION_EV = 4f
|
||||
private const val LUMA_FLOOR = 0.001f
|
||||
private const val MAX_LINEAR_LUMA = 16.0f
|
||||
private const val RAW_CURVE_NEUTRAL_WHITE_POINT = 1.0f
|
||||
private const val AUTO_DEVELOP_EPSILON = 0.0001f
|
||||
private const val AUTO_CLIP_FRACTION = 0.0002f
|
||||
private const val SRGB_TRANSFER_THRESHOLD = 0.04045f
|
||||
private const val SRGB_LINEAR_SCALE = 12.92f
|
||||
private const val SRGB_TRANSFER_A = 0.055f
|
||||
@@ -25,9 +20,6 @@ object MeteringSystem {
|
||||
private const val CENTER_WEIGHT_SIGMA = 0.32f
|
||||
private const val MIN_METERING_SAMPLE_COUNT = 32
|
||||
|
||||
const val VIEWFINDER_METERING_HIGHLIGHT_EXCLUSION_LUMA = 0.95f
|
||||
|
||||
|
||||
data class MeteringResult(
|
||||
val meteredEv: Float,
|
||||
val dynamicRangeGap: Float,
|
||||
@@ -60,65 +52,14 @@ object MeteringSystem {
|
||||
}
|
||||
}
|
||||
|
||||
data class GpuRawAutoExposureBaseStats(
|
||||
val histogram: IntArray,
|
||||
val histogramLogMin: Float,
|
||||
val histogramLogMax: Float,
|
||||
val weightedLogLumaSum: Double,
|
||||
val weightSum: Double,
|
||||
val sampleCount: Int,
|
||||
val sanitizedSampleCount: Int,
|
||||
val groupCount: Int
|
||||
)
|
||||
|
||||
data class SrgbThumbnailMeteringStats(
|
||||
val weightedLogLumaSum: Double,
|
||||
val weightSum: Double,
|
||||
val sampleCount: Int,
|
||||
val sanitizedSampleCount: Int,
|
||||
val highlightRejectedSampleCount: Int,
|
||||
val midToneLinearLuma: Float
|
||||
)
|
||||
|
||||
class GpuRawAutoExposurePlan internal constructor(
|
||||
val compensatedExposureScale: Float,
|
||||
val clipLow: Float,
|
||||
val clipHigh: Float,
|
||||
val p05: Float,
|
||||
val p25: Float,
|
||||
val p75: Float,
|
||||
val p90: Float,
|
||||
val p99: Float,
|
||||
val compensatedClipLow: Float,
|
||||
val compensatedClipHigh: Float,
|
||||
val compensatedP05: Float,
|
||||
val compensatedP25: Float,
|
||||
val compensatedP75: Float,
|
||||
val compensatedP90: Float,
|
||||
val compensatedP99: Float,
|
||||
val baselineExposure: Float,
|
||||
val baselineExposureScale: Float,
|
||||
val autoExposureScale: Float,
|
||||
val targetLuma: Float,
|
||||
val meteringCompensationEv: Float,
|
||||
val midToneLuma: Float,
|
||||
val midToneGain: Float,
|
||||
val highlightAnchorGain: Float,
|
||||
val adaptiveGain: Float,
|
||||
val viewfinderTargetLuma: Float,
|
||||
val viewfinderSampleCount: Int,
|
||||
val viewfinderSanitizedSampleCount: Int,
|
||||
val dcpBaselineExposureOffset: Float,
|
||||
val renderingEngineMeteringCompensationEv: Float,
|
||||
val totalBaseExposureEv: Float,
|
||||
val rawMeteredEv: Float,
|
||||
val dynamicRangeGap: Float,
|
||||
val sanitizedSampleCount: Int,
|
||||
val histogramSampleCount: Int,
|
||||
val histogramGroupCount: Int,
|
||||
val tag: String
|
||||
)
|
||||
|
||||
fun hasManualRawDevelopAdjustments(
|
||||
rawExposureCompensation: Float,
|
||||
rawHighlightsAdjustment: Float,
|
||||
@@ -142,7 +83,6 @@ object MeteringSystem {
|
||||
var weightSum = 0.0
|
||||
var sampleCount = 0
|
||||
var sanitizedSampleCount = 0
|
||||
var highlightRejectedSampleCount = 0
|
||||
|
||||
for (y in 0 until height) {
|
||||
val yFraction = (y + 0.5f) / height.toFloat()
|
||||
@@ -160,10 +100,6 @@ object MeteringSystem {
|
||||
if (luma != rawLuma) {
|
||||
sanitizedSampleCount++
|
||||
}
|
||||
if (isHighlightExcludedFromMetering(luma)) {
|
||||
highlightRejectedSampleCount++
|
||||
continue
|
||||
}
|
||||
|
||||
val xFraction = (x + 0.5f) / width.toFloat()
|
||||
val weight = centerWeight(xFraction, yFraction)
|
||||
@@ -186,157 +122,10 @@ object MeteringSystem {
|
||||
weightSum = weightSum,
|
||||
sampleCount = sampleCount,
|
||||
sanitizedSampleCount = sanitizedSampleCount,
|
||||
highlightRejectedSampleCount = highlightRejectedSampleCount,
|
||||
midToneLinearLuma = midToneLinearLuma
|
||||
)
|
||||
}
|
||||
|
||||
fun prepareGpuLinearRawAutoExposure(
|
||||
stats: GpuRawAutoExposureBaseStats,
|
||||
baselineExposure: Float,
|
||||
viewfinderThumbnailStats: SrgbThumbnailMeteringStats,
|
||||
dcpBaselineExposureOffset: Float,
|
||||
renderingEngineMeteringCompensationEv: Float,
|
||||
meteringCompensationEv: Float = RAW_RENDERING_ENGINE_METERING_COMPENSATION_EV,
|
||||
tag: String = "Linear RAW AE GPU"
|
||||
): GpuRawAutoExposurePlan? {
|
||||
if (stats.histogram.isEmpty() || stats.sampleCount <= 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
val safeMeteringCompensationEv = sanitizeMeteringCompensationEv(meteringCompensationEv)
|
||||
val targetLuma = viewfinderThumbnailStats.midToneLinearLuma
|
||||
val midToneLuma = if (stats.weightSum > 0.0) {
|
||||
exp2((stats.weightedLogLumaSum / stats.weightSum).toFloat())
|
||||
} else {
|
||||
targetLuma
|
||||
}.let { sanitizeAverageLuma(it, targetLuma) }
|
||||
|
||||
val clipLow = percentileFromLogHistogram(
|
||||
histogram = stats.histogram,
|
||||
percentile = AUTO_CLIP_FRACTION,
|
||||
logMin = stats.histogramLogMin,
|
||||
logMax = stats.histogramLogMax
|
||||
)
|
||||
val p05 = percentileFromLogHistogram(stats.histogram, 0.05f, stats.histogramLogMin, stats.histogramLogMax)
|
||||
val p25 = percentileFromLogHistogram(stats.histogram, 0.25f, stats.histogramLogMin, stats.histogramLogMax)
|
||||
val p75 = percentileFromLogHistogram(stats.histogram, 0.75f, stats.histogramLogMin, stats.histogramLogMax)
|
||||
val p90 = percentileFromLogHistogram(stats.histogram, 0.90f, stats.histogramLogMin, stats.histogramLogMax)
|
||||
val p99 = percentileFromLogHistogram(stats.histogram, 0.99f, stats.histogramLogMin, stats.histogramLogMax)
|
||||
val clipHigh = percentileFromLogHistogram(
|
||||
histogram = stats.histogram,
|
||||
percentile = 1f - AUTO_CLIP_FRACTION,
|
||||
logMin = stats.histogramLogMin,
|
||||
logMax = stats.histogramLogMax
|
||||
)
|
||||
|
||||
val safeDcpBaselineExposureOffset = sanitizeExposureEv(dcpBaselineExposureOffset)
|
||||
val safeRenderingEngineMeteringCompensationEv =
|
||||
sanitizeExposureEv(renderingEngineMeteringCompensationEv)
|
||||
val totalBaseExposureEv = resolveViewfinderMeteringBaseExposureEv(
|
||||
baselineExposure = baselineExposure,
|
||||
dcpBaselineExposureOffset = safeDcpBaselineExposureOffset,
|
||||
renderingEngineMeteringCompensationEv = safeRenderingEngineMeteringCompensationEv
|
||||
)
|
||||
val baselineExposureScale = exposureScaleFromEv(totalBaseExposureEv)
|
||||
val meteringClipLow = (clipLow * baselineExposureScale).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
val meteringP05 = (p05 * baselineExposureScale).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
val meteringP25 = (p25 * baselineExposureScale).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
val meteringP75 = (p75 * baselineExposureScale).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
val meteringP90 = (p90 * baselineExposureScale).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
val meteringP99 = (p99 * baselineExposureScale).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
val meteringClipHigh = (clipHigh * baselineExposureScale).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
val meteringMidToneLuma = (midToneLuma * baselineExposureScale).coerceIn(LUMA_FLOOR, MAX_LINEAR_LUMA)
|
||||
|
||||
val highlightAnchorGain = maxOf(1f, meteringClipHigh) / meteringClipHigh.coerceAtLeast(0.01f)
|
||||
val midToneGain = targetLuma / meteringMidToneLuma.coerceAtLeast(LUMA_FLOOR)
|
||||
val dynamicRangeGap = evDifference(meteringClipHigh, meteringClipLow)
|
||||
val adaptiveGain = midToneGain
|
||||
val autoExposureScale = adaptiveGain.coerceIn(0.25f, 4.0f)
|
||||
val rawMeteredEv = log2(autoExposureScale)
|
||||
val compensatedExposureScale = baselineExposureScale * autoExposureScale
|
||||
|
||||
return GpuRawAutoExposurePlan(
|
||||
compensatedExposureScale = compensatedExposureScale,
|
||||
clipLow = meteringClipLow,
|
||||
clipHigh = meteringClipHigh,
|
||||
p05 = meteringP05,
|
||||
p25 = meteringP25,
|
||||
p75 = meteringP75,
|
||||
p90 = meteringP90,
|
||||
p99 = meteringP99,
|
||||
compensatedClipLow = (meteringClipLow * autoExposureScale).coerceIn(0f, MAX_LINEAR_LUMA),
|
||||
compensatedClipHigh = (meteringClipHigh * autoExposureScale).coerceIn(0f, MAX_LINEAR_LUMA),
|
||||
compensatedP05 = (meteringP05 * autoExposureScale).coerceIn(0f, MAX_LINEAR_LUMA),
|
||||
compensatedP25 = (meteringP25 * autoExposureScale).coerceIn(0f, MAX_LINEAR_LUMA),
|
||||
compensatedP75 = (meteringP75 * autoExposureScale).coerceIn(0f, MAX_LINEAR_LUMA),
|
||||
compensatedP90 = (meteringP90 * autoExposureScale).coerceIn(0f, MAX_LINEAR_LUMA),
|
||||
compensatedP99 = (meteringP99 * autoExposureScale).coerceIn(0f, MAX_LINEAR_LUMA),
|
||||
baselineExposure = baselineExposure,
|
||||
baselineExposureScale = baselineExposureScale,
|
||||
autoExposureScale = autoExposureScale,
|
||||
targetLuma = targetLuma,
|
||||
meteringCompensationEv = safeMeteringCompensationEv,
|
||||
midToneLuma = meteringMidToneLuma,
|
||||
midToneGain = midToneGain,
|
||||
highlightAnchorGain = highlightAnchorGain,
|
||||
adaptiveGain = adaptiveGain,
|
||||
viewfinderTargetLuma = targetLuma,
|
||||
viewfinderSampleCount = viewfinderThumbnailStats.sampleCount,
|
||||
viewfinderSanitizedSampleCount = viewfinderThumbnailStats.sanitizedSampleCount,
|
||||
dcpBaselineExposureOffset = safeDcpBaselineExposureOffset,
|
||||
renderingEngineMeteringCompensationEv = safeRenderingEngineMeteringCompensationEv,
|
||||
totalBaseExposureEv = totalBaseExposureEv,
|
||||
rawMeteredEv = rawMeteredEv,
|
||||
dynamicRangeGap = dynamicRangeGap,
|
||||
sanitizedSampleCount = stats.sanitizedSampleCount,
|
||||
histogramSampleCount = stats.sampleCount,
|
||||
histogramGroupCount = stats.groupCount,
|
||||
tag = tag
|
||||
)
|
||||
}
|
||||
|
||||
fun finishGpuLinearRawAutoExposure(plan: GpuRawAutoExposurePlan): MeteringResult {
|
||||
val curveWhitePoint = if (plan.compensatedClipHigh > 1f) {
|
||||
plan.compensatedClipHigh.coerceIn(1.05f, 4.0f)
|
||||
} else {
|
||||
RAW_CURVE_NEUTRAL_WHITE_POINT
|
||||
}
|
||||
|
||||
PLog.d(
|
||||
TAG,
|
||||
"${plan.tag}: clipLow=${plan.clipLow} clipHigh=${plan.clipHigh} " +
|
||||
"p05=${plan.p05} p25=${plan.p25} p75=${plan.p75} p90=${plan.p90} p99=${plan.p99} " +
|
||||
"baselineScale=${plan.baselineExposureScale} autoScale=${plan.autoExposureScale} " +
|
||||
"finalScale=${plan.compensatedExposureScale} " +
|
||||
"compClipLow=${plan.compensatedClipLow} compClipHigh=${plan.compensatedClipHigh} " +
|
||||
"compP05=${plan.compensatedP05} compP25=${plan.compensatedP25} " +
|
||||
"compP75=${plan.compensatedP75} compP90=${plan.compensatedP90} compP99=${plan.compensatedP99} " +
|
||||
"mode=ViewfinderThumbnail baselineExposure=${plan.baselineExposure} " +
|
||||
"dcpBaselineExposureOffset=${plan.dcpBaselineExposureOffset} " +
|
||||
"engineMeteringEv=${plan.renderingEngineMeteringCompensationEv} " +
|
||||
"totalBaseExposureEv=${plan.totalBaseExposureEv} " +
|
||||
"target=${plan.targetLuma} meteringCompensationEv=${plan.meteringCompensationEv} " +
|
||||
"viewfinderTarget=${plan.viewfinderTargetLuma} " +
|
||||
"viewfinderSamples=${plan.viewfinderSampleCount} " +
|
||||
"viewfinderSanitizedSamples=${plan.viewfinderSanitizedSampleCount} " +
|
||||
"midToneLuma=${plan.midToneLuma} " +
|
||||
"midToneGain=${plan.midToneGain} highlightAnchorGain=${plan.highlightAnchorGain} " +
|
||||
"gain=${plan.adaptiveGain} ev=${plan.rawMeteredEv} gap=${plan.dynamicRangeGap} " +
|
||||
"sanitizedSamples=${plan.sanitizedSampleCount} histogramSamples=${plan.histogramSampleCount} " +
|
||||
"histogramGroups=${plan.histogramGroupCount}"
|
||||
)
|
||||
|
||||
return MeteringResult(
|
||||
meteredEv = plan.rawMeteredEv.coerceIn(-2f, 2f),
|
||||
dynamicRangeGap = plan.dynamicRangeGap,
|
||||
avgLuma = plan.midToneLuma,
|
||||
clipLow = plan.clipLow,
|
||||
clipHigh = plan.clipHigh,
|
||||
curveWhitePoint = curveWhitePoint
|
||||
)
|
||||
}
|
||||
|
||||
private fun sanitizeLinearLuma(value: Float): Float {
|
||||
return when {
|
||||
value.isFinite() -> value.coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
@@ -353,36 +142,6 @@ object MeteringSystem {
|
||||
}
|
||||
}
|
||||
|
||||
fun resolveViewfinderMeteringBaseExposureEv(
|
||||
baselineExposure: Float,
|
||||
dcpBaselineExposureOffset: Float,
|
||||
renderingEngineMeteringCompensationEv: Float
|
||||
): Float {
|
||||
return sanitizeExposureEv(
|
||||
sanitizeExposureEv(baselineExposure) +
|
||||
sanitizeExposureEv(dcpBaselineExposureOffset) +
|
||||
sanitizeExposureEv(renderingEngineMeteringCompensationEv)
|
||||
)
|
||||
}
|
||||
|
||||
fun resolveViewfinderMeteringBaseExposureScale(
|
||||
baselineExposure: Float,
|
||||
dcpBaselineExposureOffset: Float,
|
||||
renderingEngineMeteringCompensationEv: Float
|
||||
): Float {
|
||||
return exposureScaleFromEv(
|
||||
resolveViewfinderMeteringBaseExposureEv(
|
||||
baselineExposure = baselineExposure,
|
||||
dcpBaselineExposureOffset = dcpBaselineExposureOffset,
|
||||
renderingEngineMeteringCompensationEv = renderingEngineMeteringCompensationEv
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun isHighlightExcludedFromMetering(luma: Float): Boolean {
|
||||
return luma.isFinite() && luma >= VIEWFINDER_METERING_HIGHLIGHT_EXCLUSION_LUMA
|
||||
}
|
||||
|
||||
private fun centerWeight(xFraction: Float, yFraction: Float): Float {
|
||||
val dx = (xFraction - 0.5f) / CENTER_WEIGHT_SIGMA
|
||||
val dy = (yFraction - 0.5f) / CENTER_WEIGHT_SIGMA
|
||||
@@ -399,48 +158,6 @@ object MeteringSystem {
|
||||
}
|
||||
}
|
||||
|
||||
private fun percentileFromLogHistogram(
|
||||
histogram: IntArray,
|
||||
percentile: Float,
|
||||
logMin: Float,
|
||||
logMax: Float
|
||||
): Float {
|
||||
if (histogram.isEmpty()) {
|
||||
return 0f
|
||||
}
|
||||
|
||||
var total = 0L
|
||||
for (count in histogram) {
|
||||
if (count > 0) {
|
||||
total += count.toLong()
|
||||
}
|
||||
}
|
||||
if (total <= 0L) {
|
||||
return 0f
|
||||
}
|
||||
|
||||
val targetIndex = ((total - 1L) * percentile.coerceIn(0f, 1f)).toLong()
|
||||
var cumulative = 0L
|
||||
for (i in histogram.indices) {
|
||||
val count = histogram[i]
|
||||
if (count <= 0) continue
|
||||
cumulative += count.toLong()
|
||||
if (cumulative > targetIndex) {
|
||||
val fraction = (i + 0.5f) / histogram.size.toFloat()
|
||||
val logLuma = logMin + (logMax - logMin) * fraction
|
||||
return exp2(logLuma).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
}
|
||||
}
|
||||
|
||||
return exp2(logMax).coerceIn(0f, MAX_LINEAR_LUMA)
|
||||
}
|
||||
|
||||
private fun evDifference(brighter: Float, darker: Float): Float {
|
||||
val safeBrighter = sanitizeLinearLuma(brighter).coerceAtLeast(LUMA_FLOOR)
|
||||
val safeDarker = sanitizeLinearLuma(darker).coerceAtLeast(1e-5f)
|
||||
return log2(safeBrighter / safeDarker)
|
||||
}
|
||||
|
||||
private fun log2(value: Float): Float {
|
||||
return (ln(value.toDouble()) / ln(2.0)).toFloat()
|
||||
}
|
||||
@@ -449,28 +166,4 @@ object MeteringSystem {
|
||||
return exp(value * ln(2.0)).toFloat()
|
||||
}
|
||||
|
||||
private fun exposureScaleFromEv(ev: Float): Float {
|
||||
return if (ev.isFinite()) {
|
||||
exp2(ev.coerceIn(-MAX_METERING_COMPENSATION_EV, MAX_METERING_COMPENSATION_EV))
|
||||
} else {
|
||||
1f
|
||||
}
|
||||
}
|
||||
|
||||
private fun sanitizeExposureEv(ev: Float): Float {
|
||||
return if (ev.isFinite()) {
|
||||
ev.coerceIn(-MAX_METERING_COMPENSATION_EV, MAX_METERING_COMPENSATION_EV)
|
||||
} else {
|
||||
0f
|
||||
}
|
||||
}
|
||||
|
||||
private fun sanitizeMeteringCompensationEv(meteringCompensationEv: Float): Float {
|
||||
return if (meteringCompensationEv.isFinite()) {
|
||||
meteringCompensationEv.coerceIn(-MAX_METERING_COMPENSATION_EV, MAX_METERING_COMPENSATION_EV)
|
||||
} else {
|
||||
RAW_RENDERING_ENGINE_METERING_COMPENSATION_EV
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -11,6 +11,7 @@ import com.hinnka.mycamera.livephoto.LivePhotoRecorder
|
||||
import com.hinnka.mycamera.camera.MeteringMode
|
||||
import com.hinnka.mycamera.lut.LutConfig
|
||||
import com.hinnka.mycamera.lut.LutRenderer
|
||||
import com.hinnka.mycamera.lut.PreviewCaptureSource
|
||||
import com.hinnka.mycamera.model.ColorRecipeParams
|
||||
import com.hinnka.mycamera.model.ColorPaletteMapper
|
||||
import com.hinnka.mycamera.raw.RawRenderingEngine
|
||||
@@ -301,21 +302,49 @@ class CameraGLSurfaceView @JvmOverloads constructor(
|
||||
* @param callback 捕获完成后的回调,在主线程调用
|
||||
*/
|
||||
fun capturePreviewFrame(callback: (Bitmap) -> Unit) {
|
||||
capturePreviewFrameInternal(maxLongEdge = null, requestRenderImmediately = true, callback = callback)
|
||||
capturePreviewFrameInternal(
|
||||
maxLongEdge = null,
|
||||
source = PreviewCaptureSource.FinalDisplay,
|
||||
requestRenderImmediately = true
|
||||
) { bitmap ->
|
||||
bitmap?.let(callback)
|
||||
}
|
||||
}
|
||||
|
||||
fun capturePreviewFrame(maxLongEdge: Int, callback: (Bitmap) -> Unit) {
|
||||
capturePreviewFrameInternal(maxLongEdge = maxLongEdge, requestRenderImmediately = true, callback = callback)
|
||||
capturePreviewFrameInternal(
|
||||
maxLongEdge = maxLongEdge,
|
||||
source = PreviewCaptureSource.FinalDisplay,
|
||||
requestRenderImmediately = true
|
||||
) { bitmap ->
|
||||
bitmap?.let(callback)
|
||||
}
|
||||
}
|
||||
|
||||
fun captureNextPreviewFrame(maxLongEdge: Int, callback: (Bitmap) -> Unit) {
|
||||
capturePreviewFrameInternal(maxLongEdge = maxLongEdge, requestRenderImmediately = false, callback = callback)
|
||||
capturePreviewFrameInternal(
|
||||
maxLongEdge = maxLongEdge,
|
||||
source = PreviewCaptureSource.FinalDisplay,
|
||||
requestRenderImmediately = false
|
||||
) { bitmap ->
|
||||
bitmap?.let(callback)
|
||||
}
|
||||
}
|
||||
|
||||
fun captureOriginalPreviewFrame(callback: (Bitmap?) -> Unit) {
|
||||
capturePreviewFrameInternal(
|
||||
maxLongEdge = null,
|
||||
source = PreviewCaptureSource.Original,
|
||||
requestRenderImmediately = true,
|
||||
callback = callback
|
||||
)
|
||||
}
|
||||
|
||||
private fun capturePreviewFrameInternal(
|
||||
maxLongEdge: Int?,
|
||||
source: PreviewCaptureSource,
|
||||
requestRenderImmediately: Boolean,
|
||||
callback: (Bitmap) -> Unit
|
||||
callback: (Bitmap?) -> Unit
|
||||
) {
|
||||
renderer.onPreviewFrameCaptured = { bitmap ->
|
||||
// 在主线程回调
|
||||
@@ -325,9 +354,9 @@ class CameraGLSurfaceView @JvmOverloads constructor(
|
||||
}
|
||||
queueEvent {
|
||||
if (maxLongEdge != null) {
|
||||
renderer.capturePreviewFrame(maxLongEdge)
|
||||
renderer.capturePreviewFrame(maxLongEdge, source)
|
||||
} else {
|
||||
renderer.capturePreviewFrame()
|
||||
renderer.capturePreviewFrame(source = source)
|
||||
}
|
||||
if (requestRenderImmediately) {
|
||||
requestRender()
|
||||
|
||||
@@ -3272,11 +3272,15 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
val glView = glSurfaceView
|
||||
if (glView != null) {
|
||||
val thumbnailRotation = capturePreviewThumbnailRotation()
|
||||
glView.capturePreviewFrame { bitmap ->
|
||||
previewThumbnail = rotatePreviewBitmapForCapture(bitmap, thumbnailRotation)
|
||||
glView.captureOriginalPreviewFrame { bitmap ->
|
||||
val thumbnail = bitmap?.let {
|
||||
rotatePreviewBitmapForCapture(it, thumbnailRotation)
|
||||
}
|
||||
previewThumbnail = thumbnail
|
||||
isGeneratingPreviews = false
|
||||
}
|
||||
} else {
|
||||
previewThumbnail = null
|
||||
isGeneratingPreviews = false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user