优化掉
if((i+x) > -1 && (i+x) < 640 && (j+y) > -1 && (j+y) < 480)想办法放在循环外面
就可以快一些了。
/***********************************图片显示*********************************/
int show_bmp(char *namea,int x0,int y0)
{
int width, height,i,j, fp;
unsigned char fix, *buffer, pix24b[4];
DacPalette256 dac256;
if((fp = open(namea, O_RDONLY | O_BINARY)) == -1)
{
puts("Can't find file ""xy_256.bmp"".");
return 1;
}
lseek(fp, 18, SEEK_SET);
read(fp, &width, 4);
read(fp, &height, 4);
fix = (width % 4) ? (4 - width % 4) : 0;
lseek(fp, 54, SEEK_SET);
for(i = 0; i < 256; i++) /* 按照该图片的DAC色表设置色彩寄存器 */
{
read(fp, pix24b, 4); /* 读取DAC分量信息 */
/* 设置调色板 */
dac256[i][0] = pix24b[2] >> 2;
dac256[i][1] = pix24b[1] >> 2;
dac256[i][2] = pix24b[0] >> 2;
}
setvgapalette256(&dac256); /* 应用调色板, 定义参见 Svga256.h */
buffer = (unsigned char *)malloc(width);
for(j = height - 1; j >= 0; j--)
{
read(fp, buffer, width);
lseek(fp, fix, SEEK_CUR);
for(i = 0; i < width; i++) putpixel(x0+i, y0+j, buffer[i]);
}
free(buffer);
close(fp);
}
这个显示图片的程序怎么优化一下,让快速显示呢?
这两个程序运行的速度差不多呀(在486的机器上运行的),说明用Svga256.bgi和直接写屏的速度差不多呀?
----------------------------------------------------------------------------
/* Svga256.c */
#include "graphics.h"
#include "Svga256.h"
#include "stdio.h"
#include "fcntl.h"
#include "malloc.h"
#include "io.h"
int huge Return_SVGA256(void)
{
return(SVGA640x480x256); /* 返回各种分辨率的对应编号 0~6 宏定义参见 Svga256.h */
}
int main(void)
{
int width, height, i=DETECT, j=0, x0=0, y0=0, fp;
unsigned char fix, *buffer, pix24b[4];
DacPalette256 dac256;
installuserdriver("Svga256", Return_SVGA256); /* 对于svga256必需执行该函数以安装BGI驱动 */
initgraph(&i, &j, ""); /* 执行TC默认的BGI初始化函数 */
if((fp = open("main.bmp", O_RDONLY | O_BINARY)) == -1)
{
puts("Can't find file ""main.bmp"".");
return 1;
}
lseek(fp, 18, SEEK_SET);
read(fp, &width, 4);
read(fp, &height, 4);
fix = (width % 4) ? (4 - width % 4) : 0;
lseek(fp, 54, SEEK_SET);
for(i = 0; i < 256; i++) /* 按照该图片的DAC色表设置色彩寄存器 */
{
read(fp, pix24b, 4); /* 读取DAC分量信息 */
/* 设置调色板 */
dac256[i][0] = pix24b[2] >> 2;
dac256[i][1] = pix24b[1] >> 2;
dac256[i][2] = pix24b[0] >> 2;
}
setvgapalette256(&dac256); /* 应用调色板, 定义参见 Svga256.h */
buffer = (unsigned char *)malloc(width);
for(j = height - 1; j >= 0; j--)
{
read(fp, buffer, width);
lseek(fp, fix, SEEK_CUR);
for(i = 0; i < width; i++)
putpixel(x0+i, y0+j, buffer[i]);
}
free(buffer);
close(fp);
getch();
closegraph();
return 0;
}
---------------------------------------------------------------------------
/* bmp256.c */
#include "graphics.h"
#include "Svga256.h"
#include "stdio.h"
#include "fcntl.h"
#include "malloc.h"
#include "io.h"
#include "dos.h"
#define VARM_GRAPH_800(x,y) (((unsigned long)y<<9L)+((unsigned long)y<<7L)+((unsigned long)(x)))
int huge Return_SVGA256(void)
{
return(SVGA640x480x256); /* 返回各种分辨率的对应编号 0~6 宏定义参见 Svga256.h */
}
void selectpage(register char page) /*换页函数*/
{
union REGS r;
r.x.ax=0x4f05;
r.x.bx=0;
r.x.dx=page; /*选择页面*/
int86(0x10,&r,&r);
}
int g_cur_vbe_page = 0;
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);
}
}
int main(void)
{
int width, height, i=DETECT, j=0, x0=0, y0=0, fp,page;
long addr;
unsigned char fix, *buffer, pix24b[4];
char far *videoptr = (char far *)0xa0000000L;
DacPalette256 dac256;
installuserdriver("Svga256", Return_SVGA256); /* 对于svga256必需执行该函数以安装BGI驱动 */
initgraph(&i, &j, ""); /* 执行TC默认的BGI初始化函数 */
if((fp = open("main.bmp", O_RDONLY | O_BINARY)) == -1)
{
puts("Can't find file ""xy_256.bmp"".");
return 1;
}
lseek(fp, 18, SEEK_SET);
read(fp, &width, 4);
read(fp, &height, 4);
fix = (width % 4) ? (4 - width % 4) : 0;
/*x0 = (320 - width) / 2;
y0 = (200 - height) / 2; */
lseek(fp, 54, SEEK_SET);
for(i = 0; i < 256; i++) /* 按照该图片的DAC色表设置色彩寄存器 */
{
read(fp, pix24b, 4); /* 读取DAC分量信息 */
/* 设置调色板 */
dac256[i][0] = pix24b[2] >> 2;
dac256[i][1] = pix24b[1] >> 2;
dac256[i][2] = pix24b[0] >> 2;
}
setvgapalette256(&dac256); /* 应用调色板, 定义参见 Svga256.h */
buffer = (unsigned char *)malloc(width);
for(j = height - 1; j >= 0; j--)
{
read(fp, buffer, width);
lseek(fp, fix, SEEK_CUR);
for(i = 0; i < width; i++)
{ addr = VARM_GRAPH_800(i+x0,j+y0);
page = (int)(addr >> 16);
set_vbe_page(page);
if((i+x0) > -1 && (i+x0) < 640 && (j+y0) > -1 && (j+y0) < 480) /*在屏幕范围*/
/* putpixel(x0+i, y0+j, buffer[i]);*/
*(videoptr+(unsigned int)(addr&0xffff))= buffer[i];
}
}
free(buffer);
close(fp);
getch();
closegraph();
return 0;
}
不会,SVGA.BGI的速度要慢,只是可能因为你用的是486机器或是没优化到位的缘故