Here is a function to add an alpha layer mask to a loaded image.
Although you can't save an image with an alpha layer, is is useful for applying the same mask to multiple images.
I used it to add rounded edges to all the images i was using as playing cards. It meant that I could just create plain rectangular images for the cards, rather than having to save each one with an alpha layer mask.
sync on;sync rate 0
load image "test.png",1,1
load image "mask.bmp",2,1
if mask_image(1,2)=-1 then end
make object plain 1,192,256
position object 1,0,0,0
texture object 1,1
set object transparency 1,2
set object filter 1,0
position camera 0,0,-639
point camera 0,0,0
do
text 0,0,"mask_image - Written by Jason Clogg"
text 0,15,"Masking bitmap should be a grayscale image with black being"
text 0,30,"a transparent pixel and white being a solid pixel."
sync
loop
function mask_image(source,mask)
rem mask_image(source,mask) - Written By Jason Clogg
rem source - image number you want to mask
rem mask - image number of mask you want to apply
rem Mask must be a grayscale bitmap. Black pixels mean full transparency
rem white pixels mean no transparency
rem Turn images into memblocks
make memblock from image 1,source
make memblock from image 2,mask
rem Get sizes of source and mask to ensure they are same size
sw=memblock dword(1,0)
sh=memblock dword(1,4)
mw=memblock dword(2,0)
mh=memblock dword(2,4)
rem If sizes are different return an error
if sw<>mw or sh<>mh then exitfunction -1
rem pos is start position of actual pixel data
pos=12
rem Map the mask onto each pixel of the source.
for y=1 to sh
for x=1 to sw
write memblock byte 1,pos+3,memblock byte(2,pos)
pos=pos+4
next x
next y
rem Recreate the source with the newly masked image
make image from memblock source,1
rem Clean up the memblocks
delete memblock 1
delete memblock 2
endfunction 0
Attached is a zip file containing the test images to use with this example.
Cheers,
Cloggy