home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / COMPRESS / RLE16_SC.ZIP / RLE16.C next >
Encoding:
C/C++ Source or Header  |  1991-07-14  |  10.2 KB  |  278 lines

  1. /* Run Length Encoding compression program.
  2.  
  3.  * max sequence is 32767 bytes of either repeating
  4.  
  5.  * or non-repeating stuff.
  6.  
  7.  * (uses 16 bit headers.)
  8.  
  9.  *
  10.  
  11.  * This program and its source are strictly public domain
  12.  
  13.  *
  14.  
  15.  * Written by Shaun Case, 1991, Borland C++ 2.0
  16.  
  17.  *
  18.  
  19.  * doesn't do error checking on fputc()
  20.  
  21.  */
  22.  
  23.  
  24.  
  25.  
  26.  
  27. #include <stdio.h>
  28.  
  29. #include <dir.h>
  30.  
  31. #include <string.h>
  32.  
  33. #include "rle16.h"
  34.  
  35.  
  36.  
  37. int main(int argc, char **argv)
  38.  
  39. {
  40.  
  41.     register int cur_char;                /* a character                    */
  42.  
  43.     register unsigned int i;              /* generic index variable         */
  44.  
  45.     register unsigned short run_len = 0;  /* length of character run so far */
  46.  
  47.     int run_char;                         /* which char run is of           */
  48.  
  49.     unsigned int j;                       /* another index variable         */
  50.  
  51.     unsigned short seq_len=0;             /* length of non-run sequence     */
  52.  
  53.  
  54.  
  55.     char seq[MAX_LEN];                    /* buffer for uncompressible data */
  56.  
  57.     char scratch_space[256];              /* string scratch space           */
  58.  
  59.  
  60.  
  61.     char orig_name[MAXFILE+MAXEXT];       /* original filename      */
  62.  
  63.     char drive[MAXDRIVE],                 /* needed for fnsplit()   */
  64.  
  65.          dir[MAXDIR],                     /* needed for fnsplit()   */
  66.  
  67.          filename[MAXFILE],               /* 8 chars, orig name     */
  68.  
  69.          ext[MAXEXT];                     /* 3 chars, orig ext      */
  70.  
  71.     char outfile_name[MAXFILE+MAXEXT];    /* output filename        */
  72.  
  73.  
  74.  
  75.     unsigned long  orig_total;              /* total number of unencoded bytes          */
  76.  
  77.     unsigned long  rle_total;               /* total number of encoded bytes            */
  78.  
  79.     FILE *infile;                           /* file ptr to input file (uncompressed)    */
  80.  
  81.     FILE *outfile;                          /* file ptr to output file (compressed)     */
  82.  
  83.     char *infile_name;
  84.  
  85.  
  86.  
  87.     fnsplit(argv[1], drive, dir, filename, ext);    /* get filename    */
  88.  
  89.     strcpy(orig_name, filename);
  90.  
  91.     strcat(orig_name, ext);
  92.  
  93.     strcpy(outfile_name, filename);       /* build output filename  */
  94.  
  95.     strcat(outfile_name, ".r16");
  96.  
  97.  
  98.  
  99.     if (argc != 2)
  100.  
  101.     {
  102.  
  103.         puts("Usage: RLE16 filename.ext compresses file to filename.r16");
  104.  
  105.         return 1;
  106.  
  107.     }
  108.  
  109.  
  110.  
  111.     puts("rle16  by Shaun Case 1991  public domain.");
  112.  
  113.  
  114.  
  115.     infile_name=argv[1];
  116.  
  117.  
  118.  
  119.     if ((infile=fopen(infile_name, "rb")) == NULL)
  120.  
  121.     {
  122.  
  123.         strcpy(scratch_space, "Uable to open ");
  124.  
  125.         strcat(scratch_space, infile_name);
  126.  
  127.         puts(scratch_space);
  128.  
  129.         return 1;
  130.  
  131.     }
  132.  
  133.     
  134.  
  135.     if ((outfile=fopen(outfile_name, "wb")) == NULL)
  136.  
  137.     {
  138.  
  139.         strcpy(scratch_space, "Uable to open ");
  140.  
  141.         strcat(scratch_space, outfile_name);
  142.  
  143.         puts(scratch_space);
  144.  
  145.         return 1;
  146.  
  147.     }
  148.  
  149.  
  150.  
  151.     for (i = 0; i < 13; i++)            /* write original filename */
  152.  
  153.         fputc(orig_name[i], outfile);
  154.  
  155.  
  156.  
  157.     while (!feof(infile))
  158.  
  159.     {
  160.  
  161.         cur_char = fgetc(infile);
  162.  
  163.  
  164.  
  165.         if (feof(infile))
  166.  
  167.             continue;
  168.  
  169.  
  170.  
  171.  
  172.  
  173.         if (seq_len ==0)                /* haven't got a sequence yet   */
  174.  
  175.         {
  176.  
  177.             if (run_len == 0)           /* start a new run              */
  178.  
  179.             {
  180.  
  181.                 run_char = cur_char;
  182.  
  183.                 ++run_len;
  184.  
  185.                 continue;
  186.  
  187.             }
  188.  
  189.  
  190.  
  191.             if (run_char == cur_char)   /* got another char in the run  */
  192.  
  193.                 if (++run_len == MAX_LEN)
  194.  
  195.                 {
  196.  
  197.                     fputc((int)MAX_RUN_HEADER & (0xff), outfile);   /* write lo byte   */
  198.  
  199.                     fputc((int)MAX_RUN_HEADER >> 8, outfile);       /* write high byte */
  200.  
  201.                     fputc((int) run_char, outfile);
  202.  
  203.  
  204.  
  205.                     run_len = 0;
  206.  
  207.                     continue;
  208.  
  209.                 }
  210.  
  211.  
  212.  
  213.                                    /* got a different character     */
  214.  
  215.                                    /* than the run we were building */
  216.  
  217.             if (run_len > 3)       /* so write out the run and      */
  218.  
  219.                                    /* start a new one of the new    */
  220.  
  221.                                    /* character.                    */
  222.  
  223.             {
  224.  
  225.                 fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  226.  
  227.                 fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  228.  
  229.                 fputc((int)run_char, outfile);
  230.  
  231.  
  232.  
  233.                 run_len = 1;
  234.  
  235.                 run_char   = cur_char;
  236.  
  237.                 continue;
  238.  
  239.             }
  240.  
  241.  
  242.  
  243.             /* run was only one or two chars, make a seq out of it instead       */
  244.  
  245.  
  246.  
  247.             for (j = 0; j < run_len; j++);    /* copy 1 or 2 char run to seq[]   */
  248.  
  249.             {
  250.  
  251.                 seq[seq_len] = run_char;
  252.  
  253.                 ++seq_len;
  254.  
  255.                 if (seq_len == MAX_LEN)       /* if seq[] is full, write to disk */
  256.  
  257.                 {
  258.  
  259.                     fputc((int)MAX_SEQ_HEADER & 0xff, outfile);   /* lo byte */
  260.  
  261.                     fputc((int)(MAX_SEQ_HEADER >> 8) , outfile);   /* lo byte */
  262.  
  263.  
  264.  
  265.                     for (i = 0; i < seq_len; i++)
  266.  
  267.                         fputc((int)seq[i], outfile);
  268.  
  269.  
  270.  
  271.                     seq_len = 0;
  272.  
  273.                 }
  274.  
  275.             }
  276.  
  277.  
  278.  
  279.             run_len = 0;
  280.  
  281.  
  282.  
  283.             seq[seq_len++] = cur_char;
  284.  
  285.             if (seq_len == MAX_LEN)        /* if seq[] is full, write to disk */
  286.  
  287.             {
  288.  
  289.                 fputc((int)MAX_SEQ_HEADER & 0xff, outfile);   /* lo byte */
  290.  
  291.                 fputc((int)(MAX_SEQ_HEADER >> 8) , outfile);   /* lo byte */
  292.  
  293.  
  294.  
  295.                 for (i = 0; i < seq_len; i++)
  296.  
  297.                     fputc((int)seq[i], outfile);
  298.  
  299.  
  300.  
  301.                 seq_len = 0;
  302.  
  303.             }
  304.  
  305.         }
  306.  
  307.         else    /* a sequence exists */
  308.  
  309.         {
  310.  
  311.             if (run_len != 0)           /* if a run exists */
  312.  
  313.             {
  314.  
  315.                 if (cur_char == run_char )  /* add to run!  Yay.  */
  316.  
  317.                 {
  318.  
  319.                     ++run_len;
  320.  
  321.                     if (run_len == MAX_LEN)  /* if run is full */
  322.  
  323.                     {
  324.  
  325.                         /* write sequence that precedes run */
  326.  
  327.  
  328.  
  329.                         fputc((int)seq_len & 0xff, outfile);          /* lo byte */
  330.  
  331.                         fputc((int)((SEQ | seq_len) >> 8), outfile);   /* lo byte */
  332.  
  333.  
  334.  
  335.                         for (i = 0; i < seq_len; i++)
  336.  
  337.                             fputc((int)seq[i], outfile);
  338.  
  339.  
  340.  
  341.  
  342.  
  343.                         /* write run                        */
  344.  
  345.  
  346.  
  347.                         fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  348.  
  349.                         fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  350.  
  351.                 
  352.  
  353.                         fputc((int)run_char, outfile);
  354.  
  355.  
  356.  
  357.                         /* and start out fresh              */
  358.  
  359.                         seq_len = run_len = 0;
  360.  
  361.  
  362.  
  363.                     }  /* end write full run with existing sequence */
  364.  
  365.  
  366.  
  367.                     continue;
  368.  
  369.  
  370.  
  371.                 }  /* end add to run for sequence exists */
  372.  
  373.  
  374.  
  375.                 /* we couldn't add to the run, and a preceding sequence */
  376.  
  377.                 /* exists, so write the sequence and the run, and       */
  378.  
  379.                 /* try starting a new run with the current character.   */
  380.  
  381.  
  382.  
  383.                 /* write sequence that precedes run */
  384.  
  385.  
  386.  
  387.                 fputc((int)seq_len & 0xff, outfile);          /* lo byte */
  388.  
  389.                 fputc((int)((SEQ | seq_len) >> 8), outfile);   /* hi byte */
  390.  
  391.  
  392.  
  393.                 for (i = 0; i < seq_len; i++)
  394.  
  395.                     fputc((int)seq[i], outfile);
  396.  
  397.  
  398.  
  399.  
  400.  
  401.                 /* write run                        */
  402.  
  403.  
  404.  
  405.                 fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  406.  
  407.                 fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  408.  
  409.  
  410.  
  411.                 fputc((int)run_char, outfile);
  412.  
  413.  
  414.  
  415.                 /* and start a new run w/ cur_char  */
  416.  
  417.                 seq_len = 0;
  418.  
  419.                 run_len = 1;
  420.  
  421.                 run_char = cur_char;
  422.  
  423.  
  424.  
  425.                 continue;
  426.  
  427.  
  428.  
  429.             }    /* end can't add to existing run, and preceding seq exists */
  430.  
  431.  
  432.  
  433.             /* no run exists, but a sequences does.  Try to create a run    */
  434.  
  435.             /* by looking at cur_char and the last char of the sequence.    */
  436.  
  437.             /* if that fails, add the char to the sequence.                 */
  438.  
  439.             /* if the sequence is full, write it to disk.  (Slightly non    */
  440.  
  441.             /* optimal; we could wait one more char.  A small thing to fix  */
  442.  
  443.             /* if someone gets the urge...                                  */
  444.  
  445.  
  446.  
  447.             if (seq[seq_len - 1] == cur_char)       /* if we can make a run */
  448.  
  449.             {
  450.  
  451.                 run_char = cur_char;
  452.  
  453.                 run_len = 2;
  454.  
  455.                 --seq_len;
  456.  
  457.                 continue;
  458.  
  459.             }
  460.  
  461.  
  462.  
  463.             /* couldn't make a run, add char to seq.  Maybe next time       */
  464.  
  465.             /* around...                                                    */
  466.  
  467.  
  468.  
  469.             seq[seq_len++] = cur_char;
  470.  
  471.  
  472.  
  473.             if (seq_len == MAX_LEN) /* if the sequence is full, write out   */
  474.  
  475.             {
  476.  
  477.                 fputc((int)MAX_SEQ_HEADER & 0xff, outfile);   /* lo byte */
  478.  
  479.                 fputc((int)(MAX_SEQ_HEADER >> 8) , outfile);   /* lo byte */
  480.  
  481.  
  482.  
  483.                 for (i = 0; i < MAX_LEN; i++)
  484.  
  485.                     fputc((int)seq[i], outfile);
  486.  
  487.  
  488.  
  489.                 seq_len = 0;
  490.  
  491.             }
  492.  
  493.  
  494.  
  495.         }  /* end branch on sequence exists */
  496.  
  497.  
  498.  
  499.     } /* done with whole file */
  500.  
  501.  
  502.  
  503.     /* there may be stuff left that hasn't been written yet; if so, write it */
  504.  
  505.  
  506.  
  507.     if (seq_len != 0)  /* write sequence that precedes run */
  508.  
  509.     {
  510.  
  511.         fputc((int)seq_len & 0xff, outfile);          /* lo byte */
  512.  
  513.         fputc((int)((SEQ | seq_len) >> 8), outfile);   /* hi byte */
  514.  
  515.  
  516.  
  517.         for (i = 0; i < seq_len; i++)
  518.  
  519.             fputc((int)seq[i], outfile);
  520.  
  521.     }
  522.  
  523.  
  524.  
  525.     if (run_len != 0)  /* write run */
  526.  
  527.     {
  528.  
  529.         fputc((int)(run_len & 0xff), outfile);      /* lo byte */
  530.  
  531.         fputc((int)(RUN | run_len) >> 8, outfile);  /* hi byte */
  532.  
  533.  
  534.  
  535.         fputc((int)run_char, outfile);
  536.  
  537.     }
  538.  
  539.  
  540.  
  541.     fclose(infile);
  542.  
  543.     fclose (outfile);
  544.  
  545.  
  546.  
  547.     return 0;
  548.  
  549.  
  550.  
  551. }  /* end main() */
  552.  
  553.  
  554.  
  555.