From aea681446e5b5f503039fc975b570377a4f15f76 Mon Sep 17 00:00:00 2001 From: pfs-15 Date: Mon, 10 Feb 2020 18:40:40 +0100 Subject: [PATCH 1/2] added orx-object-fit --- orx-object-fit/README.md | 20 +++++ orx-object-fit/src/main/kotlin/ObjectFit.kt | 83 +++++++++++++++++++++ settings.gradle | 3 +- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 orx-object-fit/README.md create mode 100644 orx-object-fit/src/main/kotlin/ObjectFit.kt diff --git a/orx-object-fit/README.md b/orx-object-fit/README.md new file mode 100644 index 00000000..4c689b70 --- /dev/null +++ b/orx-object-fit/README.md @@ -0,0 +1,20 @@ +# orx-object-fit + +Fits images in frames with two options, contain and cover. Similar to CSS object-fit (https://www.w3schools.com/css/css3_object-fit.asp) + +## usage + +`objectFit(img: ColorBuffer, x: Double, y: Double, w: Double, h: Double, fitMethod, horizontalPosition:Double, verticalPosition:Double)` + + +objectFitType + - `contain` + - `cover` + +horizontal values + - left ... right + - `-1.0` ... `1.0` + + vertical values + - top ... bottom + - `-1.0` ... `1.0` \ No newline at end of file diff --git a/orx-object-fit/src/main/kotlin/ObjectFit.kt b/orx-object-fit/src/main/kotlin/ObjectFit.kt new file mode 100644 index 00000000..6db83d22 --- /dev/null +++ b/orx-object-fit/src/main/kotlin/ObjectFit.kt @@ -0,0 +1,83 @@ +package org.openrndr.extras.objectFit + +import org.openrndr.draw.ColorBuffer +import org.openrndr.draw.Drawer +import org.openrndr.math.map +import org.openrndr.shape.Rectangle + + +enum class FitMethod { + Cover, + Contain +} + +fun Drawer.objectFit(img: ColorBuffer, x: Double = 0.0, y: Double = 0.0, w: Double, h: Double, fitMethod:FitMethod = FitMethod.Cover, horizontalPosition:Double = 0.0, verticalPosition:Double = 0.0) { + val sourceWidth = img.width.toDouble() + val sourceHeight = img.height.toDouble() + + var targetX = x + var targetY = y + + var targetWidth: Double + var targetHeight: Double + + var source = Rectangle(x, y, w, h) + var target = Rectangle(x, y, w, h) + + if (fitMethod == FitMethod.Contain) { + + targetWidth = w + targetHeight = h + + if (w <= targetWidth) { + targetWidth = w + targetHeight = (sourceHeight / sourceWidth) * w + } + + if (h <= targetHeight) { + targetHeight = h + targetWidth = (sourceWidth / sourceHeight) * h + } + + val left = x + val right = x + w - targetWidth + val top = y + val bottom = y + h - 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) + } + + + if (fitMethod == FitMethod.Cover) { + + targetWidth = sourceWidth + targetHeight = sourceHeight + + if (sourceWidth <= targetWidth) { + targetWidth = sourceWidth + targetHeight = (h / w) * sourceWidth + } + + if (sourceHeight <= targetHeight) { + targetHeight = sourceHeight + targetWidth = (w / h) * 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, w, h) + } + + image(img, source, target) +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index e3d1033d..37171ccf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -28,4 +28,5 @@ include 'orx-camera', 'orx-kinect-v1-natives-linux-x64', 'orx-kinect-v1-natives-macos', 'orx-kinect-v1-natives-windows', - 'orx-kinect-v1-demo' + 'orx-kinect-v1-demo', + 'orx-object-fit' From 3c3e6eed08b1c0ffaab70dcca6ba2d4afeea28f9 Mon Sep 17 00:00:00 2001 From: pfs-15 Date: Mon, 10 Feb 2020 19:27:53 +0100 Subject: [PATCH 2/2] improvements to orx-object-fit -> orx-image-fit --- {orx-object-fit => orx-image-fit}/README.md | 6 +- orx-image-fit/src/main/kotlin/ImageFit.kt | 82 ++++++++++++++++++++ orx-object-fit/src/main/kotlin/ObjectFit.kt | 83 --------------------- settings.gradle | 2 +- 4 files changed, 86 insertions(+), 87 deletions(-) rename {orx-object-fit => orx-image-fit}/README.md (63%) create mode 100644 orx-image-fit/src/main/kotlin/ImageFit.kt delete mode 100644 orx-object-fit/src/main/kotlin/ObjectFit.kt diff --git a/orx-object-fit/README.md b/orx-image-fit/README.md similarity index 63% rename from orx-object-fit/README.md rename to orx-image-fit/README.md index 4c689b70..0587f658 100644 --- a/orx-object-fit/README.md +++ b/orx-image-fit/README.md @@ -1,13 +1,13 @@ -# orx-object-fit +# orx-image-fit Fits images in frames with two options, contain and cover. Similar to CSS object-fit (https://www.w3schools.com/css/css3_object-fit.asp) ## usage -`objectFit(img: ColorBuffer, x: Double, y: Double, w: Double, h: Double, fitMethod, horizontalPosition:Double, verticalPosition:Double)` +`imageFit(img: ColorBuffer, x: Double, y: Double, w: Double, h: Double, fitMethod, horizontalPosition:Double, verticalPosition:Double)` -objectFitType +fitMethod - `contain` - `cover` diff --git a/orx-image-fit/src/main/kotlin/ImageFit.kt b/orx-image-fit/src/main/kotlin/ImageFit.kt new file mode 100644 index 00000000..62a4cfae --- /dev/null +++ b/orx-image-fit/src/main/kotlin/ImageFit.kt @@ -0,0 +1,82 @@ +package org.openrndr.extras.imageFit + +import org.openrndr.draw.ColorBuffer +import org.openrndr.draw.Drawer +import org.openrndr.math.map +import org.openrndr.shape.Rectangle + + +enum class FitMethod { + Cover, + Contain +} + +fun Drawer.imageFit(img: ColorBuffer, x: Double = 0.0, y: Double = 0.0, w: Double, h: Double, fitMethod:FitMethod = FitMethod.Cover, horizontalPosition:Double = 0.0, verticalPosition:Double = 0.0) { + val sourceWidth = img.width.toDouble() + val sourceHeight = img.height.toDouble() + + var targetX = x + var targetY = y + + var targetWidth: Double + var targetHeight: Double + + val source: Rectangle + val target: Rectangle + + when (fitMethod) { + FitMethod.Contain -> { + targetWidth = w + targetHeight = h + + if (w <= targetWidth) { + targetWidth = w + targetHeight = (sourceHeight / sourceWidth) * w + } + + if (h <= targetHeight) { + targetHeight = h + targetWidth = (sourceWidth / sourceHeight) * h + } + + val left = x + val right = x + w - targetWidth + val top = y + val bottom = y + h - 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) + } + + FitMethod.Cover -> { + targetWidth = sourceWidth + targetHeight = sourceHeight + + if (sourceWidth <= targetWidth) { + targetWidth = sourceWidth + targetHeight = (h / w) * sourceWidth + } + + if (sourceHeight <= targetHeight) { + targetHeight = sourceHeight + targetWidth = (w / h) * 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, w, h) + } + } + + image(img, source, target) +} \ No newline at end of file diff --git a/orx-object-fit/src/main/kotlin/ObjectFit.kt b/orx-object-fit/src/main/kotlin/ObjectFit.kt deleted file mode 100644 index 6db83d22..00000000 --- a/orx-object-fit/src/main/kotlin/ObjectFit.kt +++ /dev/null @@ -1,83 +0,0 @@ -package org.openrndr.extras.objectFit - -import org.openrndr.draw.ColorBuffer -import org.openrndr.draw.Drawer -import org.openrndr.math.map -import org.openrndr.shape.Rectangle - - -enum class FitMethod { - Cover, - Contain -} - -fun Drawer.objectFit(img: ColorBuffer, x: Double = 0.0, y: Double = 0.0, w: Double, h: Double, fitMethod:FitMethod = FitMethod.Cover, horizontalPosition:Double = 0.0, verticalPosition:Double = 0.0) { - val sourceWidth = img.width.toDouble() - val sourceHeight = img.height.toDouble() - - var targetX = x - var targetY = y - - var targetWidth: Double - var targetHeight: Double - - var source = Rectangle(x, y, w, h) - var target = Rectangle(x, y, w, h) - - if (fitMethod == FitMethod.Contain) { - - targetWidth = w - targetHeight = h - - if (w <= targetWidth) { - targetWidth = w - targetHeight = (sourceHeight / sourceWidth) * w - } - - if (h <= targetHeight) { - targetHeight = h - targetWidth = (sourceWidth / sourceHeight) * h - } - - val left = x - val right = x + w - targetWidth - val top = y - val bottom = y + h - 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) - } - - - if (fitMethod == FitMethod.Cover) { - - targetWidth = sourceWidth - targetHeight = sourceHeight - - if (sourceWidth <= targetWidth) { - targetWidth = sourceWidth - targetHeight = (h / w) * sourceWidth - } - - if (sourceHeight <= targetHeight) { - targetHeight = sourceHeight - targetWidth = (w / h) * 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, w, h) - } - - image(img, source, target) -} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 37171ccf..20b1ee5e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -29,4 +29,4 @@ include 'orx-camera', 'orx-kinect-v1-natives-macos', 'orx-kinect-v1-natives-windows', 'orx-kinect-v1-demo', - 'orx-object-fit' + 'orx-image-fit'