home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS54.ZIP / DVIPS / REPACK.C < prev    next >
C/C++ Source or Header  |  1990-11-25  |  15KB  |  476 lines

  1. /*
  2.  *   Compressed TeX fonts in PostScript.  Copyright (C) 1989
  3.  *   by Radical Eye Software.  All Rights Reserved.
  4.  *   (Slight mods by Don Knuth in December 89.)
  5.  */
  6. #include "structures.h" /* The copyright notice in that file is included too! */
  7. #ifdef DEBUG
  8. extern integer debug_flag;
  9. #endif /* DEBUG */
  10.  
  11. /*   Given a raster that has been unpacked from PK format,
  12.  *   we compress it by another scheme that is suitable for
  13.  *   a PostScript-oriented unpacking. We write instructions for
  14.  *   a little interpreter whose one-byte instructions have the form
  15.  *   18*opcode+parameter. The interpreter forms each row based
  16.  *   on simple transformations to the previous row. */
  17.  
  18. #define MAXOUT (18)
  19. #define CMD(n) (MAXOUT*(n))
  20. #define ADVXCHG1 CMD(0)
  21. #define ADVXCHG1END CMD(1)
  22. #define CHGX CMD(2)-1
  23. #define CHGXEND CMD(3)-1
  24. #define ADVXLSH CMD(4)
  25. #define ADVXLSHEND CMD(5)
  26. #define ADVXRSH CMD(6)
  27. #define ADVXRSHEND CMD(7)
  28. #define ADVX CMD(8)-1
  29. #define REPX CMD(9)-1
  30. #define SETX CMD(10)-1
  31. #define CLRX CMD(11)-1
  32. #define ADVXCHG2 CMD(12)
  33. #define ADVXCHG2END CMD(13)
  34. #define END CMD(14)
  35.  
  36. extern char *malloc() ;
  37. extern free() ;
  38. extern void error() ;
  39. extern long getlong() ;
  40. extern long unpack() ;
  41.  
  42. static int rowlength = 0 ;
  43. static unsigned char *specdata ;
  44. static long tslen = 0 ;
  45. static unsigned char *tempstore, *tsp, *tsend ;
  46.  
  47. void putlong(a, i)
  48. register char *a ;
  49. long i ;
  50. {
  51.    a[0] = i >> 24 ;
  52.    a[1] = i >> 16 ;
  53.    a[2] = i >> 8 ;
  54.    a[3] = i ;
  55. }
  56.  
  57. long getlong(a)
  58. register unsigned char *a ;
  59. {
  60.    return ((((((a[0] << 8L) + a[1]) << 8L) + a[2]) << 8L) + a[3]) ;
  61. }
  62.  
  63. /* First, a routine that appends one byte to the compressed output. */
  64.  
  65. #define addtse(n) {lcm=tsp-tempstore;addts(n);} /* mark END option position */
  66.  
  67. static void addts(what)
  68. register unsigned char what ;
  69. {
  70.    register unsigned char *p, *q ;
  71.  
  72.    if (tsp >= tsend) {
  73.       if (tempstore == NULL) {
  74.          tslen = 4096 ;
  75.          tempstore = (unsigned char *)malloc((unsigned)tslen) ;
  76.          if (tempstore == NULL)
  77.             error("! out of memory") ;
  78.          tsp = tempstore ;
  79.       } else {
  80.          tslen = 2 * tslen ;
  81.          tsp = (unsigned char *)malloc((unsigned)tslen) ;
  82.          if (tsp == NULL)
  83.             error("! out of memory") ;
  84.          for (p=tempstore, q=tsp; p<tsend; p++, q++)
  85.             *q = *p ;
  86.          free((char *)tempstore) ;
  87.          tempstore = tsp ;
  88.          tsp = q ;
  89.       }
  90.       tsend = tempstore + tslen ;
  91.    }
  92.    *tsp++ = what ;
  93. }
  94.  
  95. /* Next, a routine that discovers how to do the compression. */
  96.  
  97. #define rsh(a,b) ( ((a)==0) ? ((b)==128) : ( ((a)==255) ? ((b)==127) :\
  98.                                     ((b)==(((a)>>1)|((a)&128))) ))
  99. #define lsh(a,b) ( ((a)==0) ? ((b)==1) : ( ((a)==255) ? ((b)==254) :\
  100.                                     ((b)==((((a)<<1)&255)|((a)&1))) ))
  101. #define DIFFERENT (1)
  102. #define LSHPOSSIB (2)
  103. #define RSHPOSSIB (4)
  104. #define BLKPOSSIB (8)
  105. #define WHTPOSSIB (16)
  106. #define ENDROW (32)
  107. #define NOPOSSIB(n) ((n&(LSHPOSSIB|RSHPOSSIB|BLKPOSSIB|WHTPOSSIB))==0)
  108. #define NOSHIFT(n) ((n&(LSHPOSSIB|RSHPOSSIB))==0)
  109. /*
  110.  *   Our input bytes are packed to the 16-bit word.  On output,
  111.  *   they're packed to bytes. Thus, we may have to skip a byte at
  112.  *   the end of each row.
  113.  */
  114. void dochar(from, width, height)
  115. unsigned char *from ;
  116. short width, height ; /* in bytes */
  117. {
  118.    register int i ;
  119.    register unsigned char *f, *t, *d ;
  120.    register unsigned char *e ;
  121.    register int accum ;
  122.    int j, k ;
  123.    int diffrow ;
  124.    int repeatcount ;
  125.    int lit, pos, cmd = 0 ;
  126.    long lcm ;
  127.    int widthc ;
  128.  
  129.    widthc = width + (width & 1) ; /* halfword correction */
  130.    lcm = -1 ;
  131.    if (widthc > rowlength) {
  132.       if (rowlength)
  133.          free((char *)specdata) ;
  134.       rowlength = widthc ;
  135.       specdata = (unsigned char *)malloc((unsigned)(rowlength + 15)) ;
  136.       if (specdata == NULL)
  137.          error("! out of memory") ;
  138.    }
  139.    for (i= -15, t=specdata; i<=widthc; i++, t++)
  140.       *t = 0 ;
  141.    repeatcount = 0 ;
  142.    f = specdata + 2 ;
  143.    for (j=0; j<height; j++, f = from, from += widthc) {
  144.       diffrow = 0 ;
  145.       for (i=0, t=from, d=specdata; i<width; i++, f++, t++, d++) {
  146.          if (*f == *t) {
  147.             if (*t == 0)
  148.                *d = WHTPOSSIB ;
  149.             else if (*t == 255)
  150.                *d = BLKPOSSIB ;
  151.             else
  152.                *d = 0 ;
  153.          } else {
  154.             accum = DIFFERENT ;
  155.             if (rsh(*f, *t))
  156.                accum |= RSHPOSSIB ;
  157.             else if (lsh(*f, *t))
  158.                accum |= LSHPOSSIB ;
  159.             if (*t == 0)
  160.                accum |= WHTPOSSIB ;
  161.             else if (*t == 255)
  162.                accum |= BLKPOSSIB ;
  163.             *d = accum ;
  164.             diffrow++ ;
  165.          }
  166.       } /* end 'for i' */
  167.       *d = ENDROW ;
  168.       if (diffrow == 0) {
  169.          repeatcount++ ;
  170.       } else {
  171.          if (repeatcount) {
  172.             while (repeatcount > MAXOUT) {
  173.                addts((unsigned char)(REPX+MAXOUT)) ;
  174.                repeatcount -= MAXOUT ;
  175.             }
  176.             addts((unsigned char)(REPX+repeatcount)) ;
  177.             repeatcount = 0 ;
  178.          }
  179.          pos = 0 ;
  180.          for (i=0, d=specdata, f=t-width; i<width;) {
  181.             if ((*d & DIFFERENT) == 0) {
  182.                i++ ;
  183.                d++ ;
  184.                f++ ;
  185.             } else {
  186.                accum = 0 ;
  187.                if (pos != i)
  188.                   lit = NOSHIFT(*d) ;
  189.                else /* N.B.: 'lit' does not imply literate programming here */
  190.                   lit = NOPOSSIB(*d) ;
  191.                for (e=d; ;e++) {
  192.                   if (NOPOSSIB(*e))
  193.                      lit = 1 ;
  194.                   if ((*e & DIFFERENT) == 0)
  195.                      break ;
  196.                   if ((*e & WHTPOSSIB) &&
  197.                       (e[1] & WHTPOSSIB)) {
  198.                      while (*e & WHTPOSSIB) {
  199.                         e++ ;
  200.                         accum++ ;
  201.                      }
  202.                      cmd = CLRX ;
  203.                      e -= accum ;
  204.                      break ;
  205.                   } else if ((*e & BLKPOSSIB) &&
  206.                       (e[1] & BLKPOSSIB)) {
  207.                      while (*e & BLKPOSSIB) {
  208.                         e++ ;
  209.                         accum++ ;
  210.                      }
  211.                      cmd = SETX ;
  212.                      e -= accum ;
  213.                      break ;
  214.                   }
  215.                } /* end 'for e'; d pts to first bad byte, e to next good one */
  216.                while (i - pos > MAXOUT) {
  217.                   addts((unsigned char)(ADVX+MAXOUT)) ;
  218.                   pos += MAXOUT ;
  219.                }
  220.                if (k = (e - d)) {
  221.                   if (lit) {
  222.                      if (k > 2) {
  223.                         if (i > pos) {
  224.                            addts((unsigned char)(ADVX + i - pos)) ;
  225.                            pos = i ;
  226.                         }
  227.                         while (k > MAXOUT) {
  228.                            addts((unsigned char)(CHGX + MAXOUT)) ;
  229.                            for (k=0; k<MAXOUT; k++)
  230.                               addts((unsigned char)(*f++)) ;
  231.                            d += MAXOUT ;
  232.                            pos += MAXOUT ;
  233.                            i += MAXOUT ;
  234.                            k -= MAXOUT ;
  235.                         }
  236.                         addtse((unsigned char)(CHGX + k)) ;
  237.                         pos += k ;
  238.                         for (; d<e; d++, i++, f++)
  239.                            addts((unsigned char)(*f)) ;
  240.                      } else {
  241.                         if (k == 1) {
  242.                            if (i == pos+MAXOUT) {
  243.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  244.                               pos = i ;
  245.                            }
  246.                            addtse((unsigned char)(ADVXCHG1 + i - pos)) ;
  247.                            addts((unsigned char)(*f)) ;
  248.                            i++ ;
  249.                            pos = i ;
  250.                            d++ ;
  251.                            f++ ;
  252.                         } else {
  253.                            if (i == pos+MAXOUT) {
  254.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  255.                               pos = i ;
  256.                            }
  257.                            addtse((unsigned char)(ADVXCHG2 + i - pos)) ;
  258.                            addts((unsigned char)(*f)) ;
  259.                            addts((unsigned char)(f[1])) ;
  260.                            i += 2 ;
  261.                            pos = i ;
  262.                            d += 2 ;
  263.                            f += 2 ;
  264.                         }
  265.                      }
  266.                   } else {
  267.                      while (e > d) {
  268.                         if (*d & LSHPOSSIB) {
  269.                            if (i == pos+MAXOUT) {
  270.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  271.                               pos = i ;
  272.                            }
  273.                            addtse((unsigned char)(ADVXLSH + i - pos)) ;
  274.                         } else if (*d & RSHPOSSIB) {
  275.                            if (i == pos+MAXOUT) {
  276.                               addts((unsigned char)(ADVX + MAXOUT)) ;
  277.                               pos = i ;
  278.                            }
  279.                            addtse((unsigned char)(ADVXRSH + i - pos)) ;
  280.                         } else if (*d & WHTPOSSIB) { /* i==pos */
  281.                            addts((unsigned char)(CLRX + 1)) ; lcm = -1 ;
  282.                         } else if (*d & BLKPOSSIB) { /* i==pos */
  283.                            addts((unsigned char)(SETX + 1)) ; lcm = -1 ;
  284.                         } else
  285.                            error("! bug") ; /* why wasn't lit true? */
  286.                         d++ ;
  287.                         f++ ;
  288.                         i++ ;
  289.                         pos = i ;
  290.                      } /* end 'while e>d' */
  291.                   }
  292.                } /* end 'if e>d' */
  293.                if (accum > 0) {
  294.                   if (i > pos) {
  295.                      addts((unsigned char)(ADVX + i - pos)) ;
  296.                      pos = i ;
  297.                   }
  298.                   i += accum ;
  299.                   d += accum ;
  300.                   f += accum ;
  301.                   while (accum > MAXOUT) {
  302.                      addts((unsigned char)(cmd + MAXOUT)) ;
  303.                      accum -= MAXOUT ;
  304.                      pos += MAXOUT ;
  305.                   }
  306.                   lcm = -1 ;
  307.                   addts((unsigned char)(cmd + accum)) ;
  308.                   pos += accum ;
  309.                }
  310.             } /* end 'else DIFFERENT' */
  311.          } /* end 'for i' */
  312.          if (lcm != -1) {
  313.             tempstore[lcm] += MAXOUT ;  /* append END */
  314.             lcm = -1 ;
  315.          } else {
  316.             addts((unsigned char)(END)) ;
  317.          }
  318.       } /* end 'else rows different' */
  319. #ifdef DEBUG
  320.       if (d != specdata + width)
  321.          error("! internal inconsistency in repack") ;
  322. #endif
  323.    } /* end 'for j' */
  324.    if (repeatcount) {
  325.       while (repeatcount > MAXOUT) {
  326.          addts((unsigned char)(REPX+MAXOUT)) ;
  327.          repeatcount -= MAXOUT ;
  328.       }
  329.       addts((unsigned char)(REPX+repeatcount)) ;
  330.       repeatcount = 0 ;
  331.    }
  332. }
  333.  
  334. extern long bytesleft ;
  335. extern quarterword *raster ;
  336. static long mbytesleft ;
  337. static quarterword *mraster ;
  338.  
  339. char *
  340. makecopy(what, len, p)
  341. register unsigned char *what ;
  342. register long len ;
  343. register unsigned char *p ;
  344. {
  345.    register unsigned char *q ;
  346.  
  347.    if (p == NULL) {
  348.       if (len > 32760)
  349.          error("! oops, raster too big") ;
  350.       if (bytesleft < len) {
  351.          if (RASTERCHUNK > len) {
  352.             raster = (quarterword *)malloc((unsigned)RASTERCHUNK) ;
  353.             bytesleft = RASTERCHUNK ;
  354.          } else {
  355.             raster = (quarterword *)malloc((unsigned)len) ;
  356.             bytesleft = len ;
  357.          }
  358.          if (raster == NULL) {
  359.             error("! out of memory during allocation") ;
  360.          }
  361.       }
  362.       p = (unsigned char *)raster ;
  363.       bytesleft -= len ;
  364.       raster += len ;
  365.    }
  366.    q = p ;
  367.    while (len > 0) {
  368.       *p++ = *what++ ;
  369.       len -- ;
  370.    }
  371.    return ((char *)q) ;
  372. }
  373.  
  374. /* Now the main routine, which is called when a character is used
  375.    for the very first time. */
  376.  
  377. void repack(cp)
  378. register chardesctype *cp ;
  379. {
  380.    register long i, j ;
  381.    register unsigned char *p ;
  382.    register int width, height ;
  383.    register int wwidth ;
  384.    char startbytes ;
  385.    int smallchar ;
  386.  
  387.    p = cp->packptr ;
  388.    if (p == NULL)
  389.       error("! no raster?") ;
  390.    tsp = tempstore ;
  391.    tsend = tempstore + tslen ;
  392.    addts(*p) ; /* copy the PK flag byte */
  393.    if (*p & 4) {
  394.       if ((*p & 7) == 7) {
  395.          startbytes = 17 ;
  396.          width = getlong(p + 1) ;
  397.          height = getlong(p + 5) ;
  398.          for (i=0; i<12; i++)
  399.             addts(*++p) ;
  400.       } else {
  401.          startbytes = 9 ;
  402.          width = p[1] * 256 + p[2] ;
  403.          height = p[3] * 256 + p[4] ;
  404.          addts(*++p) ;
  405.          addts(*++p) ;
  406.          addts(*++p) ;
  407.          addts(*++p) ;
  408.       }
  409.    } else {
  410.       startbytes = 5 ;
  411.       width = p[1] ;
  412.       height = p[2] ;
  413.    }
  414.    addts(*++p) ;
  415.    addts(*++p) ;
  416.    addts(*++p) ;
  417.    addts(*++p) ;
  418.    p++ ; /* OK, p now points to beginning of the nibbles */
  419.    addts((unsigned char)0) ;
  420.    addts((unsigned char)0) ;
  421.    addts((unsigned char)0) ;
  422.    addts((unsigned char)0) ; /* leave room to stick in a new length field */
  423.    wwidth = (width + 15) / 16 ;
  424.    i = 2 * height * (long)wwidth ;
  425.    if (i <= 0)
  426.       i = 2 ;
  427.    if ((cp->flags & BIGCHAR) == 0)
  428.       smallchar = 5 ;
  429.    else
  430.       smallchar = 0 ;
  431.    i += smallchar ;
  432.    if (mbytesleft < i) {
  433.       if (mbytesleft >= RASTERCHUNK)
  434.          (void) free((char *) mraster) ;
  435.       if (RASTERCHUNK > i) {
  436.          mraster = (quarterword *)malloc((unsigned)RASTERCHUNK) ;
  437.          mbytesleft = RASTERCHUNK ;
  438.       } else {
  439.          mraster = (quarterword *)malloc((unsigned)(i + 3)) ;
  440.          mbytesleft = i ;
  441.       }
  442.       if (mraster == NULL) {
  443.          error("! out of memory during allocation") ;
  444.       }
  445.    }
  446.    while (i > 0)
  447.       mraster[--i] = 0 ;
  448.    i = startbytes + unpack(p, (halfword *)mraster, (halfword)width,
  449.                 (halfword)height,  *(cp->packptr)) ;
  450.    dochar(mraster, (width + 7) >> 3, height) ;
  451.    if (smallchar) {
  452.       addts((unsigned char)0) ;
  453.       addts((unsigned char)0) ;
  454.       addts((unsigned char)0) ;
  455.       addts((unsigned char)0) ;
  456.       addts((unsigned char)0) ;
  457.    }
  458.    j = tsp - tempstore ;
  459. #ifdef DEBUG
  460.    if (dd(D_COMPRESS))
  461. #ifdef SHORTINT
  462.         (void)fprintf(stderr,"PK %ld bytes, unpacked %ld, compressed %ld\n",
  463. #else /* ~SHORTINT */
  464.         (void)fprintf(stderr,"PK %d bytes, unpacked %d, compressed %d\n",
  465. #endif /* ~SHORTINT */
  466.             i-(long)startbytes, ((width+7L)/8)*height, j-(long)startbytes-4) ;
  467. #endif /* DEBUG */
  468.    if ( i < j )
  469.       cp->packptr =
  470.               (unsigned char *)makecopy(tempstore, j, (unsigned char *)0) ;
  471.    else
  472.       makecopy(tempstore, j, (unsigned char *)cp->packptr) ;
  473.    putlong((char *)(cp->packptr+startbytes), j-startbytes-4-smallchar) ;
  474.    cp->flags |= REPACKED ;
  475. }
  476.