the following code is untested. it may be used in your routine without credit (public domain) but its purpose is to describe how to write your own... it may or may not work.
BMP files are complex. as graphics formats goes, some are more complex, but loading a BMP is not merely an issue of loading the file as binary. for some of us who prefer to write their own routines, it may be daunting to decode the BMP format. there is working code on a number of websites, including the ypi homepage: http://www.philipz.com/qb/
of course you could use someone else's code to make a bmp to raw converter, and i'm not sure why i haven't done this yet. in the meantime, the best converter i know is available (freeware) at http://www.irfanview.com
the main advantages of the BMP format are simple: bmps can be smaller and it is a format handled by many programs. shiftlynx has a bmp to bsave converter that you may find very useful. the advantage of RAW format is its simplicity and the simplicity of the code used to load and save it.
to convert a 24 color bitmap (or many other images for that matter, including jpeg, jp2, gif, bmp, pcx, tiff, png) to RAW, load it in irfanview. save as, and hit S or file... save as. be certain to press the OPTIONS button after selecting RAW - Raw Image Data under the Save As Type box and under the options dialog (RAW tab) select Color Order: RBG and Interleaved: (RGB RGB...) you do not want the other options.
this will save your file as a RAW format. i don't understand why, but some combinations of width and height just don't work. 640 and 480 works, and 800x600, and all those standards. smaller custom resolutions work and if nothing else i just round to the nearest 100, but for some reason when you save your new image sometimes it will save with a diagonal distortion. i wish i understood it, but saving the image with the right amount blank space on the side and bottom fixes it. your program can then ignore the space and crop the image.
RAW has the simplest file "format" i've ever seen, which is the point: it has one byte for red (ascii 97 is red 97... etc) one for green and one for blue. the first 3 bytes are the first pixel, the next 3 are the next pixel. it doesn't save a header or width and height info. you could append this information to the end or make up your own format which is really a RAW file starting with a header, or save the height and width in another file, it's up to you.
when you display a RAW file you have to tell the loader the width and height of the file... if you saved an image that was 640x480 you have to tell the loader 640x480.
lets say your image is storing a font, and its 640 pixels wide across and 16 pixels high. each pixel is 3 bytes in length, so you can load the whole file like this:
(if you are using this to load a monochrome font, skip to the part that says ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> fonts:")
open "font.raw" for binary as #1 do r$=r$+input$(1,#1) 'add the red data for each individual pixel g$=g$+input$(1,#1) 'add the green data for each individual pixel b$=b$+input$(1,#1) 'add the blue data for each individual pixel loop until eof(1) close #1
640 pixels times 16 is about 10 kb, which you can fit in a string, but with 3 bytes for each pixel its really 640 x 16 x 3 which is cutting it close to the limit.
in any case, this would load the entire 24 bit color picture. you could use an array or simply plot it to the screen, the method you choose will be of your choosing.
to display it is not as simple, because you have to be in 24bit color mode or use a 256 color palette. this is functionality that has nothing to do with the loader, which simply gives you the rgb values for every pixel. lets say you were displaying your imagine in monochrome:
q=0 for y=1 to 16 for x=1 to 640 q=q+1 r=asc(mid$(r$,q,1)) 'the red value g=asc(mid$(g$,q,1)) 'the green value b=asc(mid$(b$,q,1)) 'the blue value 'now if you were in 24 bit color mode, this would be easy. 'in monochrome, just average the red green and blue: i=int((r+g+b)/3) 'and if it's closer to 0 put a black pixel, if it's closer to 255 put white: if i > 127 then c=15 else c=0 pset (x-1,y-1), c 'and that's all there is to it! next x next y
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> fonts: (read the rest if you want to know how all this works)
of course this is all much easier if you are simply loading a monchrome FONT! you don't need nearly as much code, even if your RAW image is 24 bit color.
a monochrome image saved as 24 bit color is going to have pixels of either rgb 0,0,0 or 255,255,255.
so we can just load the reds:
open "font.raw" for binary as #1 do p$=p$+input$(1,#1) 'add the red data for each individual pixel discard$=input$(2,#1) 'forget green and blue loop until eof(1) close #1
the part that displays image on the screen is easier too:
q=0 for y=1 to 16 for x=1 to 640 q=q+1 r=asc(mid$(r$,q,1)) 'the red value if r=0 then pset (x-1,y-1), 0 else pset (x-1,y-1), 15 next x next y
now working with palettes, you're on your own there. but loading the rgb of every pixel in an image? piece of cake. and now you've got a way that you can convert a BMP to a file you can write your own loader for!
(i recommend loading the monochrome RAW and converting it to a text file with only "*" and spaces, you know. if you're going to have a file for a font routine, this is another step towards the best way to do it :)
- this was written by mennonite and put here with his permission