November 28, 2020, 11:25:50 AM

Author Topic: [bmx] Blur a pixmap by xlsior [ 1+ years ago ]  (Read 415 times)

Offline BlitzBot

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

Description : An adaptation of my old B+ routine to work with BlitzMax

This example will loop and progressively blur / unblur a pixmap. Note that it cycles through each pixel in the pixmap, so it needs a moderately fast computer for best results.


Code :
Code: BlitzMax
  1. ' Blur -- Horizontal And Vertical Blur routines
  2. ' By Marc van den Dikkenberg / xlsior
  3. '
  4. ' Original Release    11/23/2003
  5. ' BlitzMax Converison 09/30/2007
  6. '
  7. ' Usage: HorizontalBlur(perct#)
  8. '        Verticalblur(perct#)
  9. '        perct# is the level of the effect, And expects a value between 50 And 99.
  10. '    
  11. ' The color value of a pixel is smeared onto its neighbours - the higher the
  12. ' value, the more it gets smeared.
  13. '
  14. ' To get a bi-directional blur effect, simply call the horizontal And Vertical
  15. ' blur functions directly after each other, before performing a 'flip' operation.
  16. '      
  17. ' Tidbit: These two functions were created entirely by accident. I was working
  18. ' on my Monochrome() Function when I accidentally forgot to declare a set of
  19. ' placeholder variables, And color information accidentally got passed on To
  20. ' the Next pixel. Oh, well - maybe someone can use them.
  21.  
  22. Graphics 640,480,32
  23.  
  24. pimg:TPixmap=LoadPixmap("j:monkey4.jpg")
  25. dimg:TPixmap=CreatePixmap(640,480,PF_RGB888)
  26.  
  27. Repeat
  28.         DrawPixmap(pimg:TPixmap,0,0)
  29.         For t=50 To 98 Step 2
  30.            HorizontalBlur(pimg:TPixmap,dimg:TPixmap,t)
  31.            VerticalBlur(dimg:TPixmap,dimg:TPixmap,t)
  32.                 DrawPixmap dimg,0,0
  33.            Flip
  34.            If KeyDown(key_escape) Then End
  35.         Next
  36.         For t=98 To 50 Step -2
  37.            HorizontalBlur(pimg:TPixmap,dimg:TPixmap,t)
  38.            VerticalBlur(dimg:TPixmap,dimg:TPixmap,t)
  39.                 DrawPixmap dimg,0,0
  40.            Flip
  41.            If KeyDown(key_escape) Then End
  42.         Next
  43. Forever
  44.  
  45. Function HorizontalBlur(PixIn:TPixmap, PixOut:TPixmap,perct#)
  46.    desr=0
  47.         desg=0
  48.         desb=0
  49.    If perct#<50 Then perct#=50
  50.    If perct#>99 Then perct#=99
  51.    For y=0 To pixin.height-1
  52.       For x=0 To pixin.width-1
  53.          temp1=ReadPixel(PixIn:TPixmap,x,y)
  54.          orgb=(temp1 & $FF)
  55.          orgg=(temp1 & $FF00) Shr 8
  56.          orgr=(temp1 & $FF0000) Shr 16
  57.          desr=orgr*(1-(perct#/100))+desr*(perct#/100)
  58.          desg=orgg*(1-(perct#/100))+desg*(perct#/100)
  59.          desb=orgb*(1-(perct#/100))+desb*(perct#/100)
  60.          WritePixel PixOut:TPixmap,x,y,desb+(desg Shl 8)+(desr Shl 16)
  61.       Next
  62.    Next
  63. '   UnlockBuffer
  64. End Function
  65.  
  66. Function VerticalBlur(PixIn:TPixmap, PixOut:TPixmap,perct#)
  67.    desr=0
  68.         desg=0
  69.         desb=0
  70.    If perct#<50 Then perct#=50
  71.    If perct#>99 Then perct#=99
  72.    For x=0 To pixin.width-1
  73.       For y=0 To pixin.height-1
  74.          temp1=ReadPixel(PixIn:TPixmap,x,y)
  75.          orgb=(temp1 & $FF)
  76.          orgg=(temp1 & $FF00) Shr 8
  77.          orgr=(temp1 & $FF0000) Shr 16
  78.          desr=orgr*(1-(perct#/100))+desr*(perct#/100)
  79.          desg=orgg*(1-(perct#/100))+desg*(perct#/100)
  80.          desb=orgb*(1-(perct#/100))+desb*(perct#/100)
  81.          WritePixel PixOut:TPixmap,x,y,desb+(desg Shl 8)+(desr Shl 16)
  82.       Next
  83.    Next
  84. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal