Add Rectangle.fit(Rectangle):Matrix44, FitMethod.Fill, FitMethod.None (#253)

This commit is contained in:
Abe Pazos
2022-08-14 21:17:52 +02:00
committed by GitHub
parent f8d78d31c3
commit ae0c66f268
4 changed files with 237 additions and 110 deletions

View File

@@ -2,120 +2,142 @@ package org.openrndr.extra.imageFit
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.Drawer
import org.openrndr.math.Matrix44
import org.openrndr.math.Vector2
import org.openrndr.math.map
import org.openrndr.math.transforms.transform
import org.openrndr.shape.Rectangle
import kotlin.math.max
import kotlin.math.min
/**
* Available `object-fit` methods (borrowed from CSS)
*/
enum class FitMethod {
/** Cover target area. Crop the source image if needed. */
Cover,
Contain
/** Fit image in target area. Add margins if needed. */
Contain,
/** Deform source image to match the target area. */
Fill,
/** Maintain original image scale, crop to target area size. */
None
/** Not implemented */
// ScaleDown
}
/**
* Transforms [src] and [dest] into a Pair in which one of the
* two rectangles is modified to conform with the [fitMethod]. It uses
* [horizontalPosition] and [verticalPosition] to control positioning / cropping.
*/
fun fitRectangle(
src: Rectangle,
dest: Rectangle,
horizontalPosition: Double = 0.0,
verticalPosition: Double = 0.0,
fitMethod: FitMethod = FitMethod.Cover
src: Rectangle,
dest: Rectangle,
horizontalPosition: Double = 0.0,
verticalPosition: Double = 0.0,
fitMethod: FitMethod = FitMethod.Cover
): Pair<Rectangle, Rectangle> {
val sourceWidth = src.width
val sourceHeight = src.height
val positionNorm = Vector2(horizontalPosition, verticalPosition) * 0.5 + 0.5
val (scaleX, scaleY) = dest.dimensions / src.dimensions
val targetX: Double
val targetY: Double
var targetWidth: Double
var targetHeight: Double
val source: Rectangle
val target: Rectangle
val (x, y) = dest.corner
val width = dest.width
val height = dest.height
when (fitMethod) {
FitMethod.Contain -> {
targetWidth = width
targetHeight = height
if (width <= targetWidth) {
targetWidth = width
targetHeight = (sourceHeight / sourceWidth) * width
}
if (height <= targetHeight) {
targetHeight = height
targetWidth = (sourceWidth / sourceHeight) * height
}
val left = x
val right = x + width - targetWidth
val top = y
val bottom = y + height - targetHeight
targetX = map(-1.0, 1.0, left, right, horizontalPosition)
targetY = map(-1.0, 1.0, top, bottom, verticalPosition)
source = Rectangle(0.0, 0.0, sourceWidth, sourceHeight)
target = Rectangle(targetX, targetY, targetWidth, targetHeight)
return when (fitMethod) {
FitMethod.Cover -> {
val actualDimensions = dest.dimensions / max(scaleX, scaleY)
val actualSrc = Rectangle(
src.corner + (src.dimensions - actualDimensions) * positionNorm,
actualDimensions.x, actualDimensions.y
)
Pair(actualSrc, dest)
}
FitMethod.Cover -> {
targetWidth = sourceWidth
targetHeight = sourceHeight
FitMethod.Contain -> {
val actualDimensions = src.dimensions * min(scaleX, scaleY)
val actualDest = Rectangle(
dest.corner + (dest.dimensions - actualDimensions) * positionNorm,
actualDimensions.x, actualDimensions.y
)
Pair(src, actualDest)
}
if (sourceWidth <= targetWidth) {
targetWidth = sourceWidth
targetHeight = (height / width) * sourceWidth
}
if (sourceHeight <= targetHeight) {
targetHeight = sourceHeight
targetWidth = (width / height) * sourceHeight
}
val left = 0.0
val right = sourceWidth - targetWidth
val top = 0.0
val bottom = sourceHeight - targetHeight
targetX = map(-1.0, 1.0, left, right, horizontalPosition)
targetY = map(-1.0, 1.0, top, bottom, verticalPosition)
source = Rectangle(targetX, targetY, targetWidth, targetHeight)
target = Rectangle(x, y, width, height)
FitMethod.Fill -> Pair(src, dest)
FitMethod.None -> {
val actualSrc = Rectangle(
src.corner + (src.dimensions - dest.dimensions) * positionNorm,
dest.width, dest.height
)
Pair(actualSrc, dest)
}
}
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
): Pair<Rectangle, Rectangle> {
/**
* Helper function that calls [fitRectangle] and returns a [Matrix44] instead
* of a `Pair<Rectangle, Rectangle>`. The returned matrix can be used to draw
* scaled `Shape` or `ShapeContour` objects.
*
* Example scaling and centering a collection of ShapeContours inside
* `drawer.bounds` leaving a margin of 50 pixels:
*
* val src = shapeContours.map { it.bounds }.bounds
* val dest = drawer.bounds.offsetEdges(-50.0)
* val mat = src.fit(dest, fitMethod = FitMethod.Contain)
* drawer.view *= mat
* drawer.contours(shapeContours)
*/
fun Rectangle.fit(
dest: Rectangle,
horizontalPosition: Double = 0.0,
verticalPosition: Double = 0.0,
fitMethod: FitMethod = FitMethod.Cover
): Matrix44 {
val (source, target) = fitRectangle(
img.bounds,
Rectangle(x, y, width, height),
horizontalPosition,
verticalPosition,
fitMethod
this,
dest,
horizontalPosition,
verticalPosition,
fitMethod
)
image(img, source, target)
return Pair(source, target)
return transform {
translate(target.corner)
scale((target.dimensions / source.dimensions).vector3(z = 1.0))
translate(-source.corner)
}
}
/**
* Draws [img] into the bounding box defined by [x], [y], [width] and [height]
* using the specified [fitMethod]
* and aligned or cropped using [horizontalPosition] and [verticalPosition].
*/
fun Drawer.imageFit(
img: ColorBuffer,
bounds: Rectangle = Rectangle(Vector2.ZERO, img.width * 1.0, img.height * 1.0),
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
) = imageFit(
img,
Rectangle(x, y, width, height),
horizontalPosition,
verticalPosition,
fitMethod
)
/**
* Draws [img] into the bounding box defined by [bounds]
* using the specified [fitMethod]
* and aligned or cropped using [horizontalPosition] and [verticalPosition].
*/
fun Drawer.imageFit(
img: ColorBuffer,
bounds: Rectangle = img.bounds,
horizontalPosition: Double = 0.0,
verticalPosition: Double = 0.0,
fitMethod: FitMethod = FitMethod.Cover