Merge pull request #49 from ricardomatias/orx-image-fit

Externalize fitting algorithm
This commit is contained in:
Edwin Jakobs
2020-03-15 16:32:02 +01:00
committed by GitHub

View File

@@ -11,28 +11,28 @@ enum class FitMethod {
Contain Contain
} }
fun Drawer.imageFit( fun fitRectangle(
img: ColorBuffer, src: Rectangle,
x: Double = 0.0, dest: Rectangle,
y: Double = 0.0,
width: Double = img.width.toDouble(),
height: Double = img.height.toDouble(),
horizontalPosition: Double = 0.0, horizontalPosition: Double = 0.0,
verticalPosition: Double = 0.0, verticalPosition: Double = 0.0,
fitMethod: FitMethod = FitMethod.Cover fitMethod: FitMethod = FitMethod.Cover
) { ): Pair<Rectangle, Rectangle> {
val sourceWidth = img.width.toDouble() val sourceWidth = src.width
val sourceHeight = img.height.toDouble() val sourceHeight = src.height
var targetX = x
var targetY = y
var targetX: Double
var targetY: Double
var targetWidth: Double var targetWidth: Double
var targetHeight: Double var targetHeight: Double
val source: Rectangle val source: Rectangle
val target: Rectangle val target: Rectangle
val (x, y) = dest.corner
val width = dest.width
val height = dest.height
when (fitMethod) { when (fitMethod) {
FitMethod.Contain -> { FitMethod.Contain -> {
targetWidth = width targetWidth = width
@@ -87,5 +87,26 @@ fun Drawer.imageFit(
} }
} }
return Pair(source, target)
}
fun Drawer.imageFit(
img: ColorBuffer,
x: Double = 0.0,
y: Double = 0.0,
width: Double = img.width.toDouble(),
height: Double = img.height.toDouble(),
horizontalPosition: Double = 0.0,
verticalPosition: Double = 0.0,
fitMethod: FitMethod = FitMethod.Cover
) {
val (source, target) = fitRectangle(
img.bounds,
Rectangle(x, y, width, height),
horizontalPosition,
verticalPosition,
fitMethod
)
image(img, source, target) image(img, source, target)
} }