Skip to content

Instantly share code, notes, and snippets.

@mahan
Created July 18, 2016 17:55
Show Gist options
  • Save mahan/4b20dc3cf23efb22743146a10e1c86ec to your computer and use it in GitHub Desktop.
Save mahan/4b20dc3cf23efb22743146a10e1c86ec to your computer and use it in GitHub Desktop.
Demo: Mixing two images in PureBasic (assumes 2 128x128 textures)
UsePNGImageDecoder()
UsePNGImageEncoder()
Global Dim imageColors.i(1, 128, 128)
Procedure main()
LoadImage(0, "clay_cut_128px_ta.png")
LoadImage(1, "grass_cut_128_ta.png")
Protected x
Protected y
;read pixel colors for image0
StartDrawing(ImageOutput(0))
For x = 0 To ImageWidth(0)-1
For y = 0 To ImageHeight(0)-1
imageColors(0, x, y) = Point(x, y)
Next
Next
StopDrawing()
;read pixel colors for image1
StartDrawing(ImageOutput(1))
For x = 0 To ImageWidth(1)-1
For y = 0 To ImageHeight(1)-1
imageColors(1, x, y) = Point(x, y)
Next
Next
StopDrawing()
;compose image2 by mixing colors of image0 and image1 with influence depending on x-position
CreateImage(2, ImageWidth(0), ImageHeight(0))
StartDrawing(ImageOutput(2))
For x = 0 To ImageWidth(2)-1
For y = 0 To ImageHeight(2)-1
Protected c0 = imageColors(0, x, y), c1 = imageColors(1, x, y)
Protected r0 = Red(c0), g0 = Green(c0), b0 = Blue(c0)
Protected r1 = Red(c1), g1 = Green(c1), b1 = Blue(c1)
Protected r2 = (255 * (((r0 / 255.0) * (x/128)))) + (255 * (((r1 / 255.0) * ((128-x)/128))))
Protected g2 = (255 * (((g0 / 255.0) * (x/128)))) + (255 * (((g1 / 255.0) * ((128-x)/128))))
Protected b2 = (255 * (((b0 / 255.0) * (x/128)))) + (255 * (((b1 / 255.0) * ((128-x)/128))))
Plot(x, y, RGB(r2, g2, b2))
Next
Next
StopDrawing()
SaveImage(2, "output.png")
EndProcedure
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment