Scoop API

scoop:scoop.graphics:0.1.0

src/drawing/filter.scoop

Selected production source · src/drawing/filter.scoop

1package scoop.graphics
3/** Immutable image processing on the creating thread. Compositions retain their inputs. */
4public class ImageFilter internal constructor(internal val resource: GraphicsResource) : Closeable {
5 public val isClosed: Boolean get() = resource.isClosed
7 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 @Unsafe
20 {
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: Color
31 ): 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: Color
39 ): 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 @Unsafe
45 {
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 }
60private fun checkSigma(x: Double, y: Double) {
61 checkScalar(x)
62 checkScalar(y)
63 requireGraphics(x >= 0.0 && y >= 0.0, "negative Gaussian sigma")
66private fun shadow(offset: Point, x: Double, y: Double, color: Color, only: Boolean): ImageFilter {
67 checkPoint(offset)
68 checkSigma(x, y)
69 checkColor(color)
70 @Unsafe
71 {
72 return ImageFilter(ownedGraphics(filterShadow(offset, x, y, color, only), "create shadow"))
73 }
76/** Premultiplied layer composition; Plus is saturating SDR addition. */
77public enum BlendMode {
78 SrcOver, Src, DstIn, Plus, Darken, Lighten
81internal fun BlendMode.nativeCode(): Int = when (this) {
82 case SrcOver -> 0
83 case Src -> 1
84 case DstIn -> 2
85 case Plus -> 3
86 case Darken -> 4
87 case Lighten -> 5
90@Extern(name = "scoop_graphics_filter_blur")
91private fun filterBlur(x: Double, y: Double): Ptr<Unit>?
93@Extern(name = "scoop_graphics_filter_shadow")
94private fun filterShadow(offset: Point, x: Double, y: Double, color: Color, only: Boolean): Ptr<
95 Unit
96>?
98@Extern(name = "scoop_graphics_filter_then")
99private fun filterThen(first: Ptr<Unit>, next: Ptr<Unit>): Ptr<Unit>?
101@Extern(name = "scoop_graphics_filters_compatible")
102private fun filtersCompatible(first: Ptr<Unit>, next: Ptr<Unit>): Boolean