主题:那位高手指教如何用Turbo c调用.pcx图象文件
			 junguangxu
				 [专家分:0]  发布于 2002-11-07 20:22:00
 junguangxu
				 [专家分:0]  发布于 2002-11-07 20:22:00							
			我因在一个DOS程序中调用.pcx图象文件,并将其显示在屏幕的指定位置。找了许多参考书,但没有一个详细的例子,只知道大概这类文件由三个部分组成,在实现中需分别读取并显示到屏幕上,但总是显示不出来。烦那位高手指点,谢谢!
						
					 
		
			
回复列表 (共11个回复)
		
								
				沙发
				
					 爱好者 [专家分:0]  发布于 2002-11-07 21:07:00
爱好者 [专家分:0]  发布于 2002-11-07 21:07:00				
				懂的高手看了就做个回复吧!!
							 
						
				板凳
				
					 lanjingquan [专家分:510]  发布于 2002-11-07 22:13:00
lanjingquan [专家分:510]  发布于 2002-11-07 22:13:00				
				#include <stdio.h> 
#include <dos.h> 
typedef struct{ 
  char manufacturer; 
  char version; 
  char encoding; 
  char bits_per_pixel; 
  int xmin,ymin; 
  int xmax,ymax; 
  int hres; 
  int vres; 
  char palette[48]; 
  char reserved; 
  char colour_planes; 
  int bytes_per_line; 
  int palette_type; 
  char filler[58]; 
}PCXHEAD; 
PCXHEAD pcxhdr; 
char palette[768]; 
int width,depth,byte; 
int line_count=0,byte_count=0; 
unsigned vga_offset=0; 
char vga_page=1; 
void setrgbpalette(void) 
{ 
  int i; 
  for(i=0;i<768;i++) palette[i]>>=2; 
  asm{ 
    mov ax,1012h 
    mov bx,0 
    mov cx,256 
    push es 
    push ds 
    pop es 
    mov dx,offset palette 
    int 10h 
    pop es 
  } 
} 
void setvgamode(int mode) 
{ 
  asm{ 
    mov ax,mode 
    int 10h 
  } 
} 
void putpixel(char pixel) 
{ 
  char far *point=(char far *)MK_FP(0xa000,vga_offset); 
  *point=pixel; 
  if(vga_offset==0xffff){ 
    outp(0x3c4,0x0e); 
    outp(0x3c5,vga_page^0x02); 
    vga_page++; 
    vga_offset=0; 
  }else vga_offset++; 
  byte_count++; 
} 
void loadpcx(FILE *pcxfile) 
{ 
  char value,pixel; 
  char i; 
  fseek(pcxfile,128l,SEEK_SET); 
  value=fgetc(pcxfile); 
  while(!feof(pcxfile)){ 
    if((value&0xc0)==0xc0){ 
      pixel=fgetc(pcxfile); 
      for(i=0;i<(value&0x3f);++i) putpixel(pixel); 
    } 
    else{ 
      pixel=value; 
      putpixel(pixel); 
    } 
    value=fgetc(pcxfile); 
    if(byte_count>=byte){ line_count+=(byte_count/byte);byte_count%=byte; } 
    if(line_count==depth) return; 
  } 
} 
main(int argc,char *argv[]) 
{ 
  if(argc!=2){ 
    puts("Usage: PCX256 <.PCX>"); 
    return 1; 
  } 
  FILE *pcxfile; 
  if((pcxfile=fopen(argv[1],"rb"))==NULL){ 
    printf("Can't open file %s\n",argv[1]); 
    return 2; 
  } 
  if(fread((char *)&pcxhdr,1,sizeof(PCXHEAD),pcxfile)!=sizeof(PCXHEAD)){ 
    printf("Read file %s error.\n",argv[1]); 
    fclose(pcxfile); 
    return 3; 
  } 
  if(pcxhdr.manufacturer!=0x0a){ 
    printf("File %s isn't an invalid PCX file.\n",argv[1]); 
    fclose(pcxfile); 
    return 4; 
  } 
  if(pcxhdr.bits_per_pixel!=8 || pcxhdr.colour_planes!=1){ 
    printf("File %s isn't a 256 colors PCX file.\n",argv[1]); 
    fclose(pcxfile); 
    return 5; 
  } 
  width=pcxhdr.xmax-pcxhdr.xmin+1; 
  depth=pcxhdr.ymax-pcxhdr.ymin+1; 
  byte=pcxhdr.bytes_per_line; 
  fseek(pcxfile,-769l,SEEK_END); 
  if(fgetc(pcxfile)!=0x0c || fread(palette,1,768,pcxfile)!=768){ 
    printf("File %s read palette error.\n",argv[1]); 
    fclose(pcxfile); 
    return 6; 
  } 
  if(width==320 && depth==200) setvgamode(0x13); 
    else if(width==640 && depth==400) setvgamode(0x5c); 
      else if(width==640 && depth==480) setvgamode(0x5d); 
 else if(width==800 && depth==600) setvgamode(0x5e); 
   else setvgamode(0x13); 
  setrgbpalette(); 
  loadpcx(pcxfile); 
  fclose(pcxfile); 
  getchar(); 
  setvgamode(0x03); 
  return 0; 
} 
  
							 
						
				3 楼
				
					 lanjingquan [专家分:510]  发布于 2002-11-07 22:13:00
lanjingquan [专家分:510]  发布于 2002-11-07 22:13:00				
				我自己也没用过你看看
							 
						
				4 楼
				
					 lanjingquan [专家分:510]  发布于 2002-11-07 22:15:00
lanjingquan [专家分:510]  发布于 2002-11-07 22:15:00				
				下面是一个打开16色bmp图的程序
							 
						
				5 楼
				
					 lanjingquan [专家分:510]  发布于 2002-11-07 22:15:00
lanjingquan [专家分:510]  发布于 2002-11-07 22:15:00				
				int convers(int c)
{
 if(c==1)
  return 4;
 else if(c==3)
  return 6;
 else if(c==4)
  return 1;
 else if(c==6)
  return 3;
 else if(c==9)
  return 12;
 else if(c==11)
  return 14;
 else if(c==12)
  return 9;
 else if(c==14)
  return 11;
 else return c;
}
void putbmp(int setx,int sety,char* filename)
{
  ifstream f1(filename,ios::binary|ios::nocreate);
  char a;
  for(long i=1;i<=18;i++)
     f1.read(&a,1);
  int width;
  f1.read(&a,1);
  width=a<0?a+256:a;
  f1.read(&a,1);
  width+=(a<0?a+256:a)*256;
  for(i=1;i<=2;i++)
   f1.read(&a,1);
  int height;
   f1.read(&a,1);
  height=a<0?a+256:a;
  f1.read(&a,1);
  height+=(a<0?a+256:a)*256;
  for(i=1;i<=94-((8-width%8)/2==4?0:((8-width%8)/2));i++)
  f1.read(&a,1);
  for(i=0;i<(long)(width/2*2==width?width:(width+1))*height/2;i++)
  {
   if(i*2%(width/2*2==width?width:(width+1))==0)
   for(int r=0;r<((8-width%8)/2==4?0:((8-width%8)/2));r++)
   f1.read(&a,1);
   f1.read(&a,1);
   int b=(int)a;
   if(b<0)b+=256;
   putpixel(setx+i*2%(width/2*2==width?width:(width+1)),sety+height-i*2/(width/2*2==width?width:(width+1)),convers(b/16));
   putpixel(setx+(i*2+1)%(width/2*2==width?width:(width+1)),sety+height-i*2/(width/2*2==width?width:(width+1)),convers(b%16));
  }
 f1.close();
}
							 
						
				6 楼
				
					 爱好者 [专家分:0]  发布于 2002-11-08 00:09:00
爱好者 [专家分:0]  发布于 2002-11-08 00:09:00				
				我试过不行,谁试了可以的话就发表。。
							 
						
				7 楼
				
					 爱好者 [专家分:0]  发布于 2002-11-08 01:03:00
爱好者 [专家分:0]  发布于 2002-11-08 01:03:00				
				#include <graphics.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <conio.h> 
int main(void) 
{ 
   /* request auto detection */ 
   int gdriver = DETECT, gmode, errorcode; 
   /* initialize graphics mode */ 
   initgraph(&gdriver, &gmode, ""); 
   /* read result of initialization */ 
   errorcode = graphresult(); 
   if (errorcode != grOk)  /* an error occurred */ 
   { 
      printf("Graphics error: %s\n", grapherrormsg(errorcode)); 
      printf("Press any key to halt:"); 
      getch(); 
      exit(1);             /* return with error code */ 
   } 
   /* draw a line */ 
   line(0, 0, getmaxx(), getmaxy()); 
   /* clean up */ 
   getch(); 
   closegraph(); 
   return 0; 
} 
  
  
							 
						
				8 楼
				
					 lanjingquan [专家分:510]  发布于 2002-11-08 11:57:00
lanjingquan [专家分:510]  发布于 2002-11-08 11:57:00				
				打开十六色位图的那个函数一定是可行的。
呵呵,因为我最近做的好多东西都用到它,呵呵
							 
						
				9 楼
				
					 junguangxu [专家分:0]  发布于 2002-11-08 12:31:00
junguangxu [专家分:0]  发布于 2002-11-08 12:31:00				
				谢谢!
							 
						
				10 楼
				
					 junguangxu [专家分:0]  发布于 2002-11-08 18:13:00
junguangxu [专家分:0]  发布于 2002-11-08 18:13:00				
				其中的汇编语言部分在Tc2和Tc3下均不能编译。
							 
									
			
我来回复