Skip to content

Commit

Permalink
Properly set the HDR Gainmap per image
Browse files Browse the repository at this point in the history
Fixes [Enhancement] Add ultra HDR support #490

Signed-off-by: IacobIonut01 <[email protected]>
  • Loading branch information
IacobIonut01 committed Nov 10, 2024
1 parent 5c7160f commit bd65c92
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
applicationId = "com.dot.gallery"
minSdk = 30
targetSdk = 35
versionCode = 31005
versionCode = 31006
versionName = "3.1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package com.dot.gallery.feature_node.presentation.mediaview

import android.os.Build
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.FastOutLinearInEasing
Expand All @@ -29,6 +30,7 @@ import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.State
Expand Down Expand Up @@ -82,8 +84,12 @@ import com.dot.gallery.feature_node.presentation.util.ViewScreenConstants.ImageO
import com.dot.gallery.feature_node.presentation.util.getDate
import com.dot.gallery.feature_node.presentation.util.normalize
import com.dot.gallery.feature_node.presentation.util.rememberWindowInsetsController
import com.dot.gallery.feature_node.presentation.util.setHdrMode
import com.dot.gallery.feature_node.presentation.util.toggleSystemBars
import com.dot.gallery.ui.theme.BlackScrim
import com.github.panpf.sketch.getBitmapOrNull
import com.github.panpf.sketch.request.ImageRequest
import com.github.panpf.sketch.sketch
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -149,6 +155,7 @@ fun MediaViewScreen(
mutableFloatStateOf(0f)
}
var isNormalizationTargetSet by remember { mutableStateOf(false) }
val context = LocalContext.current

LaunchedEffect(mediaState.value) {
snapshotFlow { pagerState.currentPage }.collectLatest { page ->
Expand All @@ -173,6 +180,32 @@ fun MediaViewScreen(
}
}
}

// set HDR Gain map
LaunchedEffect(mediaState.value) {
snapshotFlow { pagerState.currentPage }.collectLatest { page ->
val activeMedia = mediaState.value.media.getOrNull(page)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
val request = ImageRequest(context, activeMedia?.uri.toString()) {
setExtra(
key = "mediaKey",
value = activeMedia.toString(),
)
}
val result = context.sketch.execute(request)
result.image?.getBitmapOrNull()?.let { bitmap ->
context.setHdrMode(bitmap.hasGainmap())
}
}
}
}

DisposableEffect(Unit) {
onDispose {
context.setHdrMode(false)
}
}

LaunchedEffect(sheetHeightDp) {
if (lastSheetHeightDp != sheetHeightDp) {
lastSheetHeightDp = sheetHeightDp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.NonRestartableComposable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
Expand All @@ -27,7 +26,6 @@ import androidx.compose.ui.draw.blur
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import com.dot.gallery.core.Constants.DEFAULT_TOP_BAR_ANIMATION_DURATION
import com.dot.gallery.core.Settings
Expand All @@ -36,9 +34,7 @@ import com.dot.gallery.core.presentation.components.util.ProvideBatteryStatus
import com.dot.gallery.core.presentation.components.util.swipe
import com.dot.gallery.feature_node.domain.model.Media
import com.dot.gallery.feature_node.presentation.util.rememberFeedbackManager
import com.dot.gallery.feature_node.presentation.util.setHdrMode
import com.github.panpf.sketch.AsyncImage
import com.github.panpf.sketch.getBitmapOrNull
import com.github.panpf.sketch.rememberAsyncImageState
import com.github.panpf.sketch.request.ComposableImageRequest
import com.github.panpf.sketch.request.ImageOptions
Expand Down Expand Up @@ -103,21 +99,6 @@ fun ZoomablePagerImage(
}
)

val context = LocalContext.current

DisposableEffect(media, asyncState.result) {
scope.launch {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
asyncState.result?.image?.getBitmapOrNull()?.let {
context.setHdrMode(it.hasGainmap())
}
}
}
onDispose {
context.setHdrMode(false)
}
}

SketchZoomAsyncImage(
zoomState = zoomState,
state = asyncState,
Expand Down

0 comments on commit bd65c92

Please sign in to comment.