fix: photo detail hdr
This commit is contained in:
1 parent
7b77d8a394
commit
7c0c2088b9
11 files changed
+177
-523
No files matched your search
@@ -11,7 +11,6 @@ import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Environment
|
||||
import android.provider.MediaStore
|
||||
import android.util.Log
|
||||
import androidx.core.graphics.createBitmap
|
||||
import androidx.exifinterface.media.ExifInterface
|
||||
import com.hinnka.mycamera.camera.AspectRatio
|
||||
@@ -214,7 +213,7 @@ object PhotoManager {
|
||||
bitmap = bitmap,
|
||||
outputStream = outputStream,
|
||||
quality = quality,
|
||||
gainmap = gainmapResult?.payload,
|
||||
gainmap = gainmapResult?.gainmap,
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -1893,7 +1892,7 @@ object PhotoManager {
|
||||
|
||||
val bitmap = runCatching {
|
||||
ImageDecoder.decodeBitmap(source) { decoder, info, _ ->
|
||||
if (preserveHdr && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
|
||||
if (preserveHdr) {
|
||||
decoder.allocator = ImageDecoder.ALLOCATOR_HARDWARE
|
||||
} else {
|
||||
decoder.allocator = ImageDecoder.ALLOCATOR_SOFTWARE
|
||||
|
||||
@@ -162,7 +162,7 @@ class PhotoProcessor(
|
||||
}
|
||||
|
||||
val photoFile = PhotoManager.getPhotoFile(context, photoId)
|
||||
if (photoFile.exists() && metadata.manualHdrEffectEnabled) {
|
||||
if (photoFile.exists()) {
|
||||
val bitmap = BitmapFactory.decodeFile(photoFile.absolutePath) ?: return null
|
||||
val sdrBitmap = processBitmap(
|
||||
context = context,
|
||||
@@ -487,13 +487,6 @@ class PhotoProcessor(
|
||||
): Bitmap = withContext(Dispatchers.IO) {
|
||||
var result = input
|
||||
|
||||
// 优先从元数据中获取软件处理参数
|
||||
// 智能回退:如果是导入的照片且元数据中没存过,则默认值为 0,不应用额外处理
|
||||
val finalSharpening = metadata.sharpening ?: (if (metadata.isImported) 0f else sharpening)
|
||||
val finalNoiseReduction = metadata.noiseReduction ?: (if (metadata.isImported) 0f else noiseReduction)
|
||||
val finalChromaNoiseReduction =
|
||||
metadata.chromaNoiseReduction ?: (if (metadata.isImported) 0f else chromaNoiseReduction)
|
||||
|
||||
val lutConfig = metadata.lutId?.let { lutManager.loadLut(it) }
|
||||
val colorRecipeParams = metadata.colorRecipeParams
|
||||
?: metadata.lutId?.let { lutManager.loadColorRecipeParams(it) }
|
||||
@@ -513,9 +506,9 @@ class PhotoProcessor(
|
||||
result,
|
||||
lutConfig,
|
||||
colorRecipeParams,
|
||||
finalSharpening,
|
||||
finalNoiseReduction,
|
||||
finalChromaNoiseReduction
|
||||
sharpening,
|
||||
noiseReduction,
|
||||
chromaNoiseReduction
|
||||
)
|
||||
|
||||
result = applyCrop(result, metadata)
|
||||
|
||||
@@ -27,10 +27,7 @@ class EstimatedSdrGainmapProducer : GainmapProducer {
|
||||
}
|
||||
|
||||
return GainmapResult(
|
||||
payload = GainmapPayload(
|
||||
platformGainmap = gainmap,
|
||||
description = "estimated_sdr_highlight_gainmap"
|
||||
),
|
||||
gainmap = gainmap,
|
||||
sourceKind = source.sourceKind,
|
||||
confidence = source.confidence
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.hinnka.mycamera.hdr
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Gainmap
|
||||
|
||||
enum class SourceKind {
|
||||
RAW,
|
||||
@@ -13,11 +14,6 @@ data class HdrBuffer(
|
||||
val description: String? = null,
|
||||
)
|
||||
|
||||
data class GainmapPayload(
|
||||
val platformGainmap: Any? = null,
|
||||
val description: String? = null,
|
||||
)
|
||||
|
||||
data class GainmapSourceSet(
|
||||
val sdrBase: Bitmap,
|
||||
val hdrReference: HdrBuffer? = null,
|
||||
@@ -26,7 +22,7 @@ data class GainmapSourceSet(
|
||||
)
|
||||
|
||||
data class GainmapResult(
|
||||
val payload: GainmapPayload,
|
||||
val gainmap: Gainmap,
|
||||
val sourceKind: SourceKind,
|
||||
val confidence: Float = 1.0f,
|
||||
)
|
||||
@@ -35,10 +35,7 @@ class HlgGainmapProducer : GainmapProducer {
|
||||
}
|
||||
|
||||
return GainmapResult(
|
||||
payload = GainmapPayload(
|
||||
platformGainmap = gainmap,
|
||||
description = "hlg_luminance_gainmap"
|
||||
),
|
||||
gainmap = gainmap,
|
||||
sourceKind = source.sourceKind,
|
||||
confidence = source.confidence
|
||||
)
|
||||
|
||||
@@ -35,10 +35,7 @@ class RawGainmapProducer : GainmapProducer {
|
||||
}
|
||||
|
||||
return GainmapResult(
|
||||
payload = GainmapPayload(
|
||||
platformGainmap = gainmap,
|
||||
description = "raw_luminance_gainmap"
|
||||
),
|
||||
gainmap = gainmap,
|
||||
sourceKind = source.sourceKind,
|
||||
confidence = source.confidence
|
||||
)
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.hinnka.mycamera.hdr
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Gainmap
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.OutputStream
|
||||
|
||||
object UltraHdrWriter {
|
||||
@@ -12,84 +16,21 @@ object UltraHdrWriter {
|
||||
val bitmap: Bitmap,
|
||||
val outputStream: OutputStream,
|
||||
val quality: Int = 95,
|
||||
val gainmap: GainmapPayload? = null,
|
||||
val gainmap: Gainmap? = null,
|
||||
val preferUltraHdr: Boolean = true,
|
||||
)
|
||||
|
||||
fun writeJpeg(request: Request): Boolean {
|
||||
val effectiveGainmap = request.gainmap ?: extractExistingGainmap(request.bitmap)
|
||||
if (!request.preferUltraHdr || effectiveGainmap?.platformGainmap == null) {
|
||||
return request.bitmap.compress(Bitmap.CompressFormat.JPEG, request.quality, request.outputStream)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE && request.gainmap != null) {
|
||||
request.bitmap.gainmap = request.gainmap
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||
PLog.d(TAG, "Ultra HDR requested below API 34, falling back to standard JPEG")
|
||||
return request.bitmap.compress(Bitmap.CompressFormat.JPEG, request.quality, request.outputStream)
|
||||
}
|
||||
|
||||
return writeWithGainmap(request.copy(gainmap = effectiveGainmap))
|
||||
}
|
||||
|
||||
private fun writeWithGainmap(request: Request): Boolean {
|
||||
val gainmapClass = runCatching { Class.forName("android.graphics.Gainmap") }.getOrNull()
|
||||
?: return fallbackWithLog("Gainmap class unavailable at runtime", request)
|
||||
|
||||
val gainmap = request.gainmap?.platformGainmap
|
||||
if (gainmap == null || !gainmapClass.isInstance(gainmap)) {
|
||||
return fallbackWithLog("Unsupported gainmap payload, falling back to standard JPEG", request)
|
||||
}
|
||||
|
||||
return try {
|
||||
val bitmapClass = request.bitmap.javaClass
|
||||
val setGainmap = bitmapClass.getMethod("setGainmap", gainmapClass)
|
||||
val getGainmap = bitmapClass.getMethod("getGainmap")
|
||||
val previousGainmap = getGainmap.invoke(request.bitmap)
|
||||
setGainmap.invoke(request.bitmap, gainmap)
|
||||
try {
|
||||
request.bitmap.compress(Bitmap.CompressFormat.JPEG, request.quality, request.outputStream)
|
||||
} finally {
|
||||
setGainmap.invoke(request.bitmap, previousGainmap)
|
||||
}
|
||||
} catch (t: Throwable) {
|
||||
PLog.e(TAG, "Failed to write Ultra HDR JPEG, falling back to standard JPEG", t)
|
||||
request.bitmap.compress(Bitmap.CompressFormat.JPEG, request.quality, request.outputStream)
|
||||
}
|
||||
}
|
||||
|
||||
private fun fallbackWithLog(message: String, request: Request): Boolean {
|
||||
PLog.w(TAG, message)
|
||||
return request.bitmap.compress(Bitmap.CompressFormat.JPEG, request.quality, request.outputStream)
|
||||
}
|
||||
|
||||
private fun extractExistingGainmap(bitmap: Bitmap): GainmapPayload? {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) return null
|
||||
return try {
|
||||
val getGainmap = bitmap.javaClass.getMethod("getGainmap")
|
||||
val gainmap = getGainmap.invoke(bitmap) ?: return null
|
||||
GainmapPayload(
|
||||
platformGainmap = gainmap,
|
||||
description = "embedded_bitmap_gainmap"
|
||||
)
|
||||
} catch (_: Throwable) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
fun hasGainmap(bitmap: Bitmap?): Boolean {
|
||||
if (bitmap == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) return false
|
||||
return extractExistingGainmap(bitmap)?.platformGainmap != null
|
||||
}
|
||||
|
||||
fun attachGainmap(bitmap: Bitmap, gainmap: GainmapPayload?): Boolean {
|
||||
if (gainmap?.platformGainmap == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||
return false
|
||||
}
|
||||
return try {
|
||||
val gainmapClass = Class.forName("android.graphics.Gainmap")
|
||||
val setGainmap = bitmap.javaClass.getMethod("setGainmap", gainmapClass)
|
||||
setGainmap.invoke(bitmap, gainmap.platformGainmap)
|
||||
true
|
||||
} catch (_: Throwable) {
|
||||
suspend fun hasGainmap(loadedPreviewBitmap: Bitmap?): Boolean = withContext(Dispatchers.IO) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
|
||||
loadedPreviewBitmap?.hasGainmap() ?: false
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -345,6 +345,7 @@ fun BurstDetailScreen(
|
||||
photo = photoData,
|
||||
photoFile = photo,
|
||||
showOrigin = showOrigin,
|
||||
isActive = page == pagerState.currentPage,
|
||||
viewModel = viewModel,
|
||||
onZoomChange = { zoomed ->
|
||||
if (page == pagerState.currentPage) {
|
||||
@@ -560,6 +561,7 @@ private fun ZoomableImage(
|
||||
photo: PhotoData,
|
||||
photoFile: File,
|
||||
showOrigin: Boolean,
|
||||
isActive: Boolean,
|
||||
viewModel: GalleryViewModel,
|
||||
onZoomChange: (Boolean) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
@@ -587,13 +589,13 @@ private fun ZoomableImage(
|
||||
var bitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||
val refreshKey = viewModel.photoRefreshKeys[photo.id] ?: 0L
|
||||
|
||||
LaunchedEffect(photo.id, metadataHash, showOrigin, refreshKey, photoFile) {
|
||||
LaunchedEffect(photo.id, metadataHash, showOrigin, refreshKey, photoFile, isActive) {
|
||||
val photoBitmap = withContext(Dispatchers.IO) {
|
||||
BitmapFactory.decodeFile(photoFile.path)
|
||||
}
|
||||
suspend fun loadBitmap() {
|
||||
isLoading = bitmap == null
|
||||
bitmap = viewModel.getPreviewBitmap(photo, showOrigin = showOrigin, bitmap = photoBitmap)
|
||||
bitmap = viewModel.getPreviewBitmap(photo, showOrigin = showOrigin, bitmap = photoBitmap, ignoreDenoise = !isActive)
|
||||
if (bitmap == null) {
|
||||
delay(500)
|
||||
loadBitmap()
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.hinnka.mycamera.ui.gallery
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.util.AttributeSet
|
||||
import android.widget.ImageView
|
||||
|
||||
class HdrZoomImageView @JvmOverloads constructor(
|
||||
context: Context,
|
||||
attrs: AttributeSet? = null,
|
||||
) : ImageView(context, attrs) {
|
||||
|
||||
private var currentBitmap: Bitmap? = null
|
||||
|
||||
init {
|
||||
scaleType = ScaleType.FIT_CENTER
|
||||
adjustViewBounds = false
|
||||
isClickable = false
|
||||
isFocusable = false
|
||||
setLayerType(LAYER_TYPE_HARDWARE, null)
|
||||
}
|
||||
|
||||
fun setBitmap(bitmap: Bitmap?) {
|
||||
if (bitmap === currentBitmap) return
|
||||
currentBitmap = bitmap
|
||||
setImageBitmap(bitmap)
|
||||
}
|
||||
|
||||
fun refreshForHdrMode() {
|
||||
postInvalidateOnAnimation()
|
||||
}
|
||||
|
||||
fun rebindForHdrMode() {
|
||||
val bitmap = currentBitmap ?: return
|
||||
super.setImageBitmap(null)
|
||||
super.setImageBitmap(bitmap)
|
||||
postInvalidateOnAnimation()
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,12 @@
|
||||
package com.hinnka.mycamera.ui.gallery
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.ColorSpace
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.SystemClock
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.IntentSenderRequest
|
||||
@@ -23,7 +18,6 @@ import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.pager.HorizontalPager
|
||||
import androidx.compose.foundation.pager.rememberPagerState
|
||||
import androidx.compose.foundation.shape.CircleShape
|
||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material.icons.filled.*
|
||||
@@ -31,14 +25,14 @@ import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.asImageBitmap
|
||||
import androidx.compose.ui.graphics.graphicsLayer
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.input.pointer.PointerEvent
|
||||
import androidx.compose.ui.input.pointer.PointerEventPass
|
||||
import androidx.compose.ui.input.pointer.PointerEventType
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
@@ -47,7 +41,6 @@ import androidx.compose.ui.text.font.FontWeight
|
||||
import com.hinnka.mycamera.R
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import com.hinnka.mycamera.gallery.PhotoData
|
||||
import com.hinnka.mycamera.gallery.PhotoManager
|
||||
import com.hinnka.mycamera.hdr.UltraHdrWriter
|
||||
import com.hinnka.mycamera.ui.theme.AccentOrange
|
||||
import com.hinnka.mycamera.viewmodel.GalleryViewModel
|
||||
@@ -57,17 +50,18 @@ import androidx.media3.common.MediaItem
|
||||
import androidx.media3.common.Player
|
||||
import androidx.media3.ui.PlayerView
|
||||
import androidx.compose.ui.viewinterop.AndroidView
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.media3.ui.AspectRatioFrameLayout
|
||||
import coil.request.ImageRequest
|
||||
import com.hinnka.mycamera.utils.DeviceUtil
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
import com.hinnka.mycamera.viewmodel.GalleryTab
|
||||
import me.saket.telephoto.zoomable.ZoomSpec
|
||||
import me.saket.telephoto.zoomable.ZoomableContentLocation
|
||||
import me.saket.telephoto.zoomable.coil.ZoomableAsyncImage
|
||||
import me.saket.telephoto.zoomable.rememberZoomableImageState
|
||||
import me.saket.telephoto.zoomable.rememberZoomableState
|
||||
import me.saket.telephoto.zoomable.zoomable
|
||||
import java.io.File
|
||||
import kotlin.math.min
|
||||
|
||||
/**
|
||||
@@ -96,13 +90,6 @@ fun PhotoDetailScreen(
|
||||
val isSharing by viewModel.isSharing.collectAsState()
|
||||
|
||||
val currentColorSpace = remember { mutableStateOf<ColorSpace?>(null) }
|
||||
val currentHasHdr = remember { mutableStateOf(false) }
|
||||
val hdrRefreshNonce = remember { mutableStateOf(0L) }
|
||||
LaunchedEffect(currentHasHdr.value) {
|
||||
if (currentHasHdr.value) {
|
||||
hdrRefreshNonce.value = System.nanoTime()
|
||||
}
|
||||
}
|
||||
|
||||
// Activity Result Launcher for delete confirmation
|
||||
val deletePhotoLauncher = rememberLauncherForActivityResult(
|
||||
@@ -177,13 +164,8 @@ fun PhotoDetailScreen(
|
||||
// 同步当前索引,并在快到底部时加载更多系统照片
|
||||
LaunchedEffect(pagerState.currentPage, photos.size) {
|
||||
viewModel.setCurrentPhoto(pagerState.currentPage)
|
||||
currentHasHdr.value = false
|
||||
currentColorSpace.value = null
|
||||
|
||||
photos.getOrNull(pagerState.currentPage)?.let { viewModel.prefetchDetailBitmap(it) }
|
||||
photos.getOrNull(pagerState.currentPage - 1)?.let { viewModel.prefetchDetailBitmap(it) }
|
||||
photos.getOrNull(pagerState.currentPage + 1)?.let { viewModel.prefetchDetailBitmap(it) }
|
||||
|
||||
if (viewModel.selectedTab == GalleryTab.SYSTEM && pagerState.currentPage >= photos.size - 5) {
|
||||
viewModel.loadSystemPhotos(reset = false)
|
||||
}
|
||||
@@ -443,89 +425,72 @@ fun PhotoDetailScreen(
|
||||
|
||||
var showOrigin by remember { mutableStateOf(false) }
|
||||
var isPlaying by remember { mutableStateOf(false) }
|
||||
var useNativeImageGestures by remember { mutableStateOf(false) }
|
||||
|
||||
val gestureModifier = if (useNativeImageGestures) {
|
||||
Modifier.fillMaxSize()
|
||||
} else {
|
||||
Modifier.fillMaxSize().pointerInput(Unit) {
|
||||
awaitPointerEventScope {
|
||||
while (true) {
|
||||
// 确认第一个手指按下,且当前只有一个指针
|
||||
val downEvent = awaitPointerEvent(PointerEventPass.Initial)
|
||||
if (downEvent.type == PointerEventType.Press && downEvent.changes.size == 1) {
|
||||
val touchSlop = viewConfiguration.touchSlop
|
||||
val initialPosition = downEvent.changes[0].position
|
||||
val longPressTimeout = viewConfiguration.longPressTimeoutMillis
|
||||
var upEvent: PointerEvent? = null
|
||||
var isMultiTouch = false
|
||||
var isMoved = false
|
||||
Box(modifier = Modifier.fillMaxSize().pointerInput(Unit) {
|
||||
awaitPointerEventScope {
|
||||
while (true) {
|
||||
// 确认第一个手指按下,且当前只有一个指针
|
||||
val downEvent = awaitPointerEvent(PointerEventPass.Initial)
|
||||
if (downEvent.type == PointerEventType.Press && downEvent.changes.size == 1) {
|
||||
val touchSlop = viewConfiguration.touchSlop
|
||||
val initialPosition = downEvent.changes[0].position
|
||||
val longPressTimeout = viewConfiguration.longPressTimeoutMillis
|
||||
var upEvent: PointerEvent? = null
|
||||
var isMultiTouch = false
|
||||
var isMoved = false
|
||||
|
||||
// 期间如果出现第二个手指,立即标志并退出
|
||||
withTimeoutOrNull(longPressTimeout) {
|
||||
while (true) {
|
||||
val event = awaitPointerEvent(PointerEventPass.Initial)
|
||||
if (event.changes.size > 1) {
|
||||
isMultiTouch = true
|
||||
break
|
||||
}
|
||||
// 期间如果出现第二个手指,立即标志并退出
|
||||
withTimeoutOrNull(longPressTimeout) {
|
||||
while (true) {
|
||||
val event = awaitPointerEvent(PointerEventPass.Initial)
|
||||
if (event.changes.size > 1) {
|
||||
isMultiTouch = true
|
||||
break
|
||||
}
|
||||
|
||||
val currentPosition = event.changes[0].position
|
||||
if ((currentPosition - initialPosition).getDistance() > touchSlop) {
|
||||
isMoved = true
|
||||
break
|
||||
}
|
||||
val currentPosition = event.changes[0].position
|
||||
if ((currentPosition - initialPosition).getDistance() > touchSlop) {
|
||||
isMoved = true
|
||||
break
|
||||
}
|
||||
|
||||
if (event.type == PointerEventType.Release) {
|
||||
upEvent = event
|
||||
break
|
||||
}
|
||||
if (event.type == PointerEventType.Release) {
|
||||
upEvent = event
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果不是多指操作,才根据结果执行逻辑
|
||||
if (!isMultiTouch && !isMoved) {
|
||||
if (upEvent == null) {
|
||||
// 确认为长按
|
||||
if (photo.isMotionPhoto) {
|
||||
isPlaying = true
|
||||
} else {
|
||||
showOrigin = true
|
||||
}
|
||||
// 继续监控直到手指抬起,或者变成多指(开始缩放)
|
||||
while (true) {
|
||||
val event = awaitPointerEvent(PointerEventPass.Initial)
|
||||
if (event.type == PointerEventType.Release || event.changes.size > 1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
showOrigin = false
|
||||
isPlaying = false
|
||||
// 如果不是多指操作,才根据结果执行逻辑
|
||||
if (!isMultiTouch && !isMoved) {
|
||||
if (upEvent == null) {
|
||||
// 确认为长按
|
||||
if (photo.isMotionPhoto) {
|
||||
isPlaying = true
|
||||
} else {
|
||||
showOrigin = true
|
||||
}
|
||||
// 继续监控直到手指抬起,或者变成多指(开始缩放)
|
||||
while (true) {
|
||||
val event = awaitPointerEvent(PointerEventPass.Initial)
|
||||
if (event.type == PointerEventType.Release || event.changes.size > 1) {
|
||||
break
|
||||
}
|
||||
}
|
||||
showOrigin = false
|
||||
isPlaying = false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Box(modifier = gestureModifier) {
|
||||
}) {
|
||||
ZoomableImage(
|
||||
photo = photo,
|
||||
colorSpace = currentColorSpace,
|
||||
hasHdr = currentHasHdr,
|
||||
hdrRefreshNonce = hdrRefreshNonce.value,
|
||||
isActive = page == pagerState.currentPage,
|
||||
showOrigin = showOrigin,
|
||||
isActive = page == pagerState.currentPage,
|
||||
viewModel = viewModel,
|
||||
onNativeGestureAvailabilityChange = { available ->
|
||||
useNativeImageGestures = available
|
||||
},
|
||||
onPressAndHoldChange = { pressed ->
|
||||
if (photo.isMotionPhoto) {
|
||||
isPlaying = pressed
|
||||
}
|
||||
},
|
||||
onZoomChange = { zoomed ->
|
||||
if (page == pagerState.currentPage) {
|
||||
isZoomed = zoomed
|
||||
@@ -730,59 +695,26 @@ private fun InfoRow(label: String, value: String) {
|
||||
private fun ZoomableImage(
|
||||
photo: PhotoData,
|
||||
colorSpace: MutableState<ColorSpace?>,
|
||||
hasHdr: MutableState<Boolean>,
|
||||
hdrRefreshNonce: Long,
|
||||
isActive: Boolean,
|
||||
showOrigin: Boolean,
|
||||
isActive: Boolean,
|
||||
viewModel: GalleryViewModel,
|
||||
onNativeGestureAvailabilityChange: (Boolean) -> Unit,
|
||||
onPressAndHoldChange: (Boolean) -> Unit,
|
||||
onZoomChange: (Boolean) -> Unit,
|
||||
modifier: Modifier = Modifier
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
|
||||
var isLoading by remember { mutableStateOf(true) }
|
||||
var imageViewRef by remember { mutableStateOf<HdrZoomImageView?>(null) }
|
||||
var previewBitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||
var hdrBitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||
var hdrVisible by remember { mutableStateOf(false) }
|
||||
val effectiveShowOrigin = showOrigin
|
||||
val maxZoom = remember(photo.width, photo.height) {
|
||||
min(photo.width, photo.height).coerceAtLeast(1) / 300f
|
||||
}
|
||||
val zoomableState = rememberZoomableState(
|
||||
zoomSpec = ZoomSpec(maxZoomFactor = maxZoom.coerceAtLeast(2f)),
|
||||
autoApplyTransformations = false
|
||||
val maxZoom = min(photo.width, photo.height) / 300f
|
||||
val zoomableState = rememberZoomableImageState(
|
||||
zoomableState = rememberZoomableState(zoomSpec = ZoomSpec(maxZoomFactor = maxZoom))
|
||||
)
|
||||
val hdrAlpha by animateFloatAsState(
|
||||
targetValue = if (hdrVisible && hdrBitmap != null) 1f else 0f,
|
||||
animationSpec = tween(durationMillis = 750, easing = LinearOutSlowInEasing),
|
||||
label = "hdrFadeIn"
|
||||
)
|
||||
val shouldShowLoading = isLoading && previewBitmap == null && hdrBitmap == null
|
||||
val contentTransformation = zoomableState.contentTransformation
|
||||
|
||||
LaunchedEffect(zoomableState.zoomFraction) {
|
||||
onZoomChange((zoomableState.zoomFraction ?: 0f) > 0.01f)
|
||||
}
|
||||
|
||||
LaunchedEffect(previewBitmap, hdrBitmap) {
|
||||
val activeBitmap = hdrBitmap ?: previewBitmap
|
||||
if (activeBitmap != null) {
|
||||
zoomableState.setContentLocation(
|
||||
ZoomableContentLocation.scaledInsideAndCenterAligned(
|
||||
androidx.compose.ui.geometry.Size(
|
||||
activeBitmap.width.toFloat(),
|
||||
activeBitmap.height.toFloat()
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
LaunchedEffect(zoomableState.zoomableState.zoomFraction) {
|
||||
onZoomChange((zoomableState.zoomableState.zoomFraction ?: 0f) > 0.01f)
|
||||
}
|
||||
|
||||
Box(
|
||||
modifier = modifier
|
||||
.fillMaxSize()
|
||||
.zoomable(state = zoomableState),
|
||||
modifier = modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center
|
||||
) {
|
||||
// 使用 hashCode() 代替 toJson() 序列化,避免 composition 时做 JSON 序列化
|
||||
@@ -790,170 +722,80 @@ private fun ZoomableImage(
|
||||
photo.metadata?.hashCode() ?: photo.relatedPhoto?.metadata?.hashCode() ?: 0
|
||||
}
|
||||
|
||||
var bitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||
var hdrBitmap by remember { mutableStateOf<Bitmap?>(null) }
|
||||
var showHdr by remember { mutableStateOf(false) }
|
||||
val hdrAlpha by animateFloatAsState(
|
||||
targetValue = if (showHdr) 1f else 0f,
|
||||
animationSpec = tween(durationMillis = 750, easing = LinearOutSlowInEasing),
|
||||
label = "hdrFadeIn"
|
||||
)
|
||||
val refreshKey = viewModel.photoRefreshKeys[photo.id] ?: 0L
|
||||
|
||||
LaunchedEffect(photo.id, metadataHash, effectiveShowOrigin, refreshKey, isActive) {
|
||||
LaunchedEffect(photo.id, metadataHash, showOrigin, refreshKey, isActive) {
|
||||
suspend fun loadBitmap() {
|
||||
val minPreviewDisplayMs = 250L
|
||||
isLoading = previewBitmap == null && hdrBitmap == null
|
||||
if (effectiveShowOrigin) {
|
||||
hdrVisible = false
|
||||
}
|
||||
|
||||
var previewShownAtMs = 0L
|
||||
val loadedPreviewBitmap = viewModel.getPreviewBitmap(photo, showOrigin = effectiveShowOrigin)
|
||||
if (loadedPreviewBitmap != null) {
|
||||
previewBitmap = loadedPreviewBitmap
|
||||
previewShownAtMs = SystemClock.elapsedRealtime()
|
||||
if (isActive) {
|
||||
colorSpace.value = loadedPreviewBitmap.colorSpace
|
||||
val previewHasHdr = UltraHdrWriter.hasGainmap(loadedPreviewBitmap)
|
||||
hasHdr.value = previewHasHdr
|
||||
PLog.d(
|
||||
"PhotoDetailScreen",
|
||||
"ZoomableImage preview loaded: photo=${photo.id}, active=$isActive, hasHdr=$previewHasHdr, colorSpace=${loadedPreviewBitmap.colorSpace?.name}"
|
||||
)
|
||||
}
|
||||
isLoading = false
|
||||
}
|
||||
|
||||
if (loadedPreviewBitmap == null) {
|
||||
if (isActive) {
|
||||
hasHdr.value = false
|
||||
}
|
||||
hdrBitmap = null
|
||||
hdrVisible = false
|
||||
isLoading = true
|
||||
isLoading = bitmap == null
|
||||
bitmap = viewModel.getPreviewBitmap(photo, showOrigin = showOrigin, ignoreDenoise = !isActive)
|
||||
if (bitmap == null) {
|
||||
delay(500)
|
||||
loadBitmap()
|
||||
return
|
||||
}
|
||||
colorSpace.value = bitmap?.colorSpace
|
||||
isLoading = bitmap == null
|
||||
|
||||
if (!isActive || effectiveShowOrigin) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!viewModel.shouldPrioritizeDetailBitmap(photo, showOrigin = effectiveShowOrigin)) {
|
||||
hasHdr.value = UltraHdrWriter.hasGainmap(loadedPreviewBitmap)
|
||||
hdrBitmap = null
|
||||
hdrVisible = false
|
||||
isLoading = false
|
||||
return
|
||||
}
|
||||
|
||||
hasHdr.value = false
|
||||
val detailBitmap = viewModel.getDetailBitmap(photo, showOrigin = effectiveShowOrigin)
|
||||
if (detailBitmap == null) {
|
||||
if (previewBitmap == null && hdrBitmap == null) {
|
||||
delay(500)
|
||||
loadBitmap()
|
||||
if (photo.metadata?.manualHdrEffectEnabled == true) {
|
||||
hdrBitmap = viewModel.getDetailBitmap(photo)
|
||||
hdrBitmap?.let {
|
||||
colorSpace.value = it.colorSpace
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
val detailHasHdr = UltraHdrWriter.hasGainmap(detailBitmap)
|
||||
if (previewBitmap == null || !detailHasHdr) {
|
||||
previewBitmap = detailBitmap
|
||||
}
|
||||
colorSpace.value = detailBitmap.colorSpace
|
||||
hasHdr.value = detailHasHdr
|
||||
PLog.d(
|
||||
"PhotoDetailScreen",
|
||||
"ZoomableImage detail loaded: photo=${photo.id}, active=$isActive, hasHdr=$detailHasHdr, colorSpace=${detailBitmap.colorSpace?.name}"
|
||||
)
|
||||
|
||||
if (detailHasHdr) {
|
||||
hdrBitmap = detailBitmap
|
||||
val elapsed = if (previewShownAtMs > 0L) {
|
||||
SystemClock.elapsedRealtime() - previewShownAtMs
|
||||
} else {
|
||||
minPreviewDisplayMs
|
||||
}
|
||||
val remainingDelay = (minPreviewDisplayMs - elapsed).coerceAtLeast(0L)
|
||||
if (remainingDelay > 0L) {
|
||||
delay(remainingDelay)
|
||||
}
|
||||
hdrVisible = true
|
||||
} else {
|
||||
hdrBitmap = null
|
||||
hdrVisible = false
|
||||
}
|
||||
isLoading = false
|
||||
}
|
||||
if (isActive) {
|
||||
delay(300L)
|
||||
}
|
||||
loadBitmap()
|
||||
}
|
||||
|
||||
previewBitmap?.let { sdrBitmap ->
|
||||
AndroidView(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.graphicsLayer {
|
||||
if (contentTransformation.isSpecified) {
|
||||
scaleX = contentTransformation.scale.scaleX
|
||||
scaleY = contentTransformation.scale.scaleY
|
||||
translationX = contentTransformation.offset.x
|
||||
translationY = contentTransformation.offset.y
|
||||
transformOrigin = contentTransformation.transformOrigin
|
||||
}
|
||||
},
|
||||
factory = { ctx ->
|
||||
HdrZoomImageView(ctx).apply {
|
||||
imageViewRef = this
|
||||
setBitmap(sdrBitmap)
|
||||
}
|
||||
},
|
||||
update = { view ->
|
||||
if (hdrBitmap == null) {
|
||||
imageViewRef = view
|
||||
view.setBitmap(sdrBitmap)
|
||||
}
|
||||
}
|
||||
LaunchedEffect(hdrBitmap, showOrigin, isActive) {
|
||||
delay(300)
|
||||
showHdr = hdrBitmap != null && !showOrigin && isActive
|
||||
}
|
||||
|
||||
if (bitmap != null) {
|
||||
val imageModel = remember(photo.id, metadataHash, bitmap) {
|
||||
ImageRequest.Builder(context)
|
||||
.data(bitmap)
|
||||
.crossfade(false) // 禁用交叉淡入淡出,避免滑动时同时渲染两张大图
|
||||
.build()
|
||||
}
|
||||
|
||||
ZoomableAsyncImage(
|
||||
model = imageModel,
|
||||
contentDescription = photo.displayName,
|
||||
contentScale = ContentScale.Fit,
|
||||
state = zoomableState,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
)
|
||||
}
|
||||
|
||||
hdrBitmap?.let { currentHdrBitmap ->
|
||||
key(currentHdrBitmap, hasHdr.value, hdrRefreshNonce) {
|
||||
AndroidView(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.graphicsLayer {
|
||||
alpha = hdrAlpha
|
||||
if (contentTransformation.isSpecified) {
|
||||
scaleX = contentTransformation.scale.scaleX
|
||||
scaleY = contentTransformation.scale.scaleY
|
||||
translationX = contentTransformation.offset.x
|
||||
translationY = contentTransformation.offset.y
|
||||
transformOrigin = contentTransformation.transformOrigin
|
||||
}
|
||||
},
|
||||
factory = { ctx ->
|
||||
HdrZoomImageView(ctx).apply {
|
||||
imageViewRef = this
|
||||
setBitmap(currentHdrBitmap)
|
||||
}
|
||||
},
|
||||
update = { view ->
|
||||
imageViewRef = view
|
||||
view.setBitmap(currentHdrBitmap)
|
||||
}
|
||||
)
|
||||
if (showHdr) {
|
||||
val imageModel = remember(photo.id, metadataHash, hdrBitmap) {
|
||||
ImageRequest.Builder(context)
|
||||
.data(hdrBitmap)
|
||||
.crossfade(false) // 禁用交叉淡入淡出,避免滑动时同时渲染两张大图
|
||||
.build()
|
||||
}
|
||||
|
||||
ZoomableAsyncImage(
|
||||
model = imageModel,
|
||||
contentDescription = photo.displayName,
|
||||
contentScale = ContentScale.Fit,
|
||||
state = zoomableState,
|
||||
modifier = Modifier.fillMaxSize().alpha(hdrAlpha)
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
onNativeGestureAvailabilityChange(false)
|
||||
}
|
||||
|
||||
LaunchedEffect(hasHdr.value, hdrRefreshNonce, imageViewRef, hdrBitmap, hdrVisible) {
|
||||
if (isActive && hdrBitmap != null && hdrVisible) {
|
||||
withFrameNanos { }
|
||||
imageViewRef?.rebindForHdrMode()
|
||||
withFrameNanos { }
|
||||
imageViewRef?.refreshForHdrMode()
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldShowLoading) {
|
||||
if (isLoading) {
|
||||
CircularProgressIndicator(
|
||||
color = AccentOrange,
|
||||
modifier = Modifier.size(48.dp)
|
||||
|
||||
@@ -22,7 +22,6 @@ import com.hinnka.mycamera.frame.FrameInfo
|
||||
import com.hinnka.mycamera.gallery.PhotoData
|
||||
import com.hinnka.mycamera.gallery.PhotoManager
|
||||
import com.hinnka.mycamera.gallery.PhotoMetadata
|
||||
import com.hinnka.mycamera.hdr.UltraHdrWriter
|
||||
import com.hinnka.mycamera.hdr.UnifiedGainmapProducer
|
||||
import com.hinnka.mycamera.lut.LutConfig
|
||||
import com.hinnka.mycamera.lut.LutInfo
|
||||
@@ -1458,7 +1457,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
return "detail_${previewCacheKey(photoData, metadata, showOrigin)}"
|
||||
}
|
||||
|
||||
private fun shouldUseHdrDetail(photoId: String, metadata: PhotoMetadata): Boolean {
|
||||
private fun shouldUseHdrDetail(metadata: PhotoMetadata): Boolean {
|
||||
return metadata.manualHdrEffectEnabled
|
||||
}
|
||||
|
||||
@@ -1485,6 +1484,7 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
showOrigin: Boolean = false,
|
||||
bitmap: Bitmap? = null,
|
||||
ignoreCrop: Boolean = false,
|
||||
ignoreDenoise: Boolean = false,
|
||||
recipeParamsOverride: ColorRecipeParams? = null
|
||||
): Bitmap? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
@@ -1492,9 +1492,9 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
val context = getApplication<Application>()
|
||||
|
||||
var finalMetadata: PhotoMetadata
|
||||
val finalS: Float
|
||||
val finalNR: Float
|
||||
val finalCNR: Float
|
||||
var finalS = 0f
|
||||
var finalNR = 0f
|
||||
var finalCNR = 0f
|
||||
|
||||
val metadata =
|
||||
photo.metadata
|
||||
@@ -1534,13 +1534,16 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
finalCNR = editChromaNoiseReduction.value
|
||||
} else {
|
||||
finalMetadata = metadata
|
||||
finalS = finalMetadata.sharpening
|
||||
?: (if (finalMetadata.isImported) 0f else sharpening.value)
|
||||
finalNR =
|
||||
finalMetadata.noiseReduction
|
||||
?: (if (finalMetadata.isImported) 0f else noiseReduction.value)
|
||||
finalCNR = finalMetadata.chromaNoiseReduction
|
||||
?: (if (finalMetadata.isImported) 0f else chromaNoiseReduction.value)
|
||||
|
||||
if (!ignoreDenoise) {
|
||||
finalS = finalMetadata.sharpening
|
||||
?: (if (finalMetadata.isImported) 0f else sharpening.value)
|
||||
finalNR =
|
||||
finalMetadata.noiseReduction
|
||||
?: (if (finalMetadata.isImported) 0f else noiseReduction.value)
|
||||
finalCNR = finalMetadata.chromaNoiseReduction
|
||||
?: (if (finalMetadata.isImported) 0f else chromaNoiseReduction.value)
|
||||
}
|
||||
|
||||
if (ignoreCrop) {
|
||||
finalMetadata = finalMetadata.copy(postCropRegion = null)
|
||||
@@ -1593,7 +1596,6 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
|
||||
suspend fun getDetailBitmap(
|
||||
photo: PhotoData,
|
||||
showOrigin: Boolean = false,
|
||||
): Bitmap? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
@@ -1604,94 +1606,41 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
?: PhotoManager.loadMetadata(getApplication(), photo.id)
|
||||
?: PhotoMetadata()
|
||||
|
||||
val detailCacheKey = detailCacheKey(photo, metadata, showOrigin)
|
||||
val shouldUseHdrDetail = shouldUseHdrDetail(photo.id, metadata)
|
||||
val detailCacheKey = detailCacheKey(photo, metadata, false)
|
||||
val shouldUseHdrDetail = shouldUseHdrDetail(metadata)
|
||||
|
||||
// HDR detail must be derived from the original render path, not bokeh.jpg,
|
||||
// otherwise SDR bokeh cache would override HDR-capable sources.
|
||||
|
||||
if (showOrigin) {
|
||||
val originalBitmap = if (selectedTab == GalleryTab.PHOTON) {
|
||||
PhotoManager.loadOriginalBitmap(context, photo.id, preserveHdr = true)
|
||||
} else {
|
||||
PhotoManager.loadBitmap(context, photo.id, preserveHdr = true)
|
||||
?: PhotoManager.loadBitmap(context, photo.uri, preserveHdr = true)
|
||||
}
|
||||
originalBitmap?.let { detailBitmapCache.put(detailCacheKey, it) }
|
||||
return@withContext originalBitmap
|
||||
}
|
||||
|
||||
if (!shouldUseHdrDetail) {
|
||||
return@withContext getPreviewBitmap(photo, showOrigin = false)
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
if (PhotoManager.isHdrWorkInFlight(photo.id)) {
|
||||
PLog.d(TAG, "getDetailBitmap: HDR work in flight for ${photo.id}, using preview fallback")
|
||||
return@withContext getPreviewBitmap(photo, showOrigin = false)
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
if (shouldUseHdrDetail) {
|
||||
val cachedDetail = detailBitmapCache.get(detailCacheKey)
|
||||
if (cachedDetail != null && !cachedDetail.isRecycled) {
|
||||
PLog.d(TAG, "getDetailBitmap: hit detail cache for ${photo.id}")
|
||||
return@withContext cachedDetail
|
||||
}
|
||||
val cachedDetail = detailBitmapCache.get(detailCacheKey)
|
||||
if (cachedDetail != null && !cachedDetail.isRecycled) {
|
||||
PLog.d(TAG, "getDetailBitmap: hit detail cache for ${photo.id}")
|
||||
return@withContext cachedDetail
|
||||
}
|
||||
|
||||
val detailFile = PhotoManager.getDetailHdrFile(context, photo.id)
|
||||
if (detailFile.exists()) {
|
||||
val diskCached = PhotoManager.loadBitmap(context, Uri.fromFile(detailFile), preserveHdr = true)
|
||||
if (diskCached != null) {
|
||||
detailBitmapCache.put(detailCacheKey, diskCached)
|
||||
val detailFile = PhotoManager.getDetailHdrFile(context, photo.id)
|
||||
if (detailFile.exists()) {
|
||||
val diskCached = PhotoManager.loadBitmap(context, Uri.fromFile(detailFile), preserveHdr = true)
|
||||
if (diskCached != null) {
|
||||
detailBitmapCache.put(detailCacheKey, diskCached)
|
||||
// PLog.d(
|
||||
// TAG,
|
||||
// "getDetailBitmap: using disk detail HDR cache for ${photo.id}, hasGainmap=${UltraHdrWriter.hasGainmap(diskCached)}"
|
||||
// )
|
||||
return@withContext diskCached
|
||||
}
|
||||
return@withContext diskCached
|
||||
}
|
||||
}
|
||||
|
||||
val embeddedHdrBitmap = if (selectedTab == GalleryTab.PHOTON) {
|
||||
PhotoManager.loadOriginalBitmap(context, photo.id, preserveHdr = true)
|
||||
} else {
|
||||
PhotoManager.loadBitmap(context, photo.id, preserveHdr = true)
|
||||
?: PhotoManager.loadBitmap(context, photo.uri, preserveHdr = true)
|
||||
}
|
||||
if (UltraHdrWriter.hasGainmap(embeddedHdrBitmap)) {
|
||||
PLog.d(TAG, "getDetailBitmap: using embedded gainmap bitmap for ${photo.id}")
|
||||
embeddedHdrBitmap?.let { detailBitmapCache.put(detailCacheKey, it) }
|
||||
return@withContext embeddedHdrBitmap
|
||||
}
|
||||
|
||||
val canReuseEmbeddedGainmap =
|
||||
metadata.hasEmbeddedGainmap &&
|
||||
metadata.lutId == null &&
|
||||
metadata.colorRecipeParams == null &&
|
||||
metadata.sharpening == null &&
|
||||
metadata.noiseReduction == null &&
|
||||
metadata.chromaNoiseReduction == null &&
|
||||
metadata.frameId == null &&
|
||||
metadata.cropRegion == null &&
|
||||
metadata.postCropRegion == null &&
|
||||
metadata.computationalAperture == null
|
||||
if (canReuseEmbeddedGainmap) {
|
||||
val reusedBitmap = if (selectedTab == GalleryTab.PHOTON) {
|
||||
PhotoManager.loadOriginalBitmap(context, photo.id, preserveHdr = true)
|
||||
} else {
|
||||
PhotoManager.loadBitmap(context, photo.id, preserveHdr = true)
|
||||
?: PhotoManager.loadBitmap(context, photo.uri, preserveHdr = true)
|
||||
}
|
||||
reusedBitmap?.let { detailBitmapCache.put(detailCacheKey, it) }
|
||||
return@withContext reusedBitmap
|
||||
}
|
||||
|
||||
PLog.d(
|
||||
TAG,
|
||||
"getDetailBitmap: no ready HDR detail source for ${photo.id}, using preview fallback"
|
||||
)
|
||||
val fallback = getPreviewBitmap(photo, showOrigin = false)
|
||||
fallback?.let { detailBitmapCache.put(detailCacheKey, it) }
|
||||
return@withContext fallback
|
||||
return@withContext null
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
@@ -1701,17 +1650,16 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
fun shouldPrioritizeDetailBitmap(photo: PhotoData, showOrigin: Boolean = false): Boolean {
|
||||
if (showOrigin) return true
|
||||
fun shouldPrioritizeDetailBitmap(photo: PhotoData): Boolean {
|
||||
val context = getApplication<Application>()
|
||||
val metadata =
|
||||
photo.metadata
|
||||
?: photo.relatedPhoto?.metadata
|
||||
?: PhotoMetadata()
|
||||
if (!shouldUseHdrDetail(photo.id, metadata)) {
|
||||
if (!shouldUseHdrDetail(metadata)) {
|
||||
return false
|
||||
}
|
||||
val detailCacheKey = detailCacheKey(photo, metadata, showOrigin)
|
||||
val detailCacheKey = detailCacheKey(photo, metadata, false)
|
||||
val cachedDetail = detailBitmapCache.get(detailCacheKey)
|
||||
if (cachedDetail != null && !cachedDetail.isRecycled) {
|
||||
return true
|
||||
@@ -1722,25 +1670,6 @@ class GalleryViewModel(application: Application) : AndroidViewModel(application)
|
||||
return metadata.hasEmbeddedGainmap
|
||||
}
|
||||
|
||||
fun prefetchDetailBitmap(photo: PhotoData, showOrigin: Boolean = false) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
runCatching {
|
||||
val metadata = photo.metadata ?: photo.relatedPhoto?.metadata ?: PhotoMetadata()
|
||||
if (!showOrigin && PhotoManager.isHdrWorkInFlight(photo.id)) {
|
||||
PLog.d(TAG, "prefetchDetailBitmap: skip while HDR work is in flight for ${photo.id}")
|
||||
return@runCatching
|
||||
}
|
||||
if (showOrigin || shouldPrioritizeDetailBitmap(photo, showOrigin)) {
|
||||
getDetailBitmap(photo, showOrigin)
|
||||
}
|
||||
}.onFailure {
|
||||
if (it !is CancellationException) {
|
||||
PLog.w(TAG, "Failed to prefetch detail bitmap for ${photo.id}", it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存编辑(只更新元数据,不修改原图)
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user