fix: tonemap and natural light

This commit is contained in:
zhoubinjia committed 2026-06-26 19:26:09 +08:00
1 parent b2eaf933c4
commit 4a30b9b909
10 files changed
+27 -116

No files matched your search

@@ -52,19 +52,11 @@ private fun sanitizeTonemapMode(mode: String): String {
return when (mode) {
"FAST", "HIGH_QUALITY" -> "SYSTEM_DEFAULT"
"REC709" -> "SRGB"
"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,
@@ -169,7 +161,6 @@ data class UserPreferences(
val droMode: String = "OFF", // DRO 模式
val tonemapMode: String = "SYSTEM_DEFAULT", // 色调映射模式
val naturalLightEnabled: Boolean = false, // 是否启用自然光影
val naturalLightPreviousTonemapMode: String = "SYSTEM_DEFAULT", // 自然光影关闭时恢复的色调映射模式
val naturalLightWarningShown: Boolean = false, // 是否已提示自然光影兼容性风险
val fixTonemapPreview: Boolean = false, // 修复部分设备自定义色调映射预览异常
val fixTonemapCapture: Boolean = false, // 修复部分设备自定义色调映射拍摄异常
@@ -356,7 +347,6 @@ class UserPreferencesRepository(private val context: Context) {
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 NATURAL_LIGHT_WARNING_SHOWN = booleanPreferencesKey("natural_light_warning_shown")
private val FIX_TONEMAP_PREVIEW = booleanPreferencesKey("fix_tonemap_preview")
private val FIX_TONEMAP_CAPTURE = booleanPreferencesKey("fix_tonemap_capture")
@@ -529,11 +519,7 @@ 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"
),
naturalLightEnabled = preferences[NATURAL_LIGHT_ENABLED] ?: false,
naturalLightWarningShown = preferences[NATURAL_LIGHT_WARNING_SHOWN] ?: false,
fixTonemapPreview = preferences[FIX_TONEMAP_PREVIEW] ?: false,
fixTonemapCapture = preferences[FIX_TONEMAP_CAPTURE] ?: false,
@@ -1514,22 +1500,6 @@ class UserPreferencesRepository(private val context: Context) {
}
}
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
}
}
suspend fun saveNaturalLightWarningShown(shown: Boolean) {
context.dataStore.edit { preferences ->
preferences[NATURAL_LIGHT_WARNING_SHOWN] = shown
@@ -1673,7 +1673,7 @@ object GalleryManager {
previewBitmap = BitmapUtils.flipHorizontal(previewBitmap)
}
if (metadata.usesLinearPipelineToneMap()) {
if (metadata.usesNaturalLightToneMap()) {
val toneMappedPreview = photoProcessor.processCapturePreviewToneMap(previewBitmap, metadata)
if (toneMappedPreview !== previewBitmap && !previewBitmap.isRecycled) {
previewBitmap.recycle()
@@ -2489,7 +2489,7 @@ object GalleryManager {
}
var previewBitmap = result
if (metadata.usesLinearPipelineToneMap()) {
if (metadata.usesNaturalLightToneMap()) {
previewBitmap = photoProcessor.processCapturePreviewToneMap(result, metadata)
}
@@ -20,6 +20,8 @@ import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.log2
const val TONEMAP_MODE_NATURAL_LIGHT = "NATURAL_LIGHT"
/**
* 照片元数据
*
@@ -177,15 +179,12 @@ data class MediaMetadata(
val resolution: String
get() = "${width}x${height}"
fun usesLinearPipelineToneMap(): Boolean {
return when (tonemapMode) {
"LINEAR_PIPELINE", "RAW_PREVIEW", "SRGB_ACR3", "REC709_ACR3" -> true
else -> false
}
fun usesNaturalLightToneMap(): Boolean {
return tonemapMode == TONEMAP_MODE_NATURAL_LIGHT
}
fun shouldApplyNaturalLightDefaultChromaDenoise(): Boolean {
return usesLinearPipelineToneMap() && captureNoiseReductionLevel in LOW_HARDWARE_NOISE_REDUCTION_LEVELS
return usesNaturalLightToneMap() && captureNoiseReductionLevel in LOW_HARDWARE_NOISE_REDUCTION_LEVELS
}
/**
@@ -483,7 +483,7 @@ class PhotoProcessor(
input: Bitmap,
metadata: MediaMetadata,
): Bitmap = withContext(Dispatchers.IO) {
if (!metadata.usesLinearPipelineToneMap()) return@withContext input
if (!metadata.usesNaturalLightToneMap()) return@withContext input
if (shouldDecodeHlgInput(metadata)) return@withContext input
lutImageProcessor.applyLut(
bitmap = input,
@@ -491,8 +491,7 @@ class PhotoProcessor(
lutConfig = null,
colorRecipeParams = null,
linearInputToneMap = true,
naturalLightInputSrgb = metadata.tonemapMode == "SRGB_ACR3" ||
metadata.tonemapMode == "REC709_ACR3",
naturalLightInputSrgb = true,
linearInputExposureEv = metadata.rawExposureCompensation ?: 0f,
rawRenderingEngine = metadata.rawRenderingEngine,
rawToneMappingParameters = metadata.rawToneMappingParameters
@@ -174,9 +174,6 @@ class LutRenderer : GLSurfaceView.Renderer {
@Volatile
var rawPreviewWhitePointCorrection: Float = 0f
@Volatile
var rawPreviewLinearizeInput: Boolean = false
@Volatile
var rawPreviewRenderingEngine: RawRenderingEngine = RawRenderingEngine.AdobeCurve
@@ -847,10 +844,6 @@ class LutRenderer : GLSurfaceView.Renderer {
GLES30.glGetUniformLocation(program, "uWhitePoint"),
(1f + rawPreviewWhitePointCorrection).coerceAtLeast(blackPoint + 0.01f)
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(program, "uLinearizeInput"),
if (rawPreviewLinearizeInput) 1 else 0
)
drawRawPreviewQuad(program)
GlUtils.checkGlError("renderRawPreviewInputStage")
return true
@@ -3768,7 +3761,6 @@ class LutRenderer : GLSurfaceView.Renderer {
exposureCompensation: Float,
blackPointCorrection: Float,
whitePointCorrection: Float,
linearizeInput: Boolean,
renderingEngine: RawRenderingEngine,
toneMappingParameters: RawToneMappingParameters
) {
@@ -3776,7 +3768,6 @@ class LutRenderer : GLSurfaceView.Renderer {
rawPreviewExposureCompensation = exposureCompensation
rawPreviewBlackPointCorrection = blackPointCorrection
rawPreviewWhitePointCorrection = whitePointCorrection
rawPreviewLinearizeInput = linearizeInput
rawPreviewRenderingEngine = renderingEngine
rawPreviewToneMappingParameters = toneMappingParameters
}
@@ -13,7 +13,6 @@ internal object RawPreviewShaders {
uniform float uExposureEv;
uniform float uBlackPoint;
uniform float uWhitePoint;
uniform bool uLinearizeInput;
vec3 srgbToLinear(vec3 color) {
vec3 clampedColor = max(color, vec3(0.0));
@@ -29,9 +28,7 @@ internal object RawPreviewShaders {
void main() {
vec3 color = texture(uCameraTexture, vTexCoord).rgb;
if (uLinearizeInput) {
color = srgbToLinear(color);
}
color = srgbToLinear(color);
float blackPoint = clamp(uBlackPoint, 0.0, 0.99);
float whitePoint = max(uWhitePoint, blackPoint + 0.01);
color = max((color - vec3(blackPoint)) / (whitePoint - blackPoint), vec3(0.0));
@@ -200,7 +200,6 @@ class CameraGLSurfaceView @JvmOverloads constructor(
exposureCompensation: Float,
blackPointCorrection: Float,
whitePointCorrection: Float,
linearizeInput: Boolean,
renderingEngine: RawRenderingEngine,
toneMappingParameters: RawToneMappingParameters
) {
@@ -210,7 +209,6 @@ class CameraGLSurfaceView @JvmOverloads constructor(
exposureCompensation = exposureCompensation,
blackPointCorrection = blackPointCorrection,
whitePointCorrection = whitePointCorrection,
linearizeInput = linearizeInput,
renderingEngine = renderingEngine,
toneMappingParameters = toneMappingParameters
)
@@ -69,7 +69,6 @@ fun CameraPreviewGL(
videoLogProfile: VideoLogProfile = VideoLogProfile.OFF,
isHlgInput: Boolean = false,
naturalLightEnabled: Boolean = false,
fixTonemapPreview: Boolean = false,
rawExposureCompensation: Float = 0f,
rawBlackPointCorrection: Float = 0f,
rawWhitePointCorrection: Float = 0f,
@@ -276,7 +275,6 @@ fun CameraPreviewGL(
exposureCompensation = rawExposureCompensation,
blackPointCorrection = rawBlackPointCorrection,
whitePointCorrection = rawWhitePointCorrection,
linearizeInput = naturalLightEnabled || fixTonemapPreview,
renderingEngine = rawRenderingEngine,
toneMappingParameters = rawToneMappingParameters
)
@@ -208,7 +208,6 @@ fun CameraScreen(
val rawToneMappingParameters by viewModel.rawToneMappingParameters.collectAsState()
val naturalLightEnabled by viewModel.naturalLightEnabled.collectAsState()
val naturalLightWarningShown by viewModel.naturalLightWarningShown.collectAsState()
val fixTonemapPreview by viewModel.fixTonemapPreview.collectAsState()
val rawSpectralFilmStock by viewModel.rawSpectralFilmStock.collectAsState()
val rawSpectralFilmSelection by viewModel.rawSpectralFilmSelection.collectAsState()
val rawSpectralFilmPrint by viewModel.rawSpectralFilmPrint.collectAsState()
@@ -954,7 +953,6 @@ fun CameraScreen(
videoLogProfile = state.videoConfig.logProfile,
isHlgInput = if (hlgHardwareCompatibilityEnabled) state.isHLG else false,
naturalLightEnabled = naturalLightEnabled,
fixTonemapPreview = fixTonemapPreview,
rawExposureCompensation = rawExposureCompensation,
rawBlackPointCorrection = rawBlackPointCorrection,
rawWhitePointCorrection = rawWhitePointCorrection,
@@ -30,6 +30,7 @@ import com.hinnka.mycamera.frame.FramePreviewFactory
import com.hinnka.mycamera.gallery.GalleryManager
import com.hinnka.mycamera.gallery.MediaMetadata
import com.hinnka.mycamera.gallery.PhotoSavePath
import com.hinnka.mycamera.gallery.TONEMAP_MODE_NATURAL_LIGHT
import com.hinnka.mycamera.lut.BaselineColorCorrectionTarget
import com.hinnka.mycamera.lut.BakedLutExporter
import com.hinnka.mycamera.lut.LutConfig
@@ -101,7 +102,6 @@ data class MultipleExposureSessionState(
private const val TONEMAP_MODE_SYSTEM_DEFAULT = "SYSTEM_DEFAULT"
private const val TONEMAP_MODE_SRGB = "SRGB"
private const val TONEMAP_MODE_LINEAR_PIPELINE = "LINEAR_PIPELINE"
private fun sanitizeViewModelTonemapMode(mode: String): String {
return when (mode) {
@@ -665,34 +665,17 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
private fun metadataTonemapMode(prefs: UserPreferences?): String {
return if (prefs?.naturalLightEnabled == true) {
"SRGB_ACR3"
TONEMAP_MODE_NATURAL_LIGHT
} else {
sanitizeViewModelTonemapMode(prefs?.tonemapMode ?: TONEMAP_MODE_SYSTEM_DEFAULT)
}
}
private suspend fun saveTonemapModeWithFeatureGuards(mode: String) {
val resolvedMode = sanitizeViewModelTonemapMode(mode)
val prefs = userPreferencesRepository.userPreferences.first()
if (prefs.naturalLightEnabled) {
userPreferencesRepository.saveNaturalLightState(
enabled = true,
previousTonemapMode = resolvedMode,
tonemapMode = TONEMAP_MODE_SRGB
)
} else {
userPreferencesRepository.saveTonemapMode(resolvedMode)
}
}
private suspend fun clearNaturalLightTonemapIfNeeded(reason: String, prefs: UserPreferences? = null) {
private suspend fun disableNaturalLightIfNeeded(reason: String, prefs: UserPreferences? = null) {
val currentPrefs = prefs ?: userPreferencesRepository.userPreferences.first()
if (currentPrefs.naturalLightEnabled) {
PLog.d(TAG, "Disabling Natural Light tone map: $reason")
userPreferencesRepository.saveNaturalLightState(
enabled = false,
tonemapMode = currentPrefs.naturalLightPreviousTonemapMode
)
userPreferencesRepository.saveNaturalLightEnabled(false)
}
}
@@ -1356,7 +1339,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
(multipleExposureEnabled || shouldDisableNaturalLightForHdrComposition(it))
) {
viewModelScope.launch {
clearNaturalLightTonemapIfNeeded(
disableNaturalLightIfNeeded(
reason = "conflicting persisted camera feature state",
prefs = it
)
@@ -1395,14 +1378,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
if (currentCameraState.tonemapMode != effectiveTonemapMode) {
cameraController.setTonemapMode(effectiveTonemapMode)
}
val effectiveFixTonemapPreview = it.fixTonemapPreview && !it.naturalLightEnabled
if (currentCameraState.fixTonemapPreview != effectiveFixTonemapPreview) {
cameraController.setFixTonemapPreview(effectiveFixTonemapPreview)
}
val effectiveFixTonemapCapture = it.fixTonemapCapture && !it.naturalLightEnabled
if (currentCameraState.fixTonemapCapture != effectiveFixTonemapCapture) {
cameraController.setFixTonemapCapture(effectiveFixTonemapCapture)
}
cameraController.setFixTonemapPreview(it.fixTonemapPreview)
cameraController.setFixTonemapCapture(it.fixTonemapCapture)
if (cameraController.state.value.meteringMode != it.meteringMode) {
cameraController.setMeteringMode(it.meteringMode)
}
@@ -1577,8 +1554,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
cameraController.setMultiFrameCount(prefs.multiFrameCount)
cameraController.setUseLivePhoto(prefs.useLivePhoto && prefs.captureMode == CaptureMode.PHOTO)
cameraController.setTonemapMode(effectiveCameraTonemapMode(prefs))
cameraController.setFixTonemapPreview(prefs.fixTonemapPreview && !prefs.naturalLightEnabled)
cameraController.setFixTonemapCapture(prefs.fixTonemapCapture && !prefs.naturalLightEnabled)
cameraController.setFixTonemapPreview(prefs.fixTonemapPreview)
cameraController.setFixTonemapCapture(prefs.fixTonemapCapture)
// 应用保存的虚拟光圈
if (prefs.defaultVirtualAperture > 0f) {
@@ -2078,7 +2055,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
fun setUseMultipleExposure(enabled: Boolean) {
viewModelScope.launch {
if (enabled) {
clearNaturalLightTonemapIfNeeded("multiple exposure enabled")
disableNaturalLightIfNeeded("multiple exposure enabled")
}
applyCameraFeatureUpdate(
CameraFeatureUpdate(useMultipleExposure = SettingValue(enabled))
@@ -3476,7 +3453,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
if (enabled) {
val prefs = userPreferencesRepository.userPreferences.first()
if (shouldDisableNaturalLightForHdrComposition(prefs)) {
clearNaturalLightTonemapIfNeeded(
disableNaturalLightIfNeeded(
reason = "HDR composition enabled while RAW is off",
prefs = prefs
)
@@ -3560,7 +3537,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
if (!useRaw) {
val prefs = userPreferencesRepository.userPreferences.first()
if (prefs.useHdrComposition) {
clearNaturalLightTonemapIfNeeded(
disableNaturalLightIfNeeded(
reason = "RAW disabled while HDR composition is active",
prefs = prefs
)
@@ -5028,7 +5005,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
multipleExposureFrameCount = expectedFrameCount,
baselineTarget = BaselineColorCorrectionTarget.JPG,
).let { hdrMetadata ->
if (hdrMetadata.usesLinearPipelineToneMap()) {
if (hdrMetadata.usesNaturalLightToneMap()) {
PLog.d(TAG, "YUV HDR bracket stores SYSTEM_DEFAULT tonemap metadata")
hdrMetadata.copy(tonemapMode = "SYSTEM_DEFAULT")
} else {
@@ -5561,14 +5538,14 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
*/
fun setTonemapMode(mode: String) {
viewModelScope.launch {
saveTonemapModeWithFeatureGuards(mode)
userPreferencesRepository.saveTonemapMode(sanitizeViewModelTonemapMode(mode))
}
}
fun setNaturalLightToneMapEnabled(enabled: Boolean) {
viewModelScope.launch {
val prefs = userPreferencesRepository.userPreferences.first()
if (enabled) {
val prefs = userPreferencesRepository.userPreferences.first()
val shouldDisableMultipleExposure = prefs.useMultipleExposure
val shouldDisableHdrComposition = shouldDisableNaturalLightForHdrComposition(prefs)
@@ -5589,23 +5566,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
)
}
}
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
)
}
userPreferencesRepository.saveNaturalLightEnabled(enabled)
}
}