Pixel Alpha

Share your advanced PureBasic knowledge/code with the community.
venom
User
User
Posts: 56
Joined: Fri Jul 25, 2003 1:54 pm
Location: Australia

Pixel Alpha

Post by venom »

Code updated for 5.20+

Heres somthing I was fooling around with, it supports a different alpha value in every pixel. The normal image is a color image, and the alpha image is a grayscale(it actually only takes the red into account) image. In the alpha image every pixel of 255 is solid while 0 is fully clear. Currently it only supports 32-bit screens and only been tested in BGR pixel format, RGB screens should work. *It does very little error checking.

Code: Select all

InitSprite()
InitKeyboard()

OpenScreen(640,480,32,"")

#Image=0
#Alpha=1
#Background=2

CreateImage(#Image,256,256)
StartDrawing(ImageOutput(#Image))
  For i=0 To 255
    Line(i,0,1,256,RGB(255-i,128-i/2,i))
  Next
StopDrawing()

CreateImage(#Alpha,256,256)
StartDrawing(ImageOutput(#Alpha))
  For i=0 To 255
    Line(0,i,256,1,RGB(i,0,0))
  Next
StopDrawing()

CreateImage(#Background,640,480)
StartDrawing(ImageOutput(#Background))
  For i=0 To 100
    Circle(Random(640),Random(480),Random(30)+5,Random($FFFFFF))
  Next
StopDrawing()

Dim AlphaMask(ImageWidth(#Image)-1,ImageHeight(#Image)-1)
Dim ColTable(ImageWidth(#Image)-1,ImageHeight(#Image)-1)

iw=ImageWidth(#Image)
ih=ImageHeight(#Image)

For x=0 To iw-1
  For y=0 To ih-1
    StartDrawing(ImageOutput(#Image))
      AlphaMask(x,y)=(Red(Point(x,y))*100)/255
    StopDrawing()
    StartDrawing(ImageOutput(#Image))
      ColTable(x,y)=Point(x,y)
    StopDrawing()
  Next
Next

Procedure Draw(x1,y1)
  Shared ColTable(), AlphaMask()
  iw=ImageWidth(#Image)
  ih=ImageHeight(#Image)
  StartDrawing(ScreenOutput())
    outbuf=DrawingBuffer()
    If outbuf=0
      End
    EndIf
    *pbuf.Integer
    hline=DrawingBufferPitch()
    format=DrawingBufferPixelFormat()
    For x=0 To iw-1
      bufxoff=outbuf+(x+x1)*4
      For y=0 To ih-1
        *pbuf=bufxoff+(y+y1)*hline
        ocol=*pbuf\i
        rcol=ColTable(x,y)
        alpha=AlphaMask(x,y)
        alpham=100-alpha
        
        If format=#PB_PixelFormat_32Bits_BGR
          nRed=(Blue(ocol)*alpham+Red(rcol)*alpha)/100
          nGreen=(Green(ocol)*alpham+Green(rcol)*alpha)/100
          nBlue=(Red(ocol)*alpham+Blue(rcol)*alpha)/100
          *pbuf\i=nBlue+nGreen<<8+nRed<<16
          
        ElseIf format=#PB_PixelFormat_32Bits_RGB
          nRed=(Red(ocol)*alpham+Red(rcol)*alpha)/100
          nGreen=(Green(ocol)*alpham+Green(rcol)*alpha)/100
          nBlue=(Blue(ocol)*alpham+Blue(rcol)*alpha)/100
          *pbuf\i=nRed+nGreen<<8+nBlue<<16
        EndIf
      Next
    Next
  StopDrawing()
EndProcedure

t=GetTickCount_()
Repeat
  ClearScreen(RGB(0,0,0))
  StartDrawing(ScreenOutput())
    DrawImage(ImageID(#Background),0,0)
    DrawText(400,20,"fps: "+Str(ofps))
    If GetTickCount_()>t+1000
      t=GetTickCount_()
      ofps=fps
      fps=0
    EndIf
  StopDrawing()
  
  Draw(10,10)
  FlipBuffers()
  fps+1
  ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

Nice work! Works great here. :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply