From d9475929ec573bf83c3aa672d37e04138e683805 Mon Sep 17 00:00:00 2001 From: Hinnka Date: Sun, 31 May 2026 10:06:58 +0800 Subject: [PATCH] feat: add tone map mode selection and custom curves --- .../mycamera/camera/Camera2Controller.kt | 204 +++++++++++++++++- .../com/hinnka/mycamera/camera/CameraState.kt | 1 + .../data/UserPreferencesRepository.kt | 12 ++ .../mycamera/ui/settings/SettingsScreen.kt | 23 ++ .../mycamera/viewmodel/CameraViewModel.kt | 14 ++ app/src/main/res/values-zh-rCN/strings.xml | 5 + app/src/main/res/values-zh-rHK/strings.xml | 5 + app/src/main/res/values/strings.xml | 8 + 8 files changed, 261 insertions(+), 11 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 25f2474f..02eb49f4 100644 --- a/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt +++ b/app/src/main/java/com/hinnka/mycamera/camera/Camera2Controller.kt @@ -1674,20 +1674,194 @@ class Camera2Controller(private val context: Context) { builder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, resolvedRange) } + private fun generateSrgbCurve(): FloatArray { + val points = 64 + val curve = FloatArray(points * 2) + for (i in 0 until points) { + val x = i.toFloat() / (points - 1) + val y = if (x <= 0.0031308f) { + 12.92f * x + } else { + 1.055f * Math.pow(x.toDouble(), 1.0 / 2.4).toFloat() - 0.055f + } + curve[i * 2] = x + curve[i * 2 + 1] = y.coerceIn(0f, 1f) + } + return curve + } + + private fun generateRec709Curve(): FloatArray { + val points = 64 + val curve = FloatArray(points * 2) + for (i in 0 until points) { + val x = i.toFloat() / (points - 1) + val y = if (x < 0.018f) { + 4.5f * x + } else { + 1.099f * Math.pow(x.toDouble(), 0.45).toFloat() - 0.099f + } + curve[i * 2] = x + curve[i * 2 + 1] = y.coerceIn(0f, 1f) + } + return curve + } + + private fun generateLinearCurve(): FloatArray { + return floatArrayOf(0.0f, 0.0f, 1.0f, 1.0f) + } + + private fun inverseSrgb(x: Float): Float { + return if (x <= 0.04045f) { + x / 12.92f + } else { + Math.pow(((x + 0.055) / 1.055), 2.4).toFloat() + } + } + + private fun getAcr3LinearValue(x: Float): Float { + val samples = com.hinnka.mycamera.raw.ACR3Curve.samples() + val length = samples.size + if (length == 0) return x + val index = x * (length - 1) + val low = Math.floor(index.toDouble()).toInt().coerceIn(0, length - 1) + val high = Math.ceil(index.toDouble()).toInt().coerceIn(0, length - 1) + if (low == high) return samples[low] + val weight = index - low + return samples[low] * (1f - weight) + samples[high] * weight + } + + private fun generateAcr3Curve(): FloatArray { + val points = 64 + val curve = FloatArray(points * 2) + for (i in 0 until points) { + val x = i.toFloat() / (points - 1) + // ACR3 expects linear domain input, but when only choosing ACR3, the input is sRGB-encoded. + // So we linearize the input first. + val linearInput = inverseSrgb(x) + // Apply ACR3 in the linear domain + val linearOutput = getAcr3LinearValue(linearInput) + // Convert the linear output back to sRGB domain for the display output + val y = if (linearOutput <= 0.0031308f) { + 12.92f * linearOutput + } else { + 1.055f * Math.pow(linearOutput.toDouble(), 1.0 / 2.4).toFloat() - 0.055f + } + curve[i * 2] = x + curve[i * 2 + 1] = y.coerceIn(0f, 1f) + } + return curve + } + + private fun generateSrgbAcr3Curve(): FloatArray { + val points = 64 + val curve = FloatArray(points * 2) + for (i in 0 until points) { + val x = i.toFloat() / (points - 1) + val linearVal = getAcr3LinearValue(x) + val y = if (linearVal <= 0.0031308f) { + 12.92f * linearVal + } else { + 1.055f * Math.pow(linearVal.toDouble(), 1.0 / 2.4).toFloat() - 0.055f + } + curve[i * 2] = x + curve[i * 2 + 1] = y.coerceIn(0f, 1f) + } + return curve + } + + private fun generateRec709Acr3Curve(): FloatArray { + val points = 64 + val curve = FloatArray(points * 2) + for (i in 0 until points) { + val x = i.toFloat() / (points - 1) + val linearVal = getAcr3LinearValue(x) + val y = if (linearVal < 0.018f) { + 4.5f * linearVal + } else { + 1.099f * Math.pow(linearVal.toDouble(), 0.45).toFloat() - 0.099f + } + curve[i * 2] = x + curve[i * 2 + 1] = y.coerceIn(0f, 1f) + } + return curve + } + private fun applyToneMapSettings(builder: CaptureRequest.Builder, state: CameraState, isCapture: Boolean) { - val preferredTonemapMode = when { - isCapture && state.captureMode == CaptureMode.PHOTO && - availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_HIGH_QUALITY) -> { - CaptureRequest.TONEMAP_MODE_HIGH_QUALITY + when (state.tonemapMode) { + "FAST" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_FAST)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_FAST) + } + } + "HIGH_QUALITY" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_HIGH_QUALITY)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_HIGH_QUALITY) + } else if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_FAST)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_FAST) + } + } + "OFF" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) + val linearCurve = generateLinearCurve() + val curve = TonemapCurve(linearCurve, linearCurve, linearCurve) + builder.set(CaptureRequest.TONEMAP_CURVE, curve) + } + } + "SRGB" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) + val srgbCurve = generateSrgbCurve() + val curve = TonemapCurve(srgbCurve, srgbCurve, srgbCurve) + builder.set(CaptureRequest.TONEMAP_CURVE, curve) + } + } + "REC709" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) + val rec709Curve = generateRec709Curve() + val curve = TonemapCurve(rec709Curve, rec709Curve, rec709Curve) + builder.set(CaptureRequest.TONEMAP_CURVE, curve) + } + } + "ACR3" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) + val acr3Curve = generateAcr3Curve() + val curve = TonemapCurve(acr3Curve, acr3Curve, acr3Curve) + builder.set(CaptureRequest.TONEMAP_CURVE, curve) + } + } + "SRGB_ACR3" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) + val srgbAcr3Curve = generateSrgbAcr3Curve() + val curve = TonemapCurve(srgbAcr3Curve, srgbAcr3Curve, srgbAcr3Curve) + builder.set(CaptureRequest.TONEMAP_CURVE, curve) + } + } + "REC709_ACR3" -> { + if (availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)) { + builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) + val rec709Acr3Curve = generateRec709Acr3Curve() + val curve = TonemapCurve(rec709Acr3Curve, rec709Acr3Curve, rec709Acr3Curve) + builder.set(CaptureRequest.TONEMAP_CURVE, curve) + } + } + else -> { + val preferredTonemapMode = when { + isCapture && state.captureMode == CaptureMode.PHOTO && + availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_HIGH_QUALITY) -> { + CaptureRequest.TONEMAP_MODE_HIGH_QUALITY + } + availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_FAST) -> { + CaptureRequest.TONEMAP_MODE_FAST + } + else -> null + } + preferredTonemapMode?.let { builder.set(CaptureRequest.TONEMAP_MODE, it) } } - - availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_FAST) -> { - CaptureRequest.TONEMAP_MODE_FAST - } - - else -> null } - preferredTonemapMode?.let { builder.set(CaptureRequest.TONEMAP_MODE, it) } } /** @@ -2049,6 +2223,14 @@ class Camera2Controller(private val context: Context) { PLog.d(TAG, "RAW DRO mode: $resolvedMode") } + /** + * 设置色调映射模式 + */ + fun setTonemapMode(mode: String) { + _state.value = _state.value.copy(tonemapMode = mode) + PLog.d(TAG, "色调映射模式: $mode") + } + /** * 设置虚化模拟光圈大小 */ diff --git a/app/src/main/java/com/hinnka/mycamera/camera/CameraState.kt b/app/src/main/java/com/hinnka/mycamera/camera/CameraState.kt index 6081c2ba..30c023c5 100644 --- a/app/src/main/java/com/hinnka/mycamera/camera/CameraState.kt +++ b/app/src/main/java/com/hinnka/mycamera/camera/CameraState.kt @@ -300,6 +300,7 @@ data class CameraState( val rawMinShutterSpeedNs: Long = 0L, val useLivePhoto: Boolean = false, val droMode: String = "OFF", + val tonemapMode: String = "FAST", // 是否正在拍摄 Live Photo (用于 UI 动画) val isCapturingLivePhoto: Boolean = false, val applyUltraHDR: Boolean = true, 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 18d001c6..20b37648 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt @@ -121,6 +121,7 @@ data class UserPreferences( val backgroundImage: String = "camera_bg", // 背景图资源名或文件路径 val useGpuAcceleration: Boolean = DeviceUtil.defaultGpuAcceleration, // 多帧合成是否使用 GPU 加速 val droMode: String = "OFF", // DRO 模式 + val tonemapMode: String = "FAST", // 色调映射模式 val applyUltraHDR: Boolean = false, // 是否应用 Ultra HDR 策略 val colorSpace: ColorSpace = ColorSpace.SRGB, val logCurve: TransferCurve = TransferCurve.SRGB, @@ -237,6 +238,7 @@ class UserPreferencesRepository(private val context: Context) { private val BACKGROUND_IMAGE = stringPreferencesKey("background_image") 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 APPLY_ULTRA_HDR = booleanPreferencesKey("apply_ultra_hdr") private val COLOR_SPACE = stringPreferencesKey("color_space") private val LOG_CURVE = stringPreferencesKey("log_curve") @@ -362,6 +364,7 @@ class UserPreferencesRepository(private val context: Context) { backgroundImage = preferences[BACKGROUND_IMAGE] ?: "camera_bg", useGpuAcceleration = preferences[USE_GPU_ACCELERATION] ?: DeviceUtil.defaultGpuAcceleration, droMode = preferences[DRO_MODE] ?: if (preferences[RAW_DRO_ENABLED_KEY] == true) "DR100" else "OFF", + tonemapMode = preferences[TONEMAP_MODE] ?: "FAST", applyUltraHDR = preferences[APPLY_ULTRA_HDR] ?: false, colorSpace = ColorSpace.valueOf(preferences[COLOR_SPACE] ?: ColorSpace.SRGB.name), logCurve = TransferCurve.fromPersistedName(preferences[LOG_CURVE] ?: TransferCurve.SRGB.name), @@ -1097,6 +1100,15 @@ class UserPreferencesRepository(private val context: Context) { } } + /** + * 保存色调映射模式 + */ + suspend fun saveTonemapMode(mode: String) { + context.dataStore.edit { preferences -> + preferences[TONEMAP_MODE] = mode + } + } + /** * 保存是否应用 Ultra HDR 策略 */ 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 94fc861b..a9a9c3e9 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 @@ -208,6 +208,7 @@ fun SettingsScreen( val useLivePhoto by viewModel.useLivePhoto.collectAsState() val enableDevelopAnimation by viewModel.enableDevelopAnimation.collectAsState() val photoQuality by viewModel.photoQuality.collectAsState(initial = 95) + val tonemapMode by viewModel.tonemapMode.collectAsState() val useGpuAcceleration by viewModel.useGpuAcceleration.collectAsState() val useP010 by viewModel.useP010.collectAsState() val useHlg10 by viewModel.useHlg10.collectAsState() @@ -951,6 +952,28 @@ fun SettingsScreen( onClick = { baselinePickerTarget = BaselineColorCorrectionTarget.JPG } ) + HorizontalDivider( + color = Color.White.copy(alpha = 0.1f), + modifier = Modifier.padding(vertical = 12.dp) + ) + + QualityLevelSetting( + title = stringResource(R.string.settings_tonemap_mode), + description = stringResource(R.string.settings_tonemap_mode_description), + levels = listOf( + "FAST" to stringResource(R.string.settings_tonemap_mode_fast), + "HIGH_QUALITY" to stringResource(R.string.settings_tonemap_mode_high_quality), + "OFF" to stringResource(R.string.settings_tonemap_mode_off), + "SRGB" to "sRGB", + "REC709" to "Rec.709", + "ACR3" to stringResource(R.string.settings_tonemap_mode_acr3), + "SRGB_ACR3" to stringResource(R.string.settings_tonemap_mode_srgb_acr3), + "REC709_ACR3" to stringResource(R.string.settings_tonemap_mode_rec709_acr3) + ), + currentLevel = tonemapMode, + onLevelSelected = { viewModel.setTonemapMode(it) } + ) + if (state.isP010Supported) { 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 362725bc..683d4771 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt @@ -330,6 +330,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) val droMode: StateFlow = userPreferencesRepository.userPreferences .map { it.droMode } .stateIn(viewModelScope, SharingStarted.Eagerly, "OFF") + val tonemapMode: StateFlow = userPreferencesRepository.userPreferences + .map { it.tonemapMode } + .stateIn(viewModelScope, SharingStarted.Eagerly, "FAST") val applyUltraHDR: StateFlow = userPreferencesRepository.userPreferences .map { it.applyUltraHDR } .stateIn(viewModelScope, SharingStarted.Eagerly, false) @@ -543,6 +546,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) cameraController.setUseRaw(it.useRaw) cameraController.setRawMinShutterSpeedNs(it.rawMinShutterSpeedNs) cameraController.setDroMode(it.droMode) + cameraController.setTonemapMode(it.tonemapMode) if (cameraController.state.value.meteringMode != it.meteringMode) { cameraController.setMeteringMode(it.meteringMode) } @@ -682,6 +686,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) cameraController.setMultiFrameCount(prefs.multiFrameCount) cameraController.setUseLivePhoto(prefs.useLivePhoto && prefs.captureMode == CaptureMode.PHOTO) cameraController.setDroMode(prefs.droMode) + cameraController.setTonemapMode(prefs.tonemapMode) // 应用保存的虚拟光圈 if (prefs.defaultVirtualAperture > 0f) { @@ -3508,6 +3513,15 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) } } + /** + * 设置色调映射模式 + */ + fun setTonemapMode(mode: String) { + viewModelScope.launch { + userPreferencesRepository.saveTonemapMode(mode) + } + } + fun togglePhantomMode() { val newMode = !phantomMode.value viewModelScope.launch { diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 2fa9223b..72e40ba8 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -315,6 +315,11 @@ 手动微调锐化和降噪参数 照片质量 选择 JPEG 压缩的照片质量(越高画质越好,但文件体积也越大) + 色调映射 + 调整硬件色调映射(对比度/伽马)模式 + 快速 + 高质量 + 禁用 滤镜强度 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 06cbd57f..d03c8092 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -206,6 +206,11 @@ 手動微調銳化和降噪參數 相片品質 選擇 JPEG 壓縮的相片品質(越高畫質越好,但檔案體積也越大) + 色調映射 + 調整硬體色調映射(對比度/伽馬)模式 + 快速 + 高品質 + 停用 濾鏡強度 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0da1d548..129cf05a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -317,6 +317,14 @@ Fine-tune sharpening and noise reduction parameters Photo Quality Select the JPEG compression quality for photos (higher = better quality, larger file size) + Tone Mapping + Adjust the hardware tone mapping (contrast/gamma) mode + Fast + HQ + Off + ACR3 + ACR3 + sRGB + ACR3 + Rec.709 Background Change the app background image Custom...