Skip to content

Commit

Permalink
refactor: simplify image transformation methods in ImageUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Dec 18, 2024
1 parent 92076f2 commit dcb102a
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions app/src/main/kotlin/io/sakurasou/util/ImageUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -178,28 +178,20 @@ object ImageUtils {
}
}

suspend fun transformImage(rawImage: BufferedImage, targetImageType: ImageType, quality: Double): ByteArray {
suspend fun transformImage(rawImage: BufferedImage, targetImageType: ImageType): ByteArray {
return withContext(Dispatchers.IO) {
ByteArrayOutputStream().apply {
Thumbnails.of(rawImage)
.outputFormat(targetImageType.name)
.outputQuality(quality)
.toOutputStream(this)
}.use { it.toByteArray() }
}
}

suspend fun transformImage(
rawImage: BufferedImage,
targetImageType: ImageType,
newWidth: Int,
newHeight: Int,
quality: Double
): ByteArray {
suspend fun transformImage(rawImage: BufferedImage, targetImageType: ImageType, quality: Double): ByteArray {
return withContext(Dispatchers.IO) {
ByteArrayOutputStream().apply {
Thumbnails.of(rawImage)
.size(newWidth, newHeight)
.outputFormat(targetImageType.name)
.outputQuality(quality)
.toOutputStream(this)
Expand All @@ -209,20 +201,20 @@ object ImageUtils {

private fun transformImage(
rawImage: BufferedImage,
targetType: String,
targetImageType: ImageType,
newWidth: Int,
newHeight: Int,
quality: Double
): ByteArray = ByteArrayOutputStream()
.apply {
Thumbnails.of(rawImage)
.size(newWidth, newHeight)
.outputFormat(targetType)
.outputFormat(targetImageType.name)
.outputQuality(quality)
.toOutputStream(this)
}.use { it.toByteArray() }

suspend fun transformImageByWidth(
private suspend fun transformImageByWidth(
rawImage: BufferedImage,
targetImageType: ImageType,
newWidth: Int,
Expand All @@ -232,11 +224,11 @@ object ImageUtils {
val originalWidth = rawImage.width
val originalHeight = rawImage.height
val newHeight = (originalHeight * newWidth) / originalWidth
transformImage(rawImage, targetImageType.name.lowercase(), newWidth, newHeight, quality)
transformImage(rawImage, targetImageType, newWidth, newHeight, quality)
}
}

suspend fun transformImageByHeight(
private suspend fun transformImageByHeight(
rawImage: BufferedImage,
targetImageType: ImageType,
newHeight: Int,
Expand All @@ -246,7 +238,7 @@ object ImageUtils {
val originalWidth = rawImage.width
val originalHeight = rawImage.height
val newWidth = (originalWidth * newHeight) / originalHeight
transformImage(rawImage, targetImageType.name.lowercase(), newWidth, newHeight, quality)
transformImage(rawImage, targetImageType, newWidth, newHeight, quality)
}
}
}

0 comments on commit dcb102a

Please sign in to comment.