This item was added on: 2003/02/07
(Taken directly from Lightatdawn's FAQ)
Just a few notes: I'm not going to go into great detail here.
If you want to do this in Win32 it is easy.
If you want to do this in Console mode: You can't. Console mode won't do graphics. The End.
If you want to do this in DOS mode, this is more complicated. Check out www.wotsit.org for information regarding picture files of all kind. The actual code to do this is semi-long. I'll put it in anyhow. But just so you're warned, this example is quite limited. It's cut down to make it easier to understand. This will load "test.bmp" (This can be changed, obviously) from the running directory. It will first initialize mode 13 (320x200 screen with 256 col) and then load the BMP. It will only load an 8-bit bmp of a size less than the screen. The code can be adapted to read other types of BMP's quite easily. Here goes!
#include <dos.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
char far *Screen;
void VGAScreen(void)
{
union REGS r;
r.h.ah = 0;
r.h.al = 0x13;
int86(0x10, &r, &r);
return;
}
void TextScreen(void)
{
union REGS r;
r.h.ah = 0;
r.h.al = 0x3;
int86(0x10, &r, &r);
return;
}
int SetDAC(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 LoadBMP(void)
{
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];
char *filename = "test.bmp";
unsigned int offset, lines, paddedWidth;
BMPFile = fopen(filename, "rb");
if (BMPFile == NULL)
{
printf("Cant open file.");
return(1);
}
fread(&Header, 54, 1, BMPFile);
if (Header.bfType != 19778 || Header.bfReserved != 0 || Header.biPlanes != 1)
{
printf("Not a valid bitmap.");
fclose(BMPFile);
return(1);
}
if (Header.biCompression != 0)
{
printf("Compressed file.");
fclose(BMPFile);
return(1);
}
if (Header.biBitCount != 8)
{
printf("Not an 8-bit bitmap.");
fclose(BMPFile);
return(1);
}
if (Header.biWidth > 320 || Header.biHeight > 200)
{
printf("Size too large.");
fclose(BMPFile);
return(1);
}
fread(&Palette, 1024, 1, BMPFile);
for (c = 0; c < 255; c++)
SetDAC(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;
while (lines < Header.biHeight)
{
fread(Screen + offset, paddedWidth, 1, BMPFile);
offset -= 320;
lines++;
}
fclose(BMPFile);
return(0);
}
int main(int argcount, char *argvalue[])
{
Screen = (char far *) 0xA0000000L;
VGAScreen();
LoadBMP();
getch();
TextScreen();
return(0);
}