This function will take two images of any size (equal or not) and blend them together according to the values of a third greyscale alpha map image.
It has been written as a function so that is can easily be included in any project.
IMPORTANT: In order to use the code as it is, you will need to download and install IanM's Matrix1 Utils plug-in.
You can still run the code with out the plugin but you will need to change the FIND FREE MEMBLOCK() & FIND FREE BITMAP() commands.
`------------------------------
`Project : Memblock Image Blend
`------------------------------
`Author : Scraggle
`------------------------------
`Started : 9th September 2007
`------------------------------
`------------------------------------------------------------------------------
`Note: This project will take two images of any size (equal or not)
` and blend them together according to the values of a third
` greyscale alpha map image
` It has been written as a function so that is can easily be
` included in any project. The parameters for the function are
` listed below
`IMPORTANT: In order to use the code as it is, you will need to download and
` install IanM's Matrix1 Utils plug-in.
` You can still run the code with out the plugin but you will need
` to change the FIND FREE MEMBLOCK() & FIND FREE BITMAP() commands.
`------------------------------------------------------------------------------
`------------------------------------------------------------------------------
`PARAMETERS:
`iUpper : The image that will show through in the white areas of the alpha map
`iLower : The image that will show through in the black areas of the alpha map
`iAlpha : The greyscale image used to blend iUpper & iLower
`iResult: The image number of the resultant blended image
`Width : The width you want the resultant image to be
`Height : The height you want the resultant image to be
`Note : The images do not need to be the same size nor do any of them need to
` be the size of the resultant image.
` They will all be scaled automatically.
`------------------------------------------------------------------------------
function blend_Images( iUpper, iLower, iAlpha, iResult, Width, Height)
Local Percent as float
Local UpperBlue as byte
Local UpperGreen as byte
Local UpperRed as byte
Local LowerBlue as byte
Local LowerGreen as byte
Local LowerRed as byte
Local ResultBlue as byte
Local ResultGreen as byte
Local ResultRed as byte
Local mUpper as integer
Local mLower as integer
Local mAlpha as integer
Local mResult as integer
if image width(iUpper) <> width or image height(iUpper) <> height then iUpper = ResizeImage( iUpper, Width, Height )
if image width(iLower) <> width or image height(iLower) <> height then iLower = ResizeImage( iLower, Width, Height )
if image width(iAlpha) <> width or image height(iAlpha) <> height then iAlpha = ResizeImage( iAlpha, Width, Height )
mUpper = find free memblock( 1, 10 )
mLower = find free memblock( 11, 10 )
mAlpha = find free memblock( 21, 10 )
mResult = find free memblock( 31, 10 )
make memblock from image mUpper, iUpper
make memblock from image mLower, iLower
make memblock from image mAlpha, iAlpha
make memblock from image mResult, iUpper
mbSize = get memblock size( mUpper )
for Pos = 12 to mbSize - 12 step 4
Percent = ( memblock byte( mAlpha, Pos ) / 255.0 )
UpperBlue = memblock byte( mUpper, Pos + 0 )
UpperGreen = memblock byte( mUpper, Pos + 1 )
UpperRed = memblock byte( mUpper, Pos + 2 )
LowerBlue = memblock byte( mLower, Pos + 0 )
LowerGreen = memblock byte( mLower, Pos + 1 )
LowerRed = memblock byte( mLower, Pos + 2 )
ResultBlue = UpperBlue * ( 1- Percent ) + LowerBlue * Percent
ResultGreen = UpperGreen * ( 1- Percent ) + LowerGreen * Percent
ResultRed = UpperRed * ( 1- Percent ) + LowerRed * Percent
write memblock byte mResult, Pos + 0, ResultBlue
write memblock byte mResult, Pos + 1, ResultGreen
write memblock byte mResult, Pos + 2, ResultRed
write memblock byte mResult, Pos + 3, 255
next Pos
make image from memblock iResult, mResult
delete memblock mUpper
delete memblock mLower
delete memblock mAlpha
delete memblock mResult
endfunction
function ResizeImage( Image, Width, Height )
bResize = find free bitmap()
Create Bitmap bResize, width, height
sprite 1, 5000, 5000, Image
size sprite 1, width, height
paste sprite 1, 0,0
get image Image, 0, 0, width, height, 1
delete bitmap bResize
endfunction Image
The first two images have been blended using the thrid image to produce the final image below. Note the differing sizes, none of the first three images need to be the same size or even the size of the final image.