Hi,
starting from version 2011R4 it’s possible to create picture with AlphaChannel (instead of the classic mask) using the new constructor that omits depth.
Despite official documentation say “RGBSurface has been updated to properly support alpha channels by taking them into account when rendering objects”, I’m experiencing a strange behavior when using RGBSurface.Transform with images with Alpha Channel.
For example consider the simple transformation that inverts an image (also reported into the docs):
Put this code into the Paint event of a canvas
[code]Sub Paint(g As Graphics, areas() As REALbasic.Rect)
// RGBSurface.Transform vs RGBSurface.Pixel for Images with AlphaChannel
#if RBVersion<2011.04 then
’ Pictures with alpha channel require version 2011R4 or later
return
#else
Dim sizeX,sizeY as integer
sizeX=me.Width/2
sizeY=me.Height/2
dim fillAlpha as integer = 0
Dim fillColor,borderColor as Color
fillColor=rgb(0,220,220,fillAlpha)
borderColor=rgb(40,40,0)
' Create two identical images
Dim imgWithAlpha1 as picture
imgWithAlpha1=new picture (sizeX,sizeY) ' New Constructor -> will create a picture with AlphaChannel
imgWithAlpha1.Graphics.ForeColor=fillColor
imgWithAlpha1.Graphics.FillOval 1,1,sizeX-2,sizeY-2
imgWithAlpha1.Graphics.ForeColor=borderColor
imgWithAlpha1.Graphics.DrawOval 0,0,sizeX,sizeY
' Draw original image
g.DrawPicture imgWithAlpha1,0,0
Dim imgWithAlpha2 as picture
imgWithAlpha2=new picture (sizeX,sizeY) ' New Constructor -> will create a picture with AlphaChannel
imgWithAlpha2.Graphics.ForeColor=fillColor
imgWithAlpha2.Graphics.FillOval 1,1,sizeX-2,sizeY-2
imgWithAlpha2.Graphics.ForeColor=borderColor
imgWithAlpha2.Graphics.DrawOval 0,0,sizeX,sizeY
' Draw original image
g.DrawPicture imgWithAlpha2,0,sizeY
' Invert imgWithAlpha1 with Tansform
dim i,map(255) as Integer
for i=0 to 255
map(i)= 255-i
Next
imgWithAlpha1.RGBSurface.Transform map
' Draw inverted image
g.DrawPicture imgWithAlpha1,sizeX,0
' Invert imgWithAlpha2 with RGBSurface.Pixel
dim x,y,w,h as integer
dim c as color
dim s as RGBSurface
s=imgWithAlpha2.RGBSurface
w=imgWithAlpha2.Width-1
h=imgWithAlpha2.Height-1
for y=0 to h
for x=0 to w
c=s.Pixel(x,y)
s.Pixel(x,y)=rgb(255-c.red,255-c.Green,255-c.Blue,c.Alpha)
next
next
' Draw inverted image
g.DrawPicture imgWithAlpha2,sizeX,sizeY
#endif
End Sub
[/code]
Results for fillAlpha=0 and fillAlpha=128
As you can see the Pixel method leads to better (and correct) results but its 8-10 times slower ;-(
For other image processing algorithms (like Brightness, Contrast, RGB Shift…) the results are similar, thus the question is:
how to correctly use RGBSurface.Transform for images with alpha channel (that use transparency) ?