feat: implement depth-aware metering in RAW processing

This commit is contained in:
zhoubinjia committed 2026-05-08 13:34:05 +08:00
1 parent 0f2c2b9e7b
commit 9598e7ca9f
2 files changed
+67 -2

No files matched your search

@@ -20,15 +20,43 @@ object MeteringSystem {
byteBuffer: ByteBuffer,
width: Int,
height: Int,
weightBuffer: ByteBuffer? = null // Optional weight mask (e.g. depth map)
): Float {
val pixelCount = width * height
if (pixelCount == 0) return 0f
val lumas = FloatArray(pixelCount)
var weightedLumaSum = 0f
var totalWeight = 0f
byteBuffer.position(0)
weightBuffer?.position(0)
for (y in 0 until height) {
val ny = (y.toFloat() / height) - 0.5f
for (x in 0 until width) {
val nx = (x.toFloat() / width) - 0.5f
// 1. Spatial weight (Center-Weighted)
// Default fallback as subjects are often in the center
val distSq = nx * nx + ny * ny
val spatialWeight = lerp(1.0f, 0.5f, distSq * 2f)
// 2. Depth weight (Subject-Priority)
var depthWeight = 1.0f
if (weightBuffer != null && weightBuffer.hasRemaining()) {
// Read depth value (usually Gray8 or RGBA8).
// In DepthEstimator output (Grayscale ARGB), all R/G/B channels are the same.
val wValue = (weightBuffer.get().toInt() and 0xFF) / 255f
// Higher depth value (closer) gets higher weight (up to 5x)
depthWeight = lerp(1.0f, 10.0f, wValue)
// If it's 4-channel ARGB/RGBA, skip the other 3 bytes
if (weightBuffer.capacity() >= pixelCount * 4 && weightBuffer.remaining() >= 3) {
weightBuffer.get(); weightBuffer.get(); weightBuffer.get()
}
}
val finalWeight = spatialWeight * depthWeight
val r = (byteBuffer.get().toInt() and 0xFF) / 255f
val g = (byteBuffer.get().toInt() and 0xFF) / 255f
@@ -38,6 +66,9 @@ object MeteringSystem {
val luma = r * 0.2126f + g * 0.7152f + b * 0.0722f
val idx = y * width + x
lumas[idx] = luma
weightedLumaSum += luma * finalWeight
totalWeight += finalWeight
}
}
@@ -47,8 +78,8 @@ object MeteringSystem {
val highlightAnchorGain = 1f / p998.coerceAtLeast(0.01f)
// 3. Midtone Balance Logic (Spatial Average)
val avgLuma = lumas.average().toFloat()
// 3. Midtone Balance Logic (Spatial Weighted Average)
val avgLuma = weightedLumaSum / totalWeight.coerceAtLeast(0.001f)
val midToneGain = DISPLAY_TARGET_LUMA / avgLuma.coerceAtLeast(0.001f)
val dynamicRangeGap = midToneGain / highlightAnchorGain
@@ -7,6 +7,7 @@ import android.media.Image
import android.opengl.*
import com.hinnka.mycamera.data.ContentRepository
import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.ml.DepthEstimator
import com.hinnka.mycamera.utils.BitmapUtils
import com.hinnka.mycamera.utils.PLog
import com.hinnka.mycamera.utils.RawProcessor
@@ -198,6 +199,14 @@ class RawDemosaicProcessor {
private var gfWidth = 0
private var gfHeight = 0
private var gfChromaTexId = 0
private var depthEstimator: DepthEstimator? = null
private fun getDepthEstimator(context: Context): DepthEstimator {
return depthEstimator ?: synchronized(this) {
depthEstimator ?: DepthEstimator(context.applicationContext).also { depthEstimator = it }
}
}
private var gfChromaFboId = 0
// 缓冲区
@@ -586,6 +595,7 @@ class RawDemosaicProcessor {
)
val autoExposureEv = if (rawAutoExposure) {
resolveRawAutoExposureEv(
context = context,
metadata = actualMetadata,
sourceTextureId = demosaicTextureId,
dcpRenderPlan = resolvedDcpRenderPlan
@@ -2115,6 +2125,7 @@ class RawDemosaicProcessor {
}
private fun resolveRawAutoExposureEv(
context: Context,
metadata: RawMetadata,
sourceTextureId: Int,
dcpRenderPlan: DcpRenderPlan?
@@ -2145,11 +2156,34 @@ class RawDemosaicProcessor {
)
checkGlError("resolveRawAutoExposureEv")
buffer.position(0)
// Calculate depth weighting
val bitmap = Bitmap.createBitmap(meteringWidth, meteringHeight, Bitmap.Config.ARGB_8888)
bitmap.copyPixelsFromBuffer(buffer)
buffer.position(0) // Reset for MeteringSystem
val depthMap = getDepthEstimator(context).estimateDepth(bitmap)
val weightBuffer = depthMap?.let {
val wb = ByteBuffer.allocateDirect(it.byteCount)
it.copyPixelsToBuffer(wb)
wb.position(0)
wb
}
val ev = MeteringSystem.analyzeRenderedExposureEv(
byteBuffer = buffer,
width = meteringWidth,
height = meteringHeight,
weightBuffer = weightBuffer
)
// Clean up temporary bitmaps
if (depthMap != null && !depthMap.isRecycled) {
depthMap.recycle()
}
if (!bitmap.isRecycled) {
bitmap.recycle()
}
PLog.d(
TAG,
"RAW auto exposure: renderedEv=$ev"