求教一个关于256色BMP文件的调用的问题
<P>看了以下的关于读取256色BMP文件并显示的程序都不知几遍了,就是看不出有什么不对。但就是一运行就自动退出,大家帮忙看一下吧:<BR></P><DIV class=htmlcode>
<P>#include<io.h><BR>#include<stdio.h><BR>#include<dos.h><BR>#include<string.h><BR>#include<math.h><BR>#include<stdio.h><BR>#include<bios.h><BR>#include<mem.h><BR>#include<fcntl.h><BR>#include<stdlib.h><BR>#include<conio.h></P>
<P>#define SCREEN_HEIGHT 200<BR>#define SCREEN_WIDTH 320</P>
<P>#define PALETTE_MASK 0x3c6<BR>#define PALETTE_REGISTER_RD 0x3c7<BR>#define PALETTE_REGISTER_WR 0x3c8<BR>#define PALETTE_DATA 0x3c9</P>
<P>#define VGA256 0x13<BR>#define TEXT_MODE 0x03</P>
<P>unsigned char far *video_buffer=(char far *)0xA0000000L; /*显存首地址*/</P>
<P>typedef struct BMP_file /*文件信息区结构*/<BR>{<BR> unsigned int bfType;<BR> unsigned long bfSize;<BR> unsigned int Reserved1;<BR> unsigned int reserved2;<BR> unsigned long bfOffset;<BR>}bitmapfile;</P>
<P>typedef struct BMP_info /*图像信息区结构*/<BR>{<BR> unsigned long biSize;<BR> unsigned long biWidth;<BR> unsigned long biHeight;<BR> unsigned int biPlanes;<BR> unsigned int biBitCount;<BR> unsigned long biCompression;<BR> unsigned long biSizeImage;<BR> unsigned long biXpolsPerMeter;<BR> unsigned long biYpelsPerMeter;<BR> unsigned long biClrUsed;<BR> unsigned long biClrImportant;<BR>}bitmapinfo;</P>
<P>typedef struct RGB_BMP_typ /*调色板区结构*/<BR>{<BR> unsigned char blue;<BR> unsigned char green;<BR> unsigned char red;<BR> unsigned char reserved;<BR>}RGB_BMP,*RGB_BMP_ptr;</P>
<P>typedef struct bmp_picture_typ /*BMP文件结构*/<BR>{<BR> bitmapfile file;<BR> bitmapinfo info;<BR> RGB_BMP palette[256];<BR> char far *buffer;<BR>}bmp_picture, *bmp_picture_ptr;</P>
<P>void Set_BMP_Palette_Register(int index,RGB_BMP_ptr color) /*设置调色板*/<BR>{<BR> outp(PALETTE_MASK,0xff);<BR> outp(PALETTE_REGISTER_WR,index);<BR> outp(PALETTE_DATA,color->red);<BR> outp(PALETTE_DATA,color->green);<BR> outp(PALETTE_DATA,color->blue);<BR>}</P>
<P>void Check_Bmp(bmp_picture_ptr bmp_ptr) /*检测文件信息区*/<BR>{<BR> if(bmp_ptr->file.bfType!=0x4d42)<BR> {<BR> printf("Not a BMP file!\n");<BR> exit(1);<BR> }<BR> if(bmp_ptr->info.biCompression!=0)<BR> {<BR> printf("Can not display a compressed BMP file!\n");<BR> exit(1);<BR> }<BR> if(bmp_ptr->info.biBitCount!=8)<BR> {<BR> printf("Not a index 16color BMP file!\n");<BR> exit(1);<BR> }<BR>}</P>
<P>void BMP_Load_Screen(char *bmp) /*在屏幕上显示BMP文件*/<BR>{<BR> int i,fp;<BR> bmp_picture bmp256;<BR> char *file_name;<BR> if((fp=open(bmp,O_RDONLY))==-1)<BR> { <BR> printf("\nOpen error !\n");<BR> return;<BR> }<BR> read(fp,&bmp256.file,sizeof(bitmapfile));<BR> read(fp,&bmp256.info,sizeof(bitmapinfo));<BR> Check_Bmp((bmp_picture_ptr)&bmp256);<BR> lseek(fp,54,0);<BR> for (i=0;i<256;i++)<BR> {<BR> read(fp,&bmp256.palette[i].blue,1);<BR> read(fp,&bmp256.palette[i].green,1);<BR> read(fp,&bmp256.palette[i].red,1);<BR> read(fp,&bmp256.palette[i].reserved,1);<BR> bmp256.palette[i].blue=bmp256.palette[i].blue>>2;<BR> bmp256.palette[i].green=bmp256.palette[i].green>>2;<BR> bmp256.palette[i].red=bmp256.palette[i].red>>2;<BR> }<BR> for (i=0;i<256;i++)<BR> Set_BMP_Palette_Register(i,(RGB_BMP_ptr)&bmp256.palette[i]);<BR> for(i=SCREEN_HEIGHT-1;i>=0;i--)<BR> {<BR> lseek(fp,1078+(long)(SCREEN_HEIGHT-i-1)*SCREEN_WIDTH,0);<BR> read(fp,&video_buffer[i*SCREEN_WIDTH],SCREEN_WIDTH);<BR> }<BR> close(fp);<BR>}</P>
<P>void Set_Video_Mode(int mode) /*设置显示模式*/<BR>{<BR> union REGS inregs,outregs;<BR> inregs.h.ah=0;<BR> inregs.h.al=(unsigned char)mode;<BR> int86(0x10,&inregs,&outregs);<BR>}</P>
<P>int main()<BR>{<BR> Set_Video_Mode(VGA256);<BR> BMP_Load_Screen("e:\\tc\\output\\256.bmp");/*所用图形文件已经附上,以供调试*/<BR> getch();<BR> Set_Video_Mode(TEXT_MODE);<BR>}</P></DIV>[attach]16251[/attach]<BR>
<P>#define VBE320X200X256 0X13<BR>#define VBE640X480X256 0X101<BR>#define VBE800X600X256 0X103<BR>#define VBE1024X768X256 0X105<BR>#define VARM_GRAPH_800(x,y) (((unsigned long)y<<9L)+((unsigned long)y<<8L)+((unsigned long)y<<5L)+((unsigned long)(x)))<BR>#define PALETTE_READ 0x3C7 /*VGA系统调色板读端口*/<BR>#define PALETTE_WRITE 0x3C8 /*VGA系统调色板写端口*/<BR>#define PALETTE_DATA 0x3C9 /*VGA系统调色板数据端口*/</P>
<P>/*第一部分为位图文件头BITMAPFILEHEADER,其定义如下:*/<BR>typedef struct tagBITMAPFILEHEADER<BR>{<BR> unsigned int bfType; /*指定文件类型,*.bmp文件的头两个字节都是"BM"*/<BR> unsigned long bfSize; /*指定文件大小,包括这14个字节*/<BR> unsigned int Reserved1; /*为保留字,不用考虑*/<BR> unsigned int reserved2; /*为保留字,不用考虑*/<BR> unsigned long bfOffset; /*为从文件头到实际的位图数据的偏移字节数,前三个部分的长度之和。*/<BR>}BITMAPFILEHEADER;</P>
<P>/*第二部分为位图信息头BITMAPINFOHEADER,这个结构的长度是固定的,为40个字节其定义如下:*/<BR>typedef struct tagBITMAPINFOHEADER<BR>{<BR> unsigned long biSize; /*指定这个结构的长度,为40*/<BR> unsigned long biWidth; /*指定图象的宽度,单位是象素*/<BR> unsigned long biHeight; /*指定图象的高度,单位是象素*/<BR> unsigned int biPlanes; /*必须是1,不用考虑*/<BR> unsigned int biBitCount; /*指定表示颜色时要用到的位数,常用的值为1(黑白二色图),4(16色图),8(256色),24(真彩色图)*/<BR> unsigned long biCompression; /*指定位图是否压缩,有效的值为BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS*/<BR> unsigned long biSizeImage; /*指定实际的位图数据占用的字节数*/<BR> unsigned long biXpolsPerMeter; /*指定目标设备的水平分辨率,单位是每米的象素个数。*/<BR> unsigned long biYpelsPerMeter; /*指定目标设备的垂直分辨率,单位同上。*/<BR> unsigned long biClrUsed; /*指定本图象实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次方。*/<BR> unsigned long biClrImportant; /*指定本图象中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。*/<BR>}BITMAPINFOHEADER;</P>
<P>typedef struct tagRGBQUAD /*用于读取调色板信息*/<BR>{<BR> unsigned char rgbBlue;<BR> unsigned char rgbGreen;<BR> unsigned char rgbRed;<BR> unsigned char Reserved;<BR>}RGBQUAD;<BR>int g_cur_vbe_page = 0;</P>
<P>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>// 图形模式初始化子程序<BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>void _Cdecl InitGraph(void)<BR>{<BR> _AX = 0x4f02;<BR> _BX = VBE800X600X256;<BR> __int__(0x10);<BR> if(_AH != 0)<BR> {<BR> puts("Can't Initialize the graphics mode!");<BR> exit(1);<BR> }<BR>}</P>
<P><BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>// 关闭图形模式,回到文本模式子程序<BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>void _Cdecl CloseGraph(void)<BR>{<BR> _AX = 0x4f02;<BR> _BX = 0x03;<BR> __int__(0x10);<BR>}</P>
<P><BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>//显存换页函数<BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>void _Cdecl set_vbe_page(int page)<BR>{<BR> if (g_cur_vbe_page != page)<BR> {<BR> _BX = 0;<BR> _DX = g_cur_vbe_page = page;<BR> _AX = 0x4F05;<BR> __int__(0x10);<BR> }<BR>}</P>
<P><BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>//设置单个调色板<BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>void setpal(unsigned char i, unsigned char r, unsigned char g, unsigned char b)<BR>{<BR> outportb(PALETTE_WRITE,i);<BR> outportb(PALETTE_DATA,r);<BR> outportb(PALETTE_DATA,g);<BR> outportb(PALETTE_DATA,b);<BR>}</P>
<P><BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>//画点函数<BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>void _Cdecl PutPixel(int x,int y,int color)<BR>{<BR> long addr;<BR> int page;<BR> char far *videoptr = (char far *)0xa0000000L;</P>
<P> if (x >= 0 && x < 800 && y >= 0 && y < 600)<BR> {<BR> addr = VARM_GRAPH_800(x,y);<BR> page = (int)(addr >> 16);<BR> set_vbe_page(page);<BR> *(videoptr+(unsigned int)(addr&0xffff))= color;<BR> }<BR>}</P>
<P>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>//在24位图像中,没有“DAC色表”,也没有“图像数据区”。唯一留给我们的只有图像上所有点的颜色值。<BR>//因为每个颜色都用BGR三种颜色来表示,而每个颜色占用1个字节,所以在24位图像中,每1个点就占用了<BR>//3个字节。 <BR>//那没有“DAC”色表,也没有‘数据图像区’我们怎么来显示图象呢?很简单, 24位图给我们提供了个更<BR>//加简单的方法:“所有点的颜色值”。既然是所有点,那么只要把这些点按照他们的颜色重新画出来就是<BR>//该图像完整的信息了。<BR>/////////////////////////////////////////////////////////////////////////////////////////////////<BR>void Show_BMP(char *File_Name, int x, int y)<BR>{<BR> int i, j, width, color,len;<BR> FILE *fp;<BR> unsigned char *buffer;<BR> RGBQUAD *RGB;<BR> BITMAPFILEHEADER *FileHead;<BR> BITMAPINFOHEADER *InfoHead;</P>
<P> RGB = (RGBQUAD *)malloc(sizeof(RGBQUAD));<BR> FileHead = (BITMAPFILEHEADER *)malloc(sizeof(BITMAPFILEHEADER));<BR> InfoHead = (BITMAPINFOHEADER*)malloc(sizeof(BITMAPINFOHEADER));<BR> if (RGB == NULL || FileHead == NULL || InfoHead == NULL)<BR> return;<BR> if ((fp=fopen(File_Name,"rb")) == NULL)<BR> {<BR> printf("BMP File not exist ...");<BR> return;<BR> }<BR> fread(FileHead,sizeof(BITMAPFILEHEADER),1,fp);<BR> if (FileHead->bfType!='BM')<BR> {<BR> printf("BMP File type Error ...");<BR> fclose(fp);<BR> return;<BR> }<BR> fread(InfoHead,sizeof(BITMAPINFOHEADER),1,fp);<BR> if (InfoHead->biCompression !=0 || (InfoHead->biBitCount!=8 && InfoHead->biBitCount!=24))<BR> {<BR> printf("BMP File not Support Compression type ...");<BR> fclose(fp);<BR> return;<BR> }<BR> if ((int)InfoHead->biBitCount == 8) // 色深=8(256色图片)<BR> {<BR> width =((int)InfoHead->biWidth+3)/4*4; // 每行字节数--4的整数倍<BR> if ((buffer = (unsigned char *)malloc(width)) == NULL) // 申请缓冲区<BR> {<BR> fclose(fp);<BR> return;<BR> }<BR> for (i = 0 ; i < 256 ; i++)<BR> {<BR> fread(RGB,sizeof(RGBQUAD),1,fp);<BR> setpal(i,RGB->rgbRed>>2,RGB->rgbGreen>>2,RGB->rgbBlue>>2);<BR> }<BR> for (j = (int)InfoHead->biHeight-1 ; j >= 0 ; j--)<BR> {<BR> fread(buffer,width,sizeof(unsigned char),fp);<BR> for (i = 0; i < width; i++)<BR> PutPixel(i+x,j+y,buffer[i]);<BR> }<BR> }<BR> else if ((int)InfoHead->biBitCount == 24) // 色深=24(真彩色图片)<BR> {<BR> ;<BR> }<BR> free(FileHead);<BR> free(InfoHead);<BR> free(RGB);<BR>}</P>
<P><BR>void main(void)<BR>{<BR> InitGraph();<BR> Show_BMP("JIG.BMP",0,0);<BR> getch();<BR> CloseGraph();<BR>}</P> [CODE][/CODE][attach]16261[/attach]<BR> 为什么我的电脑下运行出来只是一堆乱条? 看到头都晕了
页:
[1]
