I'll dump a bit of code on you straight from something I found in my backups written at least 5 years ago. Theres enough comments in the code that you should be able to get the gist of how this works, and thus be able to use it to do what you're after.
IMPORTANT NOTE: This was written in the days of Borland Turboo C++ and thus used the DOS header and lib. You won't be able to actually use this code unless you've got a compiler that supports this. However, everything in the BitMap loading code will work except set_DAC, so the concept of the code is still sound.
Code:
/*
******************************************
* EX_BMP.CPP *
* An example of loading & displaying *
* an 8-bit bitmap *
* *
******************************************
*/
#include <stdio.h> //fopen and other i/o stuff
#include <conio.h> //used for getch();
#include <dos.h> //Used for pretty much everything
char far *Screen;
//Sets the display to VGA 320x200x256
int init_13h_mode ()
{
union REGS r;
r.h.ah = 0;
r.h.al = 0x13;
int86(0x10, &r, &r);
return 0;
}
//Goes back to text mode
int init_text_mode ()
{
union REGS r;
r.h.ah = 0;
r.h.al = 0x3;
int86(0x10, &r, &r);
return 0;
}
//This sets a DAC register to a specific Red Green Blue-value
int set_DAC (unsigned char DAC, unsigned char R, unsigned char G, unsigned char B)
{
outportb (0x3C8, DAC);
outportb (0x3C9, R);
outportb (0x3C9, G);
outportb (0x3C9, B);
return 0;
}
int load_bmp (char *filename){
struct BMPHeader {
unsigned short bfType;
long bfSize,
bfReserved,
bfOffBits,
biSize,
biWidth,
biHeight;
unsigned short biPlanes,
biBitCount;
long biCompression,
biSizeImage,
biXPelsPerMeter,
biYPelsPerMeter,
biClrUsed,
biClrImportant;
} Header;
FILE *BMPFile;
unsigned char c, Palette[256][4];
unsigned int offset, lines, paddedWidth;
//This checks for the file
BMPFile = fopen (filename, "rb");
if (BMPFile == NULL) {
printf ("Cant open file.");
return 1;
}
//Read the headerinformation
fread (&Header, 54, 1, BMPFile);
if (Header.bfType != 19778 || Header.bfReserved != 0 || Header.biPlanes !=1) {
//Not a valid bitmap file - don't display
printf ("Not a valid bitmap.");
fclose (BMPFile);
return 1;
}
if (Header.biCompression != 0) {
//Compressed file - don't display
printf ("Compressed file.");
fclose (BMPFile);
return 1;
}
if (Header.biBitCount != 8) {
//If the file is other than 8-bit dont read.
printf ("Not an 8-bit bitmap.");
fclose (BMPFile);
return 1;
}
if (Header.biWidth > 320 || Header.biHeight > 200) {
//If its larger than 320*200 dont load.
printf ("Size too large.");
fclose (BMPFile);
return 1;
}
//Ladda paletten
fread (&Palette, 1024, 1, BMPFile);
for (c = 0; c < 255; c++)
set_DAC (c, Palette[c][2] >> 2, Palette[c][1] >> 2, Palette[c][0] >> 2);
offset = (100 + (Header.biHeight >> 1)) * 320 + 160 - (Header.biWidth >> 1);
lines = 0;
paddedWidth = Header.biWidth & 0xFFFC;
if (Header.biWidth != paddedWidth) paddedWidth += 4;
//Loop for reading lines
while (lines < Header.biHeight) {
fread (Screen + offset, paddedWidth, 1, BMPFile);
offset -= 320;
lines++;
}
fclose (BMPFile);
return 0;
}
int main ()
{
//Set up a pointer in vga memory at A000:0000
Screen = (char far *)0xA0000000L;
init_13h_mode (); //initialize VGA mode 13
load_bmp("TEST.BMP"); //load "TEST.BMP"
getch(); //wait for keypress
init_text_mode (); //return to text mode
return 0;
}