Back to index

tetex-bin  3.0
Functions | Variables
pkin.c File Reference
#include <stdlib.h>
#include "basics.h"
#include "pkin.h"

Go to the source code of this file.

Functions

static void error ()
static shalfword pkbyte ()
static integer pkquad ()
static integer pktrio ()
static integer pkpair ()
static integer pkspair ()
static Boolean pkopen (name)
static shalfword getnyb ()
static Boolean getbit ()
static halfword handlehuge ()
static halfword pkpackednum ()
static halfword rest ()
static halfword handlehuge (i, halfword i k)
static void unpack (chardesc *cd)
int readchar (name, shalfword c, chardesc *cd)
static void error (s)

Variables

static FILEpkfile
static char errbuf [80]
static halfword inputbyte
static halfword flagbyte
static halfword bitweight
static halfword dynf
static halfword repeatcount
static halfword(* realfunc )()
long PKremainder
static halfword gpower [17]

Function Documentation

static void error ( ) [static]
static void error ( s  ) [static]

Definition at line 449 of file pkin.c.

{
   fprintf(stderr, "%s\n", s);
   exit(1);
}

Here is the call graph for this function:

static Boolean getbit ( void  ) [static]

Definition at line 140 of file pkin.c.

{
    bitweight >>= 1 ; 
    if ( bitweight == 0 ) 
    { inputbyte = pkbyte();
      bitweight = 128 ;
    } 
    return(inputbyte & bitweight) ;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static shalfword getnyb ( void  ) [static]

Definition at line 126 of file pkin.c.

{   halfword temp;
    if ( bitweight == 0 ) 
    { bitweight = 16 ; 
      inputbyte = pkbyte();
      temp = inputbyte >> 4 ;
    } else {
      bitweight = 0 ;
      temp = inputbyte & 15 ;
    }
    return(temp);
} 

Here is the call graph for this function:

static halfword handlehuge ( ) [static]

Here is the caller graph for this function:

static halfword handlehuge ( i  ,
halfword i  k 
) [static]

Definition at line 213 of file pkin.c.

{
   register long j = k ;

   while (i) {
      j = (j << 4L) + getnyb() ;
      i-- ;
   }
   PKremainder = j - 15 + ( 13 - dynf ) * 16 + dynf ;
   realfunc = rest ;
   return ( rest() ) ;
}

Here is the call graph for this function:

static shalfword pkbyte ( void  ) [static]

Definition at line 38 of file pkin.c.

{
   register shalfword i ;

   if ((i=getc(pkfile))==EOF)
      error("! unexpected eof in pk file") ;
   return(i) ;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static Boolean pkopen ( name  ) [static]

Definition at line 100 of file pkin.c.

{
   if ((pkfile=fopen(name, RB))==NULL) {
      (void)sprintf(errbuf, "Could not open %s", name) ;
      error(errbuf) ;
      return(0) ;
   } else
      return(1) ;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static halfword pkpackednum ( void  ) [static]

Definition at line 154 of file pkin.c.

                               {
register halfword i, j ; 
    i = getnyb () ; 
    if ( i == 0 ) 
    { do { j = getnyb () ; 
        i++ ; 
        } while ( ! ( j != 0 ) ) ; 
      if ( i > 3 ) {
/*
 *   Damn, we got a huge count!  We *fake* it by giving an artificially
 *   large repeat count.
 */
         return( handlehuge ( i , j ) ) ;
      } else {
         while ( i > 0 ) 
           { j = j * 16 + getnyb () ; 
             i-- ; 
             } 
           return ( j - 15 + ( 13 - dynf ) * 16 + dynf ) ; 
         } 
      }
    else if ( i <= dynf ) return ( i ) ; 
    else if ( i < 14 ) return ( ( i - dynf - 1 ) * 16 + getnyb () + dynf + 1 
    ) ; 
    else 
    { if ( i == 14 ) repeatcount = pkpackednum () ; 
      else repeatcount = 1 ; 
#ifdef DEBUG
      printf("[%d]", repeatcount);
#endif
      return ( (*realfunc)() ) ;
      } 
    } 

Here is the call graph for this function:

Here is the caller graph for this function:

static integer pkpair ( ) [static]

Definition at line 73 of file pkin.c.

{
  register integer i ;
  
  i = pkbyte() ;
  i = i * 256 + pkbyte() ;
  return (i);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static integer pkquad ( void  ) [static]

Definition at line 48 of file pkin.c.

{
   register integer i ;

   i = pkbyte() ;
   if (i > 127)
      i -= 256 ;
   i = i * 256 + pkbyte() ;
   i = i * 256 + pkbyte() ;
   i = i * 256 + pkbyte() ;
   return(i) ;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static integer pkspair ( ) [static]

Definition at line 83 of file pkin.c.

{
  register integer i ;
  
  i = pkbyte() ;
  if (i > 127)
    i -= 256;
  i = i * 256 + pkbyte();
  return (i);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static integer pktrio ( void  ) [static]

Definition at line 62 of file pkin.c.

{
   register integer i ;

   i = pkbyte() ;
   i = i * 256 + pkbyte() ;
   i = i * 256 + pkbyte() ;
   return(i) ;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int readchar ( name  ,
shalfword  c,
chardesc cd 
)

Definition at line 337 of file pkin.c.

{
   register shalfword i ;
   register integer k ;
   register integer length = 0;

   if (!pkopen(name)) return(0);
/*
 *   Check the preamble of the pkfile
 */
   if (pkbyte()!=247)
      error("! bad pk file, expected pre") ;
   if (pkbyte()!=89)
      error("! bad version of pk file") ;
   for(i=pkbyte(); i>0; i--)       /* creator of pkfile */
      (void)pkbyte() ;             
   (void)pkquad(); /* design size */
   k = pkquad() ;  /* checksum    */
   k = pkquad() ;  /* hppp        */
   k = pkquad() ;  /* vppp    */
/*
 *   Now we skip to the desired character definition
 */
   while ((flagbyte=pkbyte())!=245) {
      if (flagbyte < 240) {
         switch (flagbyte & 7) {
case 0: case 1: case 2: case 3:
            length = (flagbyte & 7) * 256 + pkbyte() - 4 ;
            cd->charcode = pkbyte() ;
            (void) pktrio() ;             /* TFMwidth */
            (void) pkbyte() ;             /* pixel width */
            break ;
case 4:
            length = pkbyte() * 256 ;
            length = length + pkbyte() - 5 ;
            cd->charcode = pkbyte() ;
            (void) pktrio() ;             /* TFMwidth */
            i = pkbyte() ;
            i =  i * 256 + pkbyte() ;   /* pixelwidth */
            break ;
case 5: case 6:
            error("! lost sync in pk file (character too big)") ;
case 7:
            length = pkquad() - 12 ;
            cd->charcode = pkquad() ;
            (void) pkquad() ;             /* TFMwidth */
/*          cd->pixelwidth = (pkquad() + 32768) >> 16 ; */
           (void) pkquad();        /* pixelwidth */
            k = pkquad() ;
         }
         if (cd->charcode == c) {
            if (flagbyte & 4) {
               if ((flagbyte & 7) == 7) { /* long format */
                  cd->cwidth = pkquad() ;
                  cd->cheight = pkquad() ;
                  cd->xoff = pkquad() ;
                  cd->yoff = pkquad() ;
               } else { /* extended format */
                  cd->cwidth = pkpair(); ;
                  cd->cheight = pkpair() ;
                  cd->xoff = pkspair() ;
                  cd->yoff = pkspair() ;
               }
            } else { /* short format */
               cd->cwidth = pkbyte() ;
               cd->cheight = pkbyte() ;
               cd->xoff = pkbyte() ;
               cd->yoff = pkbyte() ;
               if (cd->xoff > 127)
                  cd->xoff -= 256 ;
               if (cd->yoff > 127)
                  cd->yoff -= 256 ;
            }
            unpack(cd);
            (void)fclose(pkfile) ;
            return(1);
         }
         for (; length>0; length--) /* skip this character */
            (void) pkbyte() ;
      } else {
         k = 0 ;
         switch (flagbyte) {
case 243:
            k = pkbyte() ;
            if (k > 127)
               k -= 256 ;
case 242:
            k = k * 256 + pkbyte() ;
case 241:
            k = k * 256 + pkbyte() ;
case 240:
            k = k * 256 + pkbyte() ;
            while (k-- > 0)
               i = pkbyte() ;
            break ;
case 244:
            k = pkquad() ;
            break ;
case 246:
            break ;
default:
            error("! lost sync in pk file") ;
         }
      }
   }
   (void)fclose(pkfile) ;
   return(0); /* character not found */
}

Here is the call graph for this function:

Here is the caller graph for this function:

static halfword rest ( void  ) [static]

Definition at line 188 of file pkin.c.

{
   halfword i ;

   if (PKremainder < 0) {
      PKremainder = - PKremainder ;
      return ( 0 ) ;
   } else if (PKremainder > 0) {
      if (PKremainder > 4000) {
         PKremainder = 4000 - PKremainder ;
         return ( 4000 ) ;
      } else {
         i = PKremainder ;
         PKremainder = 0 ;
         realfunc = pkpackednum ;
         return ( i ) ;
      }
   } else {
      error("! shouldn't happen") ;
   }

   /*NOTREACHED*/
   return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void unpack ( chardesc cd) [static]

Definition at line 235 of file pkin.c.

{ 
  register integer i, j ; 
  register halfword word, wordweight ;
  halfword *raster;
  shalfword rowsleft ; 
  Boolean turnon ; 
  shalfword hbit ; 
  halfword count ; 
  shalfword  wordwidth ;

      wordwidth = (cd->cwidth + 15) / 16 ;
      i = 2 * cd->cheight * (long)wordwidth ;
      if (i <= 0)
         i = 2 ;
      cd->raster = (halfword *)malloc((unsigned)i) ;
      if (cd->raster == NULL)
         error("! out of memory during allocation") ;
      raster = cd->raster;
      realfunc = pkpackednum ;
      dynf = flagbyte / 16 ; 
      turnon = flagbyte & 8 ; 
      if ( dynf == 14 ) 
      { bitweight = 0 ; 
        for ( i = 1 ; i <= cd->cheight ; i ++ ) 
          { word = 0 ; 
            wordweight = 32768 ; 
            for ( j = 1 ; j <= cd->cwidth ; j ++ ) 
              { if ( getbit () ) word += wordweight ; 
                wordweight >>= 1 ;
                if ( wordweight == 0 ) 
                { *raster++ = word ; 
                  word = 0 ;
                  wordweight = 32768 ; 
                  } 
                } 
              if ( wordweight != 32768 ) 
                 *raster++ = word ; 
            } 
      } else {
        rowsleft = cd->cheight ; 
        hbit = cd->cwidth ; 
        repeatcount = 0 ; 
        wordweight = 16 ; 
        word = 0 ; 
        bitweight = 0 ;
        while ( rowsleft > 0 ) 
          { count = (*realfunc)() ; 
#ifdef DEBUG
            if (turnon) printf("(%d) ",count);
            else printf("%d ",count);
#endif
            while ( count != 0 ) 
              { if ( ( count < wordweight ) && ( count < hbit ) ) 
                { if ( turnon ) word += gpower [ wordweight ] - gpower 
                  [ wordweight - count ] ; 
                  hbit -= count ; 
                  wordweight -= count ; 
                  count = 0 ; 
                  } 
                else if ( ( count >= hbit ) && ( hbit <= wordweight ) ) 
                { if ( turnon )
                     word += gpower [ wordweight ] - gpower 
                  [ wordweight - hbit ] ; 
                  *raster++ = word ; 
                  for ( i = 1 ; i <= repeatcount ; i ++ ) {
                    for ( j = 1 ; j <= wordwidth ; j ++ ) {
                      *raster = *(raster - wordwidth) ;
                      raster++ ;
                    }
                  }
                  rowsleft -= repeatcount + 1 ; 
                  repeatcount = 0 ; 
                  word = 0 ; 
                  wordweight = 16 ; 
                  count -= hbit ; 
                  hbit = cd->cwidth ; 
                  } 
                else 
                { if ( turnon ) word += gpower [ wordweight ] ; 
                  *raster++ = word ;
                  word = 0 ; 
                  count -= wordweight ; 
                  hbit -= wordweight ; 
                  wordweight = 16 ; 
                  } 
                } 
              turnon = ! turnon ; 
         }
          putchar('\n') ;
          if ( ( rowsleft != 0 ) || ( hbit != cd->cwidth ) ) 
          error ( "! error while unpacking; more bits than required" ) ; 
        } 
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

halfword bitweight [static]

Definition at line 121 of file pkin.c.

halfword dynf [static]

Definition at line 122 of file pkin.c.

char errbuf[80] [static]

Definition at line 94 of file pkin.c.

halfword flagbyte [static]

Definition at line 120 of file pkin.c.

halfword gpower[17] [static]
Initial value:
 { 0 , 1 , 3 , 7 , 15 , 31 , 63 , 127 ,
     255 , 511 , 1023 , 2047 , 4095 , 8191 , 16383 , 32767 , 65535 }

Definition at line 231 of file pkin.c.

halfword inputbyte [static]

Definition at line 120 of file pkin.c.

FILE* pkfile [static]

Definition at line 35 of file pkin.c.

Definition at line 151 of file pkin.c.

halfword(* realfunc)() [static]

Definition at line 150 of file pkin.c.

Definition at line 123 of file pkin.c.