November 28, 2020, 02:47:13 AM

Author Topic: [bmx] Pixmap Blur by impixi [ 1+ years ago ]  (Read 373 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bmx] Pixmap Blur by impixi [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
Title : Pixmap Blur
Author : impixi
Posted : 1+ years ago

Description : Blur a pixmap

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Graphics 800, 600
  4. SetBlend SOLIDBLEND
  5.  
  6. Local pic:TPixmap = createTestPixmap()
  7. 'Local pic:TPixmap = LoadPixmap("your.png")
  8.  
  9. Local pm:TPixmap = ConvertPixmap(pic, PF_RGBA8888)
  10.  
  11. blurPixmap(pm, 0.5)
  12.  
  13. While Not KeyHit(KEY_ESCAPE)
  14.  
  15.         Cls
  16.  
  17.         DrawPixmap pic, 0, 0
  18.         DrawPixmap pm, pic.width + 10, 0
  19.  
  20.         Flip
  21.        
  22.         Delay 1
  23.  
  24. Wend
  25.  
  26. End
  27.  
  28. Function createTestPixmap:TPixmap()
  29.  
  30.         SeedRnd MilliSecs()
  31.  
  32.         Local testpm:TPixmap = CreatePixmap(256, 256, PF_RGB888)
  33.        
  34.         For Local x:Int = 0 To testpm.width - 1
  35.                 For Local z:Int = 0 To testpm.height - 1
  36.                         WritePixel(testpm, x, z, (Rand(0, 255) Shl 16) | (Rand(0, 255) Shl 8) | Rand(0, 255))
  37.                 Next
  38.         Next
  39.  
  40.         Return testpm
  41.  
  42. EndFunction
  43.  
  44.  
  45. '*************** BLURRING FUNCTIONS ****************************
  46.  
  47. Function blurPixmap(pm:TPixmap, k:Float = 0.5)
  48.  
  49.         'pm - the pixmap to blur. Format must be PF_RGBA8888
  50.         'k - blurring amount. Value between 0.0 and 1.0
  51.         '        0.1 = Extreme, 0.9 = Minimal
  52.  
  53.         For Local x:Int = 1 To (pm.Width - 1)
  54.         For Local z:Int = 0 To (pm.Height - 1)
  55.                         WritePixel(pm, x, z, blurPixel(ReadPixel(pm, x, z), ReadPixel(pm, x - 1, z), k))
  56.         Next
  57.     Next
  58.  
  59.     For Local x:Int = (pm.Width - 3) To 0 Step -1
  60.         For Local z:Int = 0 To (pm.Height - 1)
  61.                         WritePixel(pm, x, z, blurPixel(ReadPixel(pm, x, z), ReadPixel(pm, x + 1, z), k))
  62.         Next
  63.     Next
  64.  
  65.     For Local x:Int = 0 To (pm.Width - 1)
  66.         For Local z:Int = 1 To (pm.Height - 1)
  67.                         WritePixel(pm, x, z, blurPixel(ReadPixel(pm, x, z), ReadPixel(pm, x, z - 1), k))
  68.         Next
  69.     Next
  70.  
  71.     For Local x:Int = 0 To (pm.Width - 1)
  72.         For Local z:Int = (pm.Height - 3) To 0 Step -1
  73.                         WritePixel(pm, x, z, blurPixel(ReadPixel(pm, x, z), ReadPixel(pm, x, z + 1), k))
  74.         Next
  75.     Next
  76.        
  77. End Function
  78.  
  79. Function blurPixel:Int(px:Int, px2:Int, k:Float)
  80.                
  81.         'Utility function used by blurPixmap.
  82.         'Uncomment the commented lines to enable alpha component
  83.         'processing (usually not required).
  84.                        
  85.         Local pxa:Byte = px Shr 24
  86.         Local pxb:Byte = px Shr 16
  87.         Local pxg:Byte = px Shr 8
  88.         Local pxr:Byte = px
  89.                                
  90.         'Local px2a:Byte = px2 Shr 24                  
  91.         Local px2b:Byte = px2 Shr 16
  92.         Local px2g:Byte = px2 Shr 8
  93.         Local px2r:Byte = px2
  94.                                
  95.         'pxa = (px2a * (1 - k)) + (pxa * k)
  96.         pxb = (px2b * (1 - k)) + (pxb * k)
  97.         pxg = (px2g * (1 - k)) + (pxg * k)
  98.         pxr = (px2r * (1 - k)) + (pxr * k)
  99.                                
  100.         Return Int(pxa Shl 24 | pxb Shl 16 | pxg Shl 8 | pxr)
  101.  
  102. EndFunction
  103.  
  104. '**************************************************************


Comments :


xlsior(Posted 1+ years ago)

 nice!


tonyg(Posted 1+ years ago)

 This extends it a bit by reducing the scale of the blur making it quicker and then drawing it with alpha and slightly bigger to produce a bloom effect. Bit clunky but OK:
Code: [Select]
SuperStrict
Graphics 800 , 600
SeedRnd MilliSecs()
Const scaledown:Float = 4.0
AutoMidHandle True
Local IMAGE:TIMAGE = LoadImage("MAX.PNG")
Local blur:Int = 1
Local image1:timage=Null
While Not KeyHit(KEY_ESCAPE)
Local t1:Int=MilliSecs()
Cls
If MouseHit(1) image1=createbloom(image)
SetScale 1.0 , 1.0
SetAlpha 1.0
SetBlend maskblend
DrawImage image , MouseX() , MouseY()
If image1
SetScale scaledown+0.5,scaledown+0.5
SetBlend lightblend
SetAlpha 0.7  ' try changing alpha value for bigger/smaller blooms.
DrawImage image1 , MouseX() , MouseY()
EndIf
Flip 0
Local t2:Int=MilliSecs()
Print t2 - t1
Wend
Function createbloom:timage(image:timage)
   setalpha 1.0

SetScale 1.0/scaledown,1.0/scaledown
DrawImage image , (ImageWidth(image)/scaledown)/2 , (ImageHeight(image)/scaledown)/2
Local mypixmap:tpixmap = GrabPixmap(0,0,ImageWidth(image)/scaledown,ImageHeight(image)/scaledown)
blurpixmap(mypixmap,0.5)
Return LoadImage(mypixmap)
End Function
Try changing the alpha value


Trader3564(Posted 1+ years ago)

 Pixmap Blur:Awesome! tough it runs at only 5FPS here (realtime).Gloom:Also cool, runs at 50FPS here (realtime). [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal