From d73d82e63a194a1be53729dc1fdc03a00ea771f3 Mon Sep 17 00:00:00 2001 From: zhoubinjia Date: Fri, 26 Jun 2026 11:29:44 +0800 Subject: [PATCH] fix: natural light from sRGB tonemap --- .../mycamera/camera/Camera2Controller.kt | 24 +-- .../data/UserPreferencesRepository.kt | 45 +++++- .../hinnka/mycamera/gallery/PhotoProcessor.kt | 2 + .../hinnka/mycamera/lut/LutImageProcessor.kt | 67 ++++++++- .../mycamera/ui/camera/CameraPreviewGL.kt | 6 +- .../hinnka/mycamera/ui/camera/CameraScreen.kt | 12 +- .../mycamera/ui/settings/SettingsScreen.kt | 8 +- .../mycamera/viewmodel/CameraViewModel.kt | 142 ++++++++++++------ 8 files changed, 216 insertions(+), 90 deletions(-) diff --git a/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt b/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt index d8603690..354ce698 100644 --- a/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt +++ b/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt @@ -2048,16 +2048,6 @@ class Camera2Controller(private val context: Context) { private fun applyToneMapSettings(builder: CaptureRequest.Builder, state: CameraState, isCapture: Boolean) { val linearizePreviewInput = state.fixTonemapPreview && !isCapture val tonemapMode = sanitizeTonemapMode(state.tonemapMode) - if (tonemapMode == "LINEAR_PIPELINE" && isCapture && state.useMultipleExposure) { - PLog.d(TAG, "Forcing default tone map for multiple exposure") - applyDefaultToneMapSettings(builder, state, isCapture) - return - } - if (tonemapMode == "LINEAR_PIPELINE" && shouldForceDefaultToneMapForHdrComposition(state, isCapture)) { - PLog.d(TAG, "Forcing default tone map for YUV HDR composition capture") - applyDefaultToneMapSettings(builder, state, isCapture) - return - } when (tonemapMode) { "SYSTEM_DEFAULT" -> applyDefaultToneMapSettings(builder, state, isCapture) "SRGB" -> { @@ -2068,16 +2058,6 @@ class Camera2Controller(private val context: Context) { builder.set(CaptureRequest.TONEMAP_CURVE, curve) } } - "LINEAR_PIPELINE" -> { - if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)) { - builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) - val linearCurve = generateLinearToneCurve() - val curve = TonemapCurve(linearCurve, linearCurve, linearCurve) - builder.set(CaptureRequest.TONEMAP_CURVE, curve) - } else { - applyDefaultToneMapSettings(builder, state, isCapture) - } - } else -> { applyDefaultToneMapSettings(builder, state, isCapture) } @@ -2113,8 +2093,8 @@ class Camera2Controller(private val context: Context) { return when (mode) { "FAST", "HIGH_QUALITY" -> "SYSTEM_DEFAULT" "REC709" -> "SRGB" - "RAW_PREVIEW", "SRGB_ACR3", "REC709_ACR3" -> "LINEAR_PIPELINE" - "SYSTEM_DEFAULT", "SRGB", "LINEAR_PIPELINE" -> mode + "RAW_PREVIEW", "SRGB_ACR3", "REC709_ACR3", "LINEAR_PIPELINE" -> "SRGB" + "SYSTEM_DEFAULT", "SRGB" -> mode else -> "SYSTEM_DEFAULT" } } diff --git a/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt b/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt index 9d3e8092..197a7a1d 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt @@ -52,12 +52,19 @@ private fun sanitizeTonemapMode(mode: String): String { return when (mode) { "FAST", "HIGH_QUALITY" -> "SYSTEM_DEFAULT" "REC709" -> "SRGB" - "RAW_PREVIEW", "SRGB_ACR3", "REC709_ACR3" -> "LINEAR_PIPELINE" - "SYSTEM_DEFAULT", "SRGB", "LINEAR_PIPELINE" -> mode + "RAW_PREVIEW", "SRGB_ACR3", "REC709_ACR3", "LINEAR_PIPELINE" -> "SRGB" + "SYSTEM_DEFAULT", "SRGB" -> mode else -> "SYSTEM_DEFAULT" } } +private fun isLegacyNaturalLightTonemap(mode: String?): Boolean { + return mode == "LINEAR_PIPELINE" || + mode == "RAW_PREVIEW" || + mode == "SRGB_ACR3" || + mode == "REC709_ACR3" +} + enum class VolumeKeyAction { NONE, CAPTURE, @@ -161,6 +168,8 @@ data class UserPreferences( val useGpuAcceleration: Boolean = DeviceUtil.defaultGpuAcceleration, // 多帧合成是否使用 GPU 加速 val droMode: String = "OFF", // DRO 模式 val tonemapMode: String = "SYSTEM_DEFAULT", // 色调映射模式 + val naturalLightEnabled: Boolean = false, // 是否启用自然光影 + val naturalLightPreviousTonemapMode: String = "SYSTEM_DEFAULT", // 自然光影关闭时恢复的色调映射模式 val fixTonemapPreview: Boolean = false, // 修复部分设备自定义色调映射预览异常 val applyUltraHDR: Boolean = false, // 是否应用 Ultra HDR 策略 val colorSpace: ColorSpace = ColorSpace.SRGB, @@ -344,6 +353,8 @@ class UserPreferencesRepository(private val context: Context) { private val USE_GPU_ACCELERATION = booleanPreferencesKey("use_gpu_acceleration") private val DRO_MODE = stringPreferencesKey("dro_mode") private val TONEMAP_MODE = stringPreferencesKey("tonemap_mode") + private val NATURAL_LIGHT_ENABLED = booleanPreferencesKey("natural_light_enabled") + private val NATURAL_LIGHT_PREVIOUS_TONEMAP_MODE = stringPreferencesKey("natural_light_previous_tonemap_mode") private val FIX_TONEMAP_PREVIEW = booleanPreferencesKey("fix_tonemap_preview") private val APPLY_ULTRA_HDR = booleanPreferencesKey("apply_ultra_hdr") private val COLOR_SPACE = stringPreferencesKey("color_space") @@ -514,6 +525,11 @@ class UserPreferencesRepository(private val context: Context) { useGpuAcceleration = preferences[USE_GPU_ACCELERATION] ?: DeviceUtil.defaultGpuAcceleration, droMode = preferences[DRO_MODE] ?: if (preferences[RAW_DRO_ENABLED_KEY] == true) "DR100" else "OFF", tonemapMode = sanitizeTonemapMode(preferences[TONEMAP_MODE] ?: "SYSTEM_DEFAULT"), + naturalLightEnabled = preferences[NATURAL_LIGHT_ENABLED] + ?: isLegacyNaturalLightTonemap(preferences[TONEMAP_MODE]), + naturalLightPreviousTonemapMode = sanitizeTonemapMode( + preferences[NATURAL_LIGHT_PREVIOUS_TONEMAP_MODE] ?: "SYSTEM_DEFAULT" + ), fixTonemapPreview = preferences[FIX_TONEMAP_PREVIEW] ?: false, applyUltraHDR = preferences[APPLY_ULTRA_HDR] ?: false, colorSpace = ColorSpace.valueOf(preferences[COLOR_SPACE] ?: ColorSpace.SRGB.name), @@ -1483,6 +1499,31 @@ class UserPreferencesRepository(private val context: Context) { } } + /** + * 保存是否启用自然光影 + */ + suspend fun saveNaturalLightEnabled(enabled: Boolean) { + context.dataStore.edit { preferences -> + preferences[NATURAL_LIGHT_ENABLED] = enabled + } + } + + suspend fun saveNaturalLightState( + enabled: Boolean, + previousTonemapMode: String? = null, + tonemapMode: String? = null + ) { + context.dataStore.edit { preferences -> + previousTonemapMode?.let { + preferences[NATURAL_LIGHT_PREVIOUS_TONEMAP_MODE] = sanitizeTonemapMode(it) + } + tonemapMode?.let { + preferences[TONEMAP_MODE] = sanitizeTonemapMode(it) + } + preferences[NATURAL_LIGHT_ENABLED] = enabled + } + } + /** * 保存是否修复自定义色调映射预览异常 */ diff --git a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt index a2c91dbf..3da6cff5 100644 --- a/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/gallery/PhotoProcessor.kt @@ -491,6 +491,8 @@ class PhotoProcessor( lutConfig = null, colorRecipeParams = null, linearInputToneMap = true, + naturalLightInputSrgb = metadata.tonemapMode == "SRGB_ACR3" || + metadata.tonemapMode == "REC709_ACR3", linearInputExposureEv = metadata.rawExposureCompensation ?: 0f, rawRenderingEngine = metadata.rawRenderingEngine, rawToneMappingParameters = metadata.rawToneMappingParameters diff --git a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt index 14a87151..a5e8117f 100644 --- a/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/lut/LutImageProcessor.kt @@ -79,6 +79,7 @@ class LutImageProcessor { private var bitmapDenoiseNlmFusedAccuProgram = 0 private var bitmapDenoiseNlmFinishProgram = 0 private var bitmapDenoisePassthroughProgram = 0 + private var naturalLightSrgbToLinearProgram = 0 private var bitmapChromaDenoiseProgram = 0 private var lutSharpenProgram = 0 @@ -522,6 +523,7 @@ class LutImageProcessor { chromaNoiseReductionValue: Float = 0f, lutMaskType: Int = 0, linearInputToneMap: Boolean = false, + naturalLightInputSrgb: Boolean = false, linearInputExposureEv: Float = 0f, naturalLightDefaultChromaDenoise: Boolean = false, rawRenderingEngine: RawRenderingEngine = RawRenderingEngine.AdobeCurve, @@ -585,9 +587,14 @@ class LutImageProcessor { } else { chromaDenoisedTexId } + val naturalLightSourceTexId = if (applyNaturalLightToneMap && naturalLightInputSrgb) { + renderSrgbInputToLinear(inputTexId, width, height) + } else { + inputTexId + } val renderInputTexId = if (applyNaturalLightToneMap) { renderNaturalLightToneMap( - inputTexId, + naturalLightSourceTexId, width, height, rawRenderingEngine, @@ -1479,6 +1486,7 @@ class LutImageProcessor { bitmapDenoiseNlmFusedAccuProgram = compileComputeProgram(DenoiseProfileShaders.FUSED_ACCU, "BitmapDenoise_NLM_FusedAccu") bitmapDenoiseNlmFinishProgram = compileComputeProgram(DenoiseProfileShaders.FINISH_V2, "BitmapDenoise_NLM_FinishV2") bitmapDenoisePassthroughProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, TEXTURE_PASSTHROUGH_SHADER, "BitmapDenoise_Passthrough") + naturalLightSrgbToLinearProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, SRGB_TO_LINEAR_SHADER, "NaturalLight_SrgbToLinear") bitmapChromaDenoiseProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.PASS_CHROMA_DENOISE, "BitmapChromaDenoise_BM3DPass0") lutSharpenProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, RawShaders.SHARPEN_FRAGMENT_SHADER, "LutSharpen") PLog.d( @@ -1486,7 +1494,7 @@ class LutImageProcessor { "Bitmap denoiseprofile programs initialized: pre=$bitmapDenoisePreconditionProgram " + "init=$bitmapDenoiseNlmInitProgram fusedAccu=$bitmapDenoiseNlmFusedAccuProgram " + "finish=$bitmapDenoiseNlmFinishProgram " + - "pass=$bitmapDenoisePassthroughProgram " + + "pass=$bitmapDenoisePassthroughProgram linearize=$naturalLightSrgbToLinearProgram " + "chroma=$bitmapChromaDenoiseProgram " + "sharpen=$lutSharpenProgram" ) @@ -1674,6 +1682,33 @@ class LutImageProcessor { drawQuad(bitmapDenoisePassthroughProgram) } + private fun renderSrgbInputToLinear(sourceTextureId: Int, width: Int, height: Int): Int { + setupBitmapDenoiseFramebuffers(width, height) + if (naturalLightSrgbToLinearProgram == 0) return sourceTextureId + val targetIndex = if (sourceTextureId == bitmapDenoiseTexId[0]) 1 else 0 + val targetFboId = bitmapDenoiseFboId[targetIndex] + val targetTextureId = bitmapDenoiseTexId[targetIndex] + val identityMatrix = FloatArray(16) + android.opengl.Matrix.setIdentityM(identityMatrix, 0) + + GLES30.glUseProgram(naturalLightSrgbToLinearProgram) + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId) + GLES30.glViewport(0, 0, width, height) + GLES30.glActiveTexture(GLES30.GL_TEXTURE0) + GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId) + GLES30.glUniform1i(GLES30.glGetUniformLocation(naturalLightSrgbToLinearProgram, "uInputTexture"), 0) + GLES30.glUniformMatrix4fv( + GLES30.glGetUniformLocation(naturalLightSrgbToLinearProgram, "uMVPMatrix"), + 1, + false, + identityMatrix, + 0 + ) + drawQuad(naturalLightSrgbToLinearProgram) + checkGlError("renderSrgbInputToLinear") + return targetTextureId + } + private fun setupLutSharpenFramebuffer(width: Int, height: Int): Boolean { if (lutSharpenFboId != 0 && lutSharpenTextureId != 0 && @@ -2645,6 +2680,7 @@ class LutImageProcessor { if (bitmapDenoiseNlmFusedAccuProgram != 0) GLES31.glDeleteProgram(bitmapDenoiseNlmFusedAccuProgram) if (bitmapDenoiseNlmFinishProgram != 0) GLES31.glDeleteProgram(bitmapDenoiseNlmFinishProgram) if (bitmapDenoisePassthroughProgram != 0) GLES30.glDeleteProgram(bitmapDenoisePassthroughProgram) + if (naturalLightSrgbToLinearProgram != 0) GLES30.glDeleteProgram(naturalLightSrgbToLinearProgram) if (bitmapChromaDenoiseProgram != 0) GLES30.glDeleteProgram(bitmapChromaDenoiseProgram) if (lutSharpenProgram != 0) { GLES30.glDeleteProgram(lutSharpenProgram) @@ -2733,6 +2769,33 @@ class LutImageProcessor { } """.trimIndent() + private val SRGB_TO_LINEAR_SHADER = """ + #version 300 es + precision highp float; + + in vec2 vTexCoord; + out vec4 fragColor; + + uniform sampler2D uInputTexture; + + vec3 srgbToLinear(vec3 srgb) { + vec3 color = max(srgb, vec3(0.0)); + bvec3 useHigh = greaterThan(color, vec3(0.04045)); + vec3 low = color / 12.92; + vec3 high = pow((color + 0.055) / 1.055, vec3(2.4)); + return vec3( + useHigh.r ? high.r : low.r, + useHigh.g ? high.g : low.g, + useHigh.b ? high.b : low.b + ); + } + + void main() { + vec4 color = texture(uInputTexture, vTexCoord); + fragColor = vec4(srgbToLinear(color.rgb), color.a); + } + """.trimIndent() + private val SHADER_BODY = """ in vec2 vTexCoord; out vec4 fragColor; diff --git a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraPreviewGL.kt b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraPreviewGL.kt index 4d80cc6d..057bec5d 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraPreviewGL.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraPreviewGL.kt @@ -68,7 +68,7 @@ fun CameraPreviewGL( videoRecorder: VideoRecorder? = null, videoLogProfile: VideoLogProfile = VideoLogProfile.OFF, isHlgInput: Boolean = false, - tonemapMode: String = "SYSTEM_DEFAULT", + naturalLightEnabled: Boolean = false, fixTonemapPreview: Boolean = false, rawExposureCompensation: Float = 0f, rawBlackPointCorrection: Float = 0f, @@ -272,11 +272,11 @@ fun CameraPreviewGL( glSurfaceView.setVideoLogProfile(videoLogProfile) glSurfaceView.setIsHlgInput(isHlgInput) glSurfaceView.setRawPreviewSettings( - enabled = tonemapMode == "LINEAR_PIPELINE" || tonemapMode == "RAW_PREVIEW", + enabled = naturalLightEnabled, exposureCompensation = rawExposureCompensation, blackPointCorrection = rawBlackPointCorrection, whitePointCorrection = rawWhitePointCorrection, - linearizeInput = fixTonemapPreview, + linearizeInput = naturalLightEnabled || fixTonemapPreview, renderingEngine = rawRenderingEngine, toneMappingParameters = rawToneMappingParameters ) diff --git a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt index 5dbb9f99..0db19fa3 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/camera/CameraScreen.kt @@ -206,7 +206,7 @@ fun CameraScreen( val droMode by viewModel.droMode.collectAsState() val rawColorEngine by viewModel.rawRenderingEngine.collectAsState() val rawToneMappingParameters by viewModel.rawToneMappingParameters.collectAsState() - val tonemapMode by viewModel.tonemapMode.collectAsState() + val naturalLightEnabled by viewModel.naturalLightEnabled.collectAsState() val fixTonemapPreview by viewModel.fixTonemapPreview.collectAsState() val rawSpectralFilmStock by viewModel.rawSpectralFilmStock.collectAsState() val rawSpectralFilmSelection by viewModel.rawSpectralFilmSelection.collectAsState() @@ -952,7 +952,7 @@ fun CameraScreen( videoRecorder = viewModel.videoRecorder, videoLogProfile = state.videoConfig.logProfile, isHlgInput = if (hlgHardwareCompatibilityEnabled) state.isHLG else false, - tonemapMode = tonemapMode, + naturalLightEnabled = naturalLightEnabled, fixTonemapPreview = fixTonemapPreview, rawExposureCompensation = rawExposureCompensation, rawBlackPointCorrection = rawBlackPointCorrection, @@ -1163,7 +1163,7 @@ fun CameraScreen( galleryViewModel = galleryViewModel, latestPhoto = latestPhoto, useMultipleExposure = useMultipleExposure, - tonemapMode = tonemapMode, + naturalLightEnabled = naturalLightEnabled, multipleExposureState = multipleExposureState, onGalleryThumbnailBoundsChanged = { bounds -> galleryThumbnailBounds = bounds @@ -1344,7 +1344,7 @@ fun CameraScreen( useRaw = useRaw && state.isRawSupported, onRawToggle = { viewModel.setUseRaw(it) }, isRawSupported = state.isRawSupported, - useNaturalLight = tonemapMode == "LINEAR_PIPELINE", + useNaturalLight = naturalLightEnabled, onNaturalLightToggle = { viewModel.setNaturalLightToneMapEnabled(it) }, rawDcpId = rawDcpId, availableDcps = viewModel.availableDcps, @@ -1686,7 +1686,7 @@ fun Controls( galleryViewModel: GalleryViewModel, latestPhoto: com.hinnka.mycamera.gallery.MediaData?, useMultipleExposure: Boolean, - tonemapMode: String, + naturalLightEnabled: Boolean, multipleExposureState: com.hinnka.mycamera.viewmodel.MultipleExposureSessionState, onGalleryThumbnailBoundsChanged: (Rect) -> Unit, onSwitchCameraClick: () -> Unit, @@ -1732,7 +1732,7 @@ fun Controls( isVideoRecording = state.videoRecordingState.isRecording, isPaused = state.videoRecordingState.isPaused, allowLongPress = state.captureMode == CaptureMode.QUICK_SHOT || - (tonemapMode != "LINEAR_PIPELINE" && + (!naturalLightEnabled && state.captureMode == CaptureMode.PHOTO && !useMultipleExposure), multipleExposureEnabled = useMultipleExposure && state.captureMode == CaptureMode.PHOTO, diff --git a/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt b/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt index 64c4ecfd..586bdcbd 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/settings/SettingsScreen.kt @@ -175,8 +175,8 @@ private fun sanitizeSettingsTonemapMode(mode: String): String { return when (mode) { "FAST", "HIGH_QUALITY" -> "SYSTEM_DEFAULT" "REC709" -> "SRGB" - "RAW_PREVIEW", "SRGB_ACR3", "REC709_ACR3" -> "LINEAR_PIPELINE" - else -> mode + "SYSTEM_DEFAULT", "SRGB" -> mode + else -> "SYSTEM_DEFAULT" } } @@ -382,9 +382,6 @@ fun SettingsScreen( } fun applyTonemapMode(mode: String) { - if (mode == "LINEAR_PIPELINE" && rawAutoExposure) { - viewModel.setRawAutoExposure(false) - } viewModel.setTonemapMode(mode) } @@ -1323,7 +1320,6 @@ fun SettingsScreen( description = stringResource(R.string.settings_tonemap_mode_description), levels = listOf( "SYSTEM_DEFAULT" to stringResource(R.string.settings_tonemap_mode_system_default), - "LINEAR_PIPELINE" to stringResource(R.string.settings_tonemap_mode_natural_light), "SRGB" to stringResource(R.string.settings_tonemap_mode_srgb), ), currentLevel = settingsTonemapMode, diff --git a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt index 2d98b05b..34725e6b 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt @@ -107,15 +107,14 @@ private fun sanitizeViewModelTonemapMode(mode: String): String { return when (mode) { "FAST", "HIGH_QUALITY" -> TONEMAP_MODE_SYSTEM_DEFAULT "REC709" -> TONEMAP_MODE_SRGB - "RAW_PREVIEW", "SRGB_ACR3", "REC709_ACR3" -> TONEMAP_MODE_LINEAR_PIPELINE - TONEMAP_MODE_SYSTEM_DEFAULT, TONEMAP_MODE_SRGB, TONEMAP_MODE_LINEAR_PIPELINE -> mode + TONEMAP_MODE_SYSTEM_DEFAULT, TONEMAP_MODE_SRGB -> mode else -> TONEMAP_MODE_SYSTEM_DEFAULT } } private fun resolvePreviewBaselineTarget(prefs: UserPreferences): BaselineColorCorrectionTarget? { return when { - prefs.useRaw && prefs.tonemapMode == TONEMAP_MODE_LINEAR_PIPELINE -> BaselineColorCorrectionTarget.RAW + prefs.useRaw && prefs.naturalLightEnabled -> BaselineColorCorrectionTarget.RAW prefs.useRaw -> null else -> BaselineColorCorrectionTarget.JPG } @@ -660,42 +659,44 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) return prefs.useHdrComposition && !isRawEnabledForNaturalLightHdrGuard(prefs) } + private fun effectiveCameraTonemapMode(prefs: UserPreferences): String { + return if (prefs.naturalLightEnabled) { + TONEMAP_MODE_SRGB + } else { + sanitizeViewModelTonemapMode(prefs.tonemapMode) + } + } + + private fun metadataTonemapMode(prefs: UserPreferences?): String { + return if (prefs?.naturalLightEnabled == true) { + "SRGB_ACR3" + } else { + sanitizeViewModelTonemapMode(prefs?.tonemapMode ?: TONEMAP_MODE_SYSTEM_DEFAULT) + } + } + private suspend fun saveTonemapModeWithFeatureGuards(mode: String) { val resolvedMode = sanitizeViewModelTonemapMode(mode) - if (resolvedMode == TONEMAP_MODE_LINEAR_PIPELINE) { - val prefs = userPreferencesRepository.userPreferences.first() - val shouldDisableMultipleExposure = prefs.useMultipleExposure - val shouldDisableHdrComposition = shouldDisableNaturalLightForHdrComposition(prefs) - - if (shouldDisableMultipleExposure || shouldDisableHdrComposition) { - applyCameraFeatureUpdate( - CameraFeatureUpdate( - useMultipleExposure = if (shouldDisableMultipleExposure) { - SettingValue(false) - } else { - null - }, - useHdrComposition = if (shouldDisableHdrComposition) { - SettingValue(false) - } else { - null - } - ) - ) - } - - if (prefs.rawAutoExposure) { - userPreferencesRepository.saveRawAutoExposure(false) - } + val prefs = userPreferencesRepository.userPreferences.first() + if (prefs.naturalLightEnabled) { + userPreferencesRepository.saveNaturalLightState( + enabled = true, + previousTonemapMode = resolvedMode, + tonemapMode = TONEMAP_MODE_SRGB + ) + } else { + userPreferencesRepository.saveTonemapMode(resolvedMode) } - userPreferencesRepository.saveTonemapMode(resolvedMode) } private suspend fun clearNaturalLightTonemapIfNeeded(reason: String, prefs: UserPreferences? = null) { val currentPrefs = prefs ?: userPreferencesRepository.userPreferences.first() - if (currentPrefs.tonemapMode == TONEMAP_MODE_LINEAR_PIPELINE) { + if (currentPrefs.naturalLightEnabled) { PLog.d(TAG, "Disabling Natural Light tone map: $reason") - userPreferencesRepository.saveTonemapMode(TONEMAP_MODE_SYSTEM_DEFAULT) + userPreferencesRepository.saveNaturalLightState( + enabled = false, + tonemapMode = currentPrefs.naturalLightPreviousTonemapMode + ) } } @@ -1085,6 +1086,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) val tonemapMode: StateFlow = userPreferencesRepository.userPreferences .map { it.tonemapMode } .stateIn(viewModelScope, SharingStarted.Eagerly, "SYSTEM_DEFAULT") + val naturalLightEnabled: StateFlow = userPreferencesRepository.userPreferences + .map { it.naturalLightEnabled } + .stateIn(viewModelScope, SharingStarted.Eagerly, false) val fixTonemapPreview: StateFlow = userPreferencesRepository.userPreferences .map { it.fixTonemapPreview } .stateIn(viewModelScope, SharingStarted.Eagerly, false) @@ -1346,7 +1350,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) val effectiveUseRaw = it.useRaw && !multipleExposureEnabled val effectiveUseMFNR = it.useMFNR && !multipleExposureEnabled val effectiveUseMFSR = it.useMFSR && !multipleExposureEnabled - if (it.tonemapMode == TONEMAP_MODE_LINEAR_PIPELINE && + if (it.naturalLightEnabled && (multipleExposureEnabled || shouldDisableNaturalLightForHdrComposition(it)) ) { viewModelScope.launch { @@ -1385,11 +1389,13 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) if (currentCameraState.rawMinShutterSpeedNs != it.rawMinShutterSpeedNs) { cameraController.setRawMinShutterSpeedNs(it.rawMinShutterSpeedNs) } - if (currentCameraState.tonemapMode != it.tonemapMode) { - cameraController.setTonemapMode(it.tonemapMode) + val effectiveTonemapMode = effectiveCameraTonemapMode(it) + if (currentCameraState.tonemapMode != effectiveTonemapMode) { + cameraController.setTonemapMode(effectiveTonemapMode) } - if (currentCameraState.fixTonemapPreview != it.fixTonemapPreview) { - cameraController.setFixTonemapPreview(it.fixTonemapPreview) + val effectiveFixTonemapPreview = it.fixTonemapPreview && !it.naturalLightEnabled + if (currentCameraState.fixTonemapPreview != effectiveFixTonemapPreview) { + cameraController.setFixTonemapPreview(effectiveFixTonemapPreview) } if (cameraController.state.value.meteringMode != it.meteringMode) { cameraController.setMeteringMode(it.meteringMode) @@ -1564,8 +1570,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) cameraController.setUseMFSR(prefs.useMFSR && !prefs.useMultipleExposure) cameraController.setMultiFrameCount(prefs.multiFrameCount) cameraController.setUseLivePhoto(prefs.useLivePhoto && prefs.captureMode == CaptureMode.PHOTO) - cameraController.setTonemapMode(prefs.tonemapMode) - cameraController.setFixTonemapPreview(prefs.fixTonemapPreview) + cameraController.setTonemapMode(effectiveCameraTonemapMode(prefs)) + cameraController.setFixTonemapPreview(prefs.fixTonemapPreview && !prefs.naturalLightEnabled) // 应用保存的虚拟光圈 if (prefs.defaultVirtualAperture > 0f) { @@ -1956,7 +1962,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) return MediaMetadata( lutId = lutIdToSave, - tonemapMode = userPrefs?.tonemapMode ?: "SYSTEM_DEFAULT", + tonemapMode = metadataTonemapMode(userPrefs), frameId = frameIdToSave, colorRecipeParams = getMergedRecipeParams(), baselineTarget = baselineMetadata?.first, @@ -2401,7 +2407,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) startQuickShotBurst() return } - if (tonemapMode.value == "LINEAR_PIPELINE") { + if (naturalLightEnabled.value) { PLog.d(TAG, "Continuous photo burst disabled while Natural Light tone map is active") return } @@ -4305,7 +4311,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) // 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息 val metadata = MediaMetadata( lutId = lutIdToSave, - tonemapMode = userPrefs?.tonemapMode ?: "SYSTEM_DEFAULT", + tonemapMode = metadataTonemapMode(userPrefs), frameId = frameIdToSave, colorRecipeParams = getMergedRecipeParams(), baselineTarget = baselineMetadata?.first, @@ -4791,7 +4797,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) // 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息 val metadata = MediaMetadata( lutId = lutIdToSave, - tonemapMode = userPrefs?.tonemapMode ?: "SYSTEM_DEFAULT", + tonemapMode = metadataTonemapMode(userPrefs), frameId = frameIdToSave, colorRecipeParams = getMergedRecipeParams(), baselineTarget = baselineMetadata?.first, @@ -5020,7 +5026,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) multipleExposureFrameCount = expectedFrameCount, baselineTarget = BaselineColorCorrectionTarget.JPG, ).let { hdrMetadata -> - if (hdrMetadata.tonemapMode == "LINEAR_PIPELINE") { + if (hdrMetadata.usesLinearPipelineToneMap()) { PLog.d(TAG, "YUV HDR bracket stores SYSTEM_DEFAULT tonemap metadata") hdrMetadata.copy(tonemapMode = "SYSTEM_DEFAULT") } else { @@ -5322,7 +5328,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) // 创建统一的 PhotoMetadata,包含编辑配置和拍摄信息 val metadata = MediaMetadata( lutId = lutIdToSave, - tonemapMode = userPrefs?.tonemapMode ?: "SYSTEM_DEFAULT", + tonemapMode = metadataTonemapMode(userPrefs), frameId = frameIdToSave, colorRecipeParams = getMergedRecipeParams(), baselineTarget = baselineMetadata?.first, @@ -5559,13 +5565,51 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) } fun setNaturalLightToneMapEnabled(enabled: Boolean) { - setTonemapMode( + viewModelScope.launch { + val prefs = userPreferencesRepository.userPreferences.first() if (enabled) { - TONEMAP_MODE_LINEAR_PIPELINE - } else { - TONEMAP_MODE_SYSTEM_DEFAULT + val shouldDisableMultipleExposure = prefs.useMultipleExposure + val shouldDisableHdrComposition = shouldDisableNaturalLightForHdrComposition(prefs) + + if (shouldDisableMultipleExposure || shouldDisableHdrComposition) { + applyCameraFeatureUpdate( + CameraFeatureUpdate( + useMultipleExposure = if (shouldDisableMultipleExposure) { + SettingValue(false) + } else { + null + }, + useHdrComposition = if (shouldDisableHdrComposition) { + SettingValue(false) + } else { + null + } + ) + ) + } + + if (prefs.rawAutoExposure) { + userPreferencesRepository.saveRawAutoExposure(false) + } } - ) + if (enabled) { + val previousTonemapMode = if (prefs.naturalLightEnabled) { + prefs.naturalLightPreviousTonemapMode + } else { + prefs.tonemapMode + } + userPreferencesRepository.saveNaturalLightState( + enabled = true, + previousTonemapMode = previousTonemapMode, + tonemapMode = TONEMAP_MODE_SRGB + ) + } else { + userPreferencesRepository.saveNaturalLightState( + enabled = false, + tonemapMode = prefs.naturalLightPreviousTonemapMode + ) + } + } } fun setFixTonemapPreview(enabled: Boolean) {