-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangulate-image.r3
167 lines (142 loc) · 3.3 KB
/
triangulate-image.r3
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Rebol [
Title: "Trianguale image (resize and add line effect)"
needs: 3.11.0 ; to automatically install Blend2D extension
]
random/seed 2021
import 'triangulate
import 'blend2d ;- Blend2D is used to draw images
inp-img:
out-img: none
sc: 10.0
pwr: 6.9
particles: 24000
light-mode?: on
bg: 0.0.0
bg2: 0.0.0.10 ; used to fade image between steps
ln-w1: 1
ln-w2: 4
ln-c1: 0.28.0.128
ln-c2: 0.155.55.200
pt-w: 16
mode: 3
steps: 50
inp: object [
points: make vector! compose [decimal! 64 (particles * 2)]
attributes: make vector! compose [decimal! 64 (particles)]
; report: true
]
out: object [
points: none
attributes: none
edges: none
triangles: none
; voronoi output:
v-points: none
v-edges: none
v-norms: none
]
drw-points: make block! 4 * particles
init-points: func [
particles [integer!]
image [image!]
pwr [number!]
/local size p c
][
insert clear drw-points [
line-width 0 point-size :pt-w alpha 30%
]
size: image/size
while [particles > 0][
p: random size
rgb: image/(p)
hsv: rgb-to-hsv rgb
accepted?: either light-mode? [
(random 0.5) < power (hsv/:mode / 255) pwr
][ (random 0.5) < power ((255 - hsv/:mode) / 255) pwr]
if accepted? [
inp/points/1: p/x: (p/x * sc) - random sc
inp/points/2: p/y: (p/y * sc) - random sc
inp/points: skip inp/points 2
inp/attributes/:particles: to-integer to-binary rgb
repend drw-points ['fill rgb 'point p]
-- particles
]
]
inp/points: head inp/points
;? inp
inp
]
draw-frame: does [
;prin "init points: " print dt [
init-points particles inp-img pwr
;]
;prin "triangulate: " print dt [
triangulate inp out
;]
;if find out 'v-points [sc * out/v-points]
if block? select out 'v-edges [
temp: make block! (length? out/v-points) / 2
norms: make block! 1000
while [not tail? out/v-edges][
either out/v-edges/2 = -1 [
i: index? out/v-edges
norm-x: pick out/v-norms i
norm-y: pick out/v-norms i + 1
;print [norm-x norm-y]
append append norms out/v-edges/1 out/v-edges/2
append append norms norm-x norm-y
][
append append temp out/v-edges/1 out/v-edges/2
]
out/v-edges: skip out/v-edges 2
]
;? norms
out/v-edges: make vector! compose/only [integer! 32 (temp)]
]
;? out/v-points
;prin "draw image: "
;print dt [
draw out-img [
fill :bg2 fill-all fill off
point-size 4
; fill white point :out/points
fill 55.200.100.100 point :out/v-points
line-width :ln-w1 pen :ln-c1
line :out/points :out/edges
line-width :ln-w2 pen :ln-c2
line :out/v-points :out/v-edges
]
draw out-img drw-points
;]
;? out/v-norms
;? out
;print "draw done"
]
triangulate-image: func[file [file!]][
print [as-green "Triangulating image:" as-yellow file]
try/with [inp-img: load file][
print "Failed to load an image!"
quit
]
;out-img: resize inp-img sc * inp-img/size ;
out-img: make image! reduce [sc * inp-img/size bg]
out-file: join %out/ file
print dt [loop steps [draw-frame]]
try/with [
save out-file resize out-img 50%
][ print "Failed to save the result."]
;view load out-file
out-file
]
make-dir %out/
ln-c1: 0.28.0.128
ln-c2: 0.55.55.200
ln-w2: 8
steps: 1
triangulate-image %Vermeer.jpg
ln-c1: 255.0.0.130
ln-c2: 0.55.55.200
ln-w2: 6
steps: 10
particles: 14000
triangulate-image %Spiral_Tribe.jpg