feat: add tone map mode selection and custom curves
This commit is contained in:
1 parent
482bcbe6be
commit
d9475929ec
8 files changed
+261
-11
No files matched your search
@@ -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")
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置虚化模拟光圈大小
|
||||
*/
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 策略
|
||||
*/
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -330,6 +330,9 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
val droMode: StateFlow<String> = userPreferencesRepository.userPreferences
|
||||
.map { it.droMode }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, "OFF")
|
||||
val tonemapMode: StateFlow<String> = userPreferencesRepository.userPreferences
|
||||
.map { it.tonemapMode }
|
||||
.stateIn(viewModelScope, SharingStarted.Eagerly, "FAST")
|
||||
val applyUltraHDR: StateFlow<Boolean> = 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 {
|
||||
|
||||
@@ -315,6 +315,11 @@
|
||||
<string name="settings_detail_enhancement_description">手动微调锐化和降噪参数</string>
|
||||
<string name="settings_photo_quality">照片质量</string>
|
||||
<string name="settings_photo_quality_description">选择 JPEG 压缩的照片质量(越高画质越好,但文件体积也越大)</string>
|
||||
<string name="settings_tonemap_mode">色调映射</string>
|
||||
<string name="settings_tonemap_mode_description">调整硬件色调映射(对比度/伽马)模式</string>
|
||||
<string name="settings_tonemap_mode_fast">快速</string>
|
||||
<string name="settings_tonemap_mode_high_quality">高质量</string>
|
||||
<string name="settings_tonemap_mode_off">禁用</string>
|
||||
|
||||
<!-- LUT -->
|
||||
<string name="filter_intensity">滤镜强度</string>
|
||||
|
||||
@@ -206,6 +206,11 @@
|
||||
<string name="settings_detail_enhancement_description">手動微調銳化和降噪參數</string>
|
||||
<string name="settings_photo_quality">相片品質</string>
|
||||
<string name="settings_photo_quality_description">選擇 JPEG 壓縮的相片品質(越高畫質越好,但檔案體積也越大)</string>
|
||||
<string name="settings_tonemap_mode">色調映射</string>
|
||||
<string name="settings_tonemap_mode_description">調整硬體色調映射(對比度/伽馬)模式</string>
|
||||
<string name="settings_tonemap_mode_fast">快速</string>
|
||||
<string name="settings_tonemap_mode_high_quality">高品質</string>
|
||||
<string name="settings_tonemap_mode_off">停用</string>
|
||||
|
||||
<!-- LUT -->
|
||||
<string name="filter_intensity">濾鏡強度</string>
|
||||
|
||||
@@ -317,6 +317,14 @@
|
||||
<string name="settings_detail_enhancement_description">Fine-tune sharpening and noise reduction parameters</string>
|
||||
<string name="settings_photo_quality">Photo Quality</string>
|
||||
<string name="settings_photo_quality_description">Select the JPEG compression quality for photos (higher = better quality, larger file size)</string>
|
||||
<string name="settings_tonemap_mode">Tone Mapping</string>
|
||||
<string name="settings_tonemap_mode_description">Adjust the hardware tone mapping (contrast/gamma) mode</string>
|
||||
<string name="settings_tonemap_mode_fast">Fast</string>
|
||||
<string name="settings_tonemap_mode_high_quality">HQ</string>
|
||||
<string name="settings_tonemap_mode_off">Off</string>
|
||||
<string name="settings_tonemap_mode_acr3">ACR3</string>
|
||||
<string name="settings_tonemap_mode_srgb_acr3">ACR3 + sRGB</string>
|
||||
<string name="settings_tonemap_mode_rec709_acr3">ACR3 + Rec.709</string>
|
||||
<string name="settings_background">Background</string>
|
||||
<string name="settings_background_description">Change the app background image</string>
|
||||
<string name="settings_custom_background">Custom...</string>
|
||||
|
||||
Reference in new issue
Block a user