home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / anim / players / mpeg_src.lha / amiga / lib / packer.c < prev    next >
C/C++ Source or Header  |  1992-12-20  |  3KB  |  122 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. static BYTE *PutDump(BYTE *, int);
  17. static BYTE *PutRun(BYTE *,int,int);
  18.  
  19. #define DUMP    0
  20. #define RUN    1
  21.  
  22. #define MinRun 3    
  23. #define MaxRun 128
  24. #define MaxDat 128
  25.  
  26. /* When used on global definitions, static means private.
  27.  * This keeps these names, which are only referenced in this
  28.  * module, from conficting with same-named objects in your program.
  29.  */ 
  30. static LONG putSize;
  31. static char buf[256];    /* [TBD] should be 128?  on stack?*/
  32.  
  33. #define GetByte()    (*source++)
  34. #define PutByte(c)    { *dest++ = (c);   ++putSize; }
  35.  
  36.  
  37. static BYTE *PutDump(BYTE *dest, int nn)
  38.     {
  39.     int i;
  40.  
  41.     PutByte(nn-1);
  42.     for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  43.     return(dest);
  44.     }
  45.  
  46. static BYTE *PutRun(BYTE *dest, int nn, int cc)
  47.     {
  48.     PutByte(-(nn-1));
  49.     PutByte(cc);
  50.     return(dest);
  51.     }
  52.  
  53. #define OutDump(nn)   dest = PutDump(dest, nn)
  54. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  55.  
  56. /*----------- packrow --------------------------------------------------*/
  57. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  58.  * destination pointers.  RETURNs count of packed bytes.
  59.  */
  60. LONG packrow(BYTE **pSource, BYTE **pDest, LONG rowSize)
  61.     {
  62.     BYTE *source, *dest;
  63.     char c,lastc = '\0';
  64.     BOOL mode = DUMP;
  65.     short nbuf = 0;        /* number of chars in buffer */
  66.     short rstart = 0;        /* buffer index current run starts */
  67.  
  68.     source = *pSource;
  69.     dest = *pDest;
  70.     putSize = 0;
  71.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  72.     nbuf = 1;   rowSize--;    /* since one byte eaten.*/
  73.  
  74.  
  75.     for (;  rowSize;  --rowSize) {
  76.     buf[nbuf++] = c = GetByte();
  77.     switch (mode) {
  78.         case DUMP: 
  79.             /* If the buffer is full, write the length byte,
  80.                then the data */
  81.             if (nbuf>MaxDat) {
  82.                 OutDump(nbuf-1);  
  83.                 buf[0] = c; 
  84.                 nbuf = 1;   rstart = 0; 
  85.                 break;
  86.                 }
  87.  
  88.             if (c == lastc) {
  89.                 if (nbuf-rstart >= MinRun) {
  90.                 if (rstart > 0) OutDump(rstart);
  91.                 mode = RUN;
  92.                 }
  93.                 else if (rstart == 0)
  94.                 mode = RUN;    /* no dump in progress,
  95.                 so can't lose by making these 2 a run.*/
  96.                 }
  97.             else  rstart = nbuf-1;        /* first of run */ 
  98.             break;
  99.  
  100.         case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  101.                 /* output run */
  102.                OutRun(nbuf-1-rstart,lastc);
  103.                 buf[0] = c;
  104.                 nbuf = 1; rstart = 0;
  105.                 mode = DUMP;
  106.                 }
  107.             break;
  108.         }
  109.  
  110.     lastc = c;
  111.     }
  112.  
  113.     switch (mode) {
  114.     case DUMP: OutDump(nbuf); break;
  115.     case RUN: OutRun(nbuf-rstart,lastc); break;
  116.     }
  117.     *pSource = source;
  118.     *pDest = dest;
  119.     return(putSize);
  120.     }
  121.  
  122.