TGC Codebase Backup



colourise image by the_winch

24th Nov 2006 11:19
Summary

Uses memblocks to colourise an image



Description

Uses memblocks to colourise an image. Useful in creating many different coloured images from a single source image.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    sync on : sync

`load an image and colourise it some different colours
load image "button.png", 1, 1

time = timer()
colourise(1, 2, rgb(255, 255, 128))
colourise(1, 3, rgb(158, 128, 255))
colourise(1, 4, rgb(255, 128, 255))
colourise(1, 5, rgb(255, 100, 128))
time = timer() - time
text 0, 0, "colourised in " + str$(time) + " milliseconds."
`paste the colourised images
y = 15
for i = 1 to 5
    paste image i,0,y
    inc y, image height(1)
next i

sync
wait key
end

function colourise(in as integer, out as integer, colour as integer)
    mem = free_memblock()
    make memblock from image mem, in
    width = memblock dword(mem, 0)
    height = memblock dword(mem, 4)
    depth = memblock dword(mem, 8)
    if (depth = 32) : `only works with 32 bit images
        pos = 12
        for i = 0 to width * height - 4
            p = memblock dword(mem, pos)
            p = rgbr(p)
            p = rgb(p * (rgbr(colour)/255.0), p * (rgbg(colour)/255.0), p * (rgbb(colour)/255.0))
            write memblock dword mem, pos, p
            inc pos, 4
        next i
    endif
    make image from memblock out, mem
    delete memblock mem
endfunction

function free_memblock()
    `find a free memblock number
    repeat
        inc i
    until memblock exist(i) = 0
endfunction i