Skip to content

Commit 5625169

Browse files
committed
separate noise2d and noise2d_u8 functions
1 parent b674d86 commit 5625169

File tree

9 files changed

+18
-16
lines changed

9 files changed

+18
-16
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ let package = Package(
1515
targets: [
1616
.target(
1717
name: "Noise",
18-
dependencies: ["MaxPNG"]),
18+
dependencies: ["MaxPNG"],
19+
path: "Sources/noise"),
1920
.testTarget(
2021
name: "NoiseTests",
2122
dependencies: ["Noise"]),

Sources/Noise/noise.swift renamed to Sources/noise/noise.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,24 @@ public
2020
extension Noise
2121
{
2222
public
23-
func noise2d(width:Int, height:Int) -> [UInt8]
23+
func noise2d(width:Int, height:Int) -> [Double]
2424
{
25-
var pixbuf:[UInt8] = []
26-
pixbuf.reserveCapacity(width * height)
27-
var y:Double = 0
28-
for _ in 0 ..< height
25+
var samples:[Double] = []
26+
samples.reserveCapacity(width * height)
27+
for y in 0 ..< height
2928
{
30-
var x:Double = 0
31-
for _ in 0 ..< width
29+
for x in 0 ..< width
3230
{
33-
x += 1
34-
let noise:Double = self.evaluate(x, y)
35-
pixbuf.append(UInt8(max(0, min(255, noise + 127))))
31+
samples.append(self.evaluate(Double(x), Double(y)))
3632
}
37-
y += 1
3833
}
39-
return pixbuf
34+
return samples
35+
}
36+
37+
public
38+
func noise2d_u8(width:Int, height:Int) -> [UInt8]
39+
{
40+
return self.noise2d(width: width, height: height).map{ UInt8(max(0, min(255, $0 + 127.5))) }
4041
}
4142
}
4243

File renamed without changes.
File renamed without changes.
File renamed without changes.

Tests/LinuxMain.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ try png_encode(path: "poisson.png", raw_data: pixbuf, properties: png_properties
3939

4040
let S:fBm<Simplex2D> = fBm<Simplex2D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
4141
t0 = clock()
42-
pixbuf = S.noise2d(width: viewer_size, height: viewer_size)
42+
pixbuf = S.noise2d_u8(width: viewer_size, height: viewer_size)
4343
print(clock() - t0)
4444
try png_encode(path: "simplex.png", raw_data: pixbuf, properties: png_properties)
4545

4646

4747
let SS:fBm<SuperSimplex2D> = fBm<SuperSimplex2D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
4848
t0 = clock()
49-
pixbuf = SS.noise2d(width: viewer_size, height: viewer_size)
49+
pixbuf = SS.noise2d_u8(width: viewer_size, height: viewer_size)
5050
print(clock() - t0)
5151
try png_encode(path: "super_simplex.png", raw_data: pixbuf, properties: png_properties)
5252

5353
let SS3D:fBm<SuperSimplex3D> = fBm<SuperSimplex3D>(amplitude: 1, frequency: 0.00083429273, octaves: 10)
5454
t0 = clock()
55-
pixbuf = SS3D.noise2d(width: viewer_size, height: viewer_size)
55+
pixbuf = SS3D.noise2d_u8(width: viewer_size, height: viewer_size)
5656
print(clock() - t0)
5757
try png_encode(path: "super_simplex3D.png", raw_data: pixbuf, properties: png_properties)

simplex.png

-171 Bytes
Loading

super_simplex.png

10 KB
Loading

super_simplex3D.png

-4.76 KB
Loading

0 commit comments

Comments
 (0)