CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

Bitmap Graphics

Summary
You display and manipulate numbers in an array as bitmap images
Objectives
  • Reinforce knowledge of arrays
  • Practice using formatted input
  • Utilize function pointers
  • Apply bitwise operations for fun and profit
  • Consider problem decomposition

Introduction

You have learned to represent color images as 2D arrays of red, green, and blue values. You also know numbers are just bits. In this assignment, we scale down our notion of an image by scaling up our notion of numbers to create, manipulate, and display a "picture" from the bits within an array of numbers.

Bitmap LCD Display
Credit: Arduino Labs (CC-BY)

A common example of this type of image is in simple LCD displays, such as the one shown at right. Such displays often accompany resource-constrained systems. Because C has no built-in "bit" type, we will have to be a little more creative to avoid wasting space with a 2D array of integers (masquerading as bools), each of which may be holding a single bit with space for 8, 16, or likely even 32 bits.

You will store an image as a 1D array of numbers, where each index holds an entire image row. The number in each row is an unsigned integer whose bits each represent a single pixel that may be "on" or "off." To maximize our resolution, you will store each row of the image as a single unsigned long, which is typically 8 bytes (or 64 bits). However, your program should use C's sizeof operator to programmatically determine this "width" and use the same value as the height (which is the array length).

As an example, the number 1150669964571590624 might be rendered as

----*********-------------****---*****------------*********-----

where each 0 in the binary version of the number has been replaced with a "-" and each 1 has been replaced with a "*". As a simpler example, the number 7L is represented as 61 zeros followed by 3 ones. Note that the L in the number constant 7L forces the compiler to interpret the integer as long, rather than the standard width.

If the underlying data type for each row was unsigned short, which is just two bytes wide, the number 4242 = 212 + 27 + 24 + 21 might be rendered as follows.

---*----*--*--*-

Note that we have made the somewhat arbitrary choice to render the "on" bits in the order we typically think of them being in memory (from higher-order to lower-order), which means the "index" to the column is decreasing from left-to-right.

Requirements

Your program should repeatedly issue a command prompt supporting the following operations:

Entering the image should consist of reading and storing all the rows as unsigned long integers (rather than a sequence of bits). That is, one should enter 4242 rather than 0001000010010010 for a 16-bit image width.

The print operation is perhaps best visualized using a space for an "off" bit and an asterisk for an "on" bit).

To utilize good program decomposition and code reuse through abstraction, your program should use a higher-order procedure, which accepts a pointer to a function that transforms a single row (i.e., an unsigned long). This higher-order procedure can then handle requests to shift all bits left or right and complement the image, given an appropriate row transformer function pointer.

You should use single-character commands, with interpretation supported by case statements.

Your program should use no global variables.

Your print function should not extract and store the bits for each row in an intermediate array. Instead, print each bit's representation directly using bitwise operations to extract the value (on or off) of each bit in turn.

Tips

Grading

In addition to the general grading guidelines for evaluation, the assignment is worth 30 points.

A five point penalty will apply to any submission that includes personally identifying information anywhere other than the references/honesty file.

Global variables may not be used anywhere; use of any global variable will result in a grade of zero. Instead, use parameters to pass values into functions.