象素显示即是如何反调色板写到显示卡上去并反象素显示出来. 对于是6色可以用C语言提供的VGA16色驱动程简单地完成显示.需要指出的是对于 16色图形,图象数据与VGA16色所设定的颜色之间有一转换关系,即红与蓝两色分量交 换位置.例如BMP中的1指的是VBA16色的4号颜色.
BMP中的数据值 实际VGA16色中的颜色序号
1 4
3 6
4 1
6 3
9 C
B E
C 9
E B
下面给出程序:
bmp.c
# include<stdio.h>
# include<graphics.h>
unsigned int im_width,pat_size; main(argc,argv) int argc;
char *argv[];
{ unsigned char buffer[54];
unsigned char pat[256][4];
int bit_count; long i;
unsigned char huge *img1;
unsigned char huge *img;
unsigned int width,higth,j,k,color; FILE *fp1,*fp2;
union { struct { unsigned lobyte:4; unsigned hibyte:4; }parts;
struct { unsigned char allbyte; }whole; }db1;
if(argc<2) { printf("Usage:bmp bmpfilename\n"); exit(1); } if(!(fp1=fopen(argv[1],"rb")))
{ printf("Open file %s error!\n",argv[1]); exit(1); }
fread(buffer,1,54,fp1);
im_width=buffer[19]*256+buffer[18];
higth=buffer[23]*256+buffer[22];
if((im_width%8)!=0) width=(im_width/8+1)*8;
else width=im_width;
pat_size=(buffer[11]*256+buffer[10]-54)/4;
bit_count=buffer[28];
/* read color pattern fread(pat,sizeof(unsigned char ),pat_size*4,fp1); */ if(!(img=farcalloc((width/(8/bit-count)),sizeof(unsigned char))))
{ printf("Can't open pointor img.\n"); abort(); }
{ if(bit_count==4) for(k=0;k<idth/2;j++){ db1.whole.allbyte=(*)img+j)); (*(img1+(long)(higth-1-k)*(long)width+(long)j*2))=db1.parts.hibyte; (*(img1+(long)(higth-1-k)*(long)width+(long)j*2+11))=db1.parts.lobyte; } }
else /*bit _count==8 for(k=0;k<idth;j++)
(*(img1+(long)(higth-1-k)*(long)width+(long)j))=(*(img+j)); } disp(img1,pat,higth,width); fclose(fp1); farfree(img); farfree(img1); } }
#define SCREEN_HIGHT 200
#define SCREEN_WIDTH 320
#define MMODE 0x13 disp(unsigned char huge *image,unsigned char *pattern,unsigned int row,unsigned int column) { unsigned char far *im; unsigned char far *buf; unsigned char pat[256][3];
unsigned int column1,x0=0,y0=0;
int i,j; long size;
void set_color_pat(unsigned char far pat[256][3]);
void set_mode(int);
if(row-x0)>SCREEN_HIGHT) row=SCREEN_HIGHT;
else row=row-x0;
if(im_width-y0>SCREEN_WIDTH) column1=SCREEN_WIDTH;
else column1=im-width-y0; set_mode(MMODE);
/* change value of red and blue */ for(i=0;i<pat_size;i++)
for(j=0;j>;j++) pati][j]=(*(pattern+i*4+(2-j)))/4;
set_color_pat(pat);
/* write to display card */ buf=(unsigned char far *)MK_FP(oxa000,0); lsize=0; for(i=0;i<row;i++)
{lsize=lsize+SCREEN_WIDTH;
if(lsize>55361) { memcpy(buf,(image+i*column),column1);buf=buf+SCREEN-WIDTH; }
else { pritf("overflow:image too large.\n"); } }
getch(); set_mode(3); }
void set_mode(int mode) { union REGS r; r.h.ah=0; r.h.al=mode; int86(0x10,&r,&r); } void set_color_pat(unsigned char far pat[256][3])
{ union REGS r; struct SREGS s; r.h.ah=0x10; r.h.al=0x12; r.x.bx=0; r.x.cx=256; r.x.dx=FP_OFF(pat); s.es=FP_SEG(pat); int86x(0x10,&r,&r,&s); }