Skip to content

Commit

Permalink
fix(komga): use NightMonkeys WebP reader if present
Browse files Browse the repository at this point in the history
Closes: #1294
  • Loading branch information
gotson committed Nov 29, 2023
1 parent 8832a0d commit 7b1a9e4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions komga/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ dependencies {
runtimeOnly("com.twelvemonkeys.imageio:imageio-webp:3.10.0")
runtimeOnly("com.github.gotson.nightmonkeys:imageio-jxl:0.6.2")
runtimeOnly("com.github.gotson.nightmonkeys:imageio-heif:0.6.2")
runtimeOnly("com.github.gotson.nightmonkeys:imageio-webp:0.6.2")
// support for jpeg2000
runtimeOnly("com.github.jai-imageio:jai-imageio-jpeg2000:1.4.0")
runtimeOnly("org.apache.pdfbox:jbig2-imageio:3.0.4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
import java.io.InputStream
import javax.imageio.ImageIO
import javax.imageio.spi.IIORegistry
import javax.imageio.spi.ImageReaderSpi
import kotlin.math.max
import kotlin.math.min

private val logger = KotlinLogging.logger {}

private const val webpNightMonkeys = "com.github.gotson.nightmonkeys.webp.imageio.plugins.WebpImageReaderSpi"

@Service
class ImageConverter(
private val imageAnalyzer: ImageAnalyzer,
Expand All @@ -26,12 +30,31 @@ class ImageConverter(
val supportedWriteMediaTypes by lazy { ImageIO.getWriterMIMETypes().toList() }

init {
chooseWebpReader()
logger.info { "Supported read formats: $supportedReadFormats" }
logger.info { "Supported read mediaTypes: $supportedReadMediaTypes" }
logger.info { "Supported write formats: $supportedWriteFormats" }
logger.info { "Supported write mediaTypes: $supportedWriteMediaTypes" }
}

private fun chooseWebpReader() {
val providers = IIORegistry.getDefaultInstance().getServiceProviders(
ImageReaderSpi::class.java,
{ it is ImageReaderSpi && it.mimeTypes.contains("image/webp") },
false,
).asSequence().toList()

if (providers.size > 1) {
logger.debug { "WebP reader providers: ${providers.map { it.javaClass.canonicalName }}" }
providers.firstOrNull { it.javaClass.canonicalName == webpNightMonkeys }?.let { nightMonkeys ->
(providers - nightMonkeys).forEach {
logger.debug { "Deregister provider: ${it.javaClass.canonicalName}" }
IIORegistry.getDefaultInstance().deregisterServiceProvider(it)
}
}
}
}

private val supportsTransparency = listOf("png")

fun canConvertMediaType(from: String, to: String) =
Expand Down

0 comments on commit 7b1a9e4

Please sign in to comment.