Ooops
March 06, 2021, 05:30:12 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Like stats
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bb] Perturbation filter by Yasha [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
Author
Topic: [bb] Perturbation filter by Yasha [ 1+ years ago ] (Read 739 times)
BlitzBot
Jr. Member
Posts: 1
Total likes: 0
[bb] Perturbation filter by Yasha [ 1+ years ago ]
«
on:
June 29, 2017, 12:28:43 AM »
Title :
Perturbation filter
Author :
Yasha
Posted :
1+ years ago
Description :
This function perturbs ("shmooshes") one image using another image as a filter. This can be used to add more of an appearance of randomness to otherwise-boring images, such as procedurally-generated heightmaps. Recommend Perlin or DSA noise for best results: <a href="codearcs5f35.html?code=2260" target="_blank">see here[/url] for one such algorithm </shameless plug>.
Magnitude increases quite sharply, I find 0.1-0.3 to be best. The function returns a new image rather than modifying the source, and the noise map needs to be at least as large as the source image. Oh and it reads the blue value of the noise map for simplicity's sake.
Code :
Code: BlitzBasic
Function
Perturb
(
img,pbase,magn#
)
;Map to perturb, noise map, magnitude
xsize=
ImageWidth
(
img
)
ysize=
ImageHeight
(
img
)
pmap=
CreateImage
(
xsize,ysize
)
LockBuffer
ImageBuffer
(
img
)
LockBuffer
ImageBuffer
(
pbase
)
LockBuffer
ImageBuffer
(
pmap
)
For
x=
0
To
xsize-
1
For
y=
0
To
ysize-
1
p#=magn*
(
(
(
ReadPixelFast
(
x,y,
ImageBuffer
(
pbase
)
)
And
$000000ff
)
-
128
)
/
255.0
)
xcoord#=x+
(
xsize*p
)
xclo=
Floor
(
xcoord
)
:xcl2=refinc
(
xclo,
0
,xsize-
1
)
xchi=
Ceil
(
xcoord
)
:xch2=refinc
(
xchi,
0
,xsize-
1
)
xfrac#=xcoord-xclo
ycoord#=y+
(
ysize*p
)
yclo=
Floor
(
ycoord
)
:ycl2=refinc
(
yclo,
0
,ysize-
1
)
ychi=
Ceil
(
ycoord
)
:ych2=refinc
(
ychi,
0
,ysize-
1
)
yfrac#=ycoord-yclo
val1#=linpol
(
ReadPixelFast
(
xcl2,ycl2,
ImageBuffer
(
img
)
)
And
$000000ff,
ReadPixelFast
(
xch2,ycl2,
ImageBuffer
(
img
)
)
And
$000000ff,xfrac
)
val2#=linpol
(
ReadPixelFast
(
xcl2,ych2,
ImageBuffer
(
img
)
)
And
$000000ff,
ReadPixelFast
(
xch2,ych2,
ImageBuffer
(
img
)
)
And
$000000ff,xfrac
)
val3=linpol
(
val1,val2,yfrac
)
WritePixelFast
(
x,y,val3
Or
(
val3
Shl
8
)
Or
(
val3
Shl
16
)
,
ImageBuffer
(
pmap
)
)
Next
Next
UnlockBuffer
ImageBuffer
(
img
)
UnlockBuffer
ImageBuffer
(
pbase
)
UnlockBuffer
ImageBuffer
(
pmap
)
Return
pmap
End Function
Function
refinc
(
x,min,max
)
;If x is outside bounds, "reflects" it back in eg. refinc(130,0,100)=70
If
x<min
Then
Return
min+
(
min-x
)
If
x>max
Then
Return
max-
(
x-max
)
Return
x
End Function
Function
linpol#
(
a#,b#,x#
)
;Linear interpolation
Return
(
a*
(
1
-x
)
)
+
(
b*x
)
End Function
Comments :
none...
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Graphics
»
[bb] Perturbation filter by Yasha [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal