Back to index

tetex-bin  3.0
Defines | Functions | Variables
repack.c File Reference
#include "dvips.h"
#include "protos.h"

Go to the source code of this file.

Defines

#define MAXOUT   (18)
#define CMD(n)   (MAXOUT*(n))
#define ADVXCHG1   CMD(0)
#define ADVXCHG1END   CMD(1)
#define CHGX   CMD(2)-1
#define CHGXEND   CMD(3)-1
#define ADVXLSH   CMD(4)
#define ADVXLSHEND   CMD(5)
#define ADVXRSH   CMD(6)
#define ADVXRSHEND   CMD(7)
#define ADVX   CMD(8)-1
#define REPX   CMD(9)-1
#define SETX   CMD(10)-1
#define CLRX   CMD(11)-1
#define ADVXCHG2   CMD(12)
#define ADVXCHG2END   CMD(13)
#define END   CMD(14)
#define addtse(n)   {lcm=tsp-tempstore;addts(n);} /* mark END option position */
#define rsh(a, b)
#define lsh(a, b)
#define DIFFERENT   (1)
#define LSHPOSSIB   (2)
#define RSHPOSSIB   (4)
#define BLKPOSSIB   (8)
#define WHTPOSSIB   (16)
#define ENDROW   (32)
#define NOPOSSIB(n)   ((n&(LSHPOSSIB|RSHPOSSIB|BLKPOSSIB|WHTPOSSIB))==0)
#define NOSHIFT(n)   ((n&(LSHPOSSIB|RSHPOSSIB))==0)

Functions

void putlong P2C (register char *, a, long, i)
long getlong P1C (register unsigned char *, a)
static void addts P1C (register unsigned char, what)
void dochar P3C (unsigned char *, from, short, width, short, height)
char *makecopy P3C (register unsigned char *, what, register long, len, register unsigned char *, p)
void repack P1C (register chardesctype *, cp)

Variables

integer debug_flag
static int rowlength = -1
static unsigned char * specdata
static long tslen = 0
static unsigned char * tempstore
static unsigned char * tsp
static unsigned char * tsend
long bytesleft
quarterwordraster
long mbytesleft
quarterwordmraster

Define Documentation

#define addtse (   n)    {lcm=tsp-tempstore;addts(n);} /* mark END option position */

Definition at line 58 of file repack.c.

#define ADVX   CMD(8)-1

Definition at line 28 of file repack.c.

#define ADVXCHG1   CMD(0)

Definition at line 20 of file repack.c.

#define ADVXCHG1END   CMD(1)

Definition at line 21 of file repack.c.

#define ADVXCHG2   CMD(12)

Definition at line 32 of file repack.c.

#define ADVXCHG2END   CMD(13)

Definition at line 33 of file repack.c.

#define ADVXLSH   CMD(4)

Definition at line 24 of file repack.c.

#define ADVXLSHEND   CMD(5)

Definition at line 25 of file repack.c.

#define ADVXRSH   CMD(6)

Definition at line 26 of file repack.c.

#define ADVXRSHEND   CMD(7)

Definition at line 27 of file repack.c.

#define BLKPOSSIB   (8)

Definition at line 92 of file repack.c.

#define CHGX   CMD(2)-1

Definition at line 22 of file repack.c.

#define CHGXEND   CMD(3)-1

Definition at line 23 of file repack.c.

#define CLRX   CMD(11)-1

Definition at line 31 of file repack.c.

#define CMD (   n)    (MAXOUT*(n))

Definition at line 19 of file repack.c.

#define DIFFERENT   (1)

Definition at line 89 of file repack.c.

#define END   CMD(14)

Definition at line 34 of file repack.c.

#define ENDROW   (32)

Definition at line 94 of file repack.c.

#define lsh (   a,
  b 
)
Value:
( ((a)==0) ? ((b)==1) : ( ((a)==255) ? ((b)==254) :\
                                    ((b)==((((a)<<1)&255)|((a)&1))) ))

Definition at line 87 of file repack.c.

#define LSHPOSSIB   (2)

Definition at line 90 of file repack.c.

#define MAXOUT   (18)

Definition at line 18 of file repack.c.

#define NOPOSSIB (   n)    ((n&(LSHPOSSIB|RSHPOSSIB|BLKPOSSIB|WHTPOSSIB))==0)

Definition at line 95 of file repack.c.

#define NOSHIFT (   n)    ((n&(LSHPOSSIB|RSHPOSSIB))==0)

Definition at line 96 of file repack.c.

#define REPX   CMD(9)-1

Definition at line 29 of file repack.c.

#define rsh (   a,
  b 
)
Value:
( ((a)==0) ? ((b)==128) : ( ((a)==255) ? ((b)==127) :\
                                    ((b)==(((a)>>1)|((a)&128))) ))

Definition at line 85 of file repack.c.

#define RSHPOSSIB   (4)

Definition at line 91 of file repack.c.

#define SETX   CMD(10)-1

Definition at line 30 of file repack.c.

#define WHTPOSSIB   (16)

Definition at line 93 of file repack.c.


Function Documentation

long getlong P1C ( register unsigned char *  ,
a   
)

Definition at line 51 of file repack.c.

{
   return ((((((a[0] << 8L) + a[1]) << 8L) + a[2]) << 8L) + a[3]) ;
}
static void addts P1C ( register unsigned  char,
what   
) [static]

Definition at line 60 of file repack.c.

{
   register unsigned char *p, *q ;

   if (tsp >= tsend) {
      if (tempstore == NULL) {
         tslen = 2020 ;
         tempstore = (unsigned char *)mymalloc((integer)tslen) ;
         tsp = tempstore ;
      } else {
         tslen = 2 * tslen ;
         tsp = (unsigned char *)mymalloc((integer)tslen) ;
         for (p=tempstore, q=tsp; p<tsend; p++, q++)
            *q = *p ;
         free((char *)tempstore) ;
         tempstore = tsp ;
         tsp = q ;
      }
      tsend = tempstore + tslen ;
   }
   *tsp++ = what ;
}

Here is the call graph for this function:

void repack P1C ( register chardesctype ,
cp   
)

Definition at line 353 of file repack.c.

{
   register long i, j ;
   register unsigned char *p ;
   register int width, height ;
   register int wwidth ;
   char startbytes ;
   int smallchar ;

   p = cp->packptr ;
   if (p == NULL)
      error("! no raster?") ;
   tsp = tempstore ;
   tsend = tempstore + tslen ;
   addts(*p) ; /* copy the PK flag byte */
   if (*p & 4) {
      if ((*p & 7) == 7) {
         startbytes = 17 ;
         width = getlong(p + 1) ;
         height = getlong(p + 5) ;
         for (i=0; i<12; i++)
            addts(*++p) ;
      } else {
         startbytes = 9 ;
         width = p[1] * 256 + p[2] ;
         height = p[3] * 256 + p[4] ;
         addts(*++p) ;
         addts(*++p) ;
         addts(*++p) ;
         addts(*++p) ;
      }
   } else {
      startbytes = 5 ;
      width = p[1] ;
      height = p[2] ;
   }
   addts(*++p) ;
   addts(*++p) ;
   addts(*++p) ;
   addts(*++p) ;
   p++ ; /* OK, p now points to beginning of the nibbles */
   addts((unsigned char)0) ;
   addts((unsigned char)0) ;
   addts((unsigned char)0) ;
   addts((unsigned char)0) ; /* leave room to stick in a new length field */
   wwidth = (width + 15) / 16 ;
   i = 2 * height * (long)wwidth ;
   if (i <= 0)
      i = 2 ;
   if ((cp->flags & BIGCHAR) == 0)
      smallchar = 5 ;
   else
      smallchar = 0 ;
   i += smallchar ;
   if (mbytesleft < i) {
      if (mbytesleft >= RASTERCHUNK)
         (void) free((char *) mraster) ;
      if (RASTERCHUNK > i) {
         mraster = (quarterword *)mymalloc((integer)RASTERCHUNK) ;
         mbytesleft = RASTERCHUNK ;
      } else {
         i += i / 4 ;
         mraster = (quarterword *)mymalloc((integer)(i + 3)) ;
         mbytesleft = i ;
      }
   }
   while (i > 0)
      mraster[--i] = 0 ;
   i = startbytes + unpack(p, (halfword *)mraster, (halfword)width,
                (halfword)height,  *(cp->packptr)) ;
   dochar(mraster, (width + 7) >> 3, height) ;
   if (smallchar) {
      addts((unsigned char)0) ;
      addts((unsigned char)0) ;
      addts((unsigned char)0) ;
      addts((unsigned char)0) ;
      addts((unsigned char)0) ;
   }
   j = tsp - tempstore ;
#ifdef DEBUG
   if (dd(D_COMPRESS))
        (void)fprintf(stderr,"PK %ld bytes, unpacked %ld, compressed %ld\n",
       i-(long)startbytes, (long)((width+7L)/8)*height, j-(long)startbytes-4) ;
#endif /* DEBUG */
   if ( i < j ) {
      if (i > MINCHUNK)
         free(cp->packptr) ;
      cp->packptr =
              (unsigned char *)makecopy(tempstore, j, (unsigned char *)0) ;
   } else
      makecopy(tempstore, j, (unsigned char *)cp->packptr) ;
   putlong((char *)(cp->packptr+startbytes), j-startbytes-4-smallchar) ;
   cp->flags |= REPACKED ;
}

Here is the call graph for this function:

void putlong P2C ( register char *  ,
a  ,
long  ,
i   
)

Definition at line 43 of file repack.c.

{
   a[0] = i >> 24 ;
   a[1] = i >> 16 ;
   a[2] = i >> 8 ;
   a[3] = i ;
}
void dochar P3C ( unsigned char *  ,
from  ,
short  ,
width  ,
short  ,
height   
)

Definition at line 102 of file repack.c.

{
   register int i ;
   register unsigned char *f, *t, *d ;
   register unsigned char *e ;
   register int accum ;
   int j, k, kk ;
   int diffrow ;
   int repeatcount ;
   int lit, pos, cmd = 0 ;
   long lcm ;
   int widthc ;

   widthc = width + (width & 1) ; /* halfword correction */
   lcm = -1 ;
   if (widthc > rowlength) {
      if (rowlength > 0)
         free((char *)specdata) ;
      rowlength = widthc + 30 ;
      specdata = (unsigned char *)mymalloc((integer)(rowlength + 15)) ;
   }
   for (i= -15, t=specdata; i<=widthc; i++, t++)
      *t = 0 ;
   repeatcount = 0 ;
   f = specdata + 2 ;
   for (j=0; j<height; j++, f = from, from += widthc) {
      diffrow = 0 ;
      for (i=0, t=from, d=specdata; i<width; i++, f++, t++, d++) {
         if (*f == *t) {
            if (*t == 0)
               *d = WHTPOSSIB ;
            else if (*t == 255)
               *d = BLKPOSSIB ;
            else
               *d = 0 ;
         } else {
            accum = DIFFERENT ;
            if (rsh(*f, *t))
               accum |= RSHPOSSIB ;
            else if (lsh(*f, *t))
               accum |= LSHPOSSIB ;
            if (*t == 0)
               accum |= WHTPOSSIB ;
            else if (*t == 255)
               accum |= BLKPOSSIB ;
            *d = accum ;
            diffrow++ ;
         }
      } /* end 'for i' */
      *d = ENDROW ;
      if (diffrow == 0) {
         repeatcount++ ;
      } else {
         if (repeatcount) {
            while (repeatcount > MAXOUT) {
               addts((unsigned char)(REPX+MAXOUT)) ;
               repeatcount -= MAXOUT ;
            }
            addts((unsigned char)(REPX+repeatcount)) ;
            repeatcount = 0 ;
         }
         pos = 0 ;
         for (i=0, d=specdata, f=t-width; i<width;) {
            if ((*d & DIFFERENT) == 0) {
               i++ ;
               d++ ;
               f++ ;
            } else {
               accum = 0 ;
               if (pos != i)
                  lit = NOSHIFT(*d) ;
               else /* N.B.: 'lit' does not imply literate programming here */
                  lit = NOPOSSIB(*d) ;
               for (e=d; ;e++) {
                  if (NOPOSSIB(*e))
                     lit = 1 ;
                  if ((*e & DIFFERENT) == 0)
                     break ;
                  if ((*e & WHTPOSSIB) &&
                      (e[1] & WHTPOSSIB)) {
                     while (*e & WHTPOSSIB) {
                        e++ ;
                        accum++ ;
                     }
                     cmd = CLRX ;
                     e -= accum ;
                     break ;
                  } else if ((*e & BLKPOSSIB) &&
                      (e[1] & BLKPOSSIB)) {
                     while (*e & BLKPOSSIB) {
                        e++ ;
                        accum++ ;
                     }
                     cmd = SETX ;
                     e -= accum ;
                     break ;
                  }
               } /* end 'for e'; d pts to first bad byte, e to next good one */
               while (i - pos > MAXOUT) {
                  addts((unsigned char)(ADVX+MAXOUT)) ;
                  pos += MAXOUT ;
               }
               if (0 != (k = (e - d))) {
                  if (lit) {
                     if (k > 2) {
                        if (i > pos) {
                           addts((unsigned char)(ADVX + i - pos)) ;
                           pos = i ;
                        }
                        while (k > MAXOUT) {
                           addts((unsigned char)(CHGX + MAXOUT)) ;
                           for (kk=0; kk<MAXOUT; kk++)
                              addts((unsigned char)(*f++)) ;
                           d += MAXOUT ;
                           pos += MAXOUT ;
                           i += MAXOUT ;
                           k -= MAXOUT ;
                        }
                        addtse((unsigned char)(CHGX + k)) ;
                        pos += k ;
                        for (; d<e; d++, i++, f++)
                           addts((unsigned char)(*f)) ;
                     } else {
                        if (k == 1) {
                           if (i == pos+MAXOUT) {
                              addts((unsigned char)(ADVX + MAXOUT)) ;
                              pos = i ;
                           }
                           addtse((unsigned char)(ADVXCHG1 + i - pos)) ;
                           addts((unsigned char)(*f)) ;
                           i++ ;
                           pos = i ;
                           d++ ;
                           f++ ;
                        } else {
                           if (i == pos+MAXOUT) {
                              addts((unsigned char)(ADVX + MAXOUT)) ;
                              pos = i ;
                           }
                           addtse((unsigned char)(ADVXCHG2 + i - pos)) ;
                           addts((unsigned char)(*f)) ;
                           addts((unsigned char)(f[1])) ;
                           i += 2 ;
                           pos = i ;
                           d += 2 ;
                           f += 2 ;
                        }
                     }
                  } else {
                     while (e > d) {
                        if (*d & LSHPOSSIB) {
                           if (i == pos+MAXOUT) {
                              addts((unsigned char)(ADVX + MAXOUT)) ;
                              pos = i ;
                           }
                           addtse((unsigned char)(ADVXLSH + i - pos)) ;
                        } else if (*d & RSHPOSSIB) {
                           if (i == pos+MAXOUT) {
                              addts((unsigned char)(ADVX + MAXOUT)) ;
                              pos = i ;
                           }
                           addtse((unsigned char)(ADVXRSH + i - pos)) ;
                        } else if (*d & WHTPOSSIB) { /* i==pos */
                           addts((unsigned char)(CLRX + 1)) ; lcm = -1 ;
                        } else if (*d & BLKPOSSIB) { /* i==pos */
                           addts((unsigned char)(SETX + 1)) ; lcm = -1 ;
                        } else
                           error("! bug") ; /* why wasn't lit true? */
                        d++ ;
                        f++ ;
                        i++ ;
                        pos = i ;
                     } /* end 'while e>d' */
                  }
               } /* end 'if e>d' */
               if (accum > 0) {
                  if (i > pos) {
                     addts((unsigned char)(ADVX + i - pos)) ;
                     pos = i ;
                  }
                  i += accum ;
                  d += accum ;
                  f += accum ;
                  while (accum > MAXOUT) {
                     addts((unsigned char)(cmd + MAXOUT)) ;
                     accum -= MAXOUT ;
                     pos += MAXOUT ;
                  }
                  lcm = -1 ;
                  addts((unsigned char)(cmd + accum)) ;
                  pos += accum ;
               }
            } /* end 'else DIFFERENT' */
         } /* end 'for i' */
         if (lcm != -1) {
            tempstore[lcm] += MAXOUT ;  /* append END */
            lcm = -1 ;
         } else {
            addts((unsigned char)(END)) ;
         }
      } /* end 'else rows different' */
#ifdef DEBUG
      if (d != specdata + width)
         error("! internal inconsistency in repack") ;
#endif
   } /* end 'for j' */
   if (repeatcount) {
      while (repeatcount > MAXOUT) {
         addts((unsigned char)(REPX+MAXOUT)) ;
         repeatcount -= MAXOUT ;
      }
      addts((unsigned char)(REPX+repeatcount)) ;
      repeatcount = 0 ;
   }
}

Here is the call graph for this function:

char* makecopy P3C ( register unsigned char *  ,
what  ,
register  long,
len  ,
register unsigned char *  ,
p   
)

Definition at line 324 of file repack.c.

{
   register unsigned char *q ;

   if (p == NULL) {
      if (len > MINCHUNK)
         p = (unsigned char *)mymalloc((integer)len) ;
      else {
         if (bytesleft < len) {
            raster = (quarterword *)mymalloc((integer)RASTERCHUNK) ;
            bytesleft = RASTERCHUNK ;
         }
         p = (unsigned char *)raster ;
         bytesleft -= len ;
         raster += len ;
      }
   }
   q = p ;
   while (len > 0) {
      *p++ = *what++ ;
      len -- ;
   }
   return ((char *)q) ;
}

Variable Documentation

long bytesleft

Definition at line 112 of file dvips.c.

Definition at line 199 of file dvips.c.

long mbytesleft

Definition at line 320 of file repack.c.

Definition at line 321 of file repack.c.

Definition at line 113 of file dvips.c.

int rowlength = -1 [static]

Definition at line 38 of file repack.c.

unsigned char* specdata [static]

Definition at line 39 of file repack.c.

unsigned char* tempstore [static]

Definition at line 41 of file repack.c.

unsigned char * tsend [static]

Definition at line 41 of file repack.c.

long tslen = 0 [static]

Definition at line 40 of file repack.c.

unsigned char * tsp [static]

Definition at line 41 of file repack.c.