|
| GAMES++ |
|
|
|
 |
|
| BROWSER UTILITIES |
|
|
|
 |
|
| AFFILIATES |
|
|
|
 |
|
| ADVERTISEMENT |
|
|
|
 |
|
| ADVERTISEMENT |
|
|
|
 |
|
Displaying BMP Graphic Files in C
C/C++ Programming
By Steven H. Don
#include
#include
#ifdef __DJGPP
#include
#endif
#ifdef __DJGPP
char *Screen;
#else
char far *Screen;
#endif
//This sets the display to VGA 320x200 in 256 colours
void VGAScreen ()
{
union REGS r;
r.h.ah = 0;
r.h.al = 0x13;
int86(0x10, &r, &r);
}
//This resets the display to text mode
void TextScreen ()
{
union REGS r;
r.h.ah = 0;
r.h.al = 0x3;
int86(0x10, &r, &r);
}
//This sets a DAC register to a specific Red Green Blue-value
void 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);
}
//This loads in the actual BMP-file and displays it.
void LoadBMP (char *FileName)
{
//BMP Header structure
struct BMPHeader {
#ifdef __DJGPP
unsigned short bfType __attribute__((packed));
long bfSize __attribute__((packed));
long bfReserved __attribute__((packed));
long bfOffBits __attribute__((packed));
long biSize __attribute__((packed));
long biWidth __attribute__((packed));
long biHeight __attribute__((packed));
unsigned short biPlanes __attribute__((packed));
unsigned short biBitCount __attribute__((packed));
long biCompression __attribute__((packed));
long biSizeImage __attribute__((packed));
long biXPelsPerMeter __attribute__((packed));
long biYPelsPerMeter __attribute__((packed));
long biClrUsed __attribute__((packed));
long biClrImportant __attribute__((packed));
#else
unsigned short bfType;
long bfSize,
bfReserved,
bfOffBits,
biSize,
biWidth,
biHeight;
unsigned short biPlanes,
biBitCount;
long biCompression,
biSizeImage,
biXPelsPerMeter,
biYPelsPerMeter,
biClrUsed,
biClrImportant;
#endif
} Header;
//File handle
FILE *BMPFile;
//Required for palette
unsigned char c, Palette[256][4];
//USed for loading into video memory
unsigned int offset, lines, paddedWidth;
//Check to see whether the file exists and can be opened
BMPFile = fopen (FileName, "rb");
if (BMPFile == NULL) {
strcat (FileName,".BMP");
BMPFile = fopen (FileName, "rb");
if (BMPFile == NULL) {
printf ("Couldn't open file.");
return;
}
}
//Read in header information
fread (&Header, 54, 1, BMPFile);
//Check to see whether we can display it
if (Header.bfType != 19778 || Header.bfReserved != 0 || Header.biPlanes !=1) {
//Not a valid bitmap file - don't display
fclose (BMPFile);
return;
}
if (Header.biCompression != 0) {
//Compressed file - don't display
fclose (BMPFile);
return;
}
if (Header.biBitCount != 8) {
//Other than 8-bit colour - don't display
fclose (BMPFile);
return;
}
if (Header.biWidth > 320 || Header.biHeight > 200) {
//Too large - don't display
fclose (BMPFile);
return;
}
//Load in the palette
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);
}
//Set appropriate position to display graphics data
offset = (100 + (Header.biHeight >> 1)) * 320 + 160 - (Header.biWidth >> 1);
//We've read no lines so far
lines = 0;
//Pad line length to 4bytes
paddedWidth = Header.biWidth & 0xFFFC;
if (Header.biWidth != paddedWidth) paddedWidth += 4;
//Decode and display graphics
while (lines < Header.biHeight) {
//Read next line
fread (Screen + offset, paddedWidth, 1, BMPFile);
//Move up one line on the screen
offset -= 320;
//increase amount of lines read
lines++;
}
fclose (BMPFile);
}
void main (int argcount, char *argvalue[])
{
char FileName[80];
//Set up a pointer to the vga memory at A000:0000
#ifdef __DJGPP
__djgpp_nearptr_enable ();
Screen = (char *)(__djgpp_conventional_base + 0xA0000);
#else
Screen = (char far *)0xA0000000L;
#endif
if (argcount>1) {
strcpy (FileName,argvalue[1]);
} else {
printf ("Enter filename:"); gets (FileName);
}
VGAScreen ();
LoadBMP (FileName);
getch ();
TextScreen ();
}
|
|
|