From acf1c33c5e5324a323329805baa95781ac91ddca Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Wed, 26 Feb 2025 21:28:54 +0100 Subject: [PATCH] [orx-image-fit] Add `imageFitSub` function Introduce the `imageFitSub` function, enabling subsections of images to be drawn with fit and alignment capabilities. Added a demo showcasing its usage with grid-based rendering and updated dependencies with `orx-noise` for randomness support. --- orx-image-fit/build.gradle.kts | 1 + .../src/commonMain/kotlin/ImageFit.kt | 32 +++++++++++++++++++ .../src/jvmDemo/kotlin/DemoImageFitSub01.kt | 28 ++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 orx-image-fit/src/jvmDemo/kotlin/DemoImageFitSub01.kt diff --git a/orx-image-fit/build.gradle.kts b/orx-image-fit/build.gradle.kts index ab2cecbd..24a6f3e5 100644 --- a/orx-image-fit/build.gradle.kts +++ b/orx-image-fit/build.gradle.kts @@ -21,6 +21,7 @@ kotlin { dependencies { implementation(project(":orx-shapes")) implementation(project(":orx-image-fit")) + implementation(project(":orx-noise")) } } } diff --git a/orx-image-fit/src/commonMain/kotlin/ImageFit.kt b/orx-image-fit/src/commonMain/kotlin/ImageFit.kt index bfb8a813..7dcdc5fb 100644 --- a/orx-image-fit/src/commonMain/kotlin/ImageFit.kt +++ b/orx-image-fit/src/commonMain/kotlin/ImageFit.kt @@ -152,4 +152,36 @@ fun Drawer.imageFit( image(img, source, target) return Pair(source, target) +} + +/** + * Draws a subsection of the given image into a target rectangle within the current `Drawer` bounds, + * using the specified fit method and alignment. + * + * @param img The `ColorBuffer` representing the image to draw. + * @param source The subsection of the image to be fitted, defined as a `Rectangle`. Defaults to the full bounds of the image. + * @param target The rectangle within the `Drawer` bounds where the image will be drawn. Defaults to the full bounds of the `Drawer`. + * @param horizontalPosition Horizontal alignment or cropping position for the image as a normalized value from -1.0 to 1.0. + * @param verticalPosition Vertical alignment or cropping position for the image as a normalized value from -1.0 to 1.0. + * @param fitMethod The method to use for fitting the image into the target rectangle. Defaults to `FitMethod.Cover`. + * @return A `Pair` of `Rectangle` objects, where the first element is the transformed source rectangle, and the second element is the target rectangle. + */ +fun Drawer.imageFitSub( + img: ColorBuffer, + source: Rectangle = img.bounds, + target: Rectangle = this.bounds, + horizontalPosition: Double = 0.0, + verticalPosition: Double = 0.0, + fitMethod: FitMethod = FitMethod.Cover +): Pair { + val (fitSource, fitTarget) = fitRectangle( + source, + target, + horizontalPosition, + verticalPosition, + fitMethod + ) + + image(img, fitSource, fitTarget) + return Pair(source, target) } \ No newline at end of file diff --git a/orx-image-fit/src/jvmDemo/kotlin/DemoImageFitSub01.kt b/orx-image-fit/src/jvmDemo/kotlin/DemoImageFitSub01.kt new file mode 100644 index 00000000..348f9084 --- /dev/null +++ b/orx-image-fit/src/jvmDemo/kotlin/DemoImageFitSub01.kt @@ -0,0 +1,28 @@ +import org.openrndr.application +import org.openrndr.draw.loadImage +import org.openrndr.extra.imageFit.imageFitSub + +import org.openrndr.extra.noise.shapes.uniformSub +import org.openrndr.extra.shapes.primitives.grid +import kotlin.random.Random + +fun main() = application { + configure { + width = 720 + height = 720 + } + program { + val image = loadImage("demo-data/images/image-001.png") + extend { + val grid = drawer.bounds.grid(5, 5).flatten() + val r = Random(seconds.toInt()) + for (cell in grid) { + drawer.imageFitSub( + image, + image.bounds.uniformSub(0.25, 0.75, 0.25, 0.75, random = r), + cell + ) + } + } + } +} \ No newline at end of file