Just had this idea the other day for how to create an image fading function. Worked it out here it is. Would like some feedback. I have noticed though that it can get REALLY slow with larger images (the second image being 640x480). Any suggestions on how to deal with that?
The idea is that it takes the color of the pixel it is on from both pictures. Then using pic 1 as a base, it will change the color data based off a percent difference from the other. Like this:
Red1=rgbr(PIC1)
Red2=rgbr(PIC2)
red3=red1-(percent*(red1-red2))/100
For some reason it doesn't always like 100 though. Percent is noted by opacity (100 is fully visible and 0 is completely transparent).
here is the code:
FUNCTION FadeImage(img1,img2,xpos,ypos,percent)
REM Make the 3 memblocks for the transformation
REM 1 is the background
REM 2 is the Foreground
REM 3 is the result
make memblock from image 1,1
make memblock from image 2,2
make memblock 3,GET memblock size(1)
REM Retrieves width and height data for both images
width=memblock dword(2,0)
height=memblock dword(2,4)
width2=memblock dword(1,0)
height2=memblock dword(1,4)
REM Copies memblock 1 to memblock 3. This is because the only pixels that change
REM are pixels from memblock 2. So everywhere that memblock 2 DOESN'T have something,
rem nothing needs to be done
copy memblock 1,3,0,0,get memblock size(1)
REM Begins scanning image 2 Left to right top to bottom
FOR y=ypos to (ypos+height)-1
FOR X=xpos TO (xpos+width)-1
newx=x-xpos
newy=y-ypos
Rem Retrieves color data for the pixel
rgb1=memblock dword(1,12+4*(y*width2+x))
REM Checks to make sure it is in a valid position (should be) and gets color data
pos=4+12+4*((newy)*width+(newx))
if pos<=get memblock size(2)
rgb2=memblock dword(2,pos-4)
endif
REM Don't do anything if img2 pixel is black
if rgb2<>0
REM calculates 1% of the difference
rd#=(rgbr(rgb1)-rgbr(rgb2))/100
gd#=(rgbg(rgb1)-rgbg(rgb2))/100
bd#=(rgbb(rgb1)-rgbb(rgb2))/100
REM multiplies the difference by the percentage and subtracts from the starting color (rgb1)
rgb3=rgb(rgbr(rgb1)-(rd#*percent),rgbg(rgb1)-(gd#*percent),rgbb(rgb1)-(bd#*percent))
write memblock dword 3,12+4*(y*width2+x),rgb3
endif
NEXT X
NEXT Y
REM Makes the final image and cleans up
make image from memblock 3,3
DELETE MEMBLOCK 1
DELETE MEMBLOCK 2
DELETE MEMBLOCK 3
ENDFUNCTION
I appreciate any advice or help given!
Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose