musikr: introduce cover params

This commit is contained in:
Alexander Capehart 2024-12-26 20:26:04 -05:00
parent 80c97cbea1
commit 0dc72b67af
No known key found for this signature in database
GPG key ID: 37DBE3621FE9AD47
3 changed files with 38 additions and 20 deletions

View file

@ -25,6 +25,7 @@ import kotlinx.coroutines.withContext
import org.oxycblt.musikr.cover.Cover
import org.oxycblt.musikr.cover.CoverFiles
import org.oxycblt.musikr.cover.CoverFormat
import org.oxycblt.musikr.cover.CoverParams
import org.oxycblt.musikr.cover.MutableStoredCovers
import org.oxycblt.musikr.cover.StoredCovers
@ -47,7 +48,8 @@ class RevisionedCovers(private val revision: UUID, private val inner: MutableSto
context.filesDir.resolve("covers/${revision}").apply { mkdirs() }
}
return RevisionedCovers(
revision, StoredCovers.from(CoverFiles.at(dir), CoverFormat.jpeg()))
revision,
StoredCovers.from(CoverFiles.at(dir), CoverFormat.jpeg(CoverParams.of(750, 80))))
}
suspend fun cleanup(context: Context, exclude: UUID) =

View file

@ -28,37 +28,24 @@ abstract class CoverFormat {
internal abstract fun transcodeInto(data: ByteArray, output: OutputStream): Boolean
companion object {
// 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)
fun jpeg(params: CoverParams): CoverFormat =
CoverFormatImpl("jpg", params, Bitmap.CompressFormat.JPEG)
}
}
private class CoverFormatImpl(
override val extension: String,
val size: Int,
val quality: Int,
val format: Bitmap.CompressFormat,
private val params: CoverParams,
private 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(params.resolution)
inJustDecodeBounds = false
val bitmap = BitmapFactory.decodeByteArray(data, 0, data.size, this) ?: return@run false
bitmap.compress(format, quality, output)
bitmap.compress(format, params.quality, output)
true
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 Auxio Project
* CoverParams.kt is part of Auxio.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.oxycblt.musikr.cover
class CoverParams private constructor(internal val resolution: Int, internal val quality: Int) {
companion object {
fun of(resolution: Int, quality: Int): CoverParams {
check(resolution > 0) { "Resolution must be positive" }
check(quality in 0..100) { "Quality must be between 0 and 100" }
return CoverParams(resolution, quality)
}
}
}