October 28, 2020, 10:35:46 PM

Author Topic: [bb] 2D Alpha Blending (as an include file) by Sunteam Software [ 1+ years ago ]  (Read 1522 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 2D Alpha Blending (as an include file)
Author : Sunteam Software
Posted : 1+ years ago

Description : July 2003: Matt Sephton has kindly upated a few things to make them faster (and slicker). I haven't tested them myself but I trust him ;)

Include this and use quite simply by using the drawimagealpha function. Quite simple really, it does all the calcs for ya, just fill in it's required parameters (see func for details) and away you go :)

Updates:

Ignores drawing pixels matching the mask values (unless 0,0,0)
Faster thanks to Mark's pointers :)
Reworked to handle inverse alpha (i.e. you can now subtract an image from the destination for example the clouds in Sunteam's 2D tech demo credits make the ground darker as in sub alpha instead of add alpha, see last param for more details).

Mike
Sunteam Software


Code :
Code: BlitzBasic
  1. ; ================================================
  2. ; Small Alpha routines by Mike of Sunteam Software
  3. ; ================================================
  4.  
  5. ; Modified by Matt Sephton (July 2003)
  6.  
  7. Global alphamw,alphamh
  8.  
  9. ; readargb
  10. ;
  11. ; Params: a as an alpha type (see below), buffer to be read from (must be locked before), x position of pixel, y position of pixel
  12. Function readargb(a.alpha,buffer,x,y)
  13.         rrgb=ReadPixelFast(x,y,buffer)
  14.         a
  15.  = (rrgb And $FF0000) Shr 16
  16.         ag = (rrgb And $00FF00) Shr 8
  17.         a = (rrgb And $0000FF)
  18. End Function
  19.  
  20. ; writeargb
  21. ;
  22. ; Params: a as an alpha type (see below), buffer to write to (must be locked before), x position of pixel, y position of pixel
  23. Function writeargb(a.alpha,buffer,x,y)
  24.         If x>=0 And y>=0 And x<=alphamw And y<=alphamh Then
  25.                 argb = (a
  26.  Shl 16) + (ag Shl 8) + a
  27.                 WritePixelFast x,y,argb,buffer
  28.         EndIf
  29. End Function
  30.  
  31. ; adjustalpha
  32. ;
  33. ; Params: a as an alpha type, red value (0-1 as float), green value (0-1 as float), blue value (0-1 as float)
  34. Function adjustalpha(a.alpha,r#,g#,b#)
  35.         a
  36.  = (a
  37. *r#)
  38.         ag = (ag*g#)
  39.         a = (a*b#)
  40. End Function
  41.  
  42. ; drawimagealpha
  43. ;
  44. ; Params: srcimage handle (does not need locking and will be unlocked after),destbuffer (does not need locking and will be unlocked after),x,y,alpha level (0-1 as float), mask red, mask green, mask blue, operation (-1 for sub, 1 for add)
  45. ; NB: srcimage handle is an image handle not a buffer handle like dest buffer is!
  46. ; NBB: the mask is an int value in r,g,b representing the colour to be ignored (masked)
  47. Function drawimagealpha(src,dest,x,y,a#,maskr,maskg,maskb,op)
  48.         w = ImageWidth(src)-1
  49.         h = ImageHeight(src)-1
  50.         LockBuffer ImageBuffer(src)
  51.         LockBuffer dest
  52.         t.alpha = New alpha
  53.         u.alpha = New alpha
  54.         mr = maskr
  55.         mg = maskg
  56.         alphamw=GraphicsWidth()-1
  57.         alphamh=GraphicsHeight()-1
  58.         For o = 0 To h
  59.                 For n = 0 To w
  60.                         readargb(t,ImageBuffer(src),n,o)
  61.                         If Not (t
  62.  = mr And tg = mg And t = maskb)
  63.                                 readargb(u,dest,x+n,y+o)
  64.                                
  65.                                 If (op = 0) Then
  66.                                         ;multiply blend
  67.                                         ;dest = dest + (source - dest) * alpha
  68.                                         u
  69.  = u
  70.  + (t
  71.  - u
  72. ) * a
  73.                                         ug = ug + (tg - ug) * a
  74.                                         u = u + (t - u) * a
  75.                                 Else
  76.                                         adjustalpha(t,a#,a#,a#)
  77.                                         u
  78.  = (u
  79.  + (t
  80. *op))
  81.                                         ug = (ug + (tg*op))
  82.                                         u = (u + (t*op))
  83.                                 EndIf
  84.  
  85.                                 ; check And remove any rollover
  86.                                 ;If u
  87.  > $FF Then
  88.                                 ;       u
  89.  = $FF
  90.                                 ;ElseIf u
  91.  < $0
  92.                                 ;       u
  93.  = $0
  94.                                 ;EndIf
  95.                                 ;If ug > $FF Then
  96.                                 ;       ug = $FF
  97.                                 ;ElseIf ug < $0
  98.                                 ;       ug = $0
  99.                                 ;EndIf
  100.                                 ;If u > $FF Then
  101.                                 ;       u = $FF
  102.                                 ;ElseIf u <$0
  103.                                 ;       u = $0
  104.                                 ;EndIf
  105.                                
  106.                                 ; faster rollover check
  107.                                 u
  108.  = u
  109.  And $FF
  110.                                 ug = ug And $FF
  111.                                 u = u And $FF
  112.  
  113.                                 writeargb(u,dest,x+n,y+o)
  114.                         EndIf
  115.                 Next
  116.         Next
  117.         Delete t
  118.         Delete u
  119.         UnlockBuffer ImageBuffer(src)
  120.         UnlockBuffer dest
  121. End Function
  122.  
  123. ; alpha type (red,green,blue as 2 bit ints)
  124. Type alpha
  125.         Field r,g,b
  126. End Type


Comments :


Regular K(Posted 1+ years ago)

 This works! It looks very nice but its too slow to use in my gam. It increases the execution time from 20-50 ms to over 1000ms!


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal