November 30, 2020, 01:35:37 AM

Author Topic: [bb] 2D Alpha Blending by Ryudin [ 1+ years ago ]  (Read 495 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2D Alpha Blending by Ryudin [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : 2D Alpha Blending
Author : Ryudin
Posted : 1+ years ago

Description : This file was originally posted here:
<a href="codearcs4008.html?code=236" target="_blank">http://www.blitzbasic.com/codearcs/codearcs.php?code=236[/url]

When I tried to use this for an alpha blended cursor, I noticed that I kept getting an error whenever my cursor went offscreen. I took a few minutes to fix this and then decided to post the fixed code for everyone.

Enjoy! ; )


Code :
Code: BlitzBasic
  1. ;Note - I changed the function name and order of parameters a bit for easiness.
  2.  
  3. ;AS ORIGINALLY POSTED:
  4. ; ID: 236
  5. ; Author: WebDext
  6. ; Date: 2002-02-13 01:17:50
  7. ; Title: AlphaBlit
  8. ; Description: Yet another way to alpha blit.
  9.  
  10. Dim pixelData(5000,5000)
  11.  
  12. Function DrawImageAlpha(Image, xOff, yOff, DestBuffer, Alpha#)
  13.         ;This function alpha blits any image smaller than 100,50 onto a desitantion
  14.         ;buffer of equal or lesser size, It has no error handling to speed up the
  15.         ;function.
  16.        
  17.         ;DestBuffer is the pointer to the desitnation image.
  18.         ;ImgBuffer is the pointer to the image to be Alpha Blited.
  19.         ;xOff, yOff are the ofsets of the image on the destination image.
  20.         ;Alpha ranges from 0 to 1 and is the opacity of the image.
  21.  
  22.         InvAlpha#       = 1.0 - Alpha#
  23.         ImgBuffer       = ImageBuffer(Image)
  24.         iWidth          = ImageWidth(Image)
  25.         iHeight         = ImageHeight(Image)
  26.        
  27.         LockBuffer ImgBuffer
  28.                 For x = 0 To iWidth-1
  29.                         For y = 0 To iHeight-1
  30.                                 If (x + xOff) < WIDTH And (y + yOff) < HEIGHT Then
  31.                                         pixelData(x, y) = ReadPixelFast(x,y,ImgBuffer)
  32.                                 EndIf
  33.                         Next
  34.                 Next
  35.         UnlockBuffer ImgBuffer
  36.         LockBuffer DestBuffer
  37.                 For x = 0 To iWidth-1
  38.                         For y = 0 To iHeight-1
  39.                                 If (x + xOff) < WIDTH And (y + yOff) < HEIGHT Then
  40.                                         PixA# = ReadPixelFast(x + xOff, y + yOff)
  41.                                         PixB# = pixelData(x, y)
  42.                                        
  43.                                         ;Calculate Alphas with the least number of computations
  44.                                         aR = Int(PixA Sar 16)
  45.                                         aRI = aR Shl 16
  46.                                         aG = Int((PixA - aRI) Sar 8)
  47.                                         aB = PixA - (aRI + (aG Shl 8))
  48.        
  49.                                         ;Calculate Alphas with the least number of computations
  50.                                         bR = Int(PixB Sar 16)
  51.                                         bRI = bR Shl 16
  52.                                         bG = Int((PixB - bRI) Sar 8)
  53.                                         bB = PixB - (bRI + (bG Shl 8))
  54.                                        
  55.                                         ;Apply Alphas
  56.                                         If bR > 0 Or bG > 0 Or bB > 0
  57.                                                 nR = (aR * InvAlpha) + (bR * Alpha)
  58.                                                 nG = (aG * InvAlpha) + (bG * Alpha)
  59.                                                 nB = (aB * InvAlpha) + (bB * Alpha)
  60.                                        
  61.                                                 ;Write Pixel to Buffer
  62.                                                 WritePixelFast x + xOff, y + yOff, (nR Shl 16) + (nG Shl 8) + nB
  63.                                         EndIf
  64.                                 EndIf
  65.                         Next
  66.                 Next
  67.         UnlockBuffer DestBuffer
  68. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal