Hi,
[Edit]I've edited the AppGameKit code to show the now working version should anyone wish to use it
[/Edit]
Back in 2007 I wrote a function that would blend two images together based on a third
alpha mask image and posted
here on the code snippets board. I'm trying to convert it to AppGameKit which I thought would be pretty straight forward but it isn't working.
Is there a BUG with the memblock image commands or am I doing something wacky that I can't see?
This is the original code from the link above:
`------------------------------
`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
And this is my attempt to convert it to AppGameKit v2:
`------------------------------
`Project : Memblock Image Blend
`------------------------------
`Author : Scraggle
`------------------------------
`Started : 4th February 2015
`------------------------------
`------------------------------------------------------------------------------
`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
` Converted to AGK from the original DBP code
`------------------------------------------------------------------------------
`------------------------------------------------------------------------------
`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, Width, Height)
Local Percent as float
Local UpperBlue as integer
Local UpperGreen as integer
Local UpperRed as integer
Local LowerBlue as integer
Local LowerGreen as integer
Local LowerRed as integer
Local ResultBlue as integer
Local ResultGreen as integer
Local ResultRed as integer
Local mUpper as integer
Local mLower as integer
Local mAlpha as integer
Local mResult as integer
Local iResult as integer
if getimagewidth(iUpper) <> width or getimageheight(iUpper) <> height then iUpper = ResizeImage( iUpper, Width, Height )
if getimagewidth(iLower) <> width or getimageheight(iLower) <> height then iLower = ResizeImage( iLower, Width, Height )
if getimagewidth(iAlpha) <> width or getimageheight(iAlpha) <> height then iAlpha = ResizeImage( iAlpha, Width, Height )
mUpper = 1
mLower = 2
mAlpha = 3
mResult = 4
CreateMemblockFromImage( mUpper, iUpper )
CreateMemblockFromImage( mLower, iLower )
CreateMemblockFromImage( mAlpha, iAlpha )
CreateMemblockFromImage( mResult, iUpper )
mbSize = getmemblocksize( mUpper )
for Pos = 12 to mbSize step 4
Percent = ( getmemblockbyte( mAlpha, Pos ) / 255.0 )
UpperBlue = getMemblockByte( mUpper, Pos + 0 )
UpperGreen = getMemblockByte( mUpper, Pos + 1 )
UpperRed = getMemblockByte( mUpper, Pos + 2 )
LowerBlue = getMemblockByte( mLower, Pos + 0 )
LowerGreen = getMemblockByte( mLower, Pos + 1 )
LowerRed = getMemblockByte( mLower, Pos + 2 )
ResultBlue = UpperBlue * ( 1- Percent ) + LowerBlue * Percent
ResultGreen = UpperGreen * ( 1- Percent ) + LowerGreen * Percent
ResultRed = UpperRed * ( 1- Percent ) + LowerRed * Percent
setmemblockbyte( mResult, Pos + 0, ResultBlue )
setmemblockbyte( mResult, Pos + 1, ResultGreen )
setmemblockbyte( mResult, Pos + 2, ResultRed )
setmemblockbyte( mResult, Pos + 3, 255 )
next Pos
iResult = CreateImageFromMemblock( mResult )
deletememblock( mUpper )
deletememblock( mLower )
deletememblock( mAlpha )
deletememblock( mResult )
endfunction iResult
function ResizeImage( Image, Width, Height )
s = CreateSprite( image )
SetSpriteSize( s, Width, Height )
SetSpritePosition( s, 0, 0 )
Render()
i = GetImage( 0, 0, Width, Height )
DeleteSprite( s )
endfunction i
If you'd like to test it then I used this test code to call the function:
// Project: Image Functions
// Created: 2015-02-04
// set window properties
SetWindowTitle( "Image Functions" )
SetWindowSize( 768, 512, 0 )
// set display properties
SetVirtualResolution( 768, 512 )
SetOrientationAllowed( 1, 1, 1, 1 )
#include "Image Functions.agc"
type tImage
img as integer
spr as integer
endtype
Grass as tImage
Rock as tImage
Mask as tImage
Result as tImage
Grass.img = LoadImage( "Grass.png" )
Rock.img = LoadImage( "Rock.png" )
Mask.img = LoadImage( "mask.png" )
Result.img = blend_Images( Grass.img, Rock.img, Mask.img, 256, 256 )
Grass.spr = CreateSprite( Grass.img )
Mask.spr = CreateSprite( Mask.img )
Rock.spr = CreateSprite( Rock.img )
Result.spr = CreateSprite( Result.img )
SetSpritePosition( Grass.spr, 0, 0 )
SetSpritePosition( Mask.spr, 256, 0 )
SetSpritePosition( Rock.spr, 512, 0 )
SetSpritePosition( Result.spr, 256, 256 )
do
Sync()
loop
And I've attached the images I used to this post
AGK V2 user - Tier 1 & 2