home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / CODECS.ZIP / codecs / english / codrle1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-13  |  6.1 KB  |  157 lines

  1. /* File: codrle1.c
  2.    Author: David Bourgin
  3.    Creation date: 28/1/94
  4.    Last update: 22/5/94
  5.    Purpose: Example of RLE type 1 encoding with a file source to compress.
  6. */
  7.  
  8. #include <stdio.h>
  9. /* For routines printf,fgetc,fputc and fwrite */
  10. #include <stdlib.h>
  11. /* For routine exit */
  12.  
  13. /* Error codes sent to the caller */
  14. #define NO_ERROR      0
  15. #define BAD_FILE_NAME 1
  16. #define BAD_ARGUMENT  2
  17.  
  18. /* Useful constants */
  19. #define FALSE 0
  20. #define TRUE  1
  21.  
  22. /* Global variables */
  23. FILE *source_file,*dest_file;
  24.  
  25.                              /* Being that fgetc=EOF only after an access
  26.                                 then 'byte_stored_status' is 'TRUE' if a byte has been stored by 'fgetc'
  27.                                 or 'FALSE' if there's no valid byte not already read and not handled in 'val_byte_stored' */
  28. int byte_stored_status=FALSE;
  29. int val_byte_stored;
  30.  
  31. /* Pseudo procedures */
  32. #define end_of_data()  (byte_stored_status?FALSE:!(byte_stored_status=((val_byte_stored=fgetc(source_file))!=EOF)))
  33. #define read_byte()  (byte_stored_status?byte_stored_status=FALSE,(unsigned char)val_byte_stored:(unsigned char)fgetc(source_file))
  34. #define write_byte(byte)  ((void)fputc((byte),dest_file))
  35. #define write_array(array,byte_nb_to_write)  ((void)fwrite((array),1,(byte_nb_to_write),dest_file))
  36.  
  37. void rle1encoding()
  38. /* Returned parameters: None
  39.    Action: Compresses with RLE type 1 method all bytes read by the function 'read_byte'
  40.    Errors: An input/output error could disturb the running of the program
  41. */
  42. { register unsigned char byte1,byte2,frame_size,
  43.                          array[129];
  44.  
  45.   if (!end_of_data())
  46.      { byte1=read_byte();    /* Is there at least a byte to analyze? */
  47.        frame_size=1;
  48.        if (!end_of_data())
  49.                              /* Are there at least two bytes to analyze? */
  50.           { byte2=read_byte();
  51.             frame_size=2;
  52.             do { if (byte1==byte2)
  53.                              /* Is there a repetition? */
  54.                     { while ((!end_of_data())&&(byte1==byte2)&&(frame_size<129))
  55.                             { byte2=read_byte();
  56.                               frame_size++;
  57.                             }
  58.                       if (byte1==byte2)
  59.                              /* Do we meet only a sequence of bytes? */
  60.                         { write_byte(126+frame_size);
  61.                           write_byte(byte1);
  62.                           if (!end_of_data())
  63.                              { byte1=read_byte();
  64.                                frame_size=1;
  65.                              }
  66.                           else frame_size=0;
  67.                         }
  68.                       else   /* No, then don't handle the last byte */
  69.                            { write_byte(125+frame_size);
  70.                              write_byte(byte1);
  71.                              byte1=byte2;
  72.                              frame_size=1;
  73.                            }
  74.                       if (!end_of_data())
  75.                          { byte2=read_byte();
  76.                            frame_size=2;
  77.                          }
  78.                     }
  79.                  else        /* Prepare the array of comparisons
  80.                                 where will be stored all the identical bytes */
  81.                       { *array = byte1;
  82.                         array[1]=byte2;
  83.                         while ((!end_of_data())&&(array[frame_size-2]!=array[frame_size-1])&&(frame_size<128))
  84.                               { array[frame_size]=read_byte();
  85.                                 frame_size++;
  86.                               }
  87.                         if (array[frame_size-2]==array[frame_size-1])
  88.                              /* Do we meet a sequence of all different bytes followed by identical byte? */
  89.                            { /* Yes, then don't count the two last bytes */
  90.                              write_byte(frame_size-3);
  91.                              write_array(array,frame_size-2);
  92.                              byte1=array[frame_size-2];
  93.                              byte2=byte1;
  94.                              frame_size=2;
  95.                            }
  96.                         else { write_byte(frame_size-1);
  97.                                write_array(array,frame_size);
  98.                                if (end_of_data())
  99.                                   frame_size=0;
  100.                                else { byte1=read_byte();
  101.                                       if (end_of_data())
  102.                                          frame_size=1;
  103.                                       else { byte2=read_byte();
  104.                                              frame_size=2;
  105.                                            }
  106.                                     }
  107.                              }
  108.                       }
  109.                }
  110.             while ((!end_of_data())||(frame_size>=2));
  111.           }
  112.        if (frame_size==1)
  113.           { write_byte(0);
  114.             write_byte(byte1);
  115.           }
  116.      }
  117. }
  118.  
  119. void help()
  120. /* Returned parameters: None
  121.    Action: Displays the help of the program and then stops its running
  122.    Errors: None
  123. */
  124. { printf("This utility enables you to compress a file by using RLE type 1 method\n");
  125.   printf("as given in 'La Video et Les Imprimantes sur PC'\n");
  126.   printf("\nUse: codrle1 source target\n");
  127.   printf("source: Name of the file to compress\n");
  128.   printf("target: Name of the compressed file\n");
  129. }
  130.  
  131. int main(argc,argv)
  132. /* Returned parameters: Returns an error code (0=None)
  133.    Action: Main procedure
  134.    Errors: Detected, handled and an error code is returned, if any
  135. */
  136. int argc;
  137. char *argv[];
  138. { if (argc!=3)
  139.      { help();
  140.        exit(BAD_ARGUMENT);
  141.      }
  142.   else if ((source_file=fopen(argv[1],"rb"))==NULL)
  143.           { help();
  144.             exit(BAD_FILE_NAME);
  145.           }
  146.        else if ((dest_file=fopen(argv[2],"wb"))==NULL)
  147.                { help();
  148.                  exit(BAD_FILE_NAME);
  149.                }
  150.             else { rle1encoding();
  151.                    fclose(source_file);
  152.                    fclose(dest_file);
  153.                  }
  154.   printf("Execution of codrle1 completed.\n");
  155.   return (NO_ERROR);
  156. }
  157.