From e391fb2548f2c67f5a7ac87a83e53fe9515e8e2e Mon Sep 17 00:00:00 2001 From: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> Date: Mon, 25 Mar 2024 17:38:56 +0500 Subject: [PATCH] PeachScans: Bump ImageDecoder and fix for new signature (#2078) * Bump ImageDecoder and fix for new signature * fix for empty svg files --- lib-multisrc/peachscan/build.gradle.kts | 4 +- .../tachiyomi/multisrc/peachscan/PeachScan.kt | 5 +- .../multisrc/peachscan/PeachScanUtils.kt | 87 ++++++++++++++++--- 3 files changed, 77 insertions(+), 19 deletions(-) diff --git a/lib-multisrc/peachscan/build.gradle.kts b/lib-multisrc/peachscan/build.gradle.kts index 14692be6b..0006c9a50 100644 --- a/lib-multisrc/peachscan/build.gradle.kts +++ b/lib-multisrc/peachscan/build.gradle.kts @@ -2,8 +2,8 @@ plugins { id("lib-multisrc") } -baseVersionCode = 3 +baseVersionCode = 4 dependencies { - compileOnly("com.github.tachiyomiorg:image-decoder:398d3c074f") + compileOnly("com.github.tachiyomiorg:image-decoder:e08e9be535") } diff --git a/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScan.kt b/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScan.kt index 435b5f4ca..4a1a71225 100644 --- a/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScan.kt +++ b/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScan.kt @@ -32,7 +32,6 @@ import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.io.ByteArrayOutputStream -import java.io.IOException import java.text.SimpleDateFormat import java.util.Locale import java.util.TimeZone @@ -183,7 +182,7 @@ abstract class PeachScan( val zis = ZipInputStream(response.body.byteStream()) val images = generateSequence { zis.nextEntry } - .map { + .mapNotNull { val entryName = it.name val splitEntryName = entryName.split('.') val entryIndex = splitEntryName.first().toInt() @@ -195,7 +194,7 @@ abstract class PeachScan( val svgBytes = zis.readBytes() val svgContent = svgBytes.toString(Charsets.UTF_8) val b64 = dataUriRegex.find(svgContent)?.groupValues?.get(1) - ?: throw IOException("Não foi possível corresponder a imagem no conteúdo SVG") + ?: return@mapNotNull null Base64.decode(b64, Base64.DEFAULT) } diff --git a/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScanUtils.kt b/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScanUtils.kt index 5421f08fe..cba074d98 100644 --- a/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScanUtils.kt +++ b/lib-multisrc/peachscan/src/eu/kanade/tachiyomi/multisrc/peachscan/PeachScanUtils.kt @@ -5,6 +5,7 @@ import android.graphics.Rect import tachiyomi.decoder.ImageDecoder import java.io.ByteArrayInputStream import java.io.IOException +import java.io.InputStream import java.lang.reflect.Method /** @@ -18,40 +19,98 @@ import java.lang.reflect.Method */ object PeachScanUtils { private var decodeMethod: Method + private var newInstanceMethod: Method - private var isNewDecodeMethod = false + private var classSignature = ClassSignature.Newest + + private enum class ClassSignature { + Old, New, Newest + } init { val rectClass = Rect::class.java val booleanClass = Boolean::class.java val intClass = Int::class.java val byteArrayClass = ByteArray::class.java + val inputStreamClass = InputStream::class.java - decodeMethod = try { - isNewDecodeMethod = true + try { + // Mihon Preview r6595+ + classSignature = ClassSignature.Newest - // decode(region, rgb565, sampleSize, applyColorManagement, displayProfile) - ImageDecoder::class.java.getMethod("decode", rectClass, booleanClass, intClass, booleanClass, byteArrayClass) - } catch (e: NoSuchMethodException) { - isNewDecodeMethod = false + // decode(region, sampleSize) + decodeMethod = ImageDecoder::class.java.getMethod( + "decode", + rectClass, + intClass, + ) - // decode(region, rgb565, sampleSize) - ImageDecoder::class.java.getMethod("decode", rectClass, booleanClass, intClass) + // newInstance(stream, cropBorders, displayProfile) + newInstanceMethod = ImageDecoder.Companion::class.java.getMethod( + "newInstance", + inputStreamClass, + booleanClass, + byteArrayClass, + ) + } catch (_: NoSuchMethodException) { + try { + // Mihon Stable & forks + classSignature = ClassSignature.New + + // decode(region, rgb565, sampleSize, applyColorManagement, displayProfile) + decodeMethod = ImageDecoder::class.java.getMethod( + "decode", + rectClass, + booleanClass, + intClass, + booleanClass, + byteArrayClass, + ) + + // newInstance(stream, cropBorders) + newInstanceMethod = ImageDecoder.Companion::class.java.getMethod( + "newInstance", + inputStreamClass, + booleanClass, + ) + } catch (_: NoSuchMethodException) { + // Tachiyomi J2k + classSignature = ClassSignature.Old + + // decode(region, rgb565, sampleSize) + decodeMethod = + ImageDecoder::class.java.getMethod( + "decode", + rectClass, + booleanClass, + intClass, + ) + + // newInstance(stream, cropBorders) + newInstanceMethod = ImageDecoder.Companion::class.java.getMethod( + "newInstance", + inputStreamClass, + booleanClass, + ) + } } } fun decodeImage(data: ByteArray, rgb565: Boolean, filename: String, entryName: String): Bitmap { - val decoder = ImageDecoder.newInstance(ByteArrayInputStream(data)) + val decoder = when (classSignature) { + ClassSignature.Newest -> newInstanceMethod.invoke(ImageDecoder.Companion, ByteArrayInputStream(data), false, null) + else -> newInstanceMethod.invoke(ImageDecoder.Companion, ByteArrayInputStream(data), false) + } as ImageDecoder? if (decoder == null || decoder.width <= 0 || decoder.height <= 0) { throw IOException("Falha ao inicializar o decodificador de imagem") } val rect = Rect(0, 0, decoder.width, decoder.height) - val bitmap = if (isNewDecodeMethod) { - decodeMethod.invoke(decoder, rect, rgb565, 1, false, null) - } else { - decodeMethod.invoke(decoder, rect, rgb565, 1) + val bitmap = when (classSignature) { + ClassSignature.Newest -> decodeMethod.invoke(decoder, rect, 1) + ClassSignature.New -> decodeMethod.invoke(decoder, rect, rgb565, 1, false, null) + else -> decodeMethod.invoke(decoder, rect, rgb565, 1) } as Bitmap? decoder.recycle()