AKTIVE

draw.tcl at trunk
Login

draw.tcl at trunk

File etc/generator/virtual/draw.tcl artifact 7e6e623241 on branch trunk


     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
   100
   101
   102
   103
   104
   105
   106
   107
   108
   109
   110
   111
   112
   113
   114
   115
   116
   117
   118
   119
   120
   121
   122
   123
   124
   125
   126
   127
   128
   129
   130
   131
   132
   133
   134
   135
   136
   137
   138
   139
   140
   141
   142
   143
   144
## -*- mode: tcl ; fill-column: 90 -*-
# # ## ### ##### ######## ############# #####################
## Generators -- Basic 2D geometry primitives
#
## At the core of the implementation are signed distance functions.
## See these in the sdf.tcl file, with runtime support in op/sdf.[ch].
#
## Instead of having to explicitly set each pixel of the primitive based on some
## drawing algorithm (bresenham, variants, etc.) here each pixel computes the
## distance to the primitive (or its outline (filled yes/no)) and either lights
## up within +/- 0.5 (b/w, jaggy), or gradually lights up within +/- 1 (grey
## scale, antialiased drawing).
#
## This is also naturally parallelizable and fits directly into the multi-
## threaded setup used in AKTIVE (see [VIPS](https://www.libvips.org) for the
## inspiring system).
#
## [..] Bezier
## [ok] Box
## [ok] Box with rounded corners
## [ok] Circle
## [..] Circle arc(s)
## [..] Circle segment(s)
## [ok] Circles (dots)
## [..] Ellipse
## [ok] Line segment
## [ok] Line segments (poly-line)
## [ok] Line segments (separate, scattered)
## [ok] Parallelogram
## [ok] Rhombus
## [..] Rotated ellipse(s)
## [ok] Triangle
## [..] ...

# # ## ### ##### ######## ############# #####################

proc draw-modifier {} {
    uplevel 1 {
	if {$sdf in {
	    line lines polyline
	}} {
	    # line element
	    import sdf/parameter/strokewidth.tcl
	} else {
	    # area element
	    import sdf/parameter/outlined.tcl
	}
    }
}

# # ## ### ##### ######## ############# #####################
## Draw elements

operator [sdf-known image::draw::] {
    section generator virtual drawing

    op -> _ mode sdf ; def on 0

    def element [sdf-label $sdf]

    note Returns an image with the given dimensions and location, \
	with a $element drawn into it.

    import sdf/note.tcl

    note The returned image is always single-band. It is grey-scale \
	when anti-aliasing is active, and black/white if not.

    note See also "<!xref: aktive op draw $sdf on>" \
	and "<!xref: aktive image sdf ${sdf}>."

    pass sdf-common-params

    bool? 1 antialiased		Draw with antialiasing for smoother contours (Default)

    draw-modifier

    # !xref-mark draw pass
    pass import sdf/parameter/$sdf.tcl

    body {
	set sdf [aktive image sdf @@sdf@@ @@passthrough@@]

	@@modifier@@

	if {$antialiased} {
	    set sdf [aktive op sdf 2image smooth $sdf]
	} else {
	    set sdf [aktive op sdf 2image pixelated $sdf]
	}
	::return $sdf
    }
}

# # ## ### ##### ######## ############# #####################
## Draw elements over a background image

operator [sdf-known op::draw:: ::on] {
    section transform drawing

    op -> _ mode sdf _ ; def on 1

    input background	The image to draw on, i.e. the background for the drawing.

    def element [sdf-label $sdf]

    note Returns an image where a $element is drawn on the input image.

    import sdf/note.tcl

    note See also "<!xref: aktive image draw ${sdf}>" \
	and "<!xref: aktive image sdf ${sdf}>."

    pass bool? 1 antialiased		Draw with antialiasing for smoother contours (Default)
    pass draw-modifier

    str color	List of band values, color of the drawn $element. \
	Their number has to match the input's depth.

    pass import sdf/parameter/$sdf.tcl

    body {
	lassign [aktive query geometry $background] x y w h d
	set n [llength $color]

	if {$n != $d} { aktive error "Band mismatch, have $n, expected $d" }

	set color [aktive image from band    width $w height $h x $x y $y values {*}$color]
	set draw  [aktive image draw @@sdf@@ width $w height $h x $x y $y @@passthrough@@]

	if {$antialiased} {
	    # grayscale ------------------ else        then   if
	    set sdf [aktive op math linear $background $color $draw]
	} else {
	    # black/white ----------------- if    then   else
	    set sdf [aktive op if-then-else $draw $color $background]
	}
	::return $sdf
    }
}

##
# # ## ### ##### ######## ############# #####################
::return