diff --git a/orx-boofcv/src/demo/kotlin/DemoResize01.kt b/orx-boofcv/src/demo/kotlin/DemoResize01.kt index 5c8fdc1b..27fb3a5f 100644 --- a/orx-boofcv/src/demo/kotlin/DemoResize01.kt +++ b/orx-boofcv/src/demo/kotlin/DemoResize01.kt @@ -15,11 +15,11 @@ fun main() { } // Load an image, convert to BoofCV format using orx-boofcv - val input = loadImage("demo-data/images/image-001.png").toPlanarU8() + val input = loadImage("demo-data/images/image-001.png") - val scaled = input.resize(0.5).toColorBuffer() - val scaled2 = input.resize(0.25).toColorBuffer() - val scaled3 = input.resize(0.1).toColorBuffer() + val scaled = input.resizeBy(0.5) + val scaled2 = input.resizeBy(0.25, convertToGray = true) + val scaled3 = input.resizeBy(0.1) extend { drawer.clear(ColorRGBa.BLACK) diff --git a/orx-boofcv/src/demo/kotlin/DemoResize02.kt b/orx-boofcv/src/demo/kotlin/DemoResize02.kt new file mode 100644 index 00000000..a1cebfbf --- /dev/null +++ b/orx-boofcv/src/demo/kotlin/DemoResize02.kt @@ -0,0 +1,32 @@ +import org.openrndr.application +import org.openrndr.boofcv.binding.* +import org.openrndr.color.ColorRGBa +import org.openrndr.draw.loadImage +import org.openrndr.extensions.SingleScreenshot + + +fun main() { + application { + program { + if (System.getProperty("takeScreenshot") == "true") { + extend(SingleScreenshot()) { + this.outputFile = System.getProperty("screenshotPath") + } + } + + // Load an image, convert to BoofCV format using orx-boofcv + val input = loadImage("demo-data/images/image-001.png") + val scaled = input.resizeTo(input.width / 3) + val scaled2 = input.resizeTo(newHeight = input.height / 4, convertToGray = true) + val scaled3 = input.resizeTo(input.width / 5, input.height / 5) + + extend { + drawer.clear(ColorRGBa.BLACK) + drawer.translate(0.0, (height - scaled.bounds.height) / 2.0) + drawer.image(scaled) + drawer.image(scaled2, scaled.bounds.width, scaled.bounds.height - scaled2.height) + drawer.image(scaled3, scaled.bounds.width + scaled2.bounds.width, scaled.bounds.height - scaled3.height) + } + } + } +} \ No newline at end of file diff --git a/orx-boofcv/src/main/kotlin/Distortion.kt b/orx-boofcv/src/main/kotlin/Distortion.kt index c9f91d2b..3b79b948 100644 --- a/orx-boofcv/src/main/kotlin/Distortion.kt +++ b/orx-boofcv/src/main/kotlin/Distortion.kt @@ -2,11 +2,69 @@ package org.openrndr.boofcv.binding import boofcv.abst.distort.FDistort import boofcv.struct.image.ImageBase +import org.openrndr.draw.ColorBuffer +import org.openrndr.draw.ColorType +import kotlin.math.roundToInt -fun >?> ImageBase.resize(scaleX: Double, scaleY: Double = scaleX): T { +fun >?> ImageBase.resizeBy(scaleX: Double, scaleY: Double = scaleX): T { val scaled = this.createNew((this.width * scaleX).toInt(), (this.height * scaleY).toInt()) FDistort(this, scaled).scaleExt().apply() return scaled +} + +fun >?> ImageBase.resizeTo(newWidth: Int? = null, newHeight: Int? = null): T { + val ar = this.width / this.height.toDouble() + + val scaled = (if (newWidth != null && newHeight != null) { + val w = newWidth + val h = newHeight + + this.createNew(w, h) + } else if (newWidth != null && newHeight == null) { + val w = newWidth + val h = newWidth / ar + + this.createNew(w, h.roundToInt()) + } else if (newWidth == null && newHeight != null) { + val w = newHeight * ar + val h = newHeight + + this.createNew(w.roundToInt(), h) + } else { + this.createNew(this.width, this.height) + }) + + FDistort(this, scaled).scaleExt().apply() + + return scaled +} + +fun ColorBuffer.resizeBy(scaleX: Double, scaleY: Double = scaleX, convertToGray: Boolean = false): ColorBuffer { + return if (convertToGray) { + when (this.type) { + ColorType.FLOAT32, ColorType.FLOAT16 -> this.toGrayF32().resizeBy(scaleX, scaleY).toColorBuffer() + else -> this.toGrayU8().resizeBy(scaleX, scaleY).toColorBuffer() + } + } else { + when (this.type) { + ColorType.FLOAT32, ColorType.FLOAT16 -> this.toPlanarF32().resizeBy(scaleX, scaleY).toColorBuffer() + else -> this.toPlanarU8().resizeBy(scaleX, scaleY).toColorBuffer() + } + } +} + +fun ColorBuffer.resizeTo(newWidth: Int? = null, newHeight: Int? = null, convertToGray: Boolean = false): ColorBuffer { + return if (convertToGray) { + when (this.type) { + ColorType.FLOAT32, ColorType.FLOAT16 -> this.toGrayF32().resizeTo(newWidth, newHeight).toColorBuffer() + else -> this.toGrayU8().resizeTo(newWidth, newHeight).toColorBuffer() + } + } else { + when (this.type) { + ColorType.FLOAT32, ColorType.FLOAT16 -> this.toPlanarF32().resizeTo(newWidth, newHeight).toColorBuffer() + else -> this.toPlanarU8().resizeTo(newWidth, newHeight).toColorBuffer() + } + } } \ No newline at end of file