mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
perf(mms): decode an image once per attachment instead of per attempt
Addresses the review feedback on #826. Compression searches for a size that fits under the carrier limit, so it encodes the same image many times: a binary search over dimensions, then a quality ladder from 90 down to 40 if nothing fit. Every one of those attempts went through ImageUtils.getScaledImage, which re-opened the source URI and re-decoded it from scratch. Decoding is the expensive part and it was the only part being repeated unnecessarily; a large photo could be decoded a dozen or more times to send once. ScaledImageEncoder decodes the source a single time, sampled down to no smaller than the largest width the search will ask for, and re-encodes that bitmap from memory for each attempt. The search itself is unchanged. The decode is guarded: with no carrier cap the target size is the original image, which on a modern camera sensor is large enough to matter, so an OutOfMemoryError returns null and the caller falls back to Glide, which streams instead of holding the whole bitmap. Also fixes three edge cases raised in review: - A bounds decode that fails leaves outWidth/outHeight at zero, making the aspect ratio 0/0 and every derived dimension garbage. Detect it and fall back rather than divide by it. - searchHi was floored at 100, which raised it above the original width, and above the carrier maximum when that was under 100. Both upscale the image and inflate the payload. The floor is now 1 and the search starts at 1 rather than 100. - The last-resort fallback hard-coded a width of 100, upscaling anything narrower. It is now bounded by searchHi, so it can only shrink.
This commit is contained in:
@@ -604,19 +604,34 @@ open class MessageRepositoryImpl @Inject constructor(
|
||||
}
|
||||
val origWidth = opts.outWidth
|
||||
val origHeight = opts.outHeight
|
||||
|
||||
// Bounds decoding fails on formats BitmapFactory can't read. Without real
|
||||
// dimensions the aspect ratio is 0/0, so hand off to Glide rather than divide by it.
|
||||
if (origWidth <= 0 || origHeight <= 0) {
|
||||
Timber.w("Could not read image bounds, falling back to Glide scaling")
|
||||
return ImageUtils.getScaledImage(context, attachment.uri, maxWidth, maxHeight)
|
||||
}
|
||||
|
||||
val aspectRatio = origWidth.toFloat() / origHeight.toFloat()
|
||||
|
||||
val maxWidthByHeight = if (maxHeight != Int.MAX_VALUE) {
|
||||
minOf((maxHeight * aspectRatio.toDouble()).toInt(), origWidth)
|
||||
} else origWidth
|
||||
val searchHi = minOf(origWidth, maxWidth, maxWidthByHeight).coerceAtLeast(100)
|
||||
// Never above the original or the carrier's ceiling: raising this to a floor of
|
||||
// 100 would upscale images smaller than that and inflate the payload.
|
||||
val searchHi = minOf(origWidth, maxWidth, maxWidthByHeight).coerceAtLeast(1)
|
||||
|
||||
var lo = minOf(100, searchHi)
|
||||
var lo = 1
|
||||
var hi = searchHi
|
||||
var bestBytes: ByteArray? = null
|
||||
var attempt = 0
|
||||
|
||||
if (isGif) {
|
||||
// Last-resort size when nothing in the search fit. Bounded by searchHi so an
|
||||
// image already smaller than this is never upscaled back up.
|
||||
val fallbackWidth = minOf(100, searchHi).coerceAtLeast(1)
|
||||
val fallbackHeight = (fallbackWidth / aspectRatio).toInt().coerceAtLeast(1)
|
||||
|
||||
val result = if (isGif) {
|
||||
bestBytes = gifCompressor.compressGif(
|
||||
context,
|
||||
attachment,
|
||||
@@ -624,55 +639,62 @@ open class MessageRepositoryImpl @Inject constructor(
|
||||
origWidth,
|
||||
aspectRatio
|
||||
)
|
||||
attachment.releaseResourceBytes()
|
||||
} else {
|
||||
while (lo <= hi) {
|
||||
val midWidth = (lo + hi) / 2
|
||||
val midHeight = (midWidth / aspectRatio).toInt().coerceAtLeast(1)
|
||||
attempt++
|
||||
val candidate = ImageUtils.getScaledImage(context, attachment.uri, midWidth, midHeight)
|
||||
Timber.d(
|
||||
"Compression attempt $attempt: ${
|
||||
candidate.size / 1024
|
||||
}/${maxBytes / 1024}Kb ($origWidth*$origHeight -> $midWidth*$midHeight)"
|
||||
)
|
||||
if (candidate.size <= maxBytes) {
|
||||
bestBytes = candidate
|
||||
lo = midWidth + 1
|
||||
} else {
|
||||
hi = midWidth - 1
|
||||
}
|
||||
|
||||
// release the attachment hold on the image bytes so the GC can reclaim
|
||||
attachment.releaseResourceBytes()
|
||||
}
|
||||
}
|
||||
|
||||
val result = bestBytes ?: run {
|
||||
val minHeight = (100 / aspectRatio).toInt().coerceAtLeast(1)
|
||||
if (isGif) {
|
||||
ImageUtils.getScaledGif(context, attachment.uri, 100, minHeight).also {
|
||||
bestBytes ?: ImageUtils
|
||||
.getScaledGif(context, attachment.uri, fallbackWidth, fallbackHeight)
|
||||
.also {
|
||||
Timber.w(
|
||||
"GIF too large after compression: ${
|
||||
it.size / 1024
|
||||
}Kb target ${maxBytes / 1024}Kb, sending anyway"
|
||||
)
|
||||
}
|
||||
} else {
|
||||
ImageUtils.getScaledImageWithQuality(
|
||||
context, attachment.uri, 100, minHeight, maxBytes
|
||||
).also {
|
||||
if (it.size > maxBytes) {
|
||||
Timber.w(
|
||||
"Failed to compress image: ${
|
||||
it.size / 1024
|
||||
}Kb target ${maxBytes / 1024}Kb, sending anyway"
|
||||
)
|
||||
} else {
|
||||
val searchHiHeight = (searchHi / aspectRatio).toInt().coerceAtLeast(1)
|
||||
// Decode once for the whole search. Every attempt below re-encodes that
|
||||
// bitmap from memory instead of re-reading and re-decoding the source file.
|
||||
val encoder = ImageUtils.ScaledImageEncoder
|
||||
.open(context, attachment.uri, searchHi, searchHiHeight)
|
||||
|
||||
if (encoder == null) {
|
||||
Timber.w("Could not decode image, falling back to Glide scaling")
|
||||
ImageUtils.getScaledImage(context, attachment.uri, maxWidth, maxHeight)
|
||||
} else encoder.use {
|
||||
while (lo <= hi) {
|
||||
val midWidth = (lo + hi) / 2
|
||||
val midHeight = (midWidth / aspectRatio).toInt().coerceAtLeast(1)
|
||||
attempt++
|
||||
val candidate = encoder.encode(midWidth, midHeight)
|
||||
Timber.d(
|
||||
"Compression attempt $attempt: ${
|
||||
candidate.size / 1024
|
||||
}/${maxBytes / 1024}Kb ($origWidth*$origHeight -> $midWidth*$midHeight)"
|
||||
)
|
||||
if (candidate.size <= maxBytes) {
|
||||
bestBytes = candidate
|
||||
lo = midWidth + 1
|
||||
} else {
|
||||
hi = midWidth - 1
|
||||
}
|
||||
}
|
||||
|
||||
bestBytes ?: encoder
|
||||
.encodeWithinBytes(fallbackWidth, fallbackHeight, maxBytes)
|
||||
.also {
|
||||
if (it.size > maxBytes) {
|
||||
Timber.w(
|
||||
"Failed to compress image: ${
|
||||
it.size / 1024
|
||||
}Kb target ${maxBytes / 1024}Kb, sending anyway"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// release the attachment hold on the image bytes so the GC can reclaim
|
||||
attachment.releaseResourceBytes()
|
||||
|
||||
Timber.v(
|
||||
"Compressed to ${result.size / 1024}Kb with target ${
|
||||
maxBytes / 1024
|
||||
|
||||
@@ -20,12 +20,15 @@ package dev.octoshrimpy.quik.util
|
||||
|
||||
import android.content.Context
|
||||
import android.content.res.AssetFileDescriptor
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.BitmapFactory
|
||||
import android.net.Uri
|
||||
import android.provider.OpenableColumns
|
||||
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
||||
import com.bumptech.glide.request.RequestOptions
|
||||
import dev.octoshrimpy.quik.model.Attachment
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.Closeable
|
||||
|
||||
object ImageUtils {
|
||||
|
||||
@@ -60,22 +63,87 @@ object ImageUtils {
|
||||
.get()
|
||||
}
|
||||
|
||||
fun getScaledImageWithQuality(
|
||||
context: Context,
|
||||
uri: Uri,
|
||||
maxWidth: Int,
|
||||
maxHeight: Int,
|
||||
targetBytes: Int
|
||||
): ByteArray {
|
||||
var result = getScaledImage(context, uri, maxWidth, maxHeight, 90)
|
||||
if (result.size <= targetBytes) return result
|
||||
// If the image is too large, then try to scale it down
|
||||
// then return when we are within targetBytes
|
||||
for (quality in 80 downTo 40 step 10) {
|
||||
result = getScaledImage(context, uri, maxWidth, maxHeight, quality)
|
||||
if (result.size <= targetBytes) return result
|
||||
/**
|
||||
* Decodes an image once and re-encodes it from memory at whatever size and quality the
|
||||
* caller asks for.
|
||||
*
|
||||
* Compression searches for a size that fits under the carrier's limit, so it encodes the
|
||||
* same image many times. Going through [getScaledImage] for each attempt re-read and
|
||||
* re-decoded the source file every time, and decoding is by far the expensive part. Callers
|
||||
* should [open] once per attachment and reuse the result for every attempt.
|
||||
*
|
||||
* Close it when done so the decoded bitmap can be reclaimed.
|
||||
*/
|
||||
class ScaledImageEncoder private constructor(private val source: Bitmap) : Closeable {
|
||||
|
||||
val width: Int get() = source.width
|
||||
val height: Int get() = source.height
|
||||
|
||||
fun encode(width: Int, height: Int, quality: Int = 90): ByteArray {
|
||||
val target = if (width == source.width && height == source.height) source
|
||||
else Bitmap.createScaledBitmap(source, width, height, true)
|
||||
|
||||
val out = ByteArrayOutputStream()
|
||||
target.compress(Bitmap.CompressFormat.JPEG, quality, out)
|
||||
if (target !== source) target.recycle()
|
||||
return out.toByteArray()
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode at [width]x[height], dropping quality until the result fits [targetBytes].
|
||||
* Returns the smallest attempt if nothing fits, so the caller still has something to send.
|
||||
*/
|
||||
fun encodeWithinBytes(width: Int, height: Int, targetBytes: Int): ByteArray {
|
||||
var result = encode(width, height, 90)
|
||||
if (result.size <= targetBytes) return result
|
||||
for (quality in 80 downTo 40 step 10) {
|
||||
result = encode(width, height, quality)
|
||||
if (result.size <= targetBytes) return result
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
override fun close() = source.recycle()
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Returns null when the image cannot be decoded, so callers can fall back rather than
|
||||
* dividing by a zero dimension.
|
||||
*/
|
||||
fun open(context: Context, uri: Uri, targetWidth: Int, targetHeight: Int): ScaledImageEncoder? {
|
||||
val bounds = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
||||
context.contentResolver.openInputStream(uri)?.use {
|
||||
BitmapFactory.decodeStream(it, null, bounds)
|
||||
}
|
||||
if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return null
|
||||
|
||||
// Decode no smaller than the largest size we'll be asked for, so re-encoding
|
||||
// never has to upscale, but no larger than necessary either.
|
||||
val opts = BitmapFactory.Options().apply {
|
||||
inSampleSize = sampleSizeFor(bounds.outWidth, bounds.outHeight, targetWidth, targetHeight)
|
||||
}
|
||||
// With no carrier cap the decode target is the original size, which on a modern
|
||||
// camera can be hundreds of megabytes. Returning null lets the caller fall back
|
||||
// to Glide, which streams rather than holding the whole bitmap.
|
||||
val bitmap = try {
|
||||
context.contentResolver.openInputStream(uri)?.use {
|
||||
BitmapFactory.decodeStream(it, null, opts)
|
||||
}
|
||||
} catch (e: OutOfMemoryError) {
|
||||
null
|
||||
} ?: return null
|
||||
|
||||
return ScaledImageEncoder(bitmap)
|
||||
}
|
||||
|
||||
private fun sampleSizeFor(width: Int, height: Int, targetWidth: Int, targetHeight: Int): Int {
|
||||
var sample = 1
|
||||
while (width / (sample * 2) >= targetWidth && height / (sample * 2) >= targetHeight) {
|
||||
sample *= 2
|
||||
}
|
||||
return sample
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user