Skip to content

Commit

Permalink
Scale images before displaying in NetImage
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumBadger committed Jul 6, 2024
1 parent 24e2620 commit 4d186ef
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 29 deletions.
63 changes: 46 additions & 17 deletions src/main/java/org/quantumbadger/redreader/compose/net/NetWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.quantumbadger.redreader.compose.net

import android.graphics.BitmapFactory
import android.util.Log
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Immutable
Expand All @@ -32,6 +33,7 @@ import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.core.graphics.scale
import org.quantumbadger.redreader.R
import org.quantumbadger.redreader.account.RedditAccount
import org.quantumbadger.redreader.account.RedditAccountId
Expand All @@ -55,8 +57,11 @@ import org.quantumbadger.redreader.compose.ctx.GlobalNetworkRetry
import org.quantumbadger.redreader.compose.ctx.LocalRedditUser
import org.quantumbadger.redreader.image.AlbumInfo
import org.quantumbadger.redreader.image.GetAlbumInfoListener
import org.quantumbadger.redreader.image.ImageSize
import java.io.IOException
import java.util.UUID
import kotlin.math.max
import kotlin.math.roundToInt

@Immutable
sealed interface NetRequestStatus<out R> {
Expand Down Expand Up @@ -165,36 +170,60 @@ fun fetchAlbum(
fun fetchImage(
uri: UriString,
user: RedditAccountId = LocalRedditUser.current,
scaleToMaxAxis: Int = 2048
): State<NetRequestStatus<FileRequestResult<ImageBitmap>>> {

val TAG = "NetWrapper:fetchImage"

val context = LocalContext.current.applicationContext

val filter: (FileRequestMetadata) -> NetRequestStatus<FileRequestResult<ImageBitmap>> = remember(uri, user) {
{
try {
val result = BitmapFactory.decodeStream(it.streamFactory.create())?.asImageBitmap()
val filter: (FileRequestMetadata) -> NetRequestStatus<FileRequestResult<ImageBitmap>> =
remember(uri, user) {
{
try {
val result = BitmapFactory.decodeStream(it.streamFactory.create())
?: throw RuntimeException("Decoded bitmap was null")

val maxAxis = max(result.width, result.height)

val scaledResult = result.invokeIf(maxAxis > scaleToMaxAxis) {

val newSize = if (result.width > result.height) {
val scale = scaleToMaxAxis / result.width.toFloat()
ImageSize(
scaleToMaxAxis,
(result.height.toFloat() * scale).roundToInt()
)
} else {
val scale = scaleToMaxAxis / result.height.toFloat()
ImageSize((result.width.toFloat() * scale).roundToInt(), scaleToMaxAxis)
}

Log.i(
TAG,
"Scaling image from ${result.width}x${result.height} to $newSize"
)

scale(newSize.width, newSize.height)
}

if (result == null) {
throw RuntimeException("Decoded bitmap was null")
} else {
NetRequestStatus.Success(
FileRequestResult(
metadata = it,
data = result
data = scaledResult.asImageBitmap()
)
)
}
} catch (e: Exception) {
NetRequestStatus.Failed(
RRError(
title = context.getString(R.string.error_image_decode_failed),
url = uri,
t = e
} catch (e: Exception) {
NetRequestStatus.Failed(
RRError(
title = context.getString(R.string.error_image_decode_failed),
url = uri,
t = e
)
)
)
}
}
}
}

return fetchFile(
uri = uri,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ fun NetImage(
modifier: Modifier,
image: ImageUrlInfo,
cropToAspect: Float? = null,
showVideoPlayOverlay: Boolean = false
showVideoPlayOverlay: Boolean = false,
maxCanvasDimension: Int = 2048
) {
val theme = LocalComposeTheme.current

Expand All @@ -60,7 +61,7 @@ fun NetImage(

val url = image.url

val data by fetchImage(url)
val data by fetchImage(url, scaleToMaxAxis = maxCanvasDimension)

Box(
modifier = modifier
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/org/quantumbadger/redreader/image/ImageInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ data class ImageSize(
obj?.getLong(fieldHeight)
)
}

override fun toString() = "${width}x$height"
}

@Immutable
Expand Down Expand Up @@ -184,11 +186,13 @@ data class ImageInfo(

val original = fileObj.getString("url")?.let { originalUrl ->
ImageUrlInfo(
url = UriString(if (originalUrl.startsWith("//")) {
"https:$originalUrl"
} else {
originalUrl
}),
url = UriString(
if (originalUrl.startsWith("//")) {
"https:$originalUrl"
} else {
originalUrl
}
),
size = ImageSize.fromJson(fileObj)
)
}
Expand Down Expand Up @@ -216,11 +220,13 @@ data class ImageInfo(

val original = links?.getString("original")?.let { originalUrl ->
ImageUrlInfo(
url = UriString(if (isAnimated) {
originalUrl.replace(".gif", ".mp4")
} else {
originalUrl
}),
url = UriString(
if (isAnimated) {
originalUrl.replace(".gif", ".mp4")
} else {
originalUrl
}
),
size = ImageSize.fromJson(image),
sizeBytes = image?.getLong("size")
)
Expand Down

0 comments on commit 4d186ef

Please sign in to comment.