News:

Function Finder  Find all the functions within source code files

Main Menu

Fade Image (Integer) - Manually Fading ARGB pixels

Started by kevin, March 19, 2023, 10:00:21 PM

Previous topic - Next topic

kevin

 Fade Image (Integer) - Manually Fading ARGB pixels


 While PlayBASIC does include its own built-in function that can fade an image, you might be wondering how you could approach the task of fading each individual RGB color within the image yourself.

 Below, we move through the image pixel by pixel. For each pixel, we read it into a variable (ThisARGB). The variable now contains the four ARGB levels of this pixel. What we could do is split out each component, fade them, and then recombine them and write them back to our image. However, that's a lot of work for each individual A, R, G, and B level within the pixel. So what if we could scale pairs together? This would reduce the amount of work required by about half.

 To scale (fade) pairs, we mask the original ARGB pixel data to separate the A, G, and R, B channels, respectively. The AG pair needs to be shifted down 8 bits (see shift operator). Once it is, we can multiply it by our scaler. Since we're multiplying by an 8-bit value, we're effectively shifting the AG channel back up 8 bits again. We repeat this for the RB pair, multiplying it by the scaler, which also shifts that pair up 8 bits.

 To construct the new color, we need to mask off any unwanted bits in the RG pair ($ff00ff00), which will remove any bits that might collide with the R/B data. We also need to perform the same mask on the RB pair and then move them right by 8 bits.

 Once both pairs are scaled, masked, and shifted into place, we OR them back together to form the newly faded color.


PlayBASIC Code: [Select]
   file$="FILENAME-OF-IMAGE-TO-LOAD-HERE"

t=timer()
Img=loadNewIMage(File$,2)
lt=timer()-t


img2=getFreeImage()

do
cls
copyimage img,Img2

FadeAngle#=wrapangle(FadeAngle#,1)
fadeLevel#=sin(FadeAngle#)

FadeIMage_Integer(IMg2,fadeLevel#)

drawimage img2,0,0,false
print "Load Time:"+str$(lt)


print fadeLevel#

sync
loop spacekey()


Function FadeIMage_Integer(ThisIMAGE,fadeLevel#)

OldSurface =getsurface()

FADE_LEVEL = cliprange(255*fadeLevel#,0,255)

Width =getImageWIdth(ThisIMAGE)

RenderToImage ThisIMAGE
lockbuffer
ThisRGB =point(0,0)

Ptr =GetIMagePtr(ThisIMAGE)
Pitch =GetIMagePitch(ThisIMAGE)

for ylp=0 to getImageHeight(ThisIMAGE)-1

RowPTR= Ptr+(ylp*Pitch)

for xlp =0 to Width-1

ThisARGB = PeekInt(RowPtr)

// Scale pairs
This_A_G =(( ThisARGB and $ff00ff00 ) >> 8 ) *FADE_LEVEL
This_R_B =(( ThisARGB and $00ff00ff ) ) *FADE_LEVEL

// Repack ARGB
ThisARGB = (This_A_G and $ff00ff00)
ThisARGB |=((This_R_B >> 8 ) and $00ff00ff)

// Output pixel to buffer
pokeint RowPtr , ThisARGB

// Move tot he next pixel
RowPTR+=4
next xlp
next ylp

unlockbuffer
rendertoimage OldSurface

EndFunction







#learnocode #coding #programmer #programming #2d #graphics #sourcecode  #indiedev #gamedev #fadeimage #image #howto #operators #bitwise