Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
laolittle committed Jun 20, 2022
1 parent b032e32 commit 15989b1
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 49 deletions.
24 changes: 21 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
plugins {
val kotlinVersion = "1.6.21"
val kotlinVersion = "1.7.0"
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion

id("net.mamoe.mirai-console") version "2.10.3"
}

group = "org.laolittle.plugin.draw"
version = "1.2.2"
version = "1.2.3"

repositories {
mavenCentral()
Expand All @@ -20,6 +20,24 @@ dependencies {
val smVer = "1.0.8"
compileOnly("com.github.LaoLittle:SkikoMirai:$smVer")
testImplementation("com.github.LaoLittle:SkikoMirai:$smVer")
testImplementation("org.jetbrains.skiko:skiko-awt-runtime-linux-x64:0.7.18")
testImplementation(kotlin("test"))

val osName = System.getProperty("os.name")
val targetOs = when {
osName == "Mac OS X" -> "macos"
osName.startsWith("Win") -> "windows"
osName.startsWith("Linux") -> "linux"
else -> error("Unsupported OS: $osName")
}

val targetArch = when (val osArch = System.getProperty("os.arch")) {
"x86_64", "amd64" -> "x64"
"aarch64" -> "arm64"
else -> error("Unsupported arch: $osArch")
}

val version = "0.7.22"
val target = "${targetOs}-${targetArch}"

testImplementation("org.jetbrains.skiko:skiko-awt-runtime-$target:$version")
}
13 changes: 9 additions & 4 deletions src/main/kotlin/DrawMeme.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import net.mamoe.mirai.utils.info
import org.laolittle.plugin.draw.custom.initCustomMemes
import org.laolittle.plugin.draw.extension.Emoji.EmojiUtil.fullEmojiRegex
import org.laolittle.plugin.draw.extension.Emoji.EmojiUtil.toEmoji
import org.laolittle.plugin.draw.extension.findUserOrNull
import org.laolittle.plugin.draw.meme.*
import org.laolittle.plugin.sendImage
import org.laolittle.plugin.toExternalResource
Expand All @@ -24,7 +25,7 @@ object DrawMeme : KotlinPlugin(
JvmPluginDescription(
id = "org.laolittle.plugin.draw.DrawMeme",
name = "DrawMeme",
version = "1.2.2",
version = "1.2.3",
) {
author("LaoLittle")

Expand Down Expand Up @@ -67,8 +68,8 @@ object DrawMeme : KotlinPlugin(
val sp = msg.split("--")
val content = sp.first()
val filter = sp.getOrElse(1) { "" }
blackWhite(content.trim(), image, filter).toExternalResource().use {
subject.sendImage(it)
blackWhite(content.trim(), image, filter).toExternalResource().use {
subject.sendImage(it)
}
}

Expand Down Expand Up @@ -231,7 +232,11 @@ object DrawMeme : KotlinPlugin(
val y = getFloatOrNull(1) ?: (foo * .1f)
val i = getFloatOrNull(2) ?: 1f

subject.sendImage(marble(skImage, MarbleFilter(x,y,i)).use { bitmap -> SkImage.makeFromBitmap(bitmap) })
subject.sendImage(
marble(
skImage,
MarbleFilter(x, y, i)
).use { bitmap -> SkImage.makeFromBitmap(bitmap) })
}

finding(emojiReg) {
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/General.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ internal fun String.splitSpace(): List<String>? {
}

internal fun SkImage.Companion.makeFromResource(name: String) = makeFromEncoded(
DrawMeme::class.java.getResourceAsStream(name)?.readBytes() ?: throw IllegalStateException("无法找到资源文件: $name")
DrawMeme::class.java.getResourceAsStream(name)?.readBytes()
?: throw IllegalStateException("无法找到资源文件: $name")
)

fun Rect.Companion.makeFromImage(image: SkImage) = Rect(0f, 0f, image.width.toFloat(), image.height.toFloat())
Expand All @@ -57,7 +58,7 @@ fun Path.toExternalResource(formatName: String? = null) = readBytes().toExternal

internal suspend fun MessageEvent.getOrWaitImage(): ByteArray? {
message.forEach { m ->
when(m) {
when (m) {
is Image -> return httpClient.get(m.queryUrl())
is At -> return httpClient.get(m.avatarUrl)
is QuoteReply -> m.source.originalMessage.firstIsInstanceOrNull<Image>()?.let { img ->
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/custom/DrawCustom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ internal fun initCustomMemes() {
startsWith("#${meme.name}") {
val avatars = arrayListOf<Deferred<Image>>()
message.forEach { m ->
if (m is At) {
avatars.add(DrawMeme.async {
val id = m.target
Image.makeFromEncoded(httpClient.get("https://q1.qlogo.cn/g?b=qq&nk=$id&s=640"))
})
}
if (m is At) {
avatars.add(DrawMeme.async {
val id = m.target
Image.makeFromEncoded(httpClient.get("https://q1.qlogo.cn/g?b=qq&nk=$id&s=640"))
})
}
}

meme.makeImage(*avatars.awaitAll().toTypedArray())
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/extension/Match.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.laolittle.plugin.draw
package org.laolittle.plugin.draw.extension

import net.mamoe.mirai.contact.*
import kotlin.math.max
Expand Down
13 changes: 7 additions & 6 deletions src/main/kotlin/meme/BlackWhite.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ internal val customFilter by lazy {

ColorFilter.makeMatrix(ColorMatrix(*arr))
}

else -> {
DrawMeme.logger.error("Unknown Type: $type at $it")
return@m
Expand All @@ -68,13 +69,13 @@ internal val customFilter by lazy {
customs
}
private val defaultFilter = ColorFilter.makeMatrix(
ColorMatrix(
0.33F, 0.38F, 0.29F, 0F, 0F,
0.33F, 0.38F, 0.29F, 0F, 0F,
0.33F, 0.38F, 0.29F, 0F, 0F,
0.33F, 0.38F, 0.29F, 1F, 0F,
)
ColorMatrix(
0.33F, 0.38F, 0.29F, 0F, 0F,
0.33F, 0.38F, 0.29F, 0F, 0F,
0.33F, 0.38F, 0.29F, 0F, 0F,
0.33F, 0.38F, 0.29F, 1F, 0F,
)
)

private val paintFilter = Paint()

Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/meme/FlashImage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fun flashImage(image: ByteArray): Image {

val foo = min(w, h) * .30f
val bar = foo * .5f
return Surface.makeRasterN32Premul(w,h).apply {
return Surface.makeRasterN32Premul(w, h).apply {
writePixels(b, 0, 0)
canvas.apply {
drawPaint(Paint().apply {
Expand All @@ -23,12 +23,12 @@ fun flashImage(image: ByteArray): Image {
translate(w * .5f - bar, h * .5f - bar)

drawPath(Path().apply {
val x1 = foo *.65f
val x1 = foo * .65f

val p1 = Point(bar*.85f, bar*.85f)
val p4 = Point(bar*1.15f, bar*1.15f)
val p2 = Point(p4.x,p1.y)
val p3 = Point(p1.x,p4.y)
val p1 = Point(bar * .85f, bar * .85f)
val p4 = Point(bar * 1.15f, bar * 1.15f)
val p2 = Point(p4.x, p1.y)
val p3 = Point(p1.x, p4.y)
moveTo(x1, 0f)
lineTo(foo * .2f, p3.y)
lineTo(p3)
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/meme/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class MessageImageNode(
drawImageRect(m.image, Rect.makeFromImage(m.image), Rect(0f, 0f, w, h))
translate(0f, 5f)
}

is Message.Plain -> {
m.content.paint(this, 0f, -10f)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/meme/Osu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fun osu(text: String = "osu!"): Image {

if (textWidth <= 250) {
yPos = 137.5F + osuText.height / 2
}else {
} else {
yPos = 210 - (textWidth - 255) / 20
osuText = TextLine.make(text, font.makeWithSize(250F / textWidth * 112.5F))
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/meme/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fun Bitmap.mosaic(size: Int) {
val h = height

for (y in 0 until h step size) for (x in 0 until w step size) {
val color = getColor(x,y)
erase(color, IRect.makeXYWH(x,y,size,size))
val color = getColor(x, y)
erase(color, IRect.makeXYWH(x, y, size, size))
}
}
40 changes: 22 additions & 18 deletions src/test/kotlin/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Tests {
val image = Image.makeFromEncoded(Path("atri.jpg").readBytes())

fun random(): Float {
return Random.nextDouble(-15.0,15.0).toFloat()
return Random.nextDouble(-15.0, 15.0).toFloat()
}

Surface.makeRaster(image.imageInfo).apply {
Expand All @@ -62,12 +62,14 @@ class Tests {
drawImage(image, 0f, 0f)
drawImage(image, random(), random(), Paint().apply {
alpha = 100
colorFilter = ColorFilter.makeMatrix(ColorMatrix(
1F,0F,0.2F,0.3F,0F,
0F,0F,0F,0F,0F,
0F,0F,0F,0F,0F,
0F,0F,0F,1F,0F,
))
colorFilter = ColorFilter.makeMatrix(
ColorMatrix(
1F, 0F, 0.2F, 0.3F, 0F,
0F, 0F, 0F, 0F, 0F,
0F, 0F, 0F, 0F, 0F,
0F, 0F, 0F, 1F, 0F,
)
)
})
}
}.makeImageSnapshot().encodeToData()?.let {
Expand All @@ -83,13 +85,15 @@ class Tests {

canvas.apply {
drawImage(image, 0f, 0f, Paint().apply {
colorFilter = ColorFilter.makeMatrix(ColorMatrix(
//R G B A ?
1F,0F,0F,0F,0F, // R
0F,1F,0F,0F,0F, // G
0.5F,0.6F,1F,.3F,0F, // B
0F,0F,0F,1F,0F, // A
))
colorFilter = ColorFilter.makeMatrix(
ColorMatrix(
//R G B A ?
1F, 0F, 0F, 0F, 0F, // R
0F, 1F, 0F, 0F, 0F, // G
0.5F, 0.6F, 1F, .3F, 0F, // B
0F, 0F, 0F, 1F, 0F, // A
)
)
})
}
}.makeImageSnapshot().encodeToData()?.let {
Expand All @@ -105,18 +109,18 @@ class Tests {
}

recorder.recordingCanvas?.apply {
drawCircle(0f,0f,100f, Paint())
drawImage(image, 0f,0f)
drawCircle(0f, 0f, 100f, Paint())
drawImage(image, 0f, 0f)

}

val pic = recorder.finishRecordingAsPicture()
pic.serializeToData().bytes.also(Path("pic.bin")::writeBytes)

Surface.makeRaster(image.imageInfo).apply {
canvas.drawImage(image,0f,0f)
canvas.drawImage(image, 0f, 0f)
canvas.drawPicture(pic)
// pic.playback(canvas)
// pic.playback(canvas)
}.makeImageSnapshot().bytes.also(Path("outpic.png")::writeBytes)
}

Expand Down

0 comments on commit 15989b1

Please sign in to comment.