⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 efaxlib.c.orig

📁 用于使用moden进行传真的源代码
💻 ORIG
📖 第 1 页 / 共 5 页
字号:
  fread2 ( &version, f ) ;  fread4 ( &(f->next), f ) ;    msg ( "F TIFF version %d.%d file (%s-endian)",       version/10, version%10, f->bigend ? "big" : "little" ) ;  fseek ( f->f, f->next, SEEK_SET ) ;  return tiff_next ( f ) ;}/* File handling for text files. */int text_next ( IFILE *f ){  int err = 0, i, nc ;  char buf [ MAXLINELEN ] ;  f->page->offset = ftell ( f->f ) ;  f->page->format = P_TEXT ;  nc = ( f->page->w - f->lmargin ) / f->charw ;  if ( nc > MAXLINELEN )     nc = MAXLINELEN ;  for ( i = 0 ; i < f->pglines && fgets ( buf, nc, f->f ) ; i++ ) ;  f->next = ! feof(f->f) ? ftell ( f->f ) : 0 ;  err = ferror(f->f) ? 2 : 0 ;  return err ;}int text_first ( IFILE *f ){  static faxfont defaultfont ;  if ( ! f->font ) {    /* use default font scaled 2X, 1 inch margin, 66 lines/page */    f->font = &defaultfont ;    readfont ( 0, f->font ) ;     if ( ! f->charw ) f->charw = 2 * f->font->w ;    if ( ! f->charh ) f->charh = 2 * f->font->h ;    if ( ! f->lmargin ) f->lmargin = 204 ;    if ( ! f->pglines ) f->pglines = DEFPGLINES ;  }  /* if not set, take default values from font  */  if ( ! f->charw ) f->charw = f->font->w ;  if ( ! f->charh ) f->charh = f->font->h ;  if ( ! f->lmargin ) f->lmargin = 0 ;  if ( ! f->pglines ) f->pglines = f->page->h / f->charh - 6 ;    return text_next ( f ) ; }/* File handling for PBM files */int pbm_first ( IFILE *f ){  int err=0 ;  fseek ( f->f, 2, SEEK_SET ) ;  if ( ! ( f->page->w = pbmdim ( f ) ) || ! ( f->page->h = pbmdim ( f ) ) ) {    err = msg ( "E2 EOF or 0 dimension in PBM header" ) ;  } else if ( f->page->w % 8 ) {    err = msg ( "E2 PBM width must be multiple of 8" ) ;  } else {    msg ( "F read %dx%d PBM header", f->page->w, f->page->h ) ;  }  f->page->offset = ftell ( f->f ) ;  f->page->format = P_PBM ;  f->next = 0 ;  return err ;}#define pbm_next 0/* File handling for FAX files */#define fax_first 0#define fax_next 0/* File handling for PCX files *//* get a 16-bit word in Intel byte order. */int fgeti ( IFILE *f ){  return fgetc ( f->f ) + fgetc ( f->f ) * 256 ;}int pcx_first ( IFILE *f ){  int err=0, xmin, xmax, ymin, ymax, nc, nb ;  long start ;  start = ftell ( f->f ) ;  fseek ( f->f, start+4, SEEK_SET ) ;  xmin = fgeti ( f ) ;  ymin = fgeti ( f ) ;  xmax = fgeti ( f ) ;  ymax = fgeti ( f ) ;  f->page->w = xmax - xmin + 1 ;  f->page->h = ymax - ymin + 1 ;  f->page->xres = fgeti ( f ) ;  f->page->yres = fgeti ( f ) ;  fseek ( f->f, start+0x41, SEEK_SET ) ;  nc = fgetc ( f->f ) ;  nb = fgeti ( f ) ;  if ( nc != 1 )     msg ( "W mono PCX file has %d colour planes", nc ) ;  if ( nb != ( f->page->w + 15 ) / 16 * 2 )    msg ( "W PCX file has %d bytes per scan line for %d pels",	  nb, f->page->w ) ;  f->page->offset = start + 128 ;  f->page->format = P_PCX ;  return err ;}#define pcx_next 0/* File handling for DCX files */int dcx_next ( IFILE *f ){  int err=0 ;  long thisp, nextp ;  /* get this and next pages' offsets */  fseek ( f->f, f->next, SEEK_SET ) ;   fread4 ( &thisp, f ) ;  fread4 ( &nextp, f ) ;  /* save address of next directory entry, if any */  f->next = nextp ? f->next + 4 : 0 ;  if ( ! thisp )    err = msg ( "E2 can't happen (dcx_next)" ) ;  else    fseek ( f->f, thisp, SEEK_SET ) ;  return err ? err : pcx_first ( f ) ;}int dcx_first ( IFILE *f ){  f->bigend = 0 ;  f->next = 4 ;  return dcx_next ( f ) ;}#define raw_reset 0#define raw_first 0#define raw_next 0/* input file state reset for different compression methods */#define pcx_reset 0#define pbm_reset 0int text_reset ( IFILE *f ){  int err=0 ;  f->lines = ( ( f->pglines * f->charh ) / f->charh ) * f->charh ;  f->txtlines = 0 ;  f->page->yres = f->lines > 1078 ? 196 : 98 ; /* BOGUS */  return err ;}int fax_reset ( IFILE *f ){  int pels ;  short runs [ MAXRUNS ] ;    newDECODER ( &f->d ) ;  if ( readruns ( f, runs, &pels ) < 0 || pels ) /* skip first EOL */    msg ( "W first line has %d pixels: probably not fax data", pels ) ;  f->lines = -1 ;  return 0 ;}/* Skip to start of same (dp=0) or next (dp=1) page image.   Returns 0 if OK, 1 if no more pages, 2 on errors. */int nextipage ( IFILE *f, int dp ){  int err=0 ;  int ( *reset [NPFORMATS] ) ( IFILE * ) = {    raw_reset, fax_reset, pbm_reset, text_reset, pcx_reset  }, (*pf)(IFILE*) ;  /* close current file if any and set to NULL */  if ( f->f ) {    fclose ( f->f ) ;    f->f = 0 ;  }  /*  if requested, point to next page and check if done */  if ( dp ) {    f->page++ ;  }    if ( f->page > f->lastpage ) {    err = 1 ;  }  /* open the file and seek to start of image data */  if ( ! err ) {    f->f = fopen ( f->page->fname, (f->page->format == P_TEXT) ? "r" : "rb" ) ;    if ( ! f->f )      err = msg ( "ES2can't open %s:", f->page->fname ) ;  }  if ( ! err && fseek ( f->f, f->page->offset, SEEK_SET ) )    err = msg ( "ES2 seek failed" ) ;  /* default initializations */  f->lines = f->page->h ;  /* coding-specific initializations for this page */    if ( ! err ) {    pf = reset[f->page->format] ;    if ( pf )      err = (*pf)(f) ;  }  return err ;}/* Returns true if on last file. */int lastpage ( IFILE *f ){  return f->page >= f->lastpage ;}#define dfax_first 0#define dfax_next 0/* Initialize an input (IFILE) structure.  This structure   collects the data about images to be processed to allow a   simple interface for functions that need to read image files.   The IFILE is initialized by building an array of information   for each page (image) in all of the files.     The page pointer index is initialized so that the first call   to nextipage with dp=1 actually opens the first file.*/int newIFILE ( IFILE *f, char **fnames ){  int err=0, i, n, fformat=0 ;  char **p ;  uchar buf[128] ;  int ( *fun ) ( IFILE * ) ;    int ( *first [NIFORMATS] ) ( IFILE * ) = {    auto_first, pbm_first, fax_first, text_first, tiff_first,     dfax_first, pcx_first, raw_first, dcx_first  } ;  int ( *next [NIFORMATS] ) ( IFILE * ) = {    auto_next, pbm_next, fax_next, text_next, tiff_next,     dfax_next, pcx_next, raw_next, dcx_next  } ;  f->page = f->pages ;  /* get info for all pages in all files */  for ( p=fnames ; ! err && *p ; p++ ) {    if ( ! ( f->f = fopen ( *p, "rb" ) ) )      err = msg ( "ES2 can't open %s:", *p ) ;        if ( ! err ) {      n = fread ( buf, 1, 128, f->f ) ;      if ( ferror ( f->f ) )	err = msg ( "ES2 can't open %s:", *p ) ;    }        if ( ! err ) {      fformat = getformat ( buf, n ) ;      if ( ! fformat )	err = msg ( "E2 can't get format of %s", *p ) ;    }    if ( ! err && fseek ( f->f, 0, SEEK_SET ) )      err = msg ( "ES2 can't rewind %s:", *p ) ;    /* get format information for all pages in this file */    for ( i=0 ; ! err ; i++ ) {      page_init ( f->page, *p ) ;      if ( ( fun = i ? next[fformat] : first[fformat] ) )	err = (*fun)(f) ;      if ( ! err ) {	page_report ( f->page, fformat, f->page - f->pages + 1 ) ;	f->page++ ;		if ( f->page >= f->pages + MAXPAGE )	  err = msg ( "E2 too many pages (max is %d)", MAXPAGE ) ;      }      if ( ! f->next ) break ;    }    if ( f->f ) {      fclose ( f->f ) ;      f->f = 0 ;    }  }  f->lastpage = f->page - 1 ;  f->page = f->pages ;    if ( ! normalbits[1] ) initbittab() ;	/* bit-reverse table initialization */  return err ;}		/* Image File Output Functions *//* Strings and function to write a bit map in HP-PCL format. The only   compression is removal of trailing zeroes.  Margins and resolution are   set before first write.  */char *PCLBEGIN =	 "\033E"		/* Printer reset. */	 "\033&l0E"		/* top  margin = 0 */	 "\033&a0L"		/* left margin = 0 */	 "\033*t%dR"		/* Set raster graphics resolution */	 "\033*r1A" ;		/* Start raster graphics, rel. adressing */char *PCLEND = 	 "\033*rB"		/* end raster graphics */	 "\014" 		/* form feed */	 "\033E" ;		/* Printer reset. */void pclwrite ( OFILE *f, unsigned char *buf, int n ){  while ( n > 0 && buf [ n-1 ] == 0 ) n-- ;   fprintf( f->f, "\033*b%dW", n ) ;  fwrite ( buf, n, 1, f->f ) ;}/* Write a bit map as (raw) Portable Gray Map (PGM) format after   decimating by a factor of 4.  Sums bits in each 4x4-pel square   to compute sample value.  This function reduces each dimension   of a bit map by 4 (it writes n*8/4 pixels per scan line and   one scan line for every 4 in).  The 17 possible sample values   are spread linearly over the range 0-255. */void pgmwrite ( OFILE *f, uchar *buf, int n ){  static uchar gval [ MAXBITS * 8 / 4 ] ;  static int init=0, lines=0 ;  static uchar hbits [ 256 ], lbits [ 256 ] ;  static int nybblecnt [ 16 ] = { 0,1,1,2, 1,2,2,3, 1,2,2,3, 2,3,3,4 } ;  static uchar corr [ 17 ] = { 255, 239, 223, 207, 191, 175, 159, 143, 127, 				111,  95,  79,  63,  47,  31,  15,   0 } ;  int m ;  uchar *p, *q ;   if ( ! init ) {	    /*  build table of bit counts in each nybble */    short i ;    for ( i=0 ; i<256 ; i++ ) {	hbits [ i ] = nybblecnt [ i >> 4 & 0x0f ] ;	lbits [ i ] = nybblecnt [ i & 0x0f ] ;      }    init = 1 ;  }  for ( m=n, p=gval, q=buf ; m-- > 0 ; q++ ) {    *p++ += hbits [ *q ] ;    *p++ += lbits [ *q ] ;  }    if ( ( lines++ & 0x03 ) == 0x03 ) {    for ( p=gval, m=2*n ; m-- > 0 ; p++ ) *p = corr [ *p ] ;    fwrite ( gval,  1, 2*n, f->f ) ;    memset ( gval,  0, 2*n ) ;  }}/* Postscript image data is differentially coded vertically and   run-length coded horizontally.  A leading byte (n) defines the type   of coding for subsequent data:   0        repeat previous line   1-127    n data bytes follow   128-254  copy n-127 bytes from previous line   255 n    repeat the next character 'n' times    The overhead for coding a copy is 2 bytes (copy count, data count),   so copies > 2 bytes should be so coded.  The overhead for coding a   run is 4 bytes (255, count, byte, data count), so runs > 4 bytes   should be so coded.  Copies decode/execute faster and code more   compactly so are preferred over runs.*/const char PSBEGIN [] =		/* start of file */  "%%!PS-Adobe-2.0 EPSF-2.0 \n"  "%%%%Creator: efax (Copyright 1995 Ed Casas) \n"  "%%%%Title: efix output\n"  "%%%%Pages: (atend) \n"  "%%%%BoundingBox: 0 0 %d %d \n"  "%%%%BeginComments \n"  "%%%%EndComments \n"  "/val 1 string def \n"  "/buf %d string def   \n"  "/getval { \n"  "  currentfile val readhexstring pop 0 get \n"  "} bind def \n"  "/readbuf { \n"  "  0  %% => index \n"  "  { \n"  "    dup buf length ge { exit } if \n"  "    getval   %% => index run_length \n"  "    dup 127 le { \n"  "      dup 0 eq { \n"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -