@@ -2,6 +2,24 @@ import Noise
2
2
3
3
import MaxPNG
4
4
5
+ func write_rgb_png( path: String , width: Int , height: Int , pixbuf: [ UInt8 ] )
6
+ {
7
+ guard let properties = PNGProperties ( width: width, height: height, bit_depth: 8 , color: . rgb, interlaced: false )
8
+ else
9
+ {
10
+ fatalError ( " failed to set png properties " )
11
+ }
12
+
13
+ do
14
+ {
15
+ try png_encode ( path: path, raw_data: pixbuf, properties: properties)
16
+ }
17
+ catch
18
+ {
19
+ print ( error)
20
+ }
21
+ }
22
+
5
23
func color_noise_png( r_noise: Noise , g_noise: Noise , b_noise: Noise ,
6
24
width: Int , height: Int , value_offset: Double , invert: Bool = false , path: String )
7
25
{
@@ -25,20 +43,7 @@ func color_noise_png(r_noise:Noise, g_noise:Noise, b_noise:Noise,
25
43
}
26
44
}
27
45
28
- guard let properties = PNGProperties ( width: width, height: height, bit_depth: 8 , color: . rgb, interlaced: false )
29
- else
30
- {
31
- fatalError ( " failed to set png properties " )
32
- }
33
-
34
- do
35
- {
36
- try png_encode ( path: path, raw_data: pixbuf, properties: properties)
37
- }
38
- catch
39
- {
40
- print ( error)
41
- }
46
+ write_rgb_png ( path: path, width: width, height: height, pixbuf: pixbuf)
42
47
}
43
48
44
49
func banner_simplex2d( width: Int , height: Int , seed: Int )
@@ -98,6 +103,47 @@ func banner_cell3d(width:Int, height:Int, seed:Int)
98
103
path: " tests/banner_cell3d.png " )
99
104
}
100
105
106
+ func circle_at( cx: Double , cy: Double , r: Double , width: Int , height: Int , _ f: ( Int , Int , Double ) -> ( ) )
107
+ {
108
+ // get bounding box
109
+ let x1 : Int = max ( 0 , Int ( cx - r) ) ,
110
+ x2 : Int = min ( width - 1 , Int ( ( cx + r) . rounded ( . up) ) ) ,
111
+ y1 : Int = max ( 0 , Int ( cy - r) ) ,
112
+ y2 : Int = min ( height - 1 , Int ( ( cy + r) . rounded ( . up) ) )
113
+
114
+ for y in y1 ... y2
115
+ {
116
+ let dy : Double = Double ( y) - cy
117
+ for x in x1 ... x2
118
+ {
119
+ let dx : Double = Double ( x) - cx,
120
+ dr : Double = ( dx*dx + dy*dy) . squareRoot ( )
121
+
122
+ f ( x, y, min ( 1 , max ( 0 , 1 - dr + r - 0.5 ) ) )
123
+ }
124
+ }
125
+ }
126
+
127
+ func banner_disk2d( width: Int , height: Int , seed: Int )
128
+ {
129
+ var poisson = DiskSampler2D ( seed: seed)
130
+ var pixbuf : [ UInt8 ] = [ UInt8] ( repeating: 255 , count: 3 * width * height)
131
+
132
+ let points = poisson. generate ( radius: 20 , width: width, height: height, k: 80 )
133
+ for point : ( x: Double , y: Double ) in points
134
+ {
135
+ circle_at ( cx: point. x, cy: point. y, r: 5 , width: width, height: height,
136
+ {
137
+ ( x: Int , y: Int , v: Double ) in
138
+
139
+ let base_addr : Int = 3 * ( y * width + x)
140
+ pixbuf [ base_addr + 1 ] = pixbuf [ base_addr + 1 ] &- UInt8 ( 255 * v)
141
+ } )
142
+ }
143
+
144
+ write_rgb_png ( path: " tests/banner_disk2d.png " , width: width, height: height, pixbuf: pixbuf)
145
+ }
146
+
101
147
public
102
148
func banners( width: Int , ratio: Double )
103
149
{
@@ -107,4 +153,6 @@ func banners(width:Int, ratio:Double)
107
153
banner_supersimplex3d ( width: width, height: height, seed: 0 )
108
154
banner_cell2d ( width: width, height: height, seed: 0 )
109
155
banner_cell3d ( width: width, height: height, seed: 0 )
156
+
157
+ banner_disk2d ( width: width, height: height, seed: 0 )
110
158
}
0 commit comments