home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / gfx / edit / tsmorph / packer.c < prev    next >
C/C++ Source or Header  |  1993-12-21  |  3KB  |  126 lines

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