'TransferMode: 0=MAX(Src,Dest)
Sub MergeAlphaLayers(DestImage As Bitmap, SrcBrush As Bitmap, DestX As Int, DestY As Int, Center As Boolean, TransferMode As Int) As Boolean
Dim Width As Int = SrcBrush.Width, Height As Int = SrcBrush.Height, tempX As Int, tempY As Int, SrcX As Int, SrcY As Int, SrcAlpha As Int, DestAlpha As Int
If Center Then
DestX = DestX - SrcBrush.Width * 0.5
DestY = DestY - SrcBrush.Height * 0.5
End If
If DestX > DestImage.Width Or DestY > DestImage.Height Then Return False
If DestX < 0 Then
SrcX = Abs(DestX)
Width = Width - SrcX
DestX = 0
End If
If DestY < 0 Then
SrcY = Abs(DestY)
Height = Height - SrcY
DestY = 0
End If
tempX = DestX + Width
If tempX > DestImage.Width Then Width = Width - (tempX - DestImage.Width)
tempY = DestY + Height
If tempY > DestImage.Height Then Height = Height - (tempY - DestImage.Height)
If Width < 1 Or Height < 1 Then Return False
Dim Pixels As Int = Width*Height, DestPixels(Pixels) As Int, SrcPixels(Pixels) As Int, ExDraw As ABExtDrawing = LCAR.ExDraw', tempSrcX As Int, tempSrcY As Int = SrcY, tempDestX As Int, tempDestY As Int = DestY
ExDraw.getPixels(SrcBrush, SrcPixels, 0, Width, SrcX,SrcY,Width,Height)
ExDraw.getPixels(DestImage, DestPixels, 0, Width, DestX,DestY,Width,Height)
For tempX = 0 To Pixels - 1
SrcAlpha = GetARGB(SrcPixels(tempX), 0)
If SrcAlpha > 0 Then
DestAlpha = GetARGB(DestPixels(tempX), 0)
Select Case TransferMode
Case 0
DestPixels(tempX) = SetARGB(SrcPixels(tempX), 0, Max(SrcAlpha, DestAlpha))
End Select
End If
Next
ExDraw.setPixels(DestImage, DestPixels, 0, Width, DestX, DestY, Width, Height)
Return True
End Sub
'Channel: 0=A, 1=R, 2=G, 3=B
'Replaces the byte of the ARGB channel with Value (which must be 0-255)
Sub SetARGB(Color As Int, Channel As Int, Value As Int) As Int
Value = Max(0,Min(Value,255))
Select Case Channel
Case 0'Alpha
Color = Bit.And(Color, 0x00ffffff)
Value = Bit.ShiftLeft(Value, 24)
Case 1'Red
Color = Bit.And(Color, 0xff00ffff)
Value = Bit.ShiftLeft(Value, 16)
Case 2'Green
Color = Bit.And(Color, 0xffff00ff)
Value = Bit.ShiftLeft(Value, 8)
Case 3'Blue
Color = Bit.And(Color, 0xffffff00)
End Select
Return Bit.Or(Color,Value)
End Sub
'Channel: 0=A, 1=R, 2=G, 3=B
Sub GetARGB(Color As Int, Channel As Int) As Int
Select Case Channel
Case 0: Return Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)'A
Case 1: Return Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)'R
Case 2: Return Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)'G
Case 3: Return Bit.And(Color, 0xff)'B
End Select
End Sub