diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt index a767f05f..0eac85ef 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutRenderer.kt @@ -3117,12 +3117,18 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer { if (now - lastNightVisionSampleTime < NIGHT_VISION_SAMPLE_INTERVAL_MS) return if (!isPixelPackBufferWritable(nightVisionPboFences, writeIndex)) return lastNightVisionSampleTime = now + // Crop the capture composition first, then sample its central 2/3 in each dimension. + // The square viewport resizes that region to the same MODEL_SIZE² as capture input. + val samplingMatrix = buildMvpMatrix(captureWidth, captureHeight).apply { + val scale = 1f / NightVisionTone.SAMPLE_FRAME_FRACTION + Matrix.scaleM(this, 0, scale, scale, 1f) + } try { drawInternal( fboId = nightVisionFboId, width = size, height = size, - targetMvpMatrix = buildMvpMatrix(size, size), + targetMvpMatrix = samplingMatrix, suppressBaselineLayer = true, suppressCreativeLayer = true, suppressCameraEngine = true, diff --git a/app/src/main/java/com/hinnka/mycamera/lut/NightVisionPreviewEstimator.kt b/app/src/main/java/com/hinnka/mycamera/lut/NightVisionPreviewEstimator.kt index 8ba9fa50..165bc1df 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/NightVisionPreviewEstimator.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/NightVisionPreviewEstimator.kt @@ -33,6 +33,7 @@ internal class NightVisionPreviewEstimator( @Volatile private var smoothed: NightVisionToneCurve? = null private var lastUpdateNanos = 0L + private var lastDiagnosticLogNanos = 0L fun submit(rgba: ByteArray) { if (!busy.compareAndSet(false, true)) return @@ -43,6 +44,10 @@ internal class NightVisionPreviewEstimator( val target = NightVisionToneModel.estimate(appContext, input) ?: return@launch if (requestGeneration != generation.get()) return@launch val now = SystemClock.elapsedRealtimeNanos() + if (now - lastDiagnosticLogNanos >= DIAGNOSTIC_LOG_INTERVAL_NANOS) { + PLog.d(TAG, "Estimated curve strength=${target.strength} whitePoint=${target.whitePoint}") + lastDiagnosticLogNanos = now + } val previous = smoothed val next = if (previous == null) { target @@ -77,5 +82,6 @@ internal class NightVisionPreviewEstimator( private companion object { const val TAG = "NightVisionPreview" const val SMOOTHING_TIME_CONSTANT_SECONDS = 0.4f + const val DIAGNOSTIC_LOG_INTERVAL_NANOS = 2_000_000_000L } } diff --git a/app/src/main/java/com/hinnka/mycamera/processor/NightVisionTone.kt b/app/src/main/java/com/hinnka/mycamera/processor/NightVisionTone.kt index 8a978c97..8f9e1e56 100644 --- a/app/src/main/java/com/hinnka/mycamera/processor/NightVisionTone.kt +++ b/app/src/main/java/com/hinnka/mycamera/processor/NightVisionTone.kt @@ -1,6 +1,11 @@ package com.hinnka.mycamera.processor import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.ColorSpace +import android.graphics.Matrix +import android.graphics.Paint +import android.graphics.RectF import java.nio.ByteBuffer import kotlin.math.abs import kotlin.math.exp @@ -103,6 +108,8 @@ internal data class NightVisionDenoiseStrength(val luma: Float, val chroma: Floa */ internal object NightVisionTone { const val MODEL_SIZE = 256 + /** Central fraction of the capture width and height sampled by both preview and capture. */ + const val SAMPLE_FRAME_FRACTION = 2f / 3f const val CURVE_SIZE = 256 const val HIGHLIGHT_HEADROOM = 0.55f @@ -246,21 +253,35 @@ internal object NightVisionTone { ) } - /** Resizes [bitmap] to the model input, HWC RGB floats of its encoded values (no color conversion). */ + /** + * Resizes the central [SAMPLE_FRAME_FRACTION] of each capture dimension to [MODEL_SIZE]². + * Matches the preview sampling matrix; the fitted curve is still applied to the whole image. + * Returns HWC RGB floats in the bitmap's encoded color space. + */ fun modelInputFrom(bitmap: Bitmap): FloatArray { - val scaled = Bitmap.createScaledBitmap(bitmap, MODEL_SIZE, MODEL_SIZE, true) + val scaled = Bitmap.createBitmap( + MODEL_SIZE, + MODEL_SIZE, + Bitmap.Config.ARGB_8888, + bitmap.hasAlpha(), + bitmap.colorSpace ?: ColorSpace.get(ColorSpace.Named.SRGB), + ) try { - val argb = if (scaled.config == Bitmap.Config.ARGB_8888) { - scaled - } else { - scaled.copy(Bitmap.Config.ARGB_8888, false) + val insetX = bitmap.width * (1f - SAMPLE_FRAME_FRACTION) * 0.5f + val insetY = bitmap.height * (1f - SAMPLE_FRAME_FRACTION) * 0.5f + val matrix = Matrix().apply { + setRectToRect( + RectF(insetX, insetY, bitmap.width - insetX, bitmap.height - insetY), + RectF(0f, 0f, MODEL_SIZE.toFloat(), MODEL_SIZE.toFloat()), + Matrix.ScaleToFit.FILL, + ) } - val bytes = ByteBuffer.allocate(argb.rowBytes * argb.height) - argb.copyPixelsToBuffer(bytes) - if (argb !== scaled) argb.recycle() - return modelInputFromRgba(bytes.array(), argb.rowBytes) + Canvas(scaled).drawBitmap(bitmap, matrix, Paint(Paint.FILTER_BITMAP_FLAG)) + val bytes = ByteBuffer.allocate(scaled.rowBytes * scaled.height) + scaled.copyPixelsToBuffer(bytes) + return modelInputFromRgba(bytes.array(), scaled.rowBytes) } finally { - if (scaled !== bitmap) scaled.recycle() + scaled.recycle() } }