#include <dos.h>
#include <stdio.h>
#include <mem.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>
#define VBE320X200X256 0X13
#define VBE640X480X256 0X101
#define VBE800X600X256 0X103
#define VBE1024X768X256 0X105
#define True 1
#define False 0
#define VARM_GRAPH_800(x,y) (((unsigned long)y<<9L)+((unsigned long)y<<8L)+((unsigned long)y<<5L)+((unsigned long)(x)))
typedef struct BIGIMAGE /*存图结构*/
{
char fill_name[16];
int height;
int width;
} BIGIMAGE; /*存图类型名*/
typedef struct tagRGBQUAD /*用于读取调色板信息*/
{
char r;
char g;
char b;
char reserved;
} RGBQUAD;
int g_cur_vbe_page = 0;
//-----------------------------------------------------------
// 图形模式初始化子程序
//-----------------------------------------------------------
void _Cdecl InitGraph(void)
{
_AX = 0x4f02;
_BX = VBE800X600X256;
__int__(0x10);
if(_AH != 0)
{
puts("Can't Initialize the graphics mode!");
exit(1);
}
}
//-----------------------------------------------------------
// 关闭图形模式,回到文本模式子程序
//-----------------------------------------------------------
void _Cdecl CloseGraph(void)
{
_AX = 0x4f02;
_BX = 0x03;
__int__(0x10);
}
void selectpage(register char page) /*换页函数*/
{
union REGS r;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=page; /*选择页面*/
int86(0x10,&r,&r);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
//显存换页函数
/////////////////////////////////////////////////////////////////////////////////////////////////
void _Cdecl set_vbe_page(int page)
{
if (g_cur_vbe_page != page)
{
_BX = 0;
_DX = g_cur_vbe_page = page;
_AX = 0x4F05;
__int__(0x10);
}
}
/*******************************************BMP图显示函数**********************************/
void show_bmp_256(char *File_Name, int x, int y) /*图片显示函数,不支持透明色*/
{
unsigned char buffer[9216];
unsigned int width, height, linebytes;
int i, j;
long addr;
int page;
char far *videoptr = (char far *)0xa0000000L;
RGBQUAD bmiColors[256];
FILE *fp;
/****************************读文件,判断是否符合条件*****************************/
if ((fp=fopen(File_Name,"rb"))==NULL) /*判断打开文件是否正确*/
{
printf("Can't open file: %s",File_Name);
bioskey(0);
exit(0);
}
fseek(fp,18L,SEEK_SET); width = getw(fp);/*图片宽度*/
fseek(fp,22L,SEEK_SET); height = getw(fp);/*图片高度*/
fseek(bmp,28L,SEEK_SET); depth=getw(bmp); /* 获得图片色深 */
/********************************************************************************/
/* 设置当前图片的调色板 */
/********************************************************************************/
if (depth==8) /* 若是8位位图,开辟调色板数组 */
{
fseek(fp, 54L, SEEK_SET);
if (fread((char *)&bmiColors[0], 4, 256, fp) != 256) /* 读当前图片调色板数据 */
{
printf("Can't get palette !\n");
bioskey(0);
exit(0);
}
for (i = 0; i < 256; i++)
{
outportb(0x03C8, i);
outportb(0x03C9, bmiColors[i].b >> 2);
outportb(0x03C9, bmiColors[i].g >> 2);
outportb(0x03C9, bmiColors[i].r >> 2);
}
}
/*************************************************************************/
if(width%4 != 0)
linebytes = width+(4-width%4); /* 每行字节数--4的整数倍 */
else
linebytes = width;
fseek(fp,1078L,SEEK_SET); /* 跳到位图数据的起始位置 */
for(j = height-1; j >= 0; j--)
{
fread(buffer, linebytes, 1, fp);
for(i = 0; i < width; i++)
{
addr = VARM_GRAPH_800(i+x,j+y);
page = (int)(addr >> 16);
set_vbe_page(page);
if((i+x) > -1 && (i+x) < 800 && (j+y) > -1 && (j+y) < 600) /*在屏幕范围*/
*(videoptr+(unsigned int)(addr&0xffff))= buffer[i];
}
}
fclose(fp);
}
void main(void)
{
InitGraph();
show_bmp_256("d:\\jig.bmp",0,0);
getch();
CloseGraph();
}