diff --git a/app/src/main/java/com/hinnka/mycamera/camera/IszLensConfig.kt b/app/src/main/java/com/hinnka/mycamera/camera/IszLensConfig.kt index 55f3420e..1118bd59 100644 --- a/app/src/main/java/com/hinnka/mycamera/camera/IszLensConfig.kt +++ b/app/src/main/java/com/hinnka/mycamera/camera/IszLensConfig.kt @@ -1,5 +1,7 @@ package com.hinnka.mycamera.camera +import com.hinnka.mycamera.raw.RawCfaCorrection +import com.hinnka.mycamera.raw.RawWhiteLevelCorrection import org.json.JSONArray import org.json.JSONObject import java.util.Locale @@ -152,3 +154,17 @@ data class RawBlackBorderCrop( val hasCrop: Boolean get() = leftPx > 0 || topPx > 0 || rightPx > 0 || bottomPx > 0 } + +/** + * RAW DNG 元数据校正参数,会以 ISZ 虚拟镜头 ID 为键持久化。 + * + * ISZ 镜头和其基础物理镜头共用同一枚传感器,但其输出的 RAW 元数据可能不同, + * 因此不能复用基础镜头的校正值。 + */ +data class IszRawDngMetadataCorrections( + val blackLevelMode: String = RawCfaCorrection.MODE_DEFAULT, + val customBlackLevel: Float = 0f, + val whiteLevelMode: String = RawWhiteLevelCorrection.MODE_DEFAULT, + val customWhiteLevel: Float = 0f, + val cfaCorrectionMode: String = RawCfaCorrection.MODE_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 87f063be..fd56ada1 100644 --- a/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt +++ b/app/src/main/java/com/hinnka/mycamera/data/UserPreferencesRepository.kt @@ -13,6 +13,7 @@ import androidx.datastore.preferences.preferencesDataStore import com.hinnka.mycamera.camera.AspectRatio import com.hinnka.mycamera.camera.CustomFocalLengthValue import com.hinnka.mycamera.camera.IszLensConfig +import com.hinnka.mycamera.camera.IszRawDngMetadataCorrections import com.hinnka.mycamera.camera.MultiFrameConfig import com.hinnka.mycamera.camera.MeteringMode import com.hinnka.mycamera.camera.VendorCaptureSettings @@ -1066,6 +1067,62 @@ class UserPreferencesRepository(private val context: Context) { } } + /** + * Stores the DNG metadata corrections selected while creating an ISZ lens. + * All values are committed together so they are always associated with the same + * virtual camera ID. + */ + suspend fun saveRawDngMetadataCorrections( + cameraId: String, + corrections: IszRawDngMetadataCorrections + ) { + if (cameraId.isBlank()) return + + context.dataStore.edit { preferences -> + val blackLevelModes = parseMapString(preferences[RAW_BLACK_LEVEL_MODES_KEY]).toMutableMap() + val customBlackLevels = parseMapFloat(preferences[RAW_CUSTOM_BLACK_LEVELS_KEY]).toMutableMap() + val whiteLevelModes = parseMapString(preferences[RAW_WHITE_LEVEL_MODES_KEY]).toMutableMap() + val customWhiteLevels = parseMapFloat(preferences[RAW_CUSTOM_WHITE_LEVELS_KEY]).toMutableMap() + val cfaCorrectionModes = parseMapString(preferences[RAW_CFA_CORRECTION_MODES_KEY]).toMutableMap() + + blackLevelModes[cameraId] = corrections.blackLevelMode + customBlackLevels[cameraId] = corrections.customBlackLevel + whiteLevelModes[cameraId] = corrections.whiteLevelMode + customWhiteLevels[cameraId] = corrections.customWhiteLevel + cfaCorrectionModes[cameraId] = corrections.cfaCorrectionMode + + preferences[RAW_BLACK_LEVEL_MODES_KEY] = serializeMapString(blackLevelModes) + preferences[RAW_CUSTOM_BLACK_LEVELS_KEY] = serializeMapFloat(customBlackLevels) + preferences[RAW_WHITE_LEVEL_MODES_KEY] = serializeMapString(whiteLevelModes) + preferences[RAW_CUSTOM_WHITE_LEVELS_KEY] = serializeMapFloat(customWhiteLevels) + preferences[RAW_CFA_CORRECTION_MODES_KEY] = serializeMapString(cfaCorrectionModes) + } + } + + suspend fun clearRawDngMetadataCorrections(cameraId: String) { + if (cameraId.isBlank()) return + + context.dataStore.edit { preferences -> + val blackLevelModes = parseMapString(preferences[RAW_BLACK_LEVEL_MODES_KEY]).toMutableMap() + val customBlackLevels = parseMapFloat(preferences[RAW_CUSTOM_BLACK_LEVELS_KEY]).toMutableMap() + val whiteLevelModes = parseMapString(preferences[RAW_WHITE_LEVEL_MODES_KEY]).toMutableMap() + val customWhiteLevels = parseMapFloat(preferences[RAW_CUSTOM_WHITE_LEVELS_KEY]).toMutableMap() + val cfaCorrectionModes = parseMapString(preferences[RAW_CFA_CORRECTION_MODES_KEY]).toMutableMap() + + blackLevelModes.remove(cameraId) + customBlackLevels.remove(cameraId) + whiteLevelModes.remove(cameraId) + customWhiteLevels.remove(cameraId) + cfaCorrectionModes.remove(cameraId) + + preferences[RAW_BLACK_LEVEL_MODES_KEY] = serializeMapString(blackLevelModes) + preferences[RAW_CUSTOM_BLACK_LEVELS_KEY] = serializeMapFloat(customBlackLevels) + preferences[RAW_WHITE_LEVEL_MODES_KEY] = serializeMapString(whiteLevelModes) + preferences[RAW_CUSTOM_WHITE_LEVELS_KEY] = serializeMapFloat(customWhiteLevels) + preferences[RAW_CFA_CORRECTION_MODES_KEY] = serializeMapString(cfaCorrectionModes) + } + } + suspend fun saveRawCustomBlackLevel(cameraId: String, value: Float) { context.dataStore.edit { preferences -> val current = parseMapFloat(preferences[RAW_CUSTOM_BLACK_LEVELS_KEY]) diff --git a/app/src/main/java/com/hinnka/mycamera/ui/components/RawEditPanel.kt b/app/src/main/java/com/hinnka/mycamera/ui/components/RawEditPanel.kt index c8ad8099..0ec1fd73 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/components/RawEditPanel.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/components/RawEditPanel.kt @@ -252,69 +252,17 @@ fun RawEditPanel( onValueChangeFinished = onAdjustmentEnd ) if (showDngMetadataControls) { - RawChoiceSetting( - title = stringResource(R.string.settings_raw_black_level_correction), - description = stringResource(R.string.settings_raw_black_level_correction_description), - levels = listOf( - RawCfaCorrection.MODE_DEFAULT to stringResource(R.string.settings_black_level_default), - "0" to "0", - "16" to "16", - "64" to "64", - "256" to "256", - "512" to "512", - "Custom" to stringResource(R.string.settings_black_level_custom) - ), - currentLevel = rawBlackLevelMode, - onLevelSelected = onRawBlackLevelModeChange - ) - if (rawBlackLevelMode == "Custom") { - RawNumberInputSetting( - title = stringResource(R.string.settings_black_level_custom), - value = rawCustomBlackLevel, - onValueChange = onRawCustomBlackLevelChange - ) - } - RawChoiceSetting( - title = stringResource(R.string.settings_raw_white_level_correction), - description = stringResource(R.string.settings_raw_white_level_correction_description), - levels = listOf( - RawWhiteLevelCorrection.MODE_DEFAULT to stringResource(R.string.settings_white_level_default), - RawWhiteLevelCorrection.MODE_RAW10 to stringResource(R.string.settings_white_level_raw10), - RawWhiteLevelCorrection.MODE_RAW12 to stringResource(R.string.settings_white_level_raw12), - RawWhiteLevelCorrection.MODE_RAW14 to stringResource(R.string.settings_white_level_raw14), - RawWhiteLevelCorrection.MODE_RAW_SENSOR to stringResource(R.string.settings_white_level_raw_sensor), - RawWhiteLevelCorrection.MODE_CUSTOM to stringResource(R.string.settings_white_level_custom) - ), - currentLevel = rawWhiteLevelMode, - onLevelSelected = onRawWhiteLevelModeChange - ) - if (rawWhiteLevelMode == RawWhiteLevelCorrection.MODE_CUSTOM) { - RawNumberInputSetting( - title = stringResource(R.string.settings_white_level_custom), - value = rawCustomWhiteLevel, - onValueChange = onRawCustomWhiteLevelChange - ) - } - RawChoiceSetting( - title = stringResource(R.string.settings_raw_cfa_correction), - description = stringResource(R.string.settings_raw_cfa_correction_description), - levels = listOf( - RawCfaCorrection.MODE_DEFAULT to stringResource(R.string.settings_cfa_correction_default), - RawCfaCorrection.MODE_2X2_RGGB to stringResource(R.string.settings_cfa_correction_2x2_rggb), - RawCfaCorrection.MODE_2X2_GRBG to stringResource(R.string.settings_cfa_correction_2x2_grbg), - RawCfaCorrection.MODE_2X2_GBRG to stringResource(R.string.settings_cfa_correction_2x2_gbrg), - RawCfaCorrection.MODE_2X2_BGGR to stringResource(R.string.settings_cfa_correction_2x2_bggr), - RawCfaCorrection.MODE_4X4_RGGB to stringResource(R.string.settings_cfa_correction_4x4_rggb), - RawCfaCorrection.MODE_4X4_GRBG to stringResource(R.string.settings_cfa_correction_4x4_grbg), - RawCfaCorrection.MODE_4X4_GBRG to stringResource(R.string.settings_cfa_correction_4x4_gbrg), - RawCfaCorrection.MODE_4X4_BGGR to stringResource(R.string.settings_cfa_correction_4x4_bggr), - RawCfaCorrection.MODE_8X8_RGGB to stringResource(R.string.settings_cfa_correction_8x8_rggb), - RawCfaCorrection.MODE_8X8_GRBG to stringResource(R.string.settings_cfa_correction_8x8_grbg), - RawCfaCorrection.MODE_8X8_GBRG to stringResource(R.string.settings_cfa_correction_8x8_gbrg), - RawCfaCorrection.MODE_8X8_BGGR to stringResource(R.string.settings_cfa_correction_8x8_bggr) - ), - currentLevel = rawCfaCorrectionMode, - onLevelSelected = onRawCfaCorrectionModeChange + RawDngMetadataCorrectionSettings( + rawBlackLevelMode = rawBlackLevelMode, + rawCustomBlackLevel = rawCustomBlackLevel, + rawWhiteLevelMode = rawWhiteLevelMode, + rawCustomWhiteLevel = rawCustomWhiteLevel, + rawCfaCorrectionMode = rawCfaCorrectionMode, + onRawBlackLevelModeChange = onRawBlackLevelModeChange, + onRawCustomBlackLevelChange = onRawCustomBlackLevelChange, + onRawWhiteLevelModeChange = onRawWhiteLevelModeChange, + onRawCustomWhiteLevelChange = onRawCustomWhiteLevelChange, + onRawCfaCorrectionModeChange = onRawCfaCorrectionModeChange ) } } @@ -331,6 +279,89 @@ fun RawEditPanel( } } +/** Shared RAW DNG metadata correction controls for a lens-specific configuration. */ +@Composable +fun RawDngMetadataCorrectionSettings( + rawBlackLevelMode: String, + rawCustomBlackLevel: Float, + rawWhiteLevelMode: String, + rawCustomWhiteLevel: Float, + rawCfaCorrectionMode: String, + onRawBlackLevelModeChange: (String) -> Unit, + onRawCustomBlackLevelChange: (Float) -> Unit, + onRawWhiteLevelModeChange: (String) -> Unit, + onRawCustomWhiteLevelChange: (Float) -> Unit, + onRawCfaCorrectionModeChange: (String) -> Unit, + modifier: Modifier = Modifier +) { + Column(modifier = modifier.fillMaxWidth()) { + RawChoiceSetting( + title = stringResource(R.string.settings_raw_black_level_correction), + description = stringResource(R.string.settings_raw_black_level_correction_description), + levels = listOf( + RawCfaCorrection.MODE_DEFAULT to stringResource(R.string.settings_black_level_default), + "0" to "0", + "16" to "16", + "64" to "64", + "256" to "256", + "512" to "512", + "Custom" to stringResource(R.string.settings_black_level_custom) + ), + currentLevel = rawBlackLevelMode, + onLevelSelected = onRawBlackLevelModeChange + ) + if (rawBlackLevelMode == "Custom") { + RawNumberInputSetting( + title = stringResource(R.string.settings_black_level_custom), + value = rawCustomBlackLevel, + onValueChange = onRawCustomBlackLevelChange + ) + } + RawChoiceSetting( + title = stringResource(R.string.settings_raw_white_level_correction), + description = stringResource(R.string.settings_raw_white_level_correction_description), + levels = listOf( + RawWhiteLevelCorrection.MODE_DEFAULT to stringResource(R.string.settings_white_level_default), + RawWhiteLevelCorrection.MODE_RAW10 to stringResource(R.string.settings_white_level_raw10), + RawWhiteLevelCorrection.MODE_RAW12 to stringResource(R.string.settings_white_level_raw12), + RawWhiteLevelCorrection.MODE_RAW14 to stringResource(R.string.settings_white_level_raw14), + RawWhiteLevelCorrection.MODE_RAW_SENSOR to stringResource(R.string.settings_white_level_raw_sensor), + RawWhiteLevelCorrection.MODE_CUSTOM to stringResource(R.string.settings_white_level_custom) + ), + currentLevel = rawWhiteLevelMode, + onLevelSelected = onRawWhiteLevelModeChange + ) + if (rawWhiteLevelMode == RawWhiteLevelCorrection.MODE_CUSTOM) { + RawNumberInputSetting( + title = stringResource(R.string.settings_white_level_custom), + value = rawCustomWhiteLevel, + onValueChange = onRawCustomWhiteLevelChange + ) + } + RawChoiceSetting( + title = stringResource(R.string.settings_raw_cfa_correction), + description = stringResource(R.string.settings_raw_cfa_correction_description), + levels = listOf( + RawCfaCorrection.MODE_DEFAULT to stringResource(R.string.settings_cfa_correction_default), + RawCfaCorrection.MODE_2X2_RGGB to stringResource(R.string.settings_cfa_correction_2x2_rggb), + RawCfaCorrection.MODE_2X2_GRBG to stringResource(R.string.settings_cfa_correction_2x2_grbg), + RawCfaCorrection.MODE_2X2_GBRG to stringResource(R.string.settings_cfa_correction_2x2_gbrg), + RawCfaCorrection.MODE_2X2_BGGR to stringResource(R.string.settings_cfa_correction_2x2_bggr), + RawCfaCorrection.MODE_4X4_RGGB to stringResource(R.string.settings_cfa_correction_4x4_rggb), + RawCfaCorrection.MODE_4X4_GRBG to stringResource(R.string.settings_cfa_correction_4x4_grbg), + RawCfaCorrection.MODE_4X4_GBRG to stringResource(R.string.settings_cfa_correction_4x4_gbrg), + RawCfaCorrection.MODE_4X4_BGGR to stringResource(R.string.settings_cfa_correction_4x4_bggr), + RawCfaCorrection.MODE_8X8_RGGB to stringResource(R.string.settings_cfa_correction_8x8_rggb), + RawCfaCorrection.MODE_8X8_GRBG to stringResource(R.string.settings_cfa_correction_8x8_grbg), + RawCfaCorrection.MODE_8X8_GBRG to stringResource(R.string.settings_cfa_correction_8x8_gbrg), + RawCfaCorrection.MODE_8X8_BGGR to stringResource(R.string.settings_cfa_correction_8x8_bggr) + ), + currentLevel = rawCfaCorrectionMode, + onLevelSelected = onRawCfaCorrectionModeChange + ) + } +} + @Composable private fun RawChoiceSetting( title: String, 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 c9515915..227b5162 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 @@ -100,6 +100,7 @@ import com.hinnka.mycamera.camera.AspectRatio import com.hinnka.mycamera.camera.CameraInfo import com.hinnka.mycamera.camera.CustomFocalLengthValue import com.hinnka.mycamera.camera.IszLensConfig +import com.hinnka.mycamera.camera.IszRawDngMetadataCorrections import com.hinnka.mycamera.camera.LensType import com.hinnka.mycamera.camera.MultiFrameConfig import com.hinnka.mycamera.camera.RawBlackBorderCrop @@ -127,6 +128,7 @@ import com.hinnka.mycamera.ui.components.SliderSettingItem import com.hinnka.mycamera.ui.components.LutSelector import com.hinnka.mycamera.ui.components.RawEditPanel import com.hinnka.mycamera.ui.components.RawEditPanelContentMode +import com.hinnka.mycamera.ui.components.RawDngMetadataCorrectionSettings import com.hinnka.mycamera.ui.components.rawDcpLensOptions import com.hinnka.mycamera.ui.components.rememberBackgroundPainter import com.hinnka.mycamera.update.AppUpdateManager @@ -2391,12 +2393,13 @@ fun SettingsScreen( availableCameras = state.availableCameras, iszLensConfigs = iszLensConfigs, vendorCaptureSettingsByLens = vendorCaptureSettingsByLens, - onAddLens = { baseCameraId, iszZoomRatio, isMacro, rawBlackBorderCrop, settings -> + onAddLens = { baseCameraId, iszZoomRatio, isMacro, rawBlackBorderCrop, rawDngMetadataCorrections, settings -> viewModel.addIszLensConfig( baseCameraId, iszZoomRatio, isMacro, rawBlackBorderCrop, + rawDngMetadataCorrections, settings ) showAddIszLensDialog = false @@ -3499,7 +3502,7 @@ private fun AddIszLensDialog( availableCameras: List, iszLensConfigs: List, vendorCaptureSettingsByLens: VendorCaptureSettingsByLens, - onAddLens: (String, Float, Boolean, RawBlackBorderCrop, VendorCaptureSettings) -> Unit, + onAddLens: (String, Float, Boolean, RawBlackBorderCrop, IszRawDngMetadataCorrections, VendorCaptureSettings) -> Unit, onRemoveLens: (IszLensConfig) -> Unit, onDismiss: () -> Unit ) { @@ -3517,6 +3520,11 @@ private fun AddIszLensDialog( var rawBlackBorderCropTopText by remember { mutableStateOf("0") } var rawBlackBorderCropRightText by remember { mutableStateOf("0") } var rawBlackBorderCropBottomText by remember { mutableStateOf("0") } + var rawBlackLevelMode by remember { mutableStateOf(RawCfaCorrection.MODE_DEFAULT) } + var rawCustomBlackLevel by remember { mutableStateOf(0f) } + var rawWhiteLevelMode by remember { mutableStateOf(RawWhiteLevelCorrection.MODE_DEFAULT) } + var rawCustomWhiteLevel by remember { mutableStateOf(0f) } + var rawCfaCorrectionMode by remember { mutableStateOf(RawCfaCorrection.MODE_DEFAULT) } var settings by remember { mutableStateOf( VendorCaptureSettings(emptyMap()) @@ -3553,6 +3561,13 @@ private fun AddIszLensDialog( bottomPx = rawBlackBorderCropBottomText.toIntOrNull() ?: 0 ) ) + val rawDngMetadataCorrections = IszRawDngMetadataCorrections( + blackLevelMode = rawBlackLevelMode, + customBlackLevel = rawCustomBlackLevel, + whiteLevelMode = rawWhiteLevelMode, + customWhiteLevel = rawCustomWhiteLevel, + cfaCorrectionMode = rawCfaCorrectionMode + ) AlertDialog( onDismissRequest = onDismiss, @@ -3669,6 +3684,24 @@ private fun AddIszLensDialog( modifier = Modifier.padding(vertical = 12.dp) ) + RawDngMetadataCorrectionSettings( + rawBlackLevelMode = rawBlackLevelMode, + rawCustomBlackLevel = rawCustomBlackLevel, + rawWhiteLevelMode = rawWhiteLevelMode, + rawCustomWhiteLevel = rawCustomWhiteLevel, + rawCfaCorrectionMode = rawCfaCorrectionMode, + onRawBlackLevelModeChange = { rawBlackLevelMode = it }, + onRawCustomBlackLevelChange = { rawCustomBlackLevel = it }, + onRawWhiteLevelModeChange = { rawWhiteLevelMode = it }, + onRawCustomWhiteLevelChange = { rawCustomWhiteLevel = it }, + onRawCfaCorrectionModeChange = { rawCfaCorrectionMode = it } + ) + + HorizontalDivider( + color = Color.White.copy(alpha = 0.1f), + modifier = Modifier.padding(vertical = 12.dp) + ) + VendorCaptureSettingsPanel( currentLensId = virtualLensId, currentLensName = virtualLensName, @@ -3740,6 +3773,7 @@ private fun AddIszLensDialog( selectedIszZoomRatio, isMacroLens, rawBlackBorderCrop, + rawDngMetadataCorrections, settings ) }, 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 f817ef45..fd990425 100644 --- a/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt +++ b/app/src/main/java/com/hinnka/mycamera/viewmodel/CameraViewModel.kt @@ -4393,6 +4393,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) iszZoomRatio: Float, isMacro: Boolean, rawBlackBorderCrop: RawBlackBorderCrop, + rawDngMetadataCorrections: IszRawDngMetadataCorrections, settings: VendorCaptureSettings ) { viewModelScope.launch { @@ -4412,6 +4413,10 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) .distinctBy { it.virtualCameraId } userPreferencesRepository.saveIszLensConfigs(updatedConfigs) userPreferencesRepository.saveVendorCaptureSettingsForLens(config.virtualCameraId, settings) + userPreferencesRepository.saveRawDngMetadataCorrections( + config.virtualCameraId, + rawDngMetadataCorrections + ) cameraController.refreshCameraList() } } @@ -4426,6 +4431,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application) config.virtualCameraId, VendorCaptureSettings.Empty ) + userPreferencesRepository.clearRawDngMetadataCorrections(config.virtualCameraId) cameraController.refreshCameraList() } }