fix: raw viewfinder exposure matching
This commit is contained in:
1 parent
05891a043e
commit
a23454ebb5
6 files changed
+167
-212
No files matched your search
@@ -18,6 +18,7 @@ import com.hinnka.mycamera.video.VideoLogProfile
|
||||
import com.hinnka.mycamera.video.VideoRecorder
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
import java.util.ArrayDeque
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
@@ -66,6 +67,13 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
val treatSourceAsHlgInput: Boolean
|
||||
)
|
||||
|
||||
private data class PreviewCaptureRequest(
|
||||
val width: Int,
|
||||
val height: Int,
|
||||
val source: PreviewCaptureSource,
|
||||
val onCaptured: (Bitmap) -> Unit
|
||||
)
|
||||
|
||||
// 纹理 ID
|
||||
private var cameraTextureId: Int = 0
|
||||
private var lutTextureId: Int = 0
|
||||
@@ -444,7 +452,6 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
// 回调
|
||||
var onSurfaceTextureAvailable: ((SurfaceTexture) -> Unit)? = null
|
||||
var onRequestRender: (() -> Unit)? = null
|
||||
var onPreviewFrameCaptured: ((Bitmap) -> Unit)? = null
|
||||
var onHistogramUpdated: ((IntArray) -> Unit)? = null
|
||||
var onMeteringUpdated: ((Double, Double) -> Unit)? = null
|
||||
var onHighlightPointUpdated: ((Float, Float) -> Unit)? = null
|
||||
@@ -458,13 +465,12 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
private var videoRenderStatsWindowStartMs: Long = 0L
|
||||
private var videoRenderStatsFrames: Int = 0
|
||||
|
||||
// 预览帧捕获标志
|
||||
private var shouldCapturePreview = false
|
||||
// 预览帧捕获请求仅在 GL 线程中写入和消费。
|
||||
private val pendingPreviewCaptureRequests = ArrayDeque<PreviewCaptureRequest>()
|
||||
private var captureWidth = 512
|
||||
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
|
||||
|
||||
@@ -1510,32 +1516,20 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
val finalDisplayWidth = viewportWidth
|
||||
val finalDisplayHeight = viewportHeight
|
||||
val needsHdfCompositeForSampling = postProcessEffectEnabled
|
||||
if (shouldCapturePreview) {
|
||||
shouldCapturePreview = false
|
||||
when (consumePendingPreviewCaptureSource()) {
|
||||
PreviewCaptureSource.Original -> captureOriginalPreviewFrameInternal(rawPreviewSource)
|
||||
|
||||
PreviewCaptureSource.FinalDisplay -> capturePreviewFrameInternal(
|
||||
sourceTextureId = finalDisplayTextureId,
|
||||
sourceWidth = finalDisplayWidth,
|
||||
sourceHeight = finalDisplayHeight,
|
||||
compositeWithHdf = needsHdfCompositeForSampling
|
||||
)
|
||||
}
|
||||
}
|
||||
capturePendingPreviewFrames(
|
||||
rawPreviewSource = rawPreviewSource,
|
||||
finalDisplayTextureId = finalDisplayTextureId,
|
||||
finalDisplayWidth = finalDisplayWidth,
|
||||
finalDisplayHeight = finalDisplayHeight,
|
||||
compositeFinalDisplay = needsHdfCompositeForSampling
|
||||
)
|
||||
if (meteringEnabled && activeVideoRecorder == null) {
|
||||
// Metering must observe the same LUT-free source used by Original preview
|
||||
// capture. Feeding finalDisplayTextureId here makes the current user-selected
|
||||
// 3D LUT part of the exposure loop and therefore biases the captured RAW.
|
||||
if (rawPreviewSource != null) {
|
||||
runMeteringInternal(
|
||||
sourceTextureId = rawPreviewSource.textureId,
|
||||
sourceWidth = viewportWidth,
|
||||
sourceHeight = viewportHeight,
|
||||
)
|
||||
} else {
|
||||
runMeteringInternal()
|
||||
}
|
||||
runMeteringInternal(
|
||||
sourceTextureId = finalDisplayTextureId,
|
||||
sourceWidth = finalDisplayWidth,
|
||||
sourceHeight = finalDisplayHeight,
|
||||
compositeWithHdf = needsHdfCompositeForSampling
|
||||
)
|
||||
}
|
||||
} else {
|
||||
// 直接渲染到屏幕
|
||||
@@ -1545,6 +1539,13 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
height = viewportHeight,
|
||||
suppressBaselineLayer = suppressBaselineLayerForVideoLog
|
||||
)
|
||||
capturePendingPreviewFrames(
|
||||
rawPreviewSource = null,
|
||||
finalDisplayTextureId = null,
|
||||
finalDisplayWidth = viewportWidth,
|
||||
finalDisplayHeight = viewportHeight,
|
||||
compositeFinalDisplay = false
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1681,16 +1682,6 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
treatSourceAsHlgInput = treatSourceAsHlgInput
|
||||
)
|
||||
|
||||
// 捕获预览帧(如果需要)
|
||||
if (fboId == 0 && shouldCapturePreview) {
|
||||
shouldCapturePreview = false
|
||||
when (consumePendingPreviewCaptureSource()) {
|
||||
PreviewCaptureSource.Original -> captureOriginalPreviewFrameInternal(rawPreviewSource = null)
|
||||
|
||||
PreviewCaptureSource.FinalDisplay -> capturePreviewFrameInternal()
|
||||
}
|
||||
}
|
||||
|
||||
// 测光和直方图(按需)
|
||||
if (meteringEnabled && videoRecorder?.isRecording() != true) {
|
||||
runMeteringInternal()
|
||||
@@ -2422,7 +2413,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
|
||||
}
|
||||
|
||||
private fun initCaptureFbo() {
|
||||
private fun initCaptureFbo(width: Int, height: Int) {
|
||||
if (captureFboId != 0) {
|
||||
GLES30.glDeleteFramebuffers(1, intArrayOf(captureFboId), 0)
|
||||
captureFboId = 0
|
||||
@@ -2443,7 +2434,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, captureTextureId)
|
||||
GLES30.glTexImage2D(
|
||||
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA,
|
||||
captureWidth, captureHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null
|
||||
width, height, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null
|
||||
)
|
||||
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR)
|
||||
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR)
|
||||
@@ -2857,18 +2848,45 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
*/
|
||||
fun capturePreviewFrame(
|
||||
maxLongEdge: Int = DEFAULT_PREVIEW_CAPTURE_MAX_LONG_EDGE,
|
||||
source: PreviewCaptureSource = PreviewCaptureSource.FinalDisplay
|
||||
source: PreviewCaptureSource = PreviewCaptureSource.FinalDisplay,
|
||||
onCaptured: (Bitmap) -> Unit
|
||||
) {
|
||||
captureMaxLongEdge = maxLongEdge.coerceAtLeast(1)
|
||||
pendingPreviewCaptureSource = source
|
||||
updateCaptureSize()
|
||||
shouldCapturePreview = true
|
||||
pendingPreviewCaptureRequests.addLast(
|
||||
PreviewCaptureRequest(
|
||||
width = captureWidth,
|
||||
height = captureHeight,
|
||||
source = source,
|
||||
onCaptured = onCaptured
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun consumePendingPreviewCaptureSource(): PreviewCaptureSource {
|
||||
val source = pendingPreviewCaptureSource
|
||||
pendingPreviewCaptureSource = PreviewCaptureSource.FinalDisplay
|
||||
return source
|
||||
private fun capturePendingPreviewFrames(
|
||||
rawPreviewSource: PreviewSourceOverride?,
|
||||
finalDisplayTextureId: Int?,
|
||||
finalDisplayWidth: Int,
|
||||
finalDisplayHeight: Int,
|
||||
compositeFinalDisplay: Boolean
|
||||
) {
|
||||
while (pendingPreviewCaptureRequests.isNotEmpty()) {
|
||||
val request = pendingPreviewCaptureRequests.removeFirst()
|
||||
when (request.source) {
|
||||
PreviewCaptureSource.Original -> captureOriginalPreviewFrameInternal(
|
||||
request = request,
|
||||
rawPreviewSource = rawPreviewSource
|
||||
)
|
||||
|
||||
PreviewCaptureSource.FinalDisplay -> capturePreviewFrameInternal(
|
||||
request = request,
|
||||
sourceTextureId = finalDisplayTextureId,
|
||||
sourceWidth = finalDisplayWidth,
|
||||
sourceHeight = finalDisplayHeight,
|
||||
compositeWithHdf = compositeFinalDisplay
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2876,6 +2894,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
* 必须在 GL 线程中调用
|
||||
*/
|
||||
private fun capturePreviewFrameInternal(
|
||||
request: PreviewCaptureRequest,
|
||||
sourceTextureId: Int? = null,
|
||||
sourceWidth: Int = viewportWidth,
|
||||
sourceHeight: Int = viewportHeight,
|
||||
@@ -2883,42 +2902,44 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
suppressColorLayers: Boolean = false
|
||||
) {
|
||||
try {
|
||||
if (captureWidth != lastCaptureWidth || captureHeight != lastCaptureHeight) {
|
||||
initCaptureFbo()
|
||||
lastCaptureWidth = captureWidth
|
||||
lastCaptureHeight = captureHeight
|
||||
val targetWidth = request.width
|
||||
val targetHeight = request.height
|
||||
if (targetWidth != lastCaptureWidth || targetHeight != lastCaptureHeight) {
|
||||
initCaptureFbo(targetWidth, targetHeight)
|
||||
lastCaptureWidth = targetWidth
|
||||
lastCaptureHeight = targetHeight
|
||||
}
|
||||
|
||||
if (sourceTextureId != null && sourceTextureId != 0) {
|
||||
if (compositeWithHdf) {
|
||||
drawPostProcessEffects(captureFboId, captureWidth, captureHeight, sourceTextureId)
|
||||
drawPostProcessEffects(captureFboId, targetWidth, targetHeight, sourceTextureId)
|
||||
} else {
|
||||
drawFboToScreen(
|
||||
fboId = captureFboId,
|
||||
width = captureWidth,
|
||||
height = captureHeight,
|
||||
width = targetWidth,
|
||||
height = targetHeight,
|
||||
sourceTextureId = sourceTextureId,
|
||||
targetMvpMatrix = buildTextureMvpMatrix(
|
||||
sourceWidth = sourceWidth,
|
||||
sourceHeight = sourceHeight,
|
||||
targetWidth = captureWidth,
|
||||
targetHeight = captureHeight
|
||||
targetWidth = targetWidth,
|
||||
targetHeight = targetHeight
|
||||
)
|
||||
)
|
||||
}
|
||||
} else {
|
||||
drawInternal(
|
||||
fboId = captureFboId,
|
||||
width = captureWidth,
|
||||
height = captureHeight,
|
||||
targetMvpMatrix = buildMvpMatrix(captureWidth, captureHeight),
|
||||
width = targetWidth,
|
||||
height = targetHeight,
|
||||
targetMvpMatrix = buildMvpMatrix(targetWidth, targetHeight),
|
||||
suppressBaselineLayer = suppressColorLayers,
|
||||
suppressCreativeLayer = suppressColorLayers
|
||||
)
|
||||
}
|
||||
|
||||
// 2. 读取像素
|
||||
val pixelSize = captureWidth * captureHeight * 4
|
||||
val pixelSize = targetWidth * targetHeight * 4
|
||||
if (pboId == 0) {
|
||||
val pbos = IntArray(1)
|
||||
GLES30.glGenBuffers(1, pbos, 0)
|
||||
@@ -2927,7 +2948,7 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, pboId)
|
||||
GLES30.glBufferData(GLES30.GL_PIXEL_PACK_BUFFER, pixelSize, null, GLES30.GL_STREAM_READ)
|
||||
GLES30.glReadPixels(0, 0, captureWidth, captureHeight, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, 0)
|
||||
GLES30.glReadPixels(0, 0, targetWidth, targetHeight, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, 0)
|
||||
|
||||
val mappedBuffer = GLES30.glMapBufferRange(
|
||||
GLES30.GL_PIXEL_PACK_BUFFER, 0, pixelSize, GLES30.GL_MAP_READ_BIT
|
||||
@@ -2938,16 +2959,16 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
return
|
||||
}
|
||||
|
||||
val bitmap = Bitmap.createBitmap(captureWidth, captureHeight, Bitmap.Config.ARGB_8888)
|
||||
val bitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888)
|
||||
bitmap.copyPixelsFromBuffer(mappedBuffer)
|
||||
GLES30.glUnmapBuffer(GLES30.GL_PIXEL_PACK_BUFFER)
|
||||
|
||||
// 翻转 Y 轴并返回
|
||||
val matrix = android.graphics.Matrix()
|
||||
matrix.preScale(1f, -1f)
|
||||
val finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, captureWidth, captureHeight, matrix, false)
|
||||
val finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, targetWidth, targetHeight, matrix, false)
|
||||
bitmap.recycle()
|
||||
onPreviewFrameCaptured?.invoke(finalBitmap)
|
||||
request.onCaptured(finalBitmap)
|
||||
|
||||
// 3. 恢复环境
|
||||
GLES30.glBindBuffer(GLES30.GL_PIXEL_PACK_BUFFER, 0)
|
||||
@@ -2959,9 +2980,13 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
private fun captureOriginalPreviewFrameInternal(rawPreviewSource: PreviewSourceOverride?) {
|
||||
private fun captureOriginalPreviewFrameInternal(
|
||||
request: PreviewCaptureRequest,
|
||||
rawPreviewSource: PreviewSourceOverride?
|
||||
) {
|
||||
if (rawPreviewSource != null) {
|
||||
capturePreviewFrameInternal(
|
||||
request = request,
|
||||
sourceTextureId = rawPreviewSource.textureId,
|
||||
sourceWidth = viewportWidth,
|
||||
sourceHeight = viewportHeight,
|
||||
@@ -2970,7 +2995,10 @@ class LutRenderer : GLSurfaceView.Renderer {
|
||||
return
|
||||
}
|
||||
|
||||
capturePreviewFrameInternal(suppressColorLayers = true)
|
||||
capturePreviewFrameInternal(
|
||||
request = request,
|
||||
suppressColorLayers = true
|
||||
)
|
||||
}
|
||||
|
||||
private var lastRunMeteringTime = 0L
|
||||
|
||||
@@ -1083,6 +1083,7 @@ class RawDemosaicProcessor {
|
||||
rawAutoWhiteBalanceEstimate = rawAutoWhiteBalanceEstimate,
|
||||
applyLensShadingCorrection = applyLensShadingCorrection,
|
||||
rawDcpId = null,
|
||||
dcpRenderPlan = input.meteringRenderPlan,
|
||||
spectralFilmStock = null,
|
||||
spectralFilmPrint = null,
|
||||
rawRenderingEngine = RawRenderingEngine.AdobeCurve,
|
||||
|
||||
@@ -22,6 +22,7 @@ data class RawDngCaptureProfileInput(
|
||||
val rowStride: Int,
|
||||
val samplesPerPixel: Int,
|
||||
val metadata: RawMetadata,
|
||||
val meteringRenderPlan: DcpRenderPlan,
|
||||
val gpuLinearRgbSource: GpuLinearRgbSource? = null,
|
||||
)
|
||||
|
||||
@@ -30,6 +31,16 @@ data class RawDngCaptureProfileResult(
|
||||
val profileGainTableMap: DngProfileGainTableMap?,
|
||||
)
|
||||
|
||||
/** Keeps the DNG color solution while enforcing the fixed Adobe/default metering pipeline. */
|
||||
internal fun DcpRenderPlan.toAdobeDefaultMeteringPlan(): DcpRenderPlan {
|
||||
return copy(
|
||||
defaultBlackRender = DcpDefaultBlackRender.None,
|
||||
hueSatMap = null,
|
||||
lookTable = null,
|
||||
toneCurveLut = null,
|
||||
)
|
||||
}
|
||||
|
||||
/** BaselineExposure and optional PGTM prepared before the DNG writer starts. */
|
||||
data class RawDngProfilePreparation(
|
||||
val baselineExposureEv: Float,
|
||||
|
||||
@@ -3,12 +3,9 @@ package com.hinnka.mycamera.raw
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Rect
|
||||
import com.hinnka.mycamera.BuildConfig
|
||||
import com.hinnka.mycamera.camera.AspectRatio
|
||||
import com.hinnka.mycamera.camera.RawBlackBorderCrop
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import kotlin.math.max
|
||||
|
||||
internal data class RawExposurePreviewFrame(
|
||||
@@ -27,7 +24,6 @@ internal data class RawExposurePreviewFrame(
|
||||
internal object RawViewfinderExposureMatcher {
|
||||
private const val TAG = "RawViewfinderExposureMatcher"
|
||||
private const val PREVIEW_LONG_EDGE = 256
|
||||
private const val DIAGNOSTIC_DIRECTORY = "raw_viewfinder_exposure_match"
|
||||
|
||||
private data class ViewfinderReference(
|
||||
val analysis: RawViewfinderExposureMath.Reference,
|
||||
@@ -60,7 +56,6 @@ internal object RawViewfinderExposureMatcher {
|
||||
height = it.analysis.height,
|
||||
solve = { renderSample ->
|
||||
solve(
|
||||
context = context,
|
||||
reference = it,
|
||||
renderSample = renderSample,
|
||||
)
|
||||
@@ -114,131 +109,12 @@ internal object RawViewfinderExposureMatcher {
|
||||
}
|
||||
|
||||
private fun solve(
|
||||
context: Context,
|
||||
reference: ViewfinderReference,
|
||||
renderSample: (Float) -> RawExposurePreviewFrame?,
|
||||
): Float? {
|
||||
val solvedExposureEv = RawViewfinderExposureMath.solve { exposureEv ->
|
||||
return RawViewfinderExposureMath.solve { exposureEv ->
|
||||
evaluate(reference.analysis, exposureEv, renderSample(exposureEv))
|
||||
}
|
||||
if (BuildConfig.DEBUG && solvedExposureEv != null) {
|
||||
val solvedFrame = renderSample(solvedExposureEv)
|
||||
if (solvedFrame != null &&
|
||||
solvedFrame.width == reference.frame.width &&
|
||||
solvedFrame.height == reference.frame.height
|
||||
) {
|
||||
saveDiagnosticFrames(
|
||||
context = context,
|
||||
reference = reference,
|
||||
candidate = solvedFrame,
|
||||
solvedExposureEv = solvedExposureEv,
|
||||
)
|
||||
} else {
|
||||
PLog.w(
|
||||
TAG,
|
||||
"RAW viewfinder diagnostic skipped: solved candidate frame is unavailable " +
|
||||
"exposureEv=$solvedExposureEv " +
|
||||
"reference=${reference.frame.width}x${reference.frame.height} " +
|
||||
"candidate=${solvedFrame?.let { "${it.width}x${it.height}" } ?: "null"}",
|
||||
)
|
||||
}
|
||||
}
|
||||
return solvedExposureEv
|
||||
}
|
||||
|
||||
private fun saveDiagnosticFrames(
|
||||
context: Context,
|
||||
reference: ViewfinderReference,
|
||||
candidate: RawExposurePreviewFrame,
|
||||
solvedExposureEv: Float,
|
||||
) {
|
||||
val rootDirectory = File(context.cacheDir, DIAGNOSTIC_DIRECTORY)
|
||||
if (!rootDirectory.exists() && !rootDirectory.mkdirs()) {
|
||||
PLog.w(TAG, "Unable to create RAW viewfinder diagnostic directory: $rootDirectory")
|
||||
return
|
||||
}
|
||||
val directory = File(rootDirectory, System.currentTimeMillis().toString())
|
||||
if (!directory.mkdirs()) {
|
||||
PLog.w(TAG, "Unable to create RAW viewfinder capture directory: $directory")
|
||||
return
|
||||
}
|
||||
val referenceFile = File(directory, "reference.png")
|
||||
val candidateFile = File(directory, "candidate.png")
|
||||
val referenceRoiFile = File(directory, "reference_roi.png")
|
||||
val candidateRoiFile = File(directory, "candidate_roi.png")
|
||||
val bounds = Rect(
|
||||
reference.analysis.left,
|
||||
reference.analysis.top,
|
||||
reference.analysis.right,
|
||||
reference.analysis.bottom,
|
||||
)
|
||||
val referenceBitmap = Bitmap.createBitmap(
|
||||
reference.frame.argbPixels,
|
||||
reference.frame.width,
|
||||
reference.frame.height,
|
||||
Bitmap.Config.ARGB_8888,
|
||||
)
|
||||
val candidateBitmap = Bitmap.createBitmap(
|
||||
candidate.argbPixels,
|
||||
candidate.width,
|
||||
candidate.height,
|
||||
Bitmap.Config.ARGB_8888,
|
||||
)
|
||||
var referenceRoiBitmap: Bitmap? = null
|
||||
var candidateRoiBitmap: Bitmap? = null
|
||||
try {
|
||||
val referenceRoi = Bitmap.createBitmap(
|
||||
referenceBitmap,
|
||||
bounds.left,
|
||||
bounds.top,
|
||||
bounds.width(),
|
||||
bounds.height(),
|
||||
)
|
||||
val candidateRoi = Bitmap.createBitmap(
|
||||
candidateBitmap,
|
||||
bounds.left,
|
||||
bounds.top,
|
||||
bounds.width(),
|
||||
bounds.height(),
|
||||
)
|
||||
referenceRoiBitmap = referenceRoi
|
||||
candidateRoiBitmap = candidateRoi
|
||||
val saved = listOf(
|
||||
writePng(referenceBitmap, referenceFile),
|
||||
writePng(candidateBitmap, candidateFile),
|
||||
writePng(referenceRoi, referenceRoiFile),
|
||||
writePng(candidateRoi, candidateRoiFile),
|
||||
).all { it }
|
||||
if (saved) {
|
||||
PLog.i(
|
||||
TAG,
|
||||
"RAW viewfinder diagnostic saved: exposureEv=$solvedExposureEv " +
|
||||
"size=${candidate.width}x${candidate.height} roi=$bounds " +
|
||||
"reference=${referenceFile.absolutePath} " +
|
||||
"candidate=${candidateFile.absolutePath} " +
|
||||
"referenceRoi=${referenceRoiFile.absolutePath} " +
|
||||
"candidateRoi=${candidateRoiFile.absolutePath}",
|
||||
)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to save RAW viewfinder diagnostic frames", e)
|
||||
} finally {
|
||||
referenceRoiBitmap?.recycle()
|
||||
candidateRoiBitmap?.recycle()
|
||||
referenceBitmap.recycle()
|
||||
candidateBitmap.recycle()
|
||||
}
|
||||
}
|
||||
|
||||
private fun writePng(bitmap: Bitmap, file: File): Boolean {
|
||||
return try {
|
||||
FileOutputStream(file, false).use { output ->
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output)
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to write RAW viewfinder diagnostic PNG: $file", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private fun evaluate(
|
||||
@@ -255,10 +131,12 @@ internal object RawViewfinderExposureMatcher {
|
||||
width = frame.width,
|
||||
height = frame.height,
|
||||
) ?: return null
|
||||
val meteringLog2Error = match.meanBrightnessLog2Error
|
||||
PLog.d(
|
||||
TAG,
|
||||
"RAW viewfinder sample: exposureEv=$exposureEv " +
|
||||
"matchLog2Error=${match.matchLog2Error} " +
|
||||
"meteringLog2Error=$meteringLog2Error " +
|
||||
"quantileBlendLog2Error=${match.matchLog2Error} " +
|
||||
"shadowPriorityTrimmedLog2Error=${match.toneWeightedLog2Error} " +
|
||||
"linearArithmeticMeanLog2Error=${match.linearArithmeticMeanLog2Error} " +
|
||||
"linearLogAverageLog2Error=${match.linearLogAverageLog2Error} " +
|
||||
@@ -278,7 +156,7 @@ internal object RawViewfinderExposureMatcher {
|
||||
"candidatePerceptualBrightnessMean=" +
|
||||
"${match.candidatePerceptualBrightnessMean}"
|
||||
)
|
||||
return match.matchLog2Error
|
||||
return meteringLog2Error
|
||||
}
|
||||
|
||||
private data class Size(val width: Int, val height: Int)
|
||||
|
||||
@@ -344,17 +344,17 @@ class CameraGLSurfaceView @JvmOverloads constructor(
|
||||
requestRenderImmediately: Boolean,
|
||||
callback: (Bitmap?) -> Unit
|
||||
) {
|
||||
renderer.onPreviewFrameCaptured = { bitmap ->
|
||||
// 在主线程回调
|
||||
post {
|
||||
callback(bitmap)
|
||||
}
|
||||
}
|
||||
queueEvent {
|
||||
val onCaptured: (Bitmap) -> Unit = { bitmap ->
|
||||
// 在主线程回调
|
||||
post {
|
||||
callback(bitmap)
|
||||
}
|
||||
}
|
||||
if (maxLongEdge != null) {
|
||||
renderer.capturePreviewFrame(maxLongEdge, source)
|
||||
renderer.capturePreviewFrame(maxLongEdge, source, onCaptured)
|
||||
} else {
|
||||
renderer.capturePreviewFrame(source = source)
|
||||
renderer.capturePreviewFrame(source = source, onCaptured = onCaptured)
|
||||
}
|
||||
if (requestRenderImmediately) {
|
||||
requestRender()
|
||||
|
||||
@@ -20,7 +20,9 @@ import com.hinnka.mycamera.raw.RawDngProfilePreparationOptions
|
||||
import com.hinnka.mycamera.raw.RawMetadata
|
||||
import com.hinnka.mycamera.raw.RawDemosaicProcessor
|
||||
import com.hinnka.mycamera.raw.RawProfileToneMapMode
|
||||
import com.hinnka.mycamera.raw.RawRenderingEngine
|
||||
import com.hinnka.mycamera.raw.RawWhiteLevelCorrection
|
||||
import com.hinnka.mycamera.raw.toAdobeDefaultMeteringPlan
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.nio.ByteBuffer
|
||||
@@ -110,23 +112,19 @@ object RawProcessor {
|
||||
rotation: Int,
|
||||
profilePreparation: RawDngProfilePreparation,
|
||||
): RawMetadata {
|
||||
val dngWhiteBalance = resolveDngWriterWhiteBalance(captureResult)
|
||||
val dngChannelNoise = resolveDngWriterNoiseProfile(captureResult)
|
||||
val metadata = RawMetadata.create(
|
||||
val metadata = buildAdobeDngColorMetadata(
|
||||
width = width,
|
||||
height = height,
|
||||
characteristics = characteristics,
|
||||
captureResult = captureResult,
|
||||
userExposureCompensation = baseMetadata.exposureBias,
|
||||
colorSpace = com.hinnka.mycamera.raw.ColorSpace.ProPhoto,
|
||||
)
|
||||
return metadata.copy(
|
||||
width = width,
|
||||
height = height,
|
||||
blackLevel = floatArrayOf(0f, 0f, 0f, 0f),
|
||||
whiteLevel = 65535f,
|
||||
whiteBalanceGains = dngWhiteBalance,
|
||||
preMul = dngWhiteBalance.copyOf(),
|
||||
lensShadingMap = null,
|
||||
lensShadingMapWidth = 0,
|
||||
lensShadingMapHeight = 0,
|
||||
@@ -148,6 +146,28 @@ object RawProcessor {
|
||||
)
|
||||
}
|
||||
|
||||
/** Color metadata shared by capture-time metering and the reopened Adobe/default DNG render. */
|
||||
private fun buildAdobeDngColorMetadata(
|
||||
width: Int,
|
||||
height: Int,
|
||||
characteristics: CameraCharacteristics,
|
||||
captureResult: CaptureResult,
|
||||
userExposureCompensation: Float? = null,
|
||||
): RawMetadata {
|
||||
val dngWhiteBalance = resolveDngWriterWhiteBalance(captureResult)
|
||||
return RawMetadata.create(
|
||||
width = width,
|
||||
height = height,
|
||||
characteristics = characteristics,
|
||||
captureResult = captureResult,
|
||||
userExposureCompensation = userExposureCompensation,
|
||||
colorSpace = RawRenderingEngine.AdobeCurve.workingColorSpace,
|
||||
).copy(
|
||||
whiteBalanceGains = dngWhiteBalance,
|
||||
preMul = dngWhiteBalance.copyOf(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun resolveDngWriterWhiteBalance(captureResult: CaptureResult): FloatArray {
|
||||
val neutral = captureResult.get(CaptureResult.SENSOR_NEUTRAL_COLOR_POINT)
|
||||
?.takeIf { it.size >= 3 }
|
||||
@@ -469,7 +489,6 @@ object RawProcessor {
|
||||
height: Int,
|
||||
characteristics: CameraCharacteristics,
|
||||
captureResult: CaptureResult,
|
||||
captureMetadataResult: CaptureResult? = null,
|
||||
cfaPattern: Int = RawMetadata.CFA_RGGB,
|
||||
blackLevel: FloatArray = floatArrayOf(0f, 0f, 0f, 0f),
|
||||
whiteLevel: Int = 65535,
|
||||
@@ -513,11 +532,11 @@ object RawProcessor {
|
||||
} else {
|
||||
resolvedWhiteLevel.toFloat()
|
||||
}
|
||||
val statsMetadata = RawMetadata.create(
|
||||
val statsMetadata = buildAdobeDngColorMetadata(
|
||||
width = width,
|
||||
height = height,
|
||||
characteristics = characteristics,
|
||||
captureResult = captureMetadataResult ?: captureResult,
|
||||
captureResult = captureResult,
|
||||
).copy(
|
||||
width = width,
|
||||
height = height,
|
||||
@@ -527,6 +546,24 @@ object RawProcessor {
|
||||
baselineExposure = sourceBaselineExposureEv,
|
||||
defaultCrop = defaultCrop,
|
||||
)
|
||||
val embeddedDngColorPlan = SuperResolutionDngWriter.resolveEmbeddedRenderPlan(
|
||||
characteristics = characteristics,
|
||||
metadata = statsMetadata,
|
||||
imageLayout = imageLayout,
|
||||
profileGainTableMap = null,
|
||||
profileToneCurve = null,
|
||||
)
|
||||
if (embeddedDngColorPlan == null) {
|
||||
PLog.e(TAG, "Unable to resolve the DNG color plan for RAW capture-profile preparation")
|
||||
return null
|
||||
}
|
||||
// Metering has one fixed Adobe/default tone pipeline. Reuse only the DNG color solution;
|
||||
// layout-specific DNG black-render tags and embedded profile curves must not alter it.
|
||||
val meteringRenderPlan = embeddedDngColorPlan.toAdobeDefaultMeteringPlan()
|
||||
val captureProfileMetadata = statsMetadata.copy(
|
||||
colorCorrectionMatrix = meteringRenderPlan.colorCorrectionMatrix.copyOf(),
|
||||
cameraWhite = meteringRenderPlan.cameraWhite.copyOf(),
|
||||
)
|
||||
val captureProfile = options.captureProfilePreparer?.prepare(
|
||||
com.hinnka.mycamera.raw.RawDngCaptureProfileInput(
|
||||
rawData = rawBuffer.duplicate().order(ByteOrder.nativeOrder()),
|
||||
@@ -534,7 +571,8 @@ object RawProcessor {
|
||||
height = height,
|
||||
rowStride = inputRowStrideBytes,
|
||||
samplesPerPixel = inputSamplesPerPixel,
|
||||
metadata = statsMetadata.copy(profileGainTableMap = null),
|
||||
metadata = captureProfileMetadata.copy(profileGainTableMap = null),
|
||||
meteringRenderPlan = meteringRenderPlan,
|
||||
gpuLinearRgbSource = gpuLinearRgbSource,
|
||||
)
|
||||
)
|
||||
@@ -641,7 +679,6 @@ object RawProcessor {
|
||||
height = height,
|
||||
characteristics = characteristics,
|
||||
captureResult = captureResult,
|
||||
captureMetadataResult = captureMetadataResult,
|
||||
cfaPattern = cfaPattern,
|
||||
blackLevel = blackLevel,
|
||||
whiteLevel = whiteLevel,
|
||||
|
||||
Reference in new issue
Block a user