TGC Codebase Backup



Create image from Data by Dewi Morgan

2nd Mar 2008 12:15
Summary

Very simple but handy li'l function to read in RGB triplets from data statements, and make an X*Y image from them, via a memblock. Very handy for making medialess programs.



Description

Very simple but handy li'l function to read in RGB triplets from data statements, and make an X*Y image from them, via a memblock.

I'm sure this has been done a billion times, since it's so handy for making medialess programs.

Changes you might want to make:
1) I write the fourth byte, alpha transparency, as the constant "255", meaning "completely opaque". You might want to read it in from a data statement instead, so you can make images with transparent areas.

2) I read from a data statement. You might want to read from a file or array or memblock or the screen or some other data source, instead.

3) I use IanM's "find free memblock()" and "find free image()" functions - if you do not want to use these, just give them an id, possibly passed as an optional parameter, or read from the data statement. In that case you might also want to do something like "if image exist ImageID then delete image ImageID", or "...endfunction 0" or something.

4) There's no reason the header of the image couldn't be stored in the data statement too. In which case, of course, you needn't pass parameters to the function.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` License: Public Domain
function ReadImageFromData(X,Y)
  ImageID  find free image()
  MemblockID = find free memblock()
  make memblock MemblockID, 4+4+4+(4*X*Y)
  write memblock dword MemblockID, 0, X ` Width
  write memblock dword MemblockID, 4, Y ` Height
  write memblock dword MemblockID, 8, 32 ` BitDepth
  for i = 12 to 12 + 4*X*Y - 1 step 4
    read r : read g : read b
    write memblock byte MemblockID, i,   b ` Stored in reverse.
    write memblock byte MemblockID, i+1, g
    write memblock byte MemblockID, i+2, r
    write memblock byte MemblockID, i+3, 255
  next i
  make image from memblock ImageID, MemblockID
  delete memblock MemblockID
endfunction ImageID