In this project students will develop routines to encode and decode data for images using run-length encoding (RLE). Students will implement encoding and decoding of raw data, conversion between data and strings, and display of information by creating procedures that can be called from within their programs and externally. This project will give students practice with loops, strings, arrays, methods, and type-casting.
RLE is a form of lossless compression used in many industry applications, including imaging. It is intended to take advantage of datasets where elements (such as bytes or characters) are repeated several times in a row in certain types of data (such as pixel art in games). Black pixels often appear in long “runs” in some animation frames; instead of representing each black pixel individually, the color is recorded once, following by the number of instances.
|0 0||2 2 2||0 0 0 0 0 0||2 2|
|2 0||3 2||6 0||2 2||1 0_|
For example, consider the first row of pixels from the pixel image of a gator (shown in Figure 1). The color back is “0”, and green is “2”:
Flat (unencoded) data: 0_
Run-length encoded data: .
Figure 1 – Gator Pixel Image
The encoding for the entire image in RLE (in hexadecimal) – width, height, and pixels – is:
|2 0||3 2||6 0||2 2||2 0||1 2||1 F||1 0||7 2||1 A||F 2||1 0||9 2||3 0||1 2||1 0||3 2||6 0||3 2||3 0||8 2||5 0|
1 E |1 6
\W/ \H/ \——————————————PIXELS———————————————–/
In this project students will be encoding and decoding images (on the console) using this approach.
The images are stored in uncompressed / unencoded format natively. In addition, there are a few other rules to make the project more tractable:
- Images are stored as an array of bytes, with the first two bytes holding image width and height.
- Pixels will be represented by a number between 0 and 15 (representing 16 unique colors).
- No run may be longer than 15 pixels; if any pixel runs longer, it should be broken into a new run.
For example, the chubby smiley image (Figure 2) would contain the data shown in Figure 3.
Figure 2 Figure 3 – Data for “Chubby Smiley”
Student programs must present a menu when run in standalone mode and must also implement several methods, defined below, during this assignment.
Standalone Mode (Menu)
When run as the program driver via the main() method, the program should:
- Display welcome message
- Display color test (testRainbow)
- Display the menu
- Prompt for input
Note: for colors to properly display, it is highly recommended that student install the “CS1” theme on the project page.
There are five ways that should be provided to load data into the program and four ways the program must be able to display data to the user.
Loading a File
Accepts a filename from the user and invokes ConsoleGfx.loadFile():
Select a Menu Option: 1
Enter name of file to load: testfiles/uga.gfx
Loading the Test Image Loads ConsoleGfx.testImage:
Select a Menu Option: 2_
Test image data loaded._
Reading RLE String
Reads RLE data from the user in decimal notation with delimiters (smiley example):
Select a Menu Option: 3
Enter an RLE string to be decoded: 28:10:6B:10:10B:10:2B:10:12B:10:2B:10:5B:20:11B:10:6B:10
Reading RLE Hex String
Reads RLE data from the user in hexadecimal notation without delimiters (smiley example):
Select a Menu Option: 4
Enter the hex string holding RLE data: 28106B10AB102B10CB102B105B20BB106B10
Reading Flat Data Hex String
Reads raw (flat) data from the user in hexadecimal notation (smiley example):
Select a Menu Option: 5
Enter the hex string holding flat data: 880bbbbbb0bbbbbbbbbb0bb0bbbbbbbbbbbb0bb0bbbbb00bbbbbbbbbbb0bbbbbb0
Displaying the Image
Displays the current image by invoking the ConsoleGfx.displayImage() method.
Displaying the RLE String
Converts the current data into a human-readable RLE representation (with delimiters):
Select a Menu Option: 7
RLE representation: 28:10:6b:10:10b:10:2b:10:12b:10:2b:10:5b:20:11b:10:6b:10
Displaying the RLE Hex Data
Converts the current data into a RLE hexadecimal representation (without delimiters):
Select a Menu Option: 8
RLE hex values: 28106b10ab102b10cb102b105b20bb106b10
Displaying the Flat Hex Data
Displays the current raw (flat) data in hexadecimal representation (without delimiters):
Select a Menu Option: 9
Flat hex values: 880bbbbbb0bbbbbbbbbb0bb0bbbbbbbbbbbb0bb0bbbbb00bbbbbbbbbbb0bbbbbb0
Student classes are required to provide the following methods with the defined behaviors.
public static int countRuns(byte flatData)
Returns the number of runs of data in an image data set. This can be used to prepare the RLE representation of a set of data in binary or string-based format.
public static int getDecodedLength(byte rleData)
Returns the total size of a set of data in bytes once the given RLE data is decompressed. This can be used to decode RLE data into individual data points (such as pixels).
public static byte encodeRle(byte flatData)
Returns an encoding (in RLE) of the raw data passed in. This is used to generate the RLE representation of a data set for later viewing or storage.
public static byte decodeRle(byte rleData)
Returns the decoded data set from RLE encoded data. This is used to decompress RLE data for use.
public static String toRleString (byte rleData)
Translates RLE data into a human-readable representation (with delimiters – see examples in standalone section.)
public static String toHexString (byte data)
Translates data (RLE or raw) a hexadecimal string (without delimiters – see examples in standalone section.)
public static byte stringToRle(String rleString)
Translates a string in human-readable RLE format (with delimiters) into RLE byte data.
public static byte stringToData(String dataString)
Translates a string in hexadecimal format into byte data (can be raw or RLE).
NOTE: Your output must match the example output *exactly*. If it does not, you will not receive full credit for your submission!
Method: Submit on ZyLabs
Do not submit any other files!