[orx-jumpflood] Make module multiplatform

This commit is contained in:
Edwin Jakobs
2023-01-12 15:54:53 +01:00
parent 58d15a1e8b
commit e27f7eb4cb
51 changed files with 571 additions and 177 deletions

View File

@@ -0,0 +1,123 @@
package org.openrndr.extra.jumpfill
import org.openrndr.draw.*
import org.openrndr.extra.fx.blend.Passthrough
import org.openrndr.extra.parameters.BooleanParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.shape.IntRectangle
import kotlin.math.ceil
import kotlin.math.log2
import kotlin.math.max
import kotlin.math.pow
@Description("Clustered field")
class ClusteredField(decodeMode: DecodeMode = DecodeMode.DIRECTION,
private val outputDistanceToContours: Boolean = true) : Filter1to1() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.5
@DoubleParameter("distance scale", 0.0, 1.0)
var distanceScale = 1.0
@BooleanParameter("normalized distance")
var normalizedDistance = false
@BooleanParameter("unit direction")
var unitDirection = false
@BooleanParameter("flip v direction")
var flipV = true
private val encodeFilter = EncodePoints()
private var encoded: ColorBuffer? = null
private val contourFilter = IdContourPoints()
private var contoured: ColorBuffer? = null
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection(decodeMode)
private var fit: ColorBuffer? = null
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
val advisedWidth = 2.0.pow(ceil(log2(source[0].effectiveWidth.toDouble()))).toInt()
val advisedHeight = 2.0.pow(ceil(log2(source[0].effectiveHeight.toDouble()))).toInt()
val advisedSize = max(advisedWidth, advisedHeight)
fit?.let {
if (it.effectiveWidth != advisedSize || it.effectiveHeight != advisedSize) {
it.destroy()
fit = null
encoded?.destroy()
encoded = null
contoured?.destroy()
contoured = null
jumpFlooder?.destroy()
jumpFlooder = null
}
}
if (fit == null) {
fit = colorBuffer(advisedSize, advisedSize, type=ColorType.FLOAT32)
}
source[0].copyTo(
fit!!,
sourceRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight),
targetRectangle = IntRectangle(
0,
advisedSize - source[0].effectiveHeight,
source[0].effectiveWidth,
source[0].effectiveHeight
)
)
if (encoded == null) {
encoded = colorBuffer(advisedSize, advisedSize, format = ColorFormat.RGBa, type = ColorType.FLOAT32)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(advisedSize, advisedSize, encodePoints = Passthrough())
}
if (outputDistanceToContours && contoured == null) {
contoured = colorBuffer(advisedSize, advisedSize, type = ColorType.FLOAT32)
}
encodeFilter.apply(fit!!, encoded!!)
var result = jumpFlooder!!.jumpFlood(encoded!!)
if (outputDistanceToContours) {
contourFilter.apply(result, contoured!!)
result = jumpFlooder!!.jumpFlood(contoured!!)
}
decodeFilter.outputIds = true
decodeFilter.originalSize = Vector2(source[0].width.toDouble(), source[0].height.toDouble())
decodeFilter.distanceScale = distanceScale
decodeFilter.normalizedDistance = normalizedDistance
decodeFilter.unitDirection = unitDirection
decodeFilter.flipV = flipV
decodeFilter.apply(arrayOf(result, encoded!!), arrayOf(result))
result.copyTo(
target[0],
sourceRectangle = IntRectangle(
0,
advisedSize - source[0].effectiveHeight,
source[0].effectiveWidth,
source[0].effectiveHeight
),
targetRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight)
)
}
override fun destroy() {
encodeFilter.destroy()
contourFilter.destroy()
fit?.destroy()
encoded?.destroy()
contoured?.destroy()
jumpFlooder?.destroy()
}
}

View File

@@ -0,0 +1,100 @@
package org.openrndr.extra.jumpfill
import org.openrndr.draw.*
import org.openrndr.extra.parameters.BooleanParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.shape.IntRectangle
import kotlin.math.ceil
import kotlin.math.log2
import kotlin.math.max
import kotlin.math.pow
@Description("Directional field")
class DirectionalField : Filter1to1() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.5
@DoubleParameter("distance scale", 0.0, 1.0)
var distanceScale = 1.0
@BooleanParameter("normalized distance")
var normalizedDistance = false
@BooleanParameter("unit direction")
var unitDirection = false
@BooleanParameter("flip v direction")
var flipV = true
private val thresholdFilter = Threshold()
private var thresholded: ColorBuffer? = null
private val contourFilter = ContourPoints()
private var contoured: ColorBuffer? = null
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection()
private var fit: ColorBuffer? = null
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
val advisedWidth = 2.0.pow(ceil(log2(source[0].effectiveWidth.toDouble()))).toInt()
val advisedHeight = 2.0.pow(ceil(log2(source[0].effectiveHeight.toDouble()))).toInt()
val advisedSize = max(advisedWidth, advisedHeight)
fit?.let {
if (it.effectiveWidth != advisedSize || it.effectiveHeight != advisedSize) {
it.destroy()
fit = null
thresholded?.destroy()
thresholded = null
contoured?.destroy()
contoured = null
jumpFlooder?.destroy()
jumpFlooder = null
}
}
if (fit == null) {
fit = colorBuffer(advisedSize, advisedSize)
}
source[0].copyTo(fit!!,
sourceRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight),
targetRectangle = IntRectangle(0, advisedSize-source[0].effectiveHeight, source[0].effectiveWidth, source[0].effectiveHeight)
)
if (thresholded == null) {
thresholded = colorBuffer(advisedSize, advisedSize, format = ColorFormat.R)
}
if (contoured == null) {
contoured = colorBuffer(advisedSize, advisedSize, format = ColorFormat.R)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(advisedSize, advisedSize)
}
thresholdFilter.threshold = threshold
thresholdFilter.apply(fit!!, thresholded!!)
contourFilter.apply(thresholded!!, contoured!!)
val result = jumpFlooder!!.jumpFlood(contoured!!)
decodeFilter.originalSize = Vector2(source[0].width.toDouble(), source[0].height.toDouble())
decodeFilter.distanceScale = distanceScale
decodeFilter.normalizedDistance = normalizedDistance
decodeFilter.unitDirection = unitDirection
decodeFilter.flipV = flipV
decodeFilter.apply(arrayOf(result, thresholded!!), arrayOf(result))
result.copyTo(target[0],
sourceRectangle = IntRectangle(0, advisedSize-source[0].effectiveHeight, source[0].effectiveWidth, source[0].effectiveHeight),
targetRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight))
}
override fun destroy() {
thresholdFilter.destroy()
contourFilter.destroy()
fit?.destroy()
thresholded?.destroy()
contoured?.destroy()
jumpFlooder?.destroy()
}
}

View File

@@ -0,0 +1,84 @@
package org.openrndr.extra.jumpfill
import org.openrndr.draw.*
import org.openrndr.extra.parameters.BooleanParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.shape.IntRectangle
import kotlin.math.ceil
import kotlin.math.log2
import kotlin.math.max
import kotlin.math.pow
@Description("Distance field")
class DistanceField : Filter1to1() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.5
@DoubleParameter("distance scale", 0.0, 1.0)
var distanceScale = 1.0
private val thresholdFilter = Threshold()
private var thresholded: ColorBuffer? = null
private val contourFilter = ContourPoints()
private var contoured: ColorBuffer? = null
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDistance()
private var fit: ColorBuffer? = null
@BooleanParameter("signed distance")
var signedDistance = true
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
val advisedWidth = 2.0.pow(ceil(log2(source[0].effectiveWidth.toDouble()))).toInt()
val advisedHeight = 2.0.pow(ceil(log2(source[0].effectiveHeight.toDouble()))).toInt()
val advisedSize = max(advisedWidth, advisedHeight)
fit?.let {
if (it.effectiveWidth != advisedSize || it.effectiveHeight != advisedSize) {
it.destroy()
fit = null
thresholded?.destroy()
thresholded = null
contoured?.destroy()
contoured = null
jumpFlooder?.destroy()
jumpFlooder = null
}
}
if (fit == null) {
fit = colorBuffer(advisedSize, advisedSize)
}
source[0].copyTo(fit!!,
sourceRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight),
targetRectangle = IntRectangle(0, advisedSize-source[0].effectiveHeight, source[0].effectiveWidth, source[0].effectiveHeight)
)
if (thresholded == null) {
thresholded = colorBuffer(advisedSize, advisedSize, format = ColorFormat.R)
}
if (contoured == null) {
contoured = colorBuffer(advisedSize, advisedSize, format = ColorFormat.R)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(advisedSize, advisedSize)
}
thresholdFilter.threshold = threshold
thresholdFilter.apply(fit!!, thresholded!!)
contourFilter.apply(thresholded!!, contoured!!)
val result = jumpFlooder!!.jumpFlood(contoured!!)
decodeFilter.originalSize = Vector2(source[0].width.toDouble(), source[0].height.toDouble())
decodeFilter.distanceScale = distanceScale
decodeFilter.apply(arrayOf(result, thresholded!!), arrayOf(result))
result.copyTo(target[0],
sourceRectangle = IntRectangle(0, advisedSize-source[0].effectiveHeight, source[0].effectiveWidth, source[0].effectiveHeight),
targetRectangle = IntRectangle(0, 0, source[0].effectiveWidth, source[0].effectiveHeight)
)
}
}

View File

@@ -0,0 +1,188 @@
package org.openrndr.extra.jumpfill
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.fx.blend.Passthrough
import org.openrndr.extra.jumpflood.*
import org.openrndr.math.Vector2
import kotlin.math.*
class EncodePoints : Filter(filterShaderFromCode(jf_encode_points, "encode-points"))
class EncodeSubpixel : Filter(filterShaderFromCode(jf_encode_subpixel, "encode-subpixel")) {
var threshold by parameters
init {
threshold = 0.5
}
}
class JumpFlood : Filter(filterShaderFromCode(jf_jumpflood, "jumpflood")) {
var maxSteps: Int by parameters
var step: Int by parameters
}
enum class DecodeMode(val shaderDefine: String) {
DISTANCE("OUTPUT_DISTANCE"),
DIRECTION("OUTPUT_DIRECTION")
}
class PixelDirection(val decodeMode: DecodeMode = DecodeMode.DIRECTION) :
Filter(
filterShaderFromCode(
"#define ${decodeMode.shaderDefine}\n $jf_pixel_direction",
"pixel-direction")
) {
var distanceScale: Double by parameters
var originalSize: Vector2 by parameters
var normalizedDistance: Boolean by parameters
var unitDirection: Boolean by parameters
var flipV: Boolean by parameters
var outputIds: Boolean by parameters
init {
distanceScale = 1.0
originalSize = Vector2(512.0, 512.0)
normalizedDistance = false
unitDirection = false
flipV = true
outputIds = false
}
}
class PixelDistance : Filter(filterShaderFromCode(jf_pixel_distance, "pixel-distance")) {
var distanceScale: Double by parameters
var originalSize: Vector2 by parameters
var signedBit: Boolean by parameters
var signedDistance: Boolean by parameters
init {
distanceScale = 1.0
originalSize = Vector2(512.0, 512.0)
signedBit = true
signedDistance = false
}
}
class ContourPoints : Filter(filterShaderFromCode(jf_contour_points, "contour-points"))
class IdContourPoints : Filter(filterShaderFromCode(jf_id_contours, "id-contour-points"))
class Threshold : Filter(filterShaderFromCode(jf_threshold, "threshold")) {
var threshold: Double by parameters
init {
threshold = 0.5
}
}
class AlphaThreshold : Filter(filterShaderFromCode(jf_alpha_threshold, "alpha-threshold")) {
var threshold: Double by parameters
init {
threshold = 0.5
}
}
private val encodePoints by lazy { persistent { EncodePoints() } }
private val pixelDistance by lazy { persistent { PixelDistance() } }
private val pixelDirection by lazy { persistent { PixelDirection() } }
private val contourPoints by lazy { persistent { ContourPoints() } }
private val threshold by lazy { persistent { Threshold() } }
private val passthrough by lazy { persistent { Passthrough() } }
class JumpFlooder(
val width: Int, val height: Int, format: ColorFormat = ColorFormat.RGBa, type: ColorType = ColorType.FLOAT32,
val encodePoints: Filter = EncodePoints()
) {
private val dimension = max(width, height)
private val exp = ceil(log2(dimension.toDouble())).toInt()
val squareDim = 2.0.pow(exp.toDouble()).toInt()
val jumpFlood = JumpFlood()
private val coordinates =
listOf(
colorBuffer(squareDim, squareDim, format = format, type = type),
colorBuffer(squareDim, squareDim, format = format, type = type)
)
val final = colorBuffer(squareDim, squareDim, format = format, type = type)
private val square = colorBuffer(squareDim, squareDim, format = ColorFormat.RGBa, type = type).apply {
fill(ColorRGBa.BLACK.opacify(0.0))
}
fun jumpFlood(input: ColorBuffer): ColorBuffer {
if (input.width != width || input.height != height) {
throw IllegalArgumentException("dimensions mismatch")
}
input.copyTo(square)
encodePoints.apply(square, coordinates[0])
jumpFlood.maxSteps = exp
for (i in 0 until exp) {
jumpFlood.step = i
jumpFlood.apply(coordinates[i % 2], coordinates[(i + 1) % 2])
}
coordinates[exp % 2].copyTo(final)
return final
}
fun destroy() {
coordinates.forEach { it.destroy() }
square.destroy()
final.destroy()
}
}
private fun encodeDecodeBitmap(
preprocess: Filter, decoder: Filter, bitmap: ColorBuffer,
jumpFlooder: JumpFlooder? = null,
result: ColorBuffer? = null
): ColorBuffer {
val _jumpFlooder = jumpFlooder ?: JumpFlooder(bitmap.width, bitmap.height)
val _result = result ?: colorBuffer(bitmap.width, bitmap.height, type = ColorType.FLOAT16)
preprocess.apply(bitmap, _result)
val encoded = _jumpFlooder.jumpFlood(_result)
decoder.parameters["originalSize"] = Vector2(_jumpFlooder.squareDim.toDouble(), _jumpFlooder.squareDim.toDouble())
decoder.apply(arrayOf(encoded, bitmap), _result)
if (jumpFlooder == null) {
_jumpFlooder.destroy()
}
return _result
}
/**
* Creates a color buffer containing the coordinates of the nearest centroids
* @param bitmap a ColorBuffer with centroids in red (> 0)
*/
fun centroidsFromBitmap(
bitmap: ColorBuffer,
jumpFlooder: JumpFlooder? = null,
result: ColorBuffer? = null
): ColorBuffer = encodeDecodeBitmap(passthrough, passthrough, bitmap, jumpFlooder, result)
fun distanceFieldFromBitmap(
bitmap: ColorBuffer,
jumpFlooder: JumpFlooder? = null,
result: ColorBuffer? = null
): ColorBuffer = encodeDecodeBitmap(contourPoints, pixelDistance, bitmap, jumpFlooder, result)
fun directionFieldFromBitmap(
bitmap: ColorBuffer,
jumpFlooder: JumpFlooder? = null,
result: ColorBuffer? = null
): ColorBuffer = encodeDecodeBitmap(contourPoints, pixelDirection, bitmap, jumpFlooder, result)

View File

@@ -0,0 +1,41 @@
package org.openrndr.extra.jumpfill.draw
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.Filter
import org.openrndr.draw.filterShaderFromCode
import org.openrndr.extra.jumpflood.jf_sdf_stroke_fill
import org.openrndr.extra.parameters.ColorParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
@Description("SDF stroke and fill")
class SDFStrokeFill : Filter(filterShaderFromCode(jf_sdf_stroke_fill, "sdf-stroke-fill")) {
@DoubleParameter("stroke weight", 0.0, 20.0, order = 0)
var strokeWeight: Double by parameters
@DoubleParameter("stroke feather", 0.0, 20.0, order = 0)
var strokeFeather: Double by parameters
@ColorParameter("stroke color", order = 1)
var strokeColor: ColorRGBa by parameters
@DoubleParameter("fill feather", 0.0, 20.0, order = 0)
var fillFeather: Double by parameters
@ColorParameter("fill color", order = 2)
var fillColor: ColorRGBa by parameters
init {
fillFeather = 1.0
strokeFeather = 1.0
strokeWeight = 1.0
strokeColor = ColorRGBa.BLACK
fillColor = ColorRGBa.WHITE
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
super.apply(source, target)
}
}

View File

@@ -0,0 +1,65 @@
package org.openrndr.extra.jumpfill.fx
import org.openrndr.draw.*
import org.openrndr.extra.jumpfill.EncodeSubpixel
import org.openrndr.extra.jumpfill.JumpFlooder
import org.openrndr.extra.jumpfill.PixelDirection
import org.openrndr.extra.jumpflood.jf_inner_bevel
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.resourceUrl
private class InnerBevelFilter : Filter(filterShaderFromCode(jf_inner_bevel, "inner-bevel")) {
var angle: Double by parameters
var width: Double by parameters
var noise:Double by parameters
init {
angle = 0.0
width = 5.0
noise = 0.0
}
}
@Description("Inner bevel")
class InnerBevel : Filter() {
@DoubleParameter("threshold", 0.0, 1.0)
var threshold = 0.01
@DoubleParameter("distance scale", 0.0, 1.0)
var distanceScale = 1.0
@DoubleParameter("angle", -180.0, 180.0)
var angle = 0.0
@DoubleParameter("width", 0.0, 50.0)
var width = 5.0
@DoubleParameter("noise", 0.0, 1.0)
var noise = 0.1
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection()
private val bevelFilter = InnerBevelFilter()
private var distance: ColorBuffer? = null
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height, encodePoints = EncodeSubpixel())
}
if (distance == null) {
distance = colorBuffer(target[0].width, target[0].height, type = ColorType.FLOAT32)
}
val result = jumpFlooder!!.jumpFlood(source[0])
decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0)
decodeFilter.distanceScale = distanceScale
decodeFilter.apply(result, result)
result.copyTo(distance!!)
bevelFilter.angle = angle
bevelFilter.width = width
bevelFilter.noise = noise
bevelFilter.apply(arrayOf(source[0], distance!!), target[0])
}
}

View File

@@ -0,0 +1,78 @@
package org.openrndr.extra.jumpfill.fx
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.jumpfill.EncodeSubpixel
import org.openrndr.extra.jumpfill.JumpFlooder
import org.openrndr.extra.jumpfill.PixelDirection
import org.openrndr.extra.jumpflood.jf_inner_glow
import org.openrndr.extra.parameters.ColorParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.resourceUrl
private class InnerGlowFilter : Filter(filterShaderFromCode(jf_inner_glow, "inner-glow")) {
var angle: Double by parameters
var width: Double by parameters
var noise: Double by parameters
var color: ColorRGBa by parameters
var shape: Double by parameters
var imageOpacity: Double by parameters
init {
angle = 0.0
width = 5.0
noise = 0.0
shape = 1.0
imageOpacity = 1.0
}
}
@Description("Inner glow")
class InnerGlow : Filter1to1() {
@DoubleParameter("width", 0.0, 50.0)
var width = 5.0
@DoubleParameter("noise", 0.0, 1.0)
var noise = 0.1
@DoubleParameter("shape", 0.0, 10.0)
var shape = 1.0
@DoubleParameter("opacity", 0.0, 1.0)
var opacity = 1.0
@DoubleParameter("image opacity", 0.0, 1.0)
var imageOpacity = 1.0
@ColorParameter("color")
var color = ColorRGBa.WHITE
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection()
private val glowFilter = InnerGlowFilter()
private var distance: ColorBuffer? = null
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height, encodePoints = EncodeSubpixel())
}
if (distance == null) {
distance = colorBuffer(target[0].width, target[0].height, type = ColorType.FLOAT32)
}
val result = jumpFlooder!!.jumpFlood(source[0])
decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0)
decodeFilter.distanceScale = 1.0
decodeFilter.apply(result, result)
result.copyTo(distance!!)
glowFilter.color = color.opacify(opacity)
glowFilter.width = width
glowFilter.noise = noise
glowFilter.shape = shape
glowFilter.imageOpacity = imageOpacity
glowFilter.apply(arrayOf(source[0], distance!!), target[0])
}
}

View File

@@ -0,0 +1,72 @@
package org.openrndr.extra.jumpfill.fx
import org.openrndr.draw.*
import org.openrndr.extra.jumpfill.EncodeSubpixel
import org.openrndr.extra.jumpfill.JumpFlooder
import org.openrndr.extra.jumpfill.PixelDirection
import org.openrndr.extra.jumpflood.jf_inpaint
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.resourceUrl
private class InpaintFilter : Filter(filterShaderFromCode(jf_inpaint, "inpaint")) {
var noise: Double by parameters
var imageOpacity: Double by parameters
var opacity: Double by parameters
var shape: Double by parameters
var width: Double by parameters
init {
noise = 0.0
imageOpacity = 1.0
opacity = 1.0
shape = 0.0
width = 0.5
}
}
@Description("Inpaint")
class Inpaint : Filter() {
@DoubleParameter("width", 0.0, 1.0)
var width = 0.5
@DoubleParameter("noise", 0.0, 1.0)
var noise = 0.1
@DoubleParameter("opacity", 0.0, 1.0)
var opacity = 1.0
@DoubleParameter("image opacity", 0.0, 1.0)
var imageOpacity = 1.0
@DoubleParameter("shape", 0.0, 10.0)
var shape = 0.0
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection()
private val inpaintFilter = InpaintFilter()
private var distance: ColorBuffer? = null
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height, encodePoints = EncodeSubpixel())
}
if (distance == null) {
distance = colorBuffer(target[0].width, target[0].height, type = ColorType.FLOAT32)
}
val result = jumpFlooder!!.jumpFlood(source[0])
decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0)
decodeFilter.distanceScale = 1.0
decodeFilter.apply(result, result)
result.copyTo(distance!!)
inpaintFilter.noise = noise
inpaintFilter.imageOpacity = imageOpacity
inpaintFilter.opacity = opacity
inpaintFilter.shape = shape
inpaintFilter.width = width
inpaintFilter.apply(arrayOf(source[0], distance!!), target[0])
}
}

View File

@@ -0,0 +1,79 @@
package org.openrndr.extra.jumpfill.fx
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.jumpfill.EncodeSubpixel
import org.openrndr.extra.jumpfill.JumpFlooder
import org.openrndr.extra.jumpfill.PixelDirection
import org.openrndr.extra.jumpflood.jf_outer_glow
import org.openrndr.extra.parameters.ColorParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.resourceUrl
private class OuterGlowFilter : Filter(filterShaderFromCode(jf_outer_glow, "outer-glow")) {
var angle: Double by parameters
var width: Double by parameters
var noise: Double by parameters
var color: ColorRGBa by parameters
var shape: Double by parameters
var imageOpacity: Double by parameters
init {
angle = 0.0
width = 5.0
noise = 0.0
shape = 1.0
imageOpacity = 1.0
}
}
@Description("Outer glow")
class OuterGlow : Filter() {
@DoubleParameter("width", 0.0, 50.0)
var width = 5.0
@DoubleParameter("noise", 0.0, 1.0)
var noise = 0.1
@DoubleParameter("shape", 0.0, 10.0)
var shape = 1.0
@DoubleParameter("opacity", 0.0, 1.0)
var opacity = 1.0
@DoubleParameter("image opacity", 0.0, 1.0)
var imageOpacity = 1.0
@ColorParameter("color")
var color = ColorRGBa.WHITE
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection()
private val glowFilter = OuterGlowFilter()
private var distance: ColorBuffer? = null
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height, encodePoints = EncodeSubpixel())
}
if (distance == null) {
distance = colorBuffer(target[0].width, target[0].height, type = ColorType.FLOAT32)
}
val result = jumpFlooder!!.jumpFlood(source[0])
decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0)
decodeFilter.distanceScale = 1.0
decodeFilter.apply(result, result)
result.copyTo(distance!!)
glowFilter.color = color.opacify(opacity)
glowFilter.width = width
glowFilter.noise = noise
glowFilter.shape = shape
glowFilter.imageOpacity = imageOpacity
glowFilter.apply(arrayOf(source[0], distance!!), target[0])
}
}

View File

@@ -0,0 +1,84 @@
package org.openrndr.extra.jumpfill.fx
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.jumpfill.*
import org.openrndr.extra.jumpflood.jf_skeleton
import org.openrndr.extra.parameters.ColorParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.resourceUrl
private class SkeletonFilter : Filter(filterShaderFromCode(jf_skeleton, "skeleton")) {
var skeletonColor: ColorRGBa by parameters
var foregroundColor: ColorRGBa by parameters
var backgroundColor: ColorRGBa by parameters
init {
skeletonColor = ColorRGBa.WHITE
foregroundColor = ColorRGBa.GRAY
backgroundColor = ColorRGBa.TRANSPARENT
}
}
@Description("Skeleton")
class Skeleton : Filter() {
@DoubleParameter("threshold", 0.0, 1.0, order = 0)
var threshold = 0.5
@DoubleParameter("distance scale", 0.0, 1.0, order = 1)
var distanceScale = 1.0
@ColorParameter("skeleton color", order = 2)
var skeletonColor = ColorRGBa.WHITE
@ColorParameter("foreground color", order = 3)
var foregroundColor = ColorRGBa.GRAY
@ColorParameter("background color", order = 4)
var backgroundColor = ColorRGBa.TRANSPARENT
private val thresholdFilter = Threshold()
private var thresholded: ColorBuffer? = null
private val contourFilter = ContourPoints()
private var contoured: ColorBuffer? = null
private var copied: ColorBuffer? = null
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDistance()
private val skeletonFilter = SkeletonFilter()
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
if (thresholded == null) {
thresholded = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
}
if (contoured == null) {
contoured = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height)
}
if (copied == null) {
copied = target[0].createEquivalent(type = ColorType.FLOAT32)
}
thresholdFilter.threshold = threshold
thresholdFilter.apply(source[0], thresholded!!)
contourFilter.apply(thresholded!!, contoured!!)
val result = jumpFlooder!!.jumpFlood(contoured!!)
decodeFilter.signedDistance = true
decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0)
decodeFilter.distanceScale = distanceScale
decodeFilter.signedBit = false
decodeFilter.apply(arrayOf(result, thresholded!!), arrayOf(result))
result.copyTo(copied!!)
skeletonFilter.skeletonColor = skeletonColor
skeletonFilter.backgroundColor = backgroundColor
skeletonFilter.foregroundColor = foregroundColor
skeletonFilter.apply(copied!!, target[0])
}
}

View File

@@ -0,0 +1,88 @@
package org.openrndr.extra.jumpfill.fx
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
import org.openrndr.extra.jumpfill.*
import org.openrndr.extra.jumpflood.jf_straight_skeleton
import org.openrndr.extra.parameters.ColorParameter
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.math.Vector2
import org.openrndr.resourceUrl
import kotlin.math.sqrt
private class StraightSkeletonFilter : Filter(filterShaderFromCode(jf_straight_skeleton, "straight-skeleton")) {
var angleThreshold: Double by parameters
var skeletonColor: ColorRGBa by parameters
var foregroundColor: ColorRGBa by parameters
var backgroundColor: ColorRGBa by parameters
init {
skeletonColor = ColorRGBa.WHITE
foregroundColor = ColorRGBa.GRAY
backgroundColor = ColorRGBa.TRANSPARENT
angleThreshold = sqrt(2.0) / 2.0;
}
}
@Description("Skeleton")
class StraightSkeleton : Filter() {
@DoubleParameter("threshold", 0.0, 1.0, order = 0)
var threshold = 0.5
@DoubleParameter("distance scale", 0.0, 1.0, order = 1)
var distanceScale = 1.0
@DoubleParameter("angle threshold", 0.0, 1.0, order = 2)
var angleThreshold = sqrt(2.0) / 2.0
@ColorParameter("skeleton color", order = 3)
var skeletonColor = ColorRGBa.WHITE
@ColorParameter("foreground color", order = 4)
var foregroundColor = ColorRGBa.GRAY
@ColorParameter("background color", order = 5)
var backgroundColor = ColorRGBa.TRANSPARENT
private val thresholdFilter = Threshold()
private var thresholded: ColorBuffer? = null
private val contourFilter = ContourPoints()
private var contoured: ColorBuffer? = null
private var copied: ColorBuffer? = null
private var jumpFlooder: JumpFlooder? = null
private val decodeFilter = PixelDirection()
private val skeletonFilter = StraightSkeletonFilter()
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
if (thresholded == null) {
thresholded = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
}
if (contoured == null) {
contoured = colorBuffer(target[0].width, target[0].height, format = ColorFormat.R)
}
if (jumpFlooder == null) {
jumpFlooder = JumpFlooder(target[0].width, target[0].height)
}
if (copied == null) {
copied = target[0].createEquivalent(type = ColorType.FLOAT32)
}
thresholdFilter.threshold = threshold
thresholdFilter.apply(source[0], thresholded!!)
contourFilter.apply(thresholded!!, contoured!!)
val result = jumpFlooder!!.jumpFlood(contoured!!)
decodeFilter.originalSize = Vector2(target[0].width * 1.0, target[0].height * 1.0)
decodeFilter.distanceScale = distanceScale
decodeFilter.apply(arrayOf(result, thresholded!!), arrayOf(result))
result.copyTo(copied!!)
skeletonFilter.angleThreshold = angleThreshold
skeletonFilter.skeletonColor = skeletonColor
skeletonFilter.backgroundColor = backgroundColor
skeletonFilter.foregroundColor = foregroundColor
skeletonFilter.apply(copied!!, target[0])
}
}

View File

@@ -0,0 +1,102 @@
package org.openrndr.extra.jumpfill.ops
import org.openrndr.draw.ColorBuffer
import org.openrndr.draw.ColorType
import org.openrndr.draw.Filter
import org.openrndr.draw.filterShaderFromCode
import org.openrndr.extra.jumpflood.*
import org.openrndr.extra.parameters.Description
import org.openrndr.extra.parameters.DoubleParameter
import org.openrndr.resourceUrl
class SDFSmoothUnion : Filter(filterShaderFromCode(jf_sdf_smooth_union, "sdf-smooth-union")) {
var radius: Double by parameters
init {
radius = 0.0
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
"needs a floating point target"
}
super.apply(source, target)
}
}
class SDFSmoothIntersection : Filter(filterShaderFromCode(jf_sdf_smooth_intersection, "sdf-smooth-intersection")) {
var radius: Double by parameters
init {
radius = 0.0
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
"needs a floating point target"
}
super.apply(source, target)
}
}
@Description("SDF smooth difference")
class SDFSmoothDifference : Filter(filterShaderFromCode(jf_sdf_smooth_difference, "sdf-smooth-differecnce")) {
@DoubleParameter("smooth radius", 0.0, 200.0, order = 0)
var radius: Double by parameters
init {
radius = 0.0
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
"needs a floating point target"
}
super.apply(source, target)
}
}
class SDFRound : Filter(filterShaderFromCode(jf_sdf_round, "sdf-round")) {
@DoubleParameter("rounding radius", 0.0, 200.0, order = 0)
var radius: Double by parameters
init {
radius = 0.0
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
"needs a floating point target"
}
super.apply(source, target)
}
}
class SDFOnion : Filter(filterShaderFromCode(jf_sdf_onion, "sdf-onion")) {
var radius: Double by parameters
init {
radius = 0.0
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
"needs a floating point target"
}
super.apply(source, target)
}
}
class SDFBlend : Filter(filterShaderFromCode(jf_sdf_blend, "sdf-blend")) {
var factor: Double by parameters
init {
factor = 0.5
}
override fun apply(source: Array<ColorBuffer>, target: Array<ColorBuffer>) {
require(target[0].type == ColorType.FLOAT16 || target[0].type == ColorType.FLOAT32) {
"needs a floating point target"
}
super.apply(source, target)
}
}