From 9598e7ca9ffbb3c26d675da16e198bb5799ee5f1 Mon Sep 17 00:00:00 2001 From: zhoubinjia Date: Fri, 8 May 2026 13:34:05 +0800 Subject: [PATCH] feat: implement depth-aware metering in RAW processing --- .../com/hinnka/mycamera/raw/MeteringSystem.kt | 35 +++++++++++++++++-- .../mycamera/raw/RawDemosaicProcessor.kt | 34 ++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/hinnka/mycamera/raw/MeteringSystem.kt b/app/src/main/java/com/hinnka/mycamera/raw/MeteringSystem.kt index f5655705..47c3ef48 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/MeteringSystem.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/MeteringSystem.kt @@ -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 diff --git a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt index bd27d927..f6529482 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt @@ -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"