📄 bmp.c
字号:
#define PALETTE_MASK 0x3c6
#define PALETTE_REGISTER_RD 0x3c7
#define PALETTE_REGISTER_WR 0x3c8
#define PALETTE_DATA 0x3c9
typedef struct BMP_file{
unsigned int bfType;
unsigned long bfSize;
unsigned int Reserved1;
unsigned int reserved2;
unsigned long bfOffset;
}
bitmapfile;
typedef struct BMP_info{
unsigned long biSize;
unsigned long biWidth;
unsigned long biHeight;
unsigned int biPlanes;
unsigned int biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
unsigned long biXpolsPerMeter;
unsigned long biYpelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
}
bitmapinfo;
typedef struct RGB_BMP_typ{
unsigned char blue;
unsigned char green;
unsigned char red;
unsigned char reserved;
}
RGB_BMP, *RGB_BMP_ptr;
typedef struct bmp_picture_typ{
bitmapfile file;
bitmapinfo info;
RGB_BMP palette[256];
char far *buffer;
}
bmp_picture, *bmp_picture_ptr;
void BMP_PaletteRegister(int index, RGB_BMP_ptr color){
outp(PALETTE_MASK, 0xff);
outp(PALETTE_REGISTER_WR, index);
outp(PALETTE_DATA, color->red);
outp(PALETTE_DATA, color->green);
outp(PALETTE_DATA, color->blue);
}
void Check_Bmp(bmp_picture_ptr bmp_ptr){
if(bmp_ptr->file.bfType != 0x4d42){
printf("Not a BMP file!\n");
getch();
exit(1);
}
if(bmp_ptr->info.biCompression != 0){
printf("Can not display a compressed BMP file!\n");
getch();
exit(1);
}
if(bmp_ptr->info.biBitCount != 8){
printf("Not a index 16color BMP file!\n");
getch();
exit(1);
}
}
void BMP_Load(char *bmp, bmp_picture *bmp256){
int i, fp, imgsize;
if ((fp = open(bmp,O_RDONLY)) == 1) return;
read(fp, &bmp256->file, sizeof(bitmapfile));
read(fp, &bmp256->info, sizeof(bitmapinfo));
Check_Bmp((bmp_picture_ptr)bmp256);
for (i = 0; i < 256; i++){
read(fp, &bmp256->palette[i].blue, 1);
read(fp, &bmp256->palette[i].green, 1);
read(fp, &bmp256->palette[i].red, 1);
read(fp, &bmp256->palette[i].reserved, 1);
bmp256->palette[i].blue = bmp256->palette[i].blue >> 2;
bmp256->palette[i].green = bmp256->palette[i].green >> 2;
bmp256->palette[i].red = bmp256->palette[i].red >> 2;
}
imgsize = (unsigned)(bmp256->info.biHeight * bmp256->info.biWidth);
if ((bmp256->buffer = (char far *)farmalloc((long)imgsize)) != NULL)
_dos_read(fp, bmp256->buffer, imgsize, &i);
else{
printf("low memory for BMP!");
getch();
exit(1);
}
close(fp);
}
void BMP_SetPalette(bmp_picture bmp256){
int i;
for (i = 0; i < 256; i++)
BMP_PaletteRegister(i, (RGB_BMP_ptr)&bmp256.palette[i]);
}
void BMP_Put(bmp_picture bmp256, int x, int y){
int i, j;
BMP_SetPalette(bmp256);
for (i = (int)bmp256.info.biHeight - 1, j = 0; i >= 0; i--, j++)
if ((y + i >= 0) && (y + i < 200))
_fmemcpy(&video_buffer[(y + i) * SCREEN_WIDTH + x], &bmp256.buffer[j * bmp256.info.biWidth], bmp256.info.biWidth);
}
void BMP_LoadScreen(char *bmp){
int i, fp, n;
bmp_picture bmp256;
if ((fp = open(bmp,O_RDONLY)) == 1) return;
read(fp, &bmp256.file, sizeof(bitmapfile));
read(fp, &bmp256.info, sizeof(bitmapinfo));
Check_Bmp((bmp_picture_ptr)&bmp256);
for (i = 0; i < 256; i++){
read(fp, &bmp256.palette[i].blue, 1);
read(fp, &bmp256.palette[i].green, 1);
read(fp, &bmp256.palette[i].red, 1);
read(fp, &bmp256.palette[i].reserved, 1);
bmp256.palette[i].blue = bmp256.palette[i].blue >> 2;
bmp256.palette[i].green = bmp256.palette[i].green >> 2;
bmp256.palette[i].red = bmp256.palette[i].red >> 2;
}
BMP_SetPalette(bmp256);
for (i=0;i<256;i++)
BMP_PaletteRegister(i,(RGB_BMP_ptr)&bmp256.palette[i]);
for (i = SCREEN_HEIGHT - 1; i >= 0; i--)
_dos_read(fp, &video_buffer[i * SCREEN_WIDTH], SCREEN_WIDTH, &n);
close(fp);
}
void BMP_DirectShow(char *bmp, int x, int y){
int i, fp, n;
bmp_picture bmp256;
if ((fp = open(bmp,O_RDONLY)) == 1) return;
read(fp, &bmp256.file, sizeof(bitmapfile));
read(fp, &bmp256.info, sizeof(bitmapinfo));
Check_Bmp((bmp_picture_ptr)&bmp256);
for (i = 0; i < 256; i++){
read(fp, &bmp256.palette[i].blue, 1);
read(fp, &bmp256.palette[i].green, 1);
read(fp, &bmp256.palette[i].red, 1);
read(fp, &bmp256.palette[i].reserved, 1);
bmp256.palette[i].blue = bmp256.palette[i].blue >> 2;
bmp256.palette[i].green = bmp256.palette[i].green >> 2;
bmp256.palette[i].red = bmp256.palette[i].red >> 2;
}
BMP_SetPalette(bmp256);
for (i=0;i<256;i++)
BMP_PaletteRegister(i,(RGB_BMP_ptr)&bmp256.palette[i]);
for (i = bmp256.info.biHeight - 1; i >= 0; i--)
_dos_read(fp, &video_buffer[(i + y) * SCREEN_WIDTH + x], bmp256.info.biWidth, &n);
close(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -