`setup
sync on : sync rate 0
 
`Create media (the 2 memblocks that have to be blended and the map)
`Create 2 images to blend
ink rgb(255,0,0),0 : box 0,0,128,128 : get image 1,0,0,128,128,1 : make memblock from image 1,1
ink rgb(0,255,0),0 : box 0,0,128,128 : get image 2,0,0,128,128,1 : make memblock from image 2,2
 
`Create map
box 0,0,128,128,rgb(0,0,0),rgb(0,0,0),rgb(0,0,0),rgb(255,255,255)
get image 3,0,0,128,128,1 : make memblock from image 3,3
 
`Create output
BlendMapImageMemblocks(1,2,4,3)
make image from memblock 4,4
 
do
 
   cls
 
   `Output
   paste image 1, 0, 0 : paste image 2,129,0 : paste image 3,258,0
   ink rgb(255,255,255),0
   center text 64,64,"Img 1"
   center text 193,64,"Img 2"
   center text 322,64,"Greyscale map"
   text 5, 185, "Output:"
   paste image 4,0,200
 
   sync
loop
 
function BlendMapImageMemblocks(Mem1, Mem2, RetMem, Map)
 
   `Get data
   Width = memblock dword(Mem1, 0)
   Height = memblock dword(Mem1, 4)
 
   `setup teh returned memblock
   if memblock exist(RetMem)=0 then make memblock RetMem, 12 + (Width*Height*4)
   write memblock dword RetMem, 0, Width
   write memblock dword RetMem, 4, Height
   write memblock dword RetMem, 8, 32
 
   `blend
   for y = 1 to Height
      for x = 1 to Width
         pos = 12 + ((y-1)*Width*4) + ((x-1)*4)
         difblue#  = (memblock byte(Mem2, pos)   - memblock byte(Mem1, pos))
         difgreen# = (memblock byte(Mem2, pos+1) - memblock byte(Mem1, pos+1))
         difred#   = (memblock byte(Mem2, pos+2) - memblock byte(Mem1, pos+2))
         blue  = memblock byte(Mem1, pos)   + int(difblue# / 255.0 * memblock byte(Map, pos))
         green = memblock byte(Mem1, pos+1) + int(difgreen#/ 255.0 * memblock byte(Map, pos+1))
         red   = memblock byte(Mem1, pos+2) + int(difred#  / 255.0 * memblock byte(Map, pos+2))
         write memblock byte RetMem, pos, blue
         write memblock byte RetMem, pos+1, green
         write memblock byte RetMem, pos+2, red
         write memblock byte RetMem, pos+3, 255
      next x
   next y
 
endfunction