当前位置:首页 > 网络编程 > 软件语言 > C语言 > GIF2PCX.C

GIF2PCX.C

点击次数:27 次 发布日期:2008-12-01 12:51:22 作者:源代码网
源代码网推荐  *   GIF2PCX.C:  GIF to PC Paintbrush file translator.
 *   AUTHOR:   Zheng QuanZhan, Peking Unviersity, 1993
 */
 
#include <stdio.h>
#include <dos.h>
#include <malloc.h>

#ifndef MK_FP
#define MK_FP(seg,ofs) ((char far *) \
      (((unsigned long)(seg) << 16) | (unsigned)(ofs)))
#endif

#pragma pack(1)

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;

typedef struct tagGLOBAL{
 char  version[3];
 char  subversion[3];
 unsigned int   screen_width;
     unsigned int   screen_depth;
     unsigned char  GlobalFlag;
     unsigned char  BackgroundColor;
 unsigned char  zero;
}GLOBAL; 

typedef struct tagLOCAL{ 
 unsigned char comma;
     unsigned int  Image_Top;
     unsigned int  Image_Left;
     unsigned int  Image_Width;
     unsigned int  Image_Depth;
     unsigned char LocalFlag;
}LOCAL;

unsigned width,depth;
int  BitsPerPixel;
char far *buffer;
char palette[768];

extern char far *farptr( char far *,long );
unsigned pixels2bytes( unsigned int n );

main(int argc,char **argv)
{
 if(argc < 3) {
  printf("Usage: GIF2PCX  giffile  pcxfile\n");
  return(1);
 }
 printf( "PCX2GIF  translate GIF to PC Paintbrush file.\n");
 printf( "Copyright(R) Zheng QuanZhan, 1992,12\n\n");
 printf( "Converting: %s ==> %s.\n",argv[1],argv[2] );

 LoadGifFile( argv[1] );
 SavePcxFile( argv[2] );

 printf("\nDone !"); 
}
  
LoadGifFile( char *giffile )
{
 FILE *fp;
 GLOBAL g;
 LOCAL  l;
 int ColorNum;
 unsigned interlaced,ImageStart;
 
 if( (fp=fopen(giffile,"rb")) == NULL ){
  printf("FILE %s open error.\n",giffile);
  exit(1);
 } 

     fseek( fp,0L,SEEK_SET);
 fread( (char *)&g,1,sizeof(GLOBAL),fp );

     if( strnicmp( g.version,"gif87a",6 ) )   {
  printf("FILE %s is an invalide GIF file.\n",giffile);
  exit( 1 );
 }

 BitsPerPixel = (g.GlobalFlag & 0x7) + 1;
 ColorNum = 1 << BitsPerPixel;
 
 if( fread( palette,1,3*ColorNum,fp ) != 3*ColorNum )
 {
  printf("FILE %s read palette error.\n",giffile);
  exit(1);
 }
 fread( (char *)&l,1,sizeof(LOCAL),fp );
 
 width = l.Image_Width;
 depth = l.Image_Depth; 
     interlaced = l.LocalFlag & 0x40;
 ImageStart = 23 + ColorNum * 3;
 
 if( (buffer = (char far *)halloc( (long)width*(long)(depth+2),
    sizeof(char) )) == NULL )
 {
  printf("No enough memory !\n");
  exit(1);
 }
 
 if( unlzw( fp,buffer,(long)ImageStart,width,depth,interlaced ) )
 {
  printf("Unpack %s fail !\n",giffile);
  hfree( buffer );
  exit(1);
 } 
 fclose( fp );
}

SavePcxFile( char *pcxfile )
{
 FILE *fp;
 
 if( (fp=fopen(pcxfile,"wb")) == NULL)
 {
  printf("FILE %s create error.\n",pcxfile);
  hfree( (char _huge *)buffer );   
  exit(1);
 }
 WritePcxHeader( fp,palette );
 PackPcx( fp,buffer );
 if( BitsPerPixel > 4 )
 Write256Palette( fp,BitsPerPixel );
 fclose( fp );
 
 hfree( (char _huge *)buffer );
}
  
WritePcxHeader(FILE *fp,char *palette )
{
 PCXHEAD h;
 
 memset((char *)&h,0,sizeof(PCXHEAD));
 h.manufacturer = 0x0a;
 h.version = 5; 源代码网供稿.

网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华