home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 365.lha / VSnap / packer.c < prev    next >
Text File  |  1990-04-10  |  3KB  |  130 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.  
  15. #define MaxPackedSize(rowSize)  ( (rowSize) + ( ((rowSize)+127) >> 7 ) )
  16.  
  17. #define DUMP    0
  18. #define RUN     1
  19. #define MinRun 3
  20. #define MaxRun 128
  21. #define MaxDat 128
  22.  
  23. short putSize;
  24. #define GetByte()       (*source++)
  25. #define PutByte(c)      { *dest++ = (c);   ++putSize; }
  26.  
  27. char buf[256];  /* [TBD] should be 128?  on stack?*/
  28.  
  29. BYTE *PutDump(dest, nn)
  30. BYTE *dest;
  31. short nn;
  32. {
  33.   short 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)
  41. BYTE *dest;
  42. short nn, cc;
  43. {
  44.   PutByte(-(nn-1));
  45.   PutByte(cc);
  46.   return(dest);
  47. }
  48.  
  49. #define OutDump(nn)   dest = PutDump(dest, nn)
  50. #define OutRun(nn,cc) dest = PutRun(dest, nn, cc)
  51.  
  52. /*----------- PackRow --------------------------------------------------*/
  53. /* Given POINTERS TO POINTERS, packs one row, updating the source and
  54.    destination pointers.  RETURNs count of packed bytes.*/
  55.  
  56. short PackRow(pSource, pDest, rowSize)
  57. BYTE **pSource, **pDest;
  58. short rowSize;
  59. {
  60.   BYTE *source, *dest;
  61.   char c,lastc = '\0';
  62.   short mode = DUMP;
  63.   short nbuf = 0;               /* number of chars in buffer */
  64.   short rstart = 0;             /* buffer index current run starts */
  65.  
  66.   source = *pSource;
  67.   dest = *pDest;
  68.   putSize = 0;
  69.   buf[0] = lastc = c = GetByte();  /* so have valid lastc */
  70.   nbuf = 1;   rowSize--;        /* since one byte eaten.*/
  71.  
  72.   for (;  rowSize;  --rowSize)
  73.   {
  74.     buf[nbuf++] = c = GetByte();
  75.     switch (mode)
  76.     {
  77.       case DUMP:
  78.         /* If the buffer is full, write the length byte,
  79.            then the data */
  80.         if (nbuf>MaxDat)
  81.         {
  82.           OutDump(nbuf-1);
  83.           buf[0] = c;
  84.           nbuf = 1;
  85.           rstart = 0;
  86.           break;
  87.         }
  88.         if (c == lastc)
  89.         {
  90.           if (nbuf-rstart >= MinRun)
  91.           {
  92.             if (rstart > 0) OutDump(rstart);
  93.             mode = RUN;
  94.           }
  95.           else if (rstart == 0)
  96.           mode = RUN;   /* no dump in progress,
  97.                            so can't lose by making these 2 a run.*/
  98.         }
  99.         else  rstart = nbuf-1; /* first of run */
  100.         break;
  101.  
  102.       case RUN:
  103.         if ( (c != lastc)|| ( nbuf-rstart > MaxRun))
  104.         {
  105.           /* output run */
  106.           OutRun(nbuf-1-rstart,lastc);
  107.           buf[0] = c;
  108.           nbuf = 1;
  109.           rstart = 0;
  110.           mode = DUMP;
  111.         }
  112.         break;
  113.     }
  114.     lastc = c;
  115.   }
  116.  
  117.   switch (mode)
  118.   {
  119.     case DUMP:
  120.       OutDump(nbuf);
  121.       break;
  122.     case RUN:
  123.       OutRun(nbuf-rstart,lastc);
  124.       break;
  125.   }
  126.   *pSource = source;
  127.   *pDest = dest;
  128.   return(putSize);
  129. }
  130.