Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
alphablending
#1
I have two arrays, a smaller getter() array and a buffer() array. Curently this is my code to blend them

Code:
SUB alphaput(x as integer, y as integer, x2 as integer, y2 as integer, getarray() as integer, destinationarray() as integer)
   'this sub will place an image on the screen and blend it with the background
   'it will not draw or blend anything that is of colour 0
   FOR xx = x to x2
      y3 = 0
      for yy = y to y2
         b = POINT (x3, y3, getarray(0))
         IF b THEN
            a = point (xx, yy, destinationarray(0))
            r = (red(b) + red(a)) SHR 1
            g = (green(b) + green(a)) SHR 1
            b = (blue(b) + blue(a)) SHR 1
            pset destinationarray(0), (xx, yy), rgb(r,g,b)
         END IF
         y3 = y3 + 1
      next
      x3 = x3 + 1
   next
END SUB

FUNCTION red(colour as integer) as integer
   red = (colour SHR 16) mod 256
END FUNCTION

FUNCTION green(colour as integer) as integer
   green = (colour SHR 8) mod 256
END FUNCTION

FUNCTION blue(colour as integer) as integer
   blue = colour mod 256
END FUNCTION

Its painfully slow. Does anbody know of any way to improve it. I hope there is a lot that can be done.
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#2
iirc FB PUT has built in alphablending by specifying the ALPHA flag:
put buffer, (x, y), buffer, alpha

Other than that, not much to be done, software alphablending is pretty slow..
Reply
#3
oh, I gotta get the new version. .14 gives me a syntax error.
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#4
You can use pointers too
pseudo code
Code:
type color32
  b as ubyte
  g as ubyte
  r as ubyte
  a as ubyte
end type
type BGRPTR as color32 ptr
sub BlendIt(byval des as BGRPTR,byval srcA as BGRPTR,byval srcB as BGRPTR)
  des->r=(srcA->r shr 1) + (srcB->r shr 1)
  des->g=(srcA->g shr 1) + (srcB->g shr 1)
  des->b=(srcA->b shr 1) + (srcB->b shr 1)
end sub
Write your red,gree,blue functions as macros every call need time and you call it for any pixel.

but the alpha code in libgfx2 is optimized in MMX assembler and very fast.

Joshy
Code:
option explicit
const scr_width  = 640
const scr_height = 480

'
' main
'
dim a as integer ptr
dim b as integer ptr
dim i as integer
dim ox as integer,oy as integer
dim mx as integer,my as integer
a=ImageCreate(scr_width,scr_height)
b=ImageCreate(scr_width,scr_height)

ScreenRes scr_width,scr_height,32
a=ImageCreate(scr_width,scr_height)
b=ImageCreate(scr_width,scr_height)
'Create Source A
for i=0 to 100
  line a,(rnd*scr_width\2,rnd*scr_height\2)-step(scr_width\2,scr_height\2),rgb(rnd*255,rnd*255,rnd*255),BF
next
'Create Source B
for i=0 to 100
  circle b,(rnd*scr_width\2,rnd*scr_height\2),rnd*scr_height\2,rgb(rnd*255,rnd*255,rnd*255),,,,F
next
'put A in Screenspace
put (0,0),a,PSET
while len(inkey)=0

  GetMouse mx,my
  'mouse in screenspace ?
  if (mx=-1) or (my=-1) then
    sleep 10
  'newpos <> oldpos ?
  elseif (mx<>ox) or (my<>oy) then
    'put A in Screenspace
    put (0,0),a,PSET
    put (mx-scr_width\2,my-scr_height\2),b,alpha,127 '0-255
    ox=mx:oy=my 'oldpos=newpos
  end if
wend
end
sorry about my english
Reply
#5
The problem that I was having when I was using it, is that I am trying to make the program extremely expandable. This means support for 15, 16, 24, and 32 bit depth.
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#6
After an Screen or ScreenRes command FreeBASIC will handel all 15, 16, 24, and 32 bit depth for you.

Joshy

for FB 15,16 are the same format
and 24,32 too
sorry about my english
Reply
#7
oh

Quote:If the ALPHA mode is used, you can also specify an optional alpha parameter,
ranging 0-255. If the alpha parameter is present, PUT will use it for all the
pixels of your sprite, and will skip mask colors like in the TRANS mode; this
will work only if you're in 15, 16, 24 or 32bit color depth. If you omit the
alpha parameter, PUT will only work if you're in 32bit color depth, and it
will use the alpha values embedded in each pixel; pixels using the mask color
will not be skipped in this case, as it is assumed the sprite has a properly
set up alpha channel.

The part about only 32 bit confused me, but I still must be doing somthing wrong, its not working.

nevermind, i didn't see the part about the alpha parameter
f you play a Microsoft CD backwards you can hear demonic voices. The scary part is that if you play it forwards it installs Windows.
Reply
#8
I'll try to better explain how PUT ALPHA mode works in gfxlib.
PUT supports two distinct ALPHA modes: an uniform mode and a per-pixel mode.
In the uniform mode, an unique alpha value is applied to your sprite that is thus uniformly blended onto the destination buffer. This means you supply an alpha value to PUT and this same value will be applied as alpha value to all the pixels of your sprite.
This mode is fast, and is useful if you do not plan to have parts of your sprite "more or less transparent" than other parts of the same sprite.
Uniform mode is achieved via the PUT form:
Code:
PUT [buffer], (x, y), sprite, ALPHA, value
Where value is the alpha value to be applied to all the pixels of the sprite.

The per-pixel mode allows to specify a different alpha value for each pixel composing your sprite; this alpha value is embedded directly into the pixel data, and since the only pixel format that allows to embed an alpha value is the RGBA 32bit format, this means this mode only works if you are using a 32bit screen mode.
Per-pixel alpha mode can be used by not specifying the alpha value when calling PUT:
Code:
PUT [buffer], (x, y), sprite, ALPHA
The alpha value in fact will be taken from each pixel stored in the sprite data.

This should answer your question about the 32bit part. Uniform alpha mode works in any direct-color depth (15, 16, 24 and 32bpp), as the pixel format does not require the alpha value (so 15, 16 and 24bpp are fine, even if they only sport a RGB and not an RGBA format). The per-pixel mode however requires an RGBA format and thus requires a 32bit mode.

The snippet you posted does not work as you probably want it to because the sprite data has 0 as alpha value for all pixels: in fact even if you are using a 32bit mode (you specified 24bpp, but 24 and 32 are equivalent in gfxlib and handled as 32bpp), you're using GET to get the sprite data from the screen, but you didn't specify an alpha value in your colors when drawing before using GET...
To specify an alpha value when drawing, use the &hAARRGGBB hex format for colors, or use the RGBA macro.

Hope it helps...
ngelo Mottola - EC++
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)