User Tools

Site Tools


Sidebar

projects

  • dsi0 (due 20160831)
  • pnc0 (due 20160831)
  • wpf0 (due 20160831)
  • pnc1 (due 20160907)
  • wpf1 (due 20160907)
  • pnc2 (due 20160914)
  • wpf2 (due 20160914)
  • wpf3 (due 20160921)
  • dcf0 (due 20160921)
  • wpf4 (due 20160928)
  • dcf1 (due 20160928)
  • wpf5 (due 20161005)
  • dcf2 (due 20161019)
  • wpf6 (due 20161019)
  • wpf7 (due 20161026)
  • nbm0 (due 20161102)
  • wpf8 (due 20161102)
  • yol0 (due 20161111)
  • wpf9 (due 20161109)
  • smh0 (due 20161116)
  • wpfA (due 20161116)
  • wpfB (due 20161130)
  • wpfC (due 20161207) – bonus
  • wpfD (due 20161207) – bonus
  • EoCE (bottom of your journal) (due 20161216-105959)
haas:fall2016:discrete:projects:dcf0

Corning Community College

CSCS2330 Discrete Structures

~~TOC~~

Project: RUN-LENGTH ENCODING - DATA COMPRESSION FUN (dcf0)

Objective

To apply your skills in implementing an encoding scheme that, in ideal circumstances, will lead to a smaller storage footprint.

Encoding

A Google search defines encoding as:

  • general: convert into a coded form.
  • COMPUTING-centric: convert (information or an instruction) into a particular form.

When we program, we encode ideas into syntax.

When you compile source code, the compiler encodes your coding syntax into machine code.

When we lay out a pattern, we encode particular aspects of a process.

Encoding can be as simple as a 1:1 translation (such as shifting letters around to encipher a message), but it can also take other forms.

Compression

Compression is a certain form of encoding, one where we focus on taking something (typically data, bits, bandwidth, instructions) and encode it in a way where it ends up requiring less storage space than when we began.

According to Wikipedia, data compression is: the process of encoding digital information using fewer bits.

Another aspect to consider is whether the act of encoding data results in a reversible pathway or not. Sometimes the process of encoding can eliminate seemingly unnecessary data, aiding in the task of taking up less space. When data is trimmed in this manner, we say the process is lossy; when all data is preserved, it is lossless.

For more information on lossy and lossless data compression:

  • lossless - no data is lost as a part of the compression process
  • lossy - unnecessary data is discarded as part of the compression process

Wikipedia has categories identifying various algorithms implemented for both lossless and lossy compression algorithms.

This week's algorithm: RLE

Our algorithm of implementation this week is a relatively simple and straightforward one known as Run-Length Encoding (RLE for short).

Sometimes associated with certain image formats, RLE is an algorithm that, when matched with appropriately patterned data, can yield some space savings.

However, like other special-purpose algorithms, it its worst cases it can actually balloon the storage footprint of your data.

To demonstrate what RLE does, let us look at the following data:

aaaabcccdddddefghhhhhhhhhhhh (28 bytes total)

Notice how there are sequences of repeating letters? Turns out in some patterns of data, that can be quite the frequent occurrence (for example, images with large swaths of the same color).

RLE adds a sort of narrative to the data, sort of like what we might do if we were describing that string:

  1. there are four a's
  2. one b
  3. three c's
  4. five d's
  5. one e
  6. one f
  7. one g
  8. twelve h's

Or, to concisely represent it:

4a1b3c5d1e1f1g12h (17 bytes total)

By applying that “narrative” to the data, we were able to both preserve the integrity of the data, while shrinking its storage footprint.

And in that case, we got some pretty decent gains:

  • 28 bytes in
  • 17 bytes out
    • 17 is ~60% of 28
    • so we were able to compress that data by about 40%. Not bad.

However, the data must be appropriately repetitive… if not, it can grow in size. Take this example:

abcdefgh (8 bytes)

Applying our same narrative:

  1. one a
  2. one b
  3. one c
  4. one h

Or, concisely:

1a1b1c1d1e1f1g1h (16 bytes)

In that case, we not only didn't shrink, but we grew to twice our original size.

When RLE works, it can work pretty well, but when the data isn't conducive, it really doesn't help out that much at all.

dcfX RLE v1 specification

You'll be writing an encode and a decode program implementing RLE, in accordance with these published specifications (this way, any one can take an RL-encoded file from someone else and decode it with their's, and vice versa).

Every RL-encoded file will start with the following 12-byte header:

  • byte 0: 0x64
  • byte 1: 0x63
  • byte 2: 0x66
  • byte 3: 0x58
  • byte 4: 0x20
  • byte 5: 0x52
  • byte 6: 0x4c
  • byte 7: 0x45
  • byte 8: 0x00 (reserved)
  • byte 9: 0x01 (version of our RLE specification)
  • byte 10: 1 byte for stride value (will be a value of 1 for time being)
  • byte 11: 1 byte for source file name's length (doesn't include NULL terminator)
  • bytes 12 through length-1 indicated in byte 11: ASCII string of original filename to write. This will also be the name of the file decode creates, leaving the RL-encoded file intact.

Following this we will have a repeating sequence of count and value fields, continuing until the end of the file.

For example, encoding our 28-byte example from above we'd get:

04 61 01 62 03 63 05 64 01 65 01 66 01 67 0c 68

Or, if looking at the ENTIRE encoded file, with header, where the source file was named 'sample.txt' and has a newline character at the end (I've wrapped it at 16 bytes per line so it better fits on the page without a horizontal scroll):

64 63 66 58 20 52 4c 45 00 01 01 0a 73 61 6d 70
6c 65 2e 74 78 74 04 61 01 62 03 63 05 64 01 65
01 66 01 67 0c 68 01 0a

Now, in this case, with full headers, our original 28-byte (29, counting the newline) example would actually end up at 40 bytes, but that sample is rather small. A repetitive file originally 40-ish bytes, and of course larger, would start to yield space savings.

Program

It is your task to write an encoder and decoder for this specification of the dcfX RLE format:

  1. encode.c: read in source data, encode according to specifications
  2. decode.c: read in RL-encoded data, decode to produce original data

Your program should:

  • obtain 1 parameter from the command-line (see command-line arguments section below):
    • argv[1]: name of source file
    • this should be a file that exists, but you should do appropriate error checking and bail out if the file cannot be accessed
    • for encode, the output file will be the filename specified in argv[1] will an “.rle” suffixed to the end. This adds a certain universal aspect to how we'll go about naming things (tar and gzip do this too).
    • for decode, this should be an RL-encoded file.
      • the output file name will be obtained from parsing the file's header data.
      • be sure to perform appropriate error checking and bail out as needed.
  • implement the specified algorithm in both encoding and decoding forms.
    • please be sure to test it against varying types of data, to make sure it works no matter what you throw at it.
  • calculate and display some statistics gleaned during the performance of the process.
    • for example, encode should display information on:
      • how many bytes read in
      • how many bytes written out
      • compression rate
    • decode should also display:
      • RLE header information
      • filename information
      • how many bytes read in
      • how many bytes written out
      • decompression rate
    • see the sample program outputs below
  • display errors to STDERR
  • display run-time information to STDOUT
  • your RL-encoded data MUST be conformant to the project specifications described above.
    • you should be able to encode/decode a set of data with 100% retrieval rate. No data should be lost.
  • decode should validate the header information (is it encoded in version 1? if not, complain to STDERR of “version mismatch!” and exit).
    • if the first 8 bytes of the header do not check out, error out with an “invalid data format detected! aborting process…” message to STDERR.
    • if the file only contains a header (and no encoded data), report to STDERR “empty data segment” and exit.

Grabit Integration

For those familiar with the grabit tool on lab46, I have made some skeleton files and a custom Makefile available for this project.

To “grab” it:

lab46:~/src/discrete$ grabit discrete dcf0
make: Entering directory '/var/public/fall2016/discrete/dcf0'

‘/var/public/fall2016/discrete/dcf0/Makefile’ -> ‘/home/USERNAME/src/discrete/dcf0/Makefile’
‘/var/public/fall2016/discrete/dcf0/encode.c’ -> ‘/home/USERNAME/src/discrete/dcf0/encode.c’
‘/var/public/fall2016/discrete/dcf0/decode.c’ -> ‘/home/USERNAME/src/discrete/dcf0/decode.c’
‘/var/public/fall2016/discrete/dcf0/data/sample0.txt’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample0.txt’
‘/var/public/fall2016/discrete/dcf0/data/sample1.txt’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample1.txt’
‘/var/public/fall2016/discrete/dcf0/data/sample2.bmp’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample2.bmp’
‘/var/public/fall2016/discrete/dcf0/data/sample3.wav’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample3.wav’
‘/var/public/fall2016/discrete/dcf0/data/sample4.bmp.rle’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample4.bmp.rle’
‘/var/public/fall2016/discrete/dcf0/data/sample5.txt.rle’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample5.txt.rle’
‘/var/public/fall2016/discrete/dcf0/data/sample6.mp3.rle’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample6.mp3.rle’
‘/var/public/fall2016/discrete/dcf0/data/sample7.txt.rle’ -> ‘/home/USERNAME/src/discrete/dcf0/data/sample7.txt.rle’

make: Leaving directory '/var/public/fall2016/discrete/dcf0'
lab46:~/src/discrete$ cd dcf0
lab46:~/src/discrete/dcf0 $ ls
Makefile          data            decode.c            encode.c
lab46:~/src/discrete/dcf0$ 

Just another “nice thing” we deserve.

NOTE: You do NOT want to do this on a populated dcf0 project directory– it will overwrite files. Only do this on an empty directory.

Makefile fun

With the Makefile, we have your basic compile and clean-up operations:

  • make: compile everything
  • make debug: compile everything with debug support
  • make clean: remove all binaries
  • make getdata: re-obtain a fresh copy of project data files
  • make save: make a backup of your project
  • make submit: submit project (uses submit tool)

Command-Line Arguments

setting up main()

To accept (or rather, to gain access) to arguments given to your program at runtime, we need to specify two parameters to the main() function. While the names don't matter, the types do.. I like the traditional argc and argv names, although it is also common to see them abbreviated as ac and av.

Please declare your main() function as follows:

int main(int argc, char **argv)

The arguments are accessible via the argv array, in the order they were specified:

  • argv[0]: program invocation (path + program name)
  • argv[1]: our input file

Simple argument checks

Although I'm not going to require extensive argument parsing or checking for this project, we should check to see if the minimal number of arguments has been provided:

    if (argc < 2)  // if less than 2 arguments have been provided
    {
        fprintf(stderr, "Not enough arguments!\n");
        exit(1);
    }

Execution

Your program output should be as follows (given the specific input):

Encode

lab46:~/src/discrete/dcf0$ ./encode data/sample0.txt 
input name length: 16 bytes
   input filename: data/sample0.txt
  output filename: data/sample0.txt.rle
     stride value: 1 byte
          read in: 82 bytes
        wrote out: 64 bytes
 compression rate: 21.95%
lab46:~/src/discrete/dcf0$ 

Decode

lab46:~/src/discrete/dcf0$ ./decode data/sample0.txt.rle 
    input filename: data/sample0.txt.rle
output name length: 16 bytes
   output filename: data/sample0.txt
       header text: dcfX RLE v1
      stride value: 1 byte
           read in: 64 bytes
         wrote out: 82 bytes
    inflation rate: 21.95%
lab46:~/src/discrete/dcf0$ 

A good way to test that both encode and decode are working is to encode data then immediately turn around and decode that same data. If the decoded file is in the same state as the original, pre-encoded file, you know things are working.

Check Results

If you'd like to verify your implementations,

Submission

To successfully complete this project, the following criteria must be met:

  • Code must compile cleanly (no warnings or errors)
  • Output must be correct, and match the form given in the sample output above.
  • Code must be nicely and consistently indented (you may use the indent tool)
  • Code must implement the algorithm(s) presented above.
    • encode.c
    • decode.c
  • indicated error conditions are identified and reported, along with expected program behavior
  • Code must be commented
    • comments must be meaningful and descriptive of the process (tell me why you're doing what you're doing)
    • have a properly filled-out comment banner at the top
      • be sure to include any compiling instructions
  • Track/version the source code in a repository
  • Submit a copy of your source code to me using the submit tool.

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

lab46:~/src/discrete/dcf0$ make submit
removed 'decode'
removed 'encode'
removed 'errors'

Project backup process commencing

Taking snapshot of current project (dcf0)      ... OK
Compressing snapshot of dcf0 project archive   ... OK
Setting secure permissions on dcf0 archive     ... OK

Project backup process complete

Submitting discrete project "dcf0":
    -> ../dcf0-DATESTRING-HOUR.tar.gz(OK) 

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

haas/fall2016/discrete/projects/dcf0.txt · Last modified: 2016/09/14 13:40 by wedge