主题:[原创]显示BMP图象的程序
这段程序是我在网上找到的,请大家参考,点评。
bmp.c
#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <io.h>
#include <dos.h>
#include <mem.h>
#include <conio.h>
#include <stdlib.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<higth;k++)
fread(img,sizeof(unsigned char),(width/2),fp1);
for(j=0;j<width/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<higth;k++) {
fread(img,sizeof(unsigned char),width,fp1);
for(j=0;j<width;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<3;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<655361)
{
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);
}
bmp.c
#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <io.h>
#include <dos.h>
#include <mem.h>
#include <conio.h>
#include <stdlib.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<higth;k++)
fread(img,sizeof(unsigned char),(width/2),fp1);
for(j=0;j<width/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<higth;k++) {
fread(img,sizeof(unsigned char),width,fp1);
for(j=0;j<width;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<3;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<655361)
{
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);
}