程序代码:
#include<stdio.h> #include<io.h> #include<malloc.h> #include<dos.h> 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 biYpolsPerMeter; 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; #define TRUE 1 #define FALSE 0 #define WIDTH 320 #define HEIGHT 200 #define PALETTE_MASK 0x3c6 #define PALETTE_REGISTER_WR 0x3c8 #define PALETTE_DATA 0x3c9 #define VGA 0x13 #define TXT 0x3 typedef int BOOL; BOOL CheckBmp(bmp_picture_ptr); BOOL bmpInit(bmp_picture_ptr ); void bmpShow(bmp_picture_ptr ); void SetPalette(int,RGB_BMP_ptr); void SetVideoMod(int); void videoCopy(char far*,char *,int); char far *videoBuf=(char far*)0xa0000000; int main(void) { FILE *fp; bmp_picture bmp; int i; unsigned char buf[WIDTH]; fp=fopen("a.bmp","rb"); SetVideoMod(VGA); if(fp==NULL) { printf("Can't Open File\n"); getch(); return 0; } /*read((int)fp,(char*)bmp.file,sizeof(bitmapfile)); read(fp,bmp.info,sizeof(bitmapinfo)); */ fread((char*)&bmp.file,sizeof(bitmapfile),1,fp); fread((char*)&bmp.info,sizeof(bitmapinfo),1,fp); for(i=0;i<256;++i) { /*read(fp,bmp.palette[i].blue,1); read(fp,bmp.palette[i].green,1); read(fp,bmp.palette[i].red,1); read(fp,bmp.palette[i].reserved,1);*/ fread((char*)&bmp.palette[i].blue,1,1,fp); fread((char*)&bmp.palette[i].green,1,1,fp); fread((char*)&bmp.palette[i].red,1,1,fp); fread((char*)&bmp.palette[i].reserved,1,1,fp); bmp.palette[i].blue>>=2; bmp.palette[i].green>>=2; bmp.palette[i].red>>=2; } if(!CheckBmp(&bmp)) return 0; /*if(!bmpInit(&bmp)) return 0; */ for(i=0;i<256;++i) SetPalette(i,(RGB_BMP_ptr)&bmp.palette[i]); for(i=HEIGHT-1;i>=0;i--) { fseek(fp,1078+(long)(HEIGHT-1-i)*WIDTH,SEEK_SET); /*fread((char*)&bmp.buffer[i*WIDTH],WIDTH,1,fp);*/ fread(buf,WIDTH,1,fp); /*memcpy(videoBuf[i*WIDTH],buf,WIDTH);*/ videoCopy(videoBuf+i*WIDTH,buf,WIDTH); } /*for(i=0;i<256;++i) videoBuf[i+100]=i; */ /*bmpShow((bmp_picture_ptr)&bmp); */ fclose(fp); getch(); /*free(bmp.buffer);*/ SetVideoMod(TXT); getch(); return 0; } BOOL CheckBmp(bmp_picture_ptr bmp) { if(bmp->file.bfType!=0x4d42) { printf("Not a Bmp File!\n"); getch(); return FALSE; } if(bmp->info.biCompression!=0) { printf("Can't Display a Compression Bmp!\n"); getch(); return FALSE; } if(bmp->info.biBitCount!=8) { printf("Not a index 16 color bmp!\n"); getch(); return FALSE; } return TRUE; } BOOL bmpInit(bmp_picture_ptr bmp) { unsigned int a=(unsigned int)(WIDTH*HEIGHT+1); if((bmp->buffer=(char far *)malloc(a))==NULL) { printf("Can't allocate a memory!\n"); getch(); return FALSE; } return TRUE; } void bmpShow(bmp_picture_ptr bmp) { memcpy((char far*)videoBuf,(char far*)bmp->buffer, (unsigned int)WIDTH*HEIGHT/2); } void SetPalette(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 SetVideoMod(int mode) { union REGS in,out; in.h.ah=0; in.h.al=(unsigned char)mode; int86(0x10,&in,&out); } void videoCopy(char far *dest,char *buf,int size) { int i; for(i=0;i<size;++i) *(dest+i)=*(buf+i); }这个是拿来读256色BMP文件的,用的是WIN-TC
好久没用win-tc了,好多函数都不会了(主要是讨厌的分段模式)
将数据写入显存的时候将数据翻转不知会不会达到效果