feat: support resetting AI denoise and improve result persistence
This commit is contained in:
1 parent
b3770ea3d3
commit
b52075093e
4 files changed
+181
-22
No files matched your search
@@ -73,6 +73,7 @@ object GalleryManager {
|
||||
private const val VIDEO_FILE = "video.mp4"
|
||||
private const val DNG_FILE = "original.dng"
|
||||
private const val METADATA_FILE = "metadata.json"
|
||||
private const val AI_DENOISE_FILE = "ai_denoise.jpg"
|
||||
private const val THUMBNAIL_FILE = "thumbnail.jpg"
|
||||
private const val BOKEH_FILE = "bokeh.jpg"
|
||||
private const val DETAIL_HDR_FILE = "detail_hdr.jpg"
|
||||
@@ -184,6 +185,10 @@ object GalleryManager {
|
||||
return File(getPhotoDir(context, photoId), METADATA_FILE)
|
||||
}
|
||||
|
||||
fun getAiDenoiseFile(context: Context, photoId: String): File {
|
||||
return File(getPhotoDir(context, photoId), AI_DENOISE_FILE)
|
||||
}
|
||||
|
||||
fun getDepthFile(context: Context, photoId: String): File {
|
||||
return File(getPhotoDir(context, photoId), "depthmap.png")
|
||||
}
|
||||
@@ -375,6 +380,7 @@ object GalleryManager {
|
||||
|
||||
private fun canReuseEmbeddedGainmap(metadata: MediaMetadata): Boolean {
|
||||
return metadata.manualHdrEffectEnabled &&
|
||||
!metadata.hasAiDenoisedBase &&
|
||||
metadata.hasEmbeddedGainmap &&
|
||||
HdrGainmapStrength.coerce(metadata.hdrEffectStrength) == HdrGainmapStrength.DEFAULT &&
|
||||
metadata.lutId == null &&
|
||||
@@ -571,7 +577,12 @@ object GalleryManager {
|
||||
PLog.d(TAG, "gainmapProducer.build took ${gainmapElapsed}ms, enabled=${gainmapResult != null}")
|
||||
|
||||
// 读取照片
|
||||
val processedBitmap = ultraHdrSource?.sdrBase ?: bitmap?.let {
|
||||
val processedBitmap = (ultraHdrSource?.sdrBase ?: if (metadata.hasAiDenoisedBase) {
|
||||
photoProcessor.process(
|
||||
context, id, metadata,
|
||||
sharpeningValue, noiseReductionValue, chromaNoiseReductionValue
|
||||
)
|
||||
} else bitmap?.let {
|
||||
photoProcessor.processBitmap(
|
||||
context, id, bitmap, metadata,
|
||||
sharpeningValue, noiseReductionValue, chromaNoiseReductionValue,
|
||||
@@ -580,7 +591,7 @@ object GalleryManager {
|
||||
} ?: photoProcessor.process(
|
||||
context, id, metadata,
|
||||
sharpeningValue, noiseReductionValue, chromaNoiseReductionValue
|
||||
) ?: return@withContext false
|
||||
)) ?: return@withContext false
|
||||
|
||||
PLog.d(
|
||||
TAG,
|
||||
|
||||
@@ -195,11 +195,12 @@ class PhotoProcessor(
|
||||
}
|
||||
}
|
||||
|
||||
// If source was generated from DNG/YUV, and we have an AI denoised base, replace the sdrBase
|
||||
// If source was generated from DNG/YUV, and we have an AI denoised base, replace the sdrBase.
|
||||
// The AI base is persisted in ai_denoise.jpg so exports and HDR gainmaps never rerun the slow model.
|
||||
if (source != null && metadata.hasAiDenoisedBase) {
|
||||
val photoFile = GalleryManager.getPhotoFile(context, photoId)
|
||||
if (photoFile.exists()) {
|
||||
val bitmap = BitmapFactory.decodeFile(photoFile.absolutePath)
|
||||
val aiFile = GalleryManager.getAiDenoiseFile(context, photoId)
|
||||
if (aiFile.exists()) {
|
||||
val bitmap = BitmapFactory.decodeFile(aiFile.absolutePath)
|
||||
if (bitmap != null) {
|
||||
val finalSharpening = metadata.sharpening ?: (if (metadata.isImported) 0f else sharpening)
|
||||
val finalNoiseReduction = metadata.noiseReduction ?: (if (metadata.isImported) 0f else noiseReduction)
|
||||
@@ -227,9 +228,13 @@ class PhotoProcessor(
|
||||
}
|
||||
|
||||
|
||||
val photoFile = GalleryManager.getPhotoFile(context, photoId)
|
||||
if (photoFile.exists()) {
|
||||
val bitmap = BitmapFactory.decodeFile(photoFile.absolutePath) ?: return null
|
||||
val fallbackFile = if (metadata.hasAiDenoisedBase) {
|
||||
GalleryManager.getAiDenoiseFile(context, photoId)
|
||||
} else {
|
||||
GalleryManager.getPhotoFile(context, photoId)
|
||||
}
|
||||
if (fallbackFile.exists()) {
|
||||
val bitmap = BitmapFactory.decodeFile(fallbackFile.absolutePath) ?: return null
|
||||
val sdrBitmap = processBitmap(
|
||||
context = context,
|
||||
photoId = photoId,
|
||||
@@ -348,8 +353,9 @@ class PhotoProcessor(
|
||||
val photoFile = GalleryManager.getPhotoFile(context, photoId)
|
||||
|
||||
if (metadata.hasAiDenoisedBase) {
|
||||
if (photoFile.exists()) {
|
||||
val bitmap = GalleryManager.loadOriginalBitmap(context, photoId) ?: return null
|
||||
val aiFile = GalleryManager.getAiDenoiseFile(context, photoId)
|
||||
if (aiFile.exists()) {
|
||||
val bitmap = GalleryManager.loadBitmap(context, android.net.Uri.fromFile(aiFile)) ?: return null
|
||||
return processBitmap(
|
||||
context,
|
||||
photoId,
|
||||
|
||||
@@ -748,14 +748,34 @@ fun PhotoEditScreen(
|
||||
} else if (editTab == 1) {
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
var isDnCNNDenoising by remember { mutableStateOf(false) }
|
||||
var isDnCNNResetting by remember { mutableStateOf(false) }
|
||||
var dnCNNProgress by remember { mutableFloatStateOf(0f) }
|
||||
val aiDenoiseMetadataEnabled =
|
||||
(viewModel.currentMediaMetadata ?: currentPhoto.metadata)?.hasAiDenoisedBase == true
|
||||
var isAiDenoiseEnabled by remember(currentPhoto.id) {
|
||||
mutableStateOf(aiDenoiseMetadataEnabled)
|
||||
}
|
||||
|
||||
LaunchedEffect(
|
||||
currentPhoto.id,
|
||||
aiDenoiseMetadataEnabled,
|
||||
isDnCNNDenoising,
|
||||
isDnCNNResetting
|
||||
) {
|
||||
if (!isDnCNNDenoising && !isDnCNNResetting) {
|
||||
isAiDenoiseEnabled = aiDenoiseMetadataEnabled
|
||||
}
|
||||
}
|
||||
|
||||
SwitchSettingItem(
|
||||
title = stringResource(R.string.ai_denoise_title),
|
||||
description = if (isDnCNNDenoising) stringResource(R.string.ai_denoise_processing, dnCNNProgress * 100) else stringResource(R.string.ai_denoise_description),
|
||||
checked = isDnCNNDenoising,
|
||||
checked = isAiDenoiseEnabled || isDnCNNDenoising,
|
||||
onCheckedChange = { checked ->
|
||||
if (checked && !isDnCNNDenoising) {
|
||||
if (isDnCNNDenoising || isDnCNNResetting) {
|
||||
return@SwitchSettingItem
|
||||
}
|
||||
if (checked && !isAiDenoiseEnabled) {
|
||||
isDnCNNDenoising = true
|
||||
dnCNNProgress = 0f
|
||||
viewModel.applyDnCNNDenoise(
|
||||
@@ -764,8 +784,24 @@ fun PhotoEditScreen(
|
||||
onComplete = { success ->
|
||||
isDnCNNDenoising = false
|
||||
if (success) {
|
||||
isAiDenoiseEnabled = true
|
||||
Toast.makeText(context, context.getString(R.string.ai_denoise_success), Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
isAiDenoiseEnabled = false
|
||||
Toast.makeText(context, context.getString(R.string.ai_denoise_failed), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
)
|
||||
} else if (!checked && isAiDenoiseEnabled) {
|
||||
isDnCNNResetting = true
|
||||
viewModel.resetDnCNNDenoise(
|
||||
photo = currentPhoto,
|
||||
onComplete = { success ->
|
||||
isDnCNNResetting = false
|
||||
if (success) {
|
||||
isAiDenoiseEnabled = false
|
||||
} else {
|
||||
isAiDenoiseEnabled = true
|
||||
Toast.makeText(context, context.getString(R.string.ai_denoise_failed), Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,6 +323,30 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
updateBokehPhoto()
|
||||
}
|
||||
|
||||
private suspend fun updatePhotoMetadata(
|
||||
photoId: String,
|
||||
transform: (MediaMetadata) -> MediaMetadata
|
||||
): MediaMetadata? {
|
||||
val context = getApplication<Application>()
|
||||
val current = GalleryManager.loadMetadata(context, photoId)
|
||||
?: _photos.value.firstOrNull { it.id == photoId }?.metadata
|
||||
?: MediaMetadata()
|
||||
val updated = transform(current)
|
||||
if (!GalleryManager.saveMetadata(context, photoId, updated)) return null
|
||||
withContext(Dispatchers.Main) {
|
||||
if (currentPhotoMetadataId == photoId) {
|
||||
currentMediaMetadata = updated
|
||||
}
|
||||
_photos.value = _photos.value.map { p ->
|
||||
if (p.id == photoId) p.copy(metadata = updated) else p
|
||||
}
|
||||
if (_latestPhoto.value?.id == photoId) {
|
||||
_latestPhoto.value = _latestPhoto.value?.copy(metadata = updated)
|
||||
}
|
||||
}
|
||||
return updated
|
||||
}
|
||||
|
||||
private fun updateBokehPhoto() {
|
||||
bokehJob?.cancel()
|
||||
bokehJob = viewModelScope.launch(Dispatchers.IO) {
|
||||
@@ -331,11 +355,17 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
val aperture = editComputationalAperture.value
|
||||
if (aperture == null || aperture <= 0) {
|
||||
GalleryManager.getBokehFile(context, photoData.id).takeIf { it.exists() }?.delete()
|
||||
GalleryManager.deleteDetailHdrFile(context, photoData.id)
|
||||
return@launch
|
||||
}
|
||||
val focusPointX = editFocusPointX.value
|
||||
val focusPointY = editFocusPointY.value
|
||||
val bitmap = GalleryManager.loadOriginalBitmap(context, photoData.id)
|
||||
val metadata = GalleryManager.loadMetadata(context, photoData.id) ?: photoData.metadata ?: MediaMetadata()
|
||||
val bitmap = if (metadata.hasAiDenoisedBase) {
|
||||
GalleryManager.loadBitmap(context, Uri.fromFile(GalleryManager.getAiDenoiseFile(context, photoData.id)))
|
||||
} else {
|
||||
GalleryManager.loadOriginalBitmap(context, photoData.id)
|
||||
}
|
||||
?: GalleryManager.loadBitmap(context, photoData.uri) ?: return@launch
|
||||
if (!isActive) return@launch
|
||||
val bokeh = contentRepository.depthBokehProcessor.applyHighQualityBokeh(
|
||||
@@ -348,6 +378,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
)
|
||||
if (!isActive) return@launch
|
||||
GalleryManager.saveBokehPhoto(context, photoData.id, bokeh)
|
||||
GalleryManager.deleteDetailHdrFile(context, photoData.id)
|
||||
photoRefreshKeys[photoData.id] = System.currentTimeMillis()
|
||||
}
|
||||
}
|
||||
@@ -385,15 +416,32 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
return@launch
|
||||
}
|
||||
|
||||
val photoDir = File(context.getExternalFilesDir(android.os.Environment.DIRECTORY_PICTURES), "photos/${photo.id}")
|
||||
val file = File(photoDir, "original.jpg")
|
||||
val file = GalleryManager.getAiDenoiseFile(context, photo.id)
|
||||
file.parentFile?.mkdirs()
|
||||
FileOutputStream(file).use { outputStream ->
|
||||
denoised.compress(Bitmap.CompressFormat.JPEG, 95, outputStream)
|
||||
}
|
||||
|
||||
// Update metadata to mark that we have an AI denoised base
|
||||
val metadata = currentMediaMetadata ?: photo.metadata ?: MediaMetadata()
|
||||
val updatedMetadata = metadata.copy(hasAiDenoisedBase = true)
|
||||
val aperture = metadata.computationalAperture ?: 0f
|
||||
if (aperture > 0f) {
|
||||
val bokeh = contentRepository.depthBokehProcessor.applyHighQualityBokeh(
|
||||
context,
|
||||
photo.id,
|
||||
denoised,
|
||||
metadata.focusPointX,
|
||||
metadata.focusPointY,
|
||||
aperture
|
||||
)
|
||||
GalleryManager.saveBokehPhoto(context, photo.id, bokeh)
|
||||
if (bokeh !== denoised && !bokeh.isRecycled) {
|
||||
bokeh.recycle()
|
||||
}
|
||||
}
|
||||
val updatedMetadata = metadata.copy(
|
||||
hasAiDenoisedBase = true
|
||||
)
|
||||
GalleryManager.saveMetadata(context, photo.id, updatedMetadata)
|
||||
currentMediaMetadata = updatedMetadata
|
||||
|
||||
@@ -427,6 +475,50 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
fun resetDnCNNDenoise(photo: MediaData, onComplete: (Boolean) -> Unit = {}) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val context = getApplication<Application>()
|
||||
try {
|
||||
GalleryManager.getAiDenoiseFile(context, photo.id).takeIf { it.exists() }?.delete()
|
||||
val updatedMetadata = updatePhotoMetadata(photo.id) {
|
||||
it.copy(hasAiDenoisedBase = false)
|
||||
} ?: run {
|
||||
withContext(Dispatchers.Main) { onComplete(false) }
|
||||
return@launch
|
||||
}
|
||||
|
||||
val aperture = updatedMetadata.computationalAperture ?: 0f
|
||||
if (aperture > 0f) {
|
||||
val originalBitmap = GalleryManager.loadOriginalBitmap(context, photo.id)
|
||||
if (originalBitmap != null) {
|
||||
GalleryManager.generateBokehPhoto(context, photo.id, updatedMetadata, originalBitmap)
|
||||
if (!originalBitmap.isRecycled) originalBitmap.recycle()
|
||||
}
|
||||
} else {
|
||||
GalleryManager.getBokehFile(context, photo.id).takeIf { it.exists() }?.delete()
|
||||
}
|
||||
|
||||
GalleryManager.deleteDetailHdrFile(context, photo.id)
|
||||
if (updatedMetadata.manualHdrEffectEnabled) {
|
||||
GalleryManager.queueDetailHdrCacheBuild(
|
||||
context = context,
|
||||
photoId = photo.id,
|
||||
metadata = updatedMetadata,
|
||||
sharpening = updatedMetadata.sharpening ?: 0f,
|
||||
noiseReduction = updatedMetadata.noiseReduction ?: 0f,
|
||||
chromaNoiseReduction = updatedMetadata.chromaNoiseReduction ?: 0f
|
||||
)
|
||||
}
|
||||
invalidatePreviewCache(photo.id)
|
||||
photoRefreshKeys[photo.id] = System.currentTimeMillis()
|
||||
withContext(Dispatchers.Main) { onComplete(true) }
|
||||
} catch (e: Exception) {
|
||||
PLog.e(TAG, "Failed to reset DnCNN denoise", e)
|
||||
withContext(Dispatchers.Main) { onComplete(false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun hasDepthInfo(photo: MediaData?): Boolean {
|
||||
if (photo == null) return false
|
||||
if (selectedTab == GalleryTab.SYSTEM) return false
|
||||
@@ -1847,15 +1939,24 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
|
||||
// 2. 原始底图缓存(按 maxEdge 加载,快速预览走小尺寸,正式预览走高分辨率)
|
||||
val currentBitmap = bitmap ?: if (selectedTab == GalleryTab.PHOTON) {
|
||||
GalleryManager.loadBitmap(context, photo.id, maxEdge)
|
||||
val currentBitmap = bitmap ?: if (showOrigin) {
|
||||
GalleryManager.loadOriginalBitmap(context, photo.id, maxEdge)
|
||||
?: if (selectedTab == GalleryTab.SYSTEM) GalleryManager.loadBitmap(context, photo.uri, maxEdge) else null
|
||||
} else {
|
||||
GalleryManager.loadBitmap(context, photo.id, maxEdge)
|
||||
?: GalleryManager.loadBitmap(context, photo.uri, maxEdge)
|
||||
val bokehFile = GalleryManager.getBokehFile(context, photo.id)
|
||||
when {
|
||||
bokehFile.exists() -> GalleryManager.loadBitmap(context, Uri.fromFile(bokehFile), maxEdge)
|
||||
finalMetadata.hasAiDenoisedBase -> GalleryManager.loadBitmap(
|
||||
context,
|
||||
Uri.fromFile(GalleryManager.getAiDenoiseFile(context, photo.id)),
|
||||
maxEdge
|
||||
)
|
||||
else -> GalleryManager.loadOriginalBitmap(context, photo.id, maxEdge)
|
||||
} ?: if (selectedTab == GalleryTab.SYSTEM) GalleryManager.loadBitmap(context, photo.uri, maxEdge) else null
|
||||
} ?: return@withContext null
|
||||
|
||||
// 只在全分辨率路径下缓存原始底图(避免低分辨率污染 origin 缓存)
|
||||
if (maxEdge >= 4096) {
|
||||
if (showOrigin && maxEdge >= 4096) {
|
||||
previewBitmapCache.put(previewCacheKey(photo, finalMetadata, true), currentBitmap)
|
||||
}
|
||||
|
||||
@@ -2140,6 +2241,10 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
RawProcessingPreferences.DROMode.valueOf(droMode.value)
|
||||
)
|
||||
if (result != null) {
|
||||
GalleryManager.getAiDenoiseFile(context, photo.id).takeIf { it.exists() }?.delete()
|
||||
updatePhotoMetadata(photo.id) {
|
||||
it.copy(hasAiDenoisedBase = false)
|
||||
}
|
||||
// 更新刷新密钥以强制 UI 重新加载
|
||||
photoRefreshKeys[photo.id] = System.currentTimeMillis()
|
||||
invalidatePreviewCache(photo.id)
|
||||
@@ -2410,6 +2515,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
return metadata.hasEmbeddedGainmap ||
|
||||
metadata.dynamicRangeProfile == "HLG10" ||
|
||||
GalleryManager.getDngFile(getApplication(), photo.id).exists() ||
|
||||
GalleryManager.getYuvFile(getApplication(), photo.id).exists() ||
|
||||
GalleryManager.getPhotoFile(getApplication(), photo.id).exists()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user