Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Sync from next
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz committed Jul 10, 2021
1 parent cdd2791 commit 0c5d2fc
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 43 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ group=com.soywiz.korlibs.korim
version=2.0.0-SNAPSHOT

# kotlinx
korioVersion=2.2.0
kormaVersion=2.2.1
korioVersion=2.2.1
kormaVersion=2.2.2

# bintray location
project.bintray.org=korlibs
Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ case "`uname`" in
Darwin* )
darwin=true
;;
MINGW* )
MSYS* | MINGW* )
msys=true
;;
NONSTOP* )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ abstract class Bitmap(
}
}

fun <T : Bitmap> T.createWithThisFormatTyped(width: Int, height: Int): T = this.createWithThisFormat(width, height) as T
fun <T : Bitmap> T.createWithThisFormatTyped(width: Int, height: Int): T = this.createWithThisFormat(width, height).fastCastTo<T>()

fun <T : Bitmap> T.extract(x: Int, y: Int, width: Int, height: Int): T {
val out = this.createWithThisFormatTyped(width, height)
Expand Down
35 changes: 23 additions & 12 deletions korim/src/commonMain/kotlin/com/soywiz/korim/bitmap/BitmapSlice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ import com.soywiz.korim.color.*
import com.soywiz.korio.resources.*
import com.soywiz.korma.geom.*

interface BmpCoords {
val tl_x: Float
val tl_y: Float

val tr_x: Float
val tr_y: Float

val br_x: Float
val br_y: Float

val bl_x: Float
val bl_y: Float
}

/**
* @property virtFrame This defines a virtual frame [RectangleInt] which surrounds the bounds [RectangleInt] of the [Bitmap].
* It is used in a trimmed texture atlas to specify the original size of a single texture.
Expand All @@ -18,7 +32,7 @@ abstract class BmpSlice(
val name: String? = null,
val rotated: Boolean = false,
val virtFrame: RectangleInt? = null
) : Extra, Resourceable<BmpSlice> {
) : Extra, BmpCoords, Resourceable<BmpSlice> {
override fun getOrNull() = this
override suspend fun get() = this
open val bmp: Bitmap = bmpBase
Expand Down Expand Up @@ -53,17 +67,14 @@ abstract class BmpSlice(

var parent: Any? = null

val tl_x = p0.x.toFloat()
val tl_y = p0.y.toFloat()

val tr_x = p1.x.toFloat()
val tr_y = p1.y.toFloat()

val br_x = p2.x.toFloat()
val br_y = p2.y.toFloat()

val bl_x = p3.x.toFloat()
val bl_y = p3.y.toFloat()
override val tl_x = p0.x.toFloat()
override val tl_y = p0.y.toFloat()
override val tr_x = p1.x.toFloat()
override val tr_y = p1.y.toFloat()
override val br_x = p2.x.toFloat()
override val br_y = p2.y.toFloat()
override val bl_x = p3.x.toFloat()
override val bl_y = p3.y.toFloat()

val rotatedAngle: Int = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ import com.soywiz.korma.geom.*
import com.soywiz.korma.geom.vector.*
import kotlin.math.*

class NinePatchInfo(
class NinePatchInfo constructor(
val xranges: List<Pair<Boolean, IntRange>>,
val yranges: List<Pair<Boolean, IntRange>>,
val width: Int,
val height: Int
val height: Int,
val content: BmpSlice? = null
) {
constructor(
width: Int, height: Int,
left: Int, top: Int, right: Int, bottom: Int
left: Int, top: Int, right: Int, bottom: Int,
content: BmpSlice? = null
) : this(
listOf(false to (0 until left), true to (left until right), false to (right until width)),
listOf(false to (0 until top), true to (top until bottom), false to (bottom until height)),
width, height
width, height, content
)

class AxisSegment(val scaled: Boolean, val range: IntRange) {
Expand All @@ -44,9 +46,9 @@ class NinePatchInfo(
}

class AxisInfo(ranges: List<Pair<Boolean, IntRange>>, val totalLen: Int) {
val segments = ranges.map { AxisSegment(it.first, it.second) }
val fixedLen = max(1, segments.filter { it.fixed }.map { it.length }.sum())
val scaledLen = max(1, segments.filter { it.scaled }.map { it.length }.sum())
val segments = ranges.map { AxisSegment(it.first, it.second) }.toFastList()
val fixedLen = max(1, segments.filter { it.fixed }.sumOf { it.length })
val scaledLen = max(1, segments.filter { it.scaled }.sumOf { it.length })
}

val xaxis = AxisInfo(xranges, width)
Expand All @@ -58,22 +60,30 @@ class NinePatchInfo(
val fixedWidth = xaxis.fixedLen
val fixedHeight = yaxis.fixedLen

val totalSegments get() = xsegments.size * ysegments.size

val scaledWidth = xaxis.scaledLen
val scaledHeight = yaxis.scaledLen

class Segment(val rect: RectangleInt, val x: AxisSegment, val y: AxisSegment) : Extra by Extra.Mixin() {
class Segment(val info: NinePatchInfo, val rect: RectangleInt, val x: AxisSegment, val y: AxisSegment) : Extra by Extra.Mixin() {
val scaleX: Boolean = x.scaled
val scaleY: Boolean = y.scaled

val bmpSlice = info.content?.slice(this.rect)
val bmp by lazy { bmpSlice?.extract() }
}

val segments = ysegments.map { y ->
xsegments.map { x ->
Segment(
this,
RectangleInt.fromBounds(x.range.start, y.range.start, x.range.endExclusive, y.range.endExclusive),
x, y
)
}
}
}.toFastList()
}.toFastList()

//init { println("Created NinePatchInfo") }

fun computeScale(
bounds: RectangleInt,
Expand Down Expand Up @@ -192,8 +202,10 @@ open class NinePatchBmpSlice(
val bmpSlice: BmpSlice = content
) {
companion object {
//val DUMMY = createSimple(Bitmap32(3, 3).slice(), 1, 1, 2, 2)

fun createSimple(bmp: BmpSlice, left: Int, top: Int, right: Int, bottom: Int): NinePatchBmpSlice {
return NinePatchBmpSlice(bmp, NinePatchInfo(bmp.width, bmp.height, left, top, right, bottom))
return NinePatchBmpSlice(bmp, NinePatchInfo(bmp.width, bmp.height, left, top, right, bottom, bmp))
}

operator fun invoke(bmp: Bitmap) = invoke(bmp.slice())
Expand All @@ -207,7 +219,7 @@ open class NinePatchBmpSlice(
NinePatchInfo(
(1 until bmpSlice.width - 1).computeRle { topPixels[it].a != 0 },
(1 until bmpSlice.height - 1).computeRle { leftPixels[it].a != 0 },
content.width, content.height
content.width, content.height, content
)
},
bmpSlice = bmpSlice
Expand All @@ -220,15 +232,7 @@ open class NinePatchBmpSlice(
val dwidth get() = width.toDouble()
val dheight get() = height.toDouble()

val NinePatchInfo.Segment.bmpSlice by Extra.PropertyThis<NinePatchInfo.Segment, BmpSlice> {
this@NinePatchBmpSlice.content.slice(this.rect)
}

val NinePatchInfo.Segment.bmp by Extra.PropertyThis<NinePatchInfo.Segment, Bitmap> {
this@NinePatchBmpSlice.bmpSlice.extract()
}

fun getSegmentBmpSlice(segment: NinePatchInfo.Segment) = segment.bmpSlice
fun getSegmentBmpSlice(segment: NinePatchInfo.Segment) = segment.bmpSlice!!

fun <T : Bitmap> drawTo(
other: T,
Expand All @@ -238,7 +242,7 @@ open class NinePatchBmpSlice(
): T {
other.context2d(antialiased) {
info.computeScale(bounds) { seg, segLeft, segTop, segWidth, segHeight ->
drawImage(seg.bmp, segLeft, segTop, segWidth.toInt(), segHeight.toInt())
drawImage(seg.bmp!!, segLeft, segTop, segWidth.toInt(), segHeight.toInt())
if (drawRegions) {
stroke(Colors.RED) { rect(segLeft, segTop, segWidth, segHeight) }
}
Expand Down Expand Up @@ -267,7 +271,7 @@ fun BmpSlice.asNinePatchSimpleRatio(left: Double, top: Double, right: Double, bo
(left * width).toInt(), (top * height).toInt(),
(right * width).toInt(), (bottom * height).toInt()
)
fun BmpSlice.asNinePatchSimple(left: Int, top: Int, right: Int, bottom: Int) = NinePatchBmpSlice(this, NinePatchInfo(width, height, left, top, right, bottom))
fun BmpSlice.asNinePatchSimple(left: Int, top: Int, right: Int, bottom: Int) = NinePatchBmpSlice.createSimple(this, left, top, right, bottom)
fun Bitmap.asNinePatchSimple(left: Int, top: Int, right: Int, bottom: Int) = this.slice().asNinePatchSimple(left, top, right, bottom)

suspend fun VfsFile.readNinePatch(format: ImageFormat = RegisteredImageFormats) = NinePatchBitmap32(readBitmap(format).toBMP32())
Expand All @@ -277,7 +281,7 @@ private inline fun <T, R : Any> Iterable<T>.computeRle(callback: (T) -> R): List
var pos = 0
var startpos = 0
lateinit var lastRes: R
val out = arrayListOf<Pair<R, IntRange>>()
val out = FastArrayList<Pair<R, IntRange>>()
for (it in this) {
val current = callback(it)
if (!first) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ data class ImageDecodingProps(
val filename: String = "unknown",
val width: Int? = null,
val height: Int? = null,
override var extra: LinkedHashMap<String, Any?>? = null
override var extra: ExtraType = null
) : Extra

data class ImageEncodingProps(
val filename: String = "",
val quality: Double = 0.81,
override var extra: LinkedHashMap<String, Any?>? = null
override var extra: ExtraType = null
) : Extra

0 comments on commit 0c5d2fc

Please sign in to comment.