home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Misc / TRSICAT.LZX / CATS_CD2_TRSI / Reference_Library / Devices / modules / packer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-21  |  2.9 KB  |  116 lines

  1. /*----------------------------------------------------------------------*
  2.  * packer.c Convert data to "cmpByteRun1" run compression.     11/15/85
  3.  *
  4.  * By Jerry Morrison and Steve Shaw, Electronic Arts.
  5.  * This software is in the public domain.
  6.  *
  7.  *    control bytes:
  8.  *     [0..127]   : followed by n+1 bytes of data.
  9.  *     [-1..-127] : followed by byte to be repeated (-n)+1 times.
  10.  *     -128       : NOOP.
  11.  *
  12.  * This version for the Commodore-Amiga computer.
  13.  *----------------------------------------------------------------------*/
  14. #include "iffp/packer.h"
  15.  
  16. #define DUMP    0
  17. #define RUN    1
  18.  
  19. #define MinRun 3    
  20. #define MaxRun 128
  21. #define MaxDat 128
  22.  
  23. /* When used on global definitions, static means private.
  24.  * This keeps these names, which are only referenced in this
  25.  * module, from conficting with same-named objects in your program.
  26.  */ 
  27. static LONG putSize;
  28. static char buf[256];    /* [TBD] should be 128?  on stack?*/
  29.  
  30. #define GetByte()    (*source++)
  31. #define PutByte(c)    { *dest++ = (c);   ++putSize; }
  32.  
  33. static BYTE *PutDump(dest, nn)  BYTE *dest;  int nn; {
  34.     int i;
  35.  
  36.     PutByte(nn-1);
  37.     for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  38.     return(dest);
  39.     }
  40.  
  41. static BYTE *PutRun(dest, nn, cc)   BYTE *dest;  int nn, cc; {
  42.     PutByte(-(nn-1));
  43.     PutByte(cc);
  44.     return(dest);
  45.     }
  46.  
  47. #define OutDump(nn)   dest = PutDump(dest, nn)
  48. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  49.  
  50. /*----------- packrow --------------------------------------------------*/
  51. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  52.  * destination pointers.  RETURNs count of packed bytes.
  53.  */
  54. LONG packrow(BYTE **pSource, BYTE **pDest, LONG rowSize)
  55.     {
  56.     BYTE *source, *dest;
  57.     char c,lastc = '\0';
  58.     BOOL mode = DUMP;
  59.     short nbuf = 0;        /* number of chars in buffer */
  60.     short rstart = 0;        /* buffer index current run starts */
  61.  
  62.     source = *pSource;
  63.     dest = *pDest;
  64.     putSize = 0;
  65.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  66.     nbuf = 1;   rowSize--;    /* since one byte eaten.*/
  67.  
  68.  
  69.     for (;  rowSize;  --rowSize) {
  70.     buf[nbuf++] = c = GetByte();
  71.     switch (mode) {
  72.         case DUMP: 
  73.             /* If the buffer is full, write the length byte,
  74.                then the data */
  75.             if (nbuf>MaxDat) {
  76.                 OutDump(nbuf-1);  
  77.                 buf[0] = c; 
  78.                 nbuf = 1;   rstart = 0; 
  79.                 break;
  80.                 }
  81.  
  82.             if (c == lastc) {
  83.                 if (nbuf-rstart >= MinRun) {
  84.                 if (rstart > 0) OutDump(rstart);
  85.                 mode = RUN;
  86.                 }
  87.                 else if (rstart == 0)
  88.                 mode = RUN;    /* no dump in progress,
  89.                 so can't lose by making these 2 a run.*/
  90.                 }
  91.             else  rstart = nbuf-1;        /* first of run */ 
  92.             break;
  93.  
  94.         case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  95.                 /* output run */
  96.                OutRun(nbuf-1-rstart,lastc);
  97.                 buf[0] = c;
  98.                 nbuf = 1; rstart = 0;
  99.                 mode = DUMP;
  100.                 }
  101.             break;
  102.         }
  103.  
  104.     lastc = c;
  105.     }
  106.  
  107.     switch (mode) {
  108.     case DUMP: OutDump(nbuf); break;
  109.     case RUN: OutRun(nbuf-rstart,lastc); break;
  110.     }
  111.     *pSource = source;
  112.     *pDest = dest;
  113.     return(putSize);
  114.     }
  115.  
  116.