Ooops
November 28, 2020, 02:39:17 AM

Author Topic: [bb] Perturbation filter by Yasha [ 1+ years ago ]  (Read 718 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[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
  1. Function Perturb(img,pbase,magn#)               ;Map to perturb, noise map, magnitude
  2.  
  3. xsize=ImageWidth(img)
  4. ysize=ImageHeight(img)
  5. pmap=CreateImage(xsize,ysize)
  6. LockBuffer ImageBuffer(img)
  7. LockBuffer ImageBuffer(pbase)
  8. LockBuffer ImageBuffer(pmap)
  9.  
  10. For x=0 To xsize-1
  11. For y=0 To ysize-1
  12.         p#=magn*(((ReadPixelFast(x,y,ImageBuffer(pbase)) And $000000ff)-128)/255.0)
  13.        
  14.         xcoord#=x+(xsize*p)
  15.         xclo=Floor(xcoord):xcl2=refinc(xclo,0,xsize-1)
  16.         xchi=Ceil(xcoord):xch2=refinc(xchi,0,xsize-1)
  17.         xfrac#=xcoord-xclo
  18.        
  19.         ycoord#=y+(ysize*p)
  20.         yclo=Floor(ycoord):ycl2=refinc(yclo,0,ysize-1)
  21.         ychi=Ceil(ycoord):ych2=refinc(ychi,0,ysize-1)
  22.         yfrac#=ycoord-yclo
  23.                
  24.         val1#=linpol(ReadPixelFast(xcl2,ycl2,ImageBuffer(img)) And $000000ff,ReadPixelFast(xch2,ycl2,ImageBuffer(img)) And $000000ff,xfrac)
  25.         val2#=linpol(ReadPixelFast(xcl2,ych2,ImageBuffer(img)) And $000000ff,ReadPixelFast(xch2,ych2,ImageBuffer(img)) And $000000ff,xfrac)
  26.        
  27.         val3=linpol(val1,val2,yfrac)
  28.        
  29.         WritePixelFast(x,y,val3 Or (val3 Shl 8) Or (val3 Shl 16),ImageBuffer(pmap))
  30.                
  31. Next
  32. Next
  33.  
  34. UnlockBuffer ImageBuffer(img)
  35. UnlockBuffer ImageBuffer(pbase)
  36. UnlockBuffer ImageBuffer(pmap)
  37.  
  38. Return pmap
  39.  
  40. End Function
  41.  
  42. Function refinc(x,min,max)      ;If x is outside bounds, "reflects" it back in eg. refinc(130,0,100)=70
  43.  
  44. If x<min Then Return min+(min-x)
  45. If x>max Then Return max-(x-max)
  46. Return x
  47.  
  48. End Function
  49.  
  50. Function linpol#(a#,b#,x#)                              ;Linear interpolation
  51.         Return (a*(1-x))+(b*x)
  52. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal