home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 523b.lha / Drawmap_v2.25d / Sources.LZH / Sources / packer.c < prev    next >
C/C++ Source or Header  |  1991-06-10  |  4KB  |  114 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 "iff/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. LONG putSize;
  24. #define GetByte()       (*source++)
  25. #define PutByte(c)      { *dest++ = (c);   ++putSize; }
  26. /* protos for internal routines */
  27. BYTE *PutDump( BYTE *dest, int nn );
  28. BYTE *PutRun( BYTE *dest, int nn, int cc);
  29.  
  30. char buf[256];  /* [TBD] should be 128?  on stack?*/
  31.  
  32. BYTE *PutDump(dest, nn)  BYTE *dest;  int nn; {
  33.         int i;
  34.  
  35.         PutByte(nn-1);
  36.         for(i = 0;  i < nn;  i++)   PutByte(buf[i]);
  37.         return(dest);
  38.         }
  39.  
  40. BYTE *PutRun(dest, nn, cc)   BYTE *dest;  int nn, cc; {
  41.         PutByte(-(nn-1));
  42.         PutByte(cc);
  43.         return(dest);
  44.         }
  45.  
  46. #define OutDump(nn)   dest = PutDump(dest, nn)
  47. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  48.  
  49. /*----------- PackRow --------------------------------------------------*/
  50. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  51.    destination pointers.  RETURNs count of packed bytes.*/
  52. LONG PackRow(pSource, pDest, rowSize)
  53.     BYTE **pSource, **pDest;   LONG rowSize; {
  54.     BYTE *source, *dest;
  55.     char c,lastc = '\0';
  56.     BOOL mode = DUMP;
  57.     short nbuf = 0;             /* number of chars in buffer */
  58.     short rstart = 0;           /* buffer index current run starts */
  59.  
  60.     source = *pSource;
  61.     dest = *pDest;
  62.     putSize = 0;
  63.     buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  64.     nbuf = 1;   rowSize--;      /* since one byte eaten.*/
  65.  
  66.  
  67.     for (;  rowSize;  --rowSize) {
  68.         buf[nbuf++] = c = GetByte();
  69.         switch (mode) {
  70.                 case DUMP:
  71.                         /* If the buffer is full, write the length byte,
  72.                            then the data */
  73.                         if (nbuf>MaxDat) {
  74.                                 OutDump(nbuf-1);
  75.                                 buf[0] = c;
  76.                                 nbuf = 1;   rstart = 0;
  77.                                 break;
  78.                                 }
  79.  
  80.                         if (c == lastc) {
  81.                             if (nbuf-rstart >= MinRun) {
  82.                                 if (rstart > 0) OutDump(rstart);
  83.                                 mode = RUN;
  84.                                 }
  85.                             else if (rstart == 0)
  86.                                 mode = RUN;     /* no dump in progress,
  87.                                 so can't lose by making these 2 a run.*/
  88.                             }
  89.                         else  rstart = nbuf-1;          /* first of run */
  90.                         break;
  91.  
  92.                 case RUN: if ( (c != lastc)|| ( nbuf-rstart > MaxRun)) {
  93.                         /* output run */
  94.                         OutRun(nbuf-1-rstart,lastc);
  95.                         buf[0] = c;
  96.                         nbuf = 1; rstart = 0;
  97.                         mode = DUMP;
  98.                         }
  99.                         break;
  100.                 }
  101.  
  102.         lastc = c;
  103.         }
  104.  
  105.     switch (mode) {
  106.         case DUMP: OutDump(nbuf); break;
  107.         case RUN: OutRun(nbuf-rstart,lastc); break;
  108.         }
  109.     *pSource = source;
  110.     *pDest = dest;
  111.     return(putSize);
  112.     }
  113.  
  114.