musikr.cover: use jpeg for covers
Way faster to encode and the artifacts are minimal at 1kx1k and 100 compression. Still not fully ideal, but webp is so flow to encode.
This commit is contained in:
parent
1843986f75
commit
a598f39dea
2 changed files with 29 additions and 20 deletions
|
@ -35,7 +35,7 @@ interface CoverFiles {
|
|||
|
||||
companion object {
|
||||
fun at(context: Context, path: String): CoverFiles =
|
||||
CoverFilesImpl(File(context.filesDir, path).also { it.mkdirs() }, CoverFormat.webp())
|
||||
CoverFilesImpl(File(context.filesDir, path).also { it.mkdirs() }, CoverFormat.jpeg())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,21 +29,42 @@ internal interface CoverFormat {
|
|||
fun transcodeInto(data: ByteArray, output: OutputStream): Boolean
|
||||
|
||||
companion object {
|
||||
fun webp(): CoverFormat = WebpCoverFormat()
|
||||
// Enable if perhaps you want to try other formats.
|
||||
// Currently this is just far too slow.
|
||||
// fun webp(): CoverFormat = CoverFormatImpl(
|
||||
// "webp",
|
||||
// 750,
|
||||
// 80,
|
||||
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
// Bitmap.CompressFormat.WEBP_LOSSY
|
||||
// } else {
|
||||
// Bitmap.CompressFormat.WEBP
|
||||
// }
|
||||
// )
|
||||
|
||||
fun jpeg(): CoverFormat = CoverFormatImpl(
|
||||
"jpg",
|
||||
1000,
|
||||
100,
|
||||
Bitmap.CompressFormat.JPEG
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private class WebpCoverFormat() : CoverFormat {
|
||||
override val extension = EXTENSION
|
||||
|
||||
private class CoverFormatImpl(
|
||||
override val extension: String,
|
||||
val size: Int,
|
||||
val quality: Int,
|
||||
val format: Bitmap.CompressFormat,
|
||||
) : CoverFormat {
|
||||
override fun transcodeInto(data: ByteArray, output: OutputStream) =
|
||||
BitmapFactory.Options().run {
|
||||
inJustDecodeBounds = true
|
||||
BitmapFactory.decodeByteArray(data, 0, data.size, this)
|
||||
inSampleSize = calculateInSampleSize(SIZE)
|
||||
inSampleSize = calculateInSampleSize(size)
|
||||
inJustDecodeBounds = false
|
||||
val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size, this) ?: return@run false
|
||||
bitmap.compress(FORMAT, QUALITY, output)
|
||||
bitmap.compress(format, quality, output)
|
||||
true
|
||||
}
|
||||
|
||||
|
@ -60,16 +81,4 @@ private class WebpCoverFormat() : CoverFormat {
|
|||
}
|
||||
return inSampleSize
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val SIZE = 750
|
||||
val FORMAT =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||
Bitmap.CompressFormat.WEBP_LOSSY
|
||||
} else {
|
||||
Bitmap.CompressFormat.WEBP
|
||||
}
|
||||
const val QUALITY = 80
|
||||
const val EXTENSION = "webp"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue