scoop:scoop.graphics:0.1.0
src/drawing/filter.scoop
Selected production source · src/drawing/filter.scoop
1
package scoop.graphics3
/** Immutable image processing on the creating thread. Compositions retain their inputs. */4
public class ImageFilter internal constructor(internal val resource: GraphicsResource) : Closeable {5
public val isClosed: Boolean get() = resource.isClosed7
public companion object {8
/** Uses the named shader child for the layer or backdrop being filtered. */9
public fun runtimeShader(10
program: ShaderProgram,11
sourceChild: String,12
sampleRadius: Double,13
configure: ShaderBindings.() -> Unit = {}14
): ImageFilter = program.filter(sourceChild, sampleRadius, configure)16
/** Gaussian standard deviations in local logical units, with transparent outside samples. */17
public fun blur(sigmaX: Double, sigmaY: Double): ImageFilter {18
checkSigma(sigmaX, sigmaY)19
@Unsafe20
{21
return ImageFilter(ownedGraphics(filterBlur(sigmaX, sigmaY), "create blur"))22
}23
}25
/** Composites the source over its tinted, blurred and translated alpha. */26
public fun dropShadow(27
offset: Point,28
sigmaX: Double,29
sigmaY: Double,30
color: Color31
): ImageFilter = shadow(offset, sigmaX, sigmaY, color, false)33
/** Outputs only the tinted, blurred and translated source alpha. */34
public fun dropShadowOnly(35
offset: Point,36
sigmaX: Double,37
sigmaY: Double,38
color: Color39
): ImageFilter = shadow(offset, sigmaX, sigmaY, color, true)40
}42
/** Applies this filter first and next second: next(this(source)). */43
public fun then(next: ImageFilter): ImageFilter {44
@Unsafe45
{46
val first = resource.get()47
val second = next.resource.get()48
requireGraphics(filtersCompatible(first, second), "incompatible filter inputs")49
return ImageFilter(50
ownedGraphics(filterThen(first, second), "compose filters")51
)52
}53
}55
public override fun close() {56
resource.close()57
}58
}60
private fun checkSigma(x: Double, y: Double) {61
checkScalar(x)62
checkScalar(y)63
requireGraphics(x >= 0.0 && y >= 0.0, "negative Gaussian sigma")64
}66
private fun shadow(offset: Point, x: Double, y: Double, color: Color, only: Boolean): ImageFilter {67
checkPoint(offset)68
checkSigma(x, y)69
checkColor(color)70
@Unsafe71
{72
return ImageFilter(ownedGraphics(filterShadow(offset, x, y, color, only), "create shadow"))73
}74
}76
/** Premultiplied layer composition; Plus is saturating SDR addition. */77
public enum BlendMode {78
SrcOver, Src, DstIn, Plus, Darken, Lighten79
}81
internal fun BlendMode.nativeCode(): Int = when (this) {82
case SrcOver -> 083
case Src -> 184
case DstIn -> 285
case Plus -> 386
case Darken -> 487
case Lighten -> 588
}90
@Extern(name = "scoop_graphics_filter_blur")91
private fun filterBlur(x: Double, y: Double): Ptr<Unit>?93
@Extern(name = "scoop_graphics_filter_shadow")94
private fun filterShadow(offset: Point, x: Double, y: Double, color: Color, only: Boolean): Ptr<95
Unit96
>?98
@Extern(name = "scoop_graphics_filter_then")99
private fun filterThen(first: Ptr<Unit>, next: Ptr<Unit>): Ptr<Unit>?101
@Extern(name = "scoop_graphics_filters_compatible")102
private fun filtersCompatible(first: Ptr<Unit>, next: Ptr<Unit>): Boolean