Code: Select all
' ---------------------------------- '
' accurate alpha blend demo '
' by cRex '
' ---------------------------------- '
' - Overview -
'Many internal alpha blending functions sacrifice quality for speed.
'Blending artifacts emerge because r,g,b get multiplied by an alpha
'ranging from 0 to 255, and divided by 256. (common case)
'This is easily demonstrated with similar (source, dest) components.
'For example: (src_r = 0, dst_r = 1)
#include "fbgfx.bi"
#Define TRUE -1
#Define FALSE 0
ScreenRes 400, 400, 32,, fb.GFX_ALPHA_PRIMITIVES
Sub ShowBits( ByVal value_ As UInteger, ByVal NoNewline As Integer = FALSE)
Dim As UInteger strPos, bitPos, AryFG(3), AryBG(3),I,J
Dim s_ As String * 8
AryFG(0) = RGB(200,200,200)
AryFG(1) = RGB(255,0,0)
AryFG(2) = RGB(0,255,0)
AryFG(3) = RGB(0,32,255)
AryBG(0) = RGB(100,100,100)
AryBG(1) = RGB(127,0,0)
AryBG(2) = RGB(0,127,0)
AryBG(3) = RGB(0,0,86)
For I = 0 To 3
Color AryFG(I), AryBG(I)
For J = 0 To 7
bitPos = (3 - I) Shl 3 + J
strPos = 7 - J
If bit(value_, bitPos) Then s_[strPos] = 49 Else s_[strPos] = 48
Next
Print s_;
Next
If Not NoNewline Then Print
End Sub
Sub InfoAlpha(ByVal A As Integer,ByVal Divisor As Integer = 255)
Color RGB(255,255,255),RGB(0,0,0)
? " A = " & Str(A/Divisor)
End Sub
' ===========================================
#Macro Alpha256(ret,back,fore, am, a256)
ret=((_
(fore And &Hff00ff) * a256 + _
(back And &Hff00ff) * am + &H800080) And &Hff00ff00 Or (_
(fore And &H00ff00) * a256 + _
(back And &H00ff00) * am + &H008000) And &H00ff0000) Shr 8
#EndMacro
Sub Test(backV As UByte,foreV As UByte,ByVal Accurate As Integer = 0,ByVal steps As uByte = 5)
Dim As UInteger Ptr pixel = ScreenPtr
Dim As UInteger fore = RGBA(foreV,foreV,foreV,foreV)
Dim As UInteger back = RGBA(backV,backV,backV,backV)
If Accurate Then
Dim As Single step_acc = 256 / steps
For s As Single = 0 To 256 Step step_acc
Dim As Integer A = s
Dim As Integer vm = 256 - A
Dim As Integer c
Alpha256(c,back,fore, vm, A)
ShowBits c,TRUE
InfoAlpha A,256
Next
Else
Dim As Single step_reg = 255 / steps
For s As single = 0 To 255 Step step_reg
Dim As Integer A = s
*pixel = back
PSet (0,0), RGBA(foreV,foreV,foreV,A)
ShowBits Point(0,0),TRUE
InfoAlpha A,255
Next
EndIf
End Sub
Dim Shared As Integer N
Sub DualTest(back As UByte,fore As UByte)
N += 1
Cls
Color RGB(255,255,255),RGB(0,0,0)
? "Test " & N & " / 2"
?:?
? "Back: " & back & ", Fore: " & fore
?:?
? "standard"
Test back,fore
?:?
Color RGB(255,255,255),RGB(0,0,0)
? "accurate"
Test back,fore,TRUE
Sleep
End Sub
DualTest 1,0
DualTest 255,254