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