Ok, I was a bit curious so I had to try this a bit further.
This code allows you to blend 2 images together using clip, add, multiply, overlay, and soft light modes.
You will need to supply your own images (image1.png and image2.png) or change the code to fit what you need.
I have a use for this so I'm going to develop this a bit further by adding support for a transparency mask, and also allow tinting of an image for color effects. Since it only needs to be done once per effect, it is quite fast and could easily be used to mix textures together.
let me know what you think. The code is a bit of a mess but should work fine. The only thing you need to do is make both images the same size for now. (I recommend 256x256 for testing)
Graphics 800,600,0,2
SetBuffer BackBuffer()
img_comp = compimage("image1.png", "image2.png", 0)
While Not KeyHit(1)
If KeyHit (2) ; 1 key
img_comp = compimage("image1.png", "image2.png", 0)
info$ = "clip mode"
EndIf
If KeyHit (3) ; 2 key
img_comp = compimage("image1.png", "image2.png", 1)
info$ = "add mode"
EndIf
If KeyHit (4) ; 3 key
img_comp = compimage("image1.png", "image2.png", 2)
info$ = "multiply mode"
EndIf
If KeyHit (5) ; 4 key
img_comp = compimage("image1.png", "image2.png", 3)
info$ = "overlay mode"
EndIf
If KeyHit (6) ; 5 key
img_comp = compimage("image1.png", "image2.png", 4)
info$ = "soft light mode"
EndIf
DrawImage img_comp,50,50
Text 0,0,"press a key 1-5 to change blend modes"
Text 0,20,"current mode: " + info$
Flip
Cls
Wend ; end main loop
End ;end program
Function compimage(image1$, image2$, mode, mask$=0, tint_R=0, tint_G=0, tint_B=0 )
Local img1,img2,r,g,b,r2,g2,b2,r3,g3,b3,rgb3,tmp
img1 = LoadImage (image1)
img2 = LoadImage (image2)
;img3 = CreateImage(ImageWidth(img1),ImageHeight(img1))
If mask <> 0 Then maskimg = LoadImage (mask)
LockBuffer (ImageBuffer(img1))
LockBuffer (ImageBuffer(img2))
Select mode
Case 0 ; clip mask. all non black pixels from img1 are copied to img2
For y = 0 To ImageWidth(img1) -1
For x = 0 To ImageHeight(img1) -1
rgb=ReadPixelFast(x,y,ImageBuffer(img1))
r=(rgb Shr 16) And $ff
g=(rgb Shr 8) And $ff
b=rgb And $ff
If r + g + b > 0 ; if the color is non-black
WritePixelFast(x,y,rgb,ImageBuffer(img2))
EndIf
Next
Next
Case 1 ; add mode. All pixels from both img1 and img2 are added
For y = 0 To ImageWidth(img1) -1
For x = 0 To ImageHeight(img1) -1
rgb=ReadPixelFast(x,y,ImageBuffer(img1))
r=(rgb Shr 16) And $ff
g=(rgb Shr 8) And $ff
b=rgb And $ff
rgb2=ReadPixelFast(x,y,ImageBuffer(img2))
r2=(rgb2 Shr 16) And $ff
g2=(rgb2 Shr 8) And $ff
b2=rgb2 And $ff
r3 = (r+ r2)
g3 = (g + g2)
b3 = (b + b2)
If r3 > 255 Then r3 = 255
If g3 > 255 Then g3 = 255
If b3 > 255 Then b3 = 255
rgb3=(b3 Or (g3 Shl 8) Or (r3 Shl 16) Or ($ff000000))
WritePixelFast(x,y,rgb3,ImageBuffer(img2))
Next
Next
Case 2 ; multiply mode.
For y = 0 To ImageWidth(img1) -1
For x = 0 To ImageHeight(img1) -1
rgb=ReadPixelFast(x,y,ImageBuffer(img1))
r=(rgb Shr 16) And $ff
g=(rgb Shr 8) And $ff
b=rgb And $ff
rgb2=ReadPixelFast(x,y,ImageBuffer(img2))
r2=(rgb2 Shr 16) And $ff
g2=(rgb2 Shr 8) And $ff
b2=rgb2 And $ff
r3 = (r* r2) Shr 8
g3 = (g * g2) Shr 8
b3 = (b * b2) Shr 8
r3 = minmax (r3,0,255)
g3 = minmax (g3,0,255)
b3 = minmax (b3,0,255)
rgb3=(b3 Or (g3 Shl 8) Or (r3 Shl 16) Or ($ff000000))
WritePixelFast(x,y,rgb3,ImageBuffer(img2))
Next
Next
Case 3 ; overlay/mod2x Pixels >128 are added, pixels < 128 are subtracted
For y = 0 To ImageWidth(img1) -1
For x = 0 To ImageHeight(img1) -1
rgb=ReadPixelFast(x,y,ImageBuffer(img1))
r=(rgb Shr 16) And $ff
g=(rgb Shr 8) And $ff
b=rgb And $ff
rgb2=ReadPixelFast(x,y,ImageBuffer(img2))
r2=(rgb2 Shr 16) And $ff
g2=(rgb2 Shr 8) And $ff
b2=rgb2 And $ff
If r + g + b > 127+127+127 Then ;screenmode light pixels
r3 = 255 -((255-r) * (255 - r2) Shr 7)
g3 = 255 -((255-g) * (255 - g2) Shr 7)
b3 = 255 -((255-b) * (255 - b2) Shr 7)
Else ; multiply
r3 = (r* r2) Shr 7
g3 = (g * g2) Shr 7
b3 = (b * b2) Shr 7
EndIf
r3 = minmax (r3,0,255)
g3 = minmax (g3,0,255)
b3 = minmax (b3,0,255)
rgb3=(b3 Or (g3 Shl 8) Or (r3 Shl 16) Or ($ff000000))
WritePixelFast(x,y,rgb3,ImageBuffer(img2))
Next
Next
Case 4 ; softlight
For y = 0 To ImageWidth(img1) -1
For x = 0 To ImageHeight(img1) -1
rgb=ReadPixelFast(x,y,ImageBuffer(img1))
r=(rgb Shr 16) And $ff
g=(rgb Shr 8) And $ff
b=rgb And $ff
rgb2=ReadPixelFast(x,y,ImageBuffer(img2))
r2=(rgb2 Shr 16) And $ff
g2=(rgb2 Shr 8) And $ff
b2=rgb2 And $ff
tmp = r * r2 Shr 8;
r3 = tmp + r * (255 - ((255-r)*(255-r2) Shr 8)-tmp) Shr 8
tmp = g * g2 Shr 8;
g3 = tmp + g * (255 - ((255-g)*(255-g2) Shr 8)-tmp) Shr 8
tmp = b * b2 Shr 8;
b3 = tmp + b * (255 - ((255-b)*(255-b2) Shr 8)-tmp) Shr 8
r3 = minmax (r3,0,255)
g3 = minmax (g3,0,255)
b3 = minmax (b3,0,255)
rgb3=(b3 Or (g3 Shl 8) Or (r3 Shl 16) Or ($ff000000))
WritePixelFast(x,y,rgb3,ImageBuffer(img2))
Next
Next
End Select
UnlockBuffer (ImageBuffer(img1))
UnlockBuffer (ImageBuffer(img2))
Return img2
End Function
Function minmax#(value#,min#,max#)
If value > max Return max
If value < min Return min
Return value
End Function