Lab 5 Structured Data Solution



5.1 Assignment

In digital systems, an image is usually stored as a 2-dimensional array of Picture Elements (pixels). Each pixel is stored as a data structure which specifies the color for one small region in the image image. The most common way to specify the color of each pixel is to provide values for the red, green, and blue components of the color. Eight bits are usually adequate for each component, so a pixel could be specified in C as follows:




typedef struct{

unsigned char red, green, blue;


Given that definition, color image data can be stored as a two-dimensional (hei ght£width) array, but it is often more efficient to structure the image as an array of rows, where each row is a pointer to a one-dimensional array of pixels. Also, it is useful to create a data structure containing a pointer to the array of rows, along with other information about the image, such as the width and height. This arrangement is shown in Figure 5.1, and the following listing shows the corresponding C data structure:

  • typedef struct{

  • rgbpixel **rows; /* pointer to array of pointers */







rows (length of

array of pointers) */







columns (length

of each row) */



For a grayscale image, each pixel is an unsigned char, rather than an rgbpixel.

There are many file formats for storing digital images. Netpbm is a very simple and widely used format which is particularly useful as an intermediate format when converting between other




Figure 5.1: Data structure for storing an image in memory.



formats. Netpbm supports color, grayscale, or bitmap images stored in either ASCII or binary. This results in three formats for the data structure in memory, and six possible file formats.

For this laboratory assignment, you will be given a program, written in C, which converts a color Netpbm file to grayscale. The goal is to write an assembly language version of the function which does the actual conversion of the image. For each pixel, the color must be converted to a single number giving a grayscale value. The luminosity method performs the conversion using a weighted average. Human eyes are more sensitive to green than other colors, so green is weighted most heavily. Humans are much less sensitive to colors in the blue spectrum, so that component is weighted least heavily. The formula for luminosity is gra y ˘ 0.21r ¯0.72g ¯0.07b where gra y is the grayscale value, and r, g, and b are the color components of the pixel. The trick is to perform the calculation using only integer mathematics. Applying a little algebra:

54r ¯184g ¯18b

gra y ˘ 0.21r ¯0.72g ¯0.07b ˘ .

Since a division by 256 is the same as shifting right by 8 bits, the entire calculation can be performed with three multiplications, two additions, and a shift.

5.2 Instructions

  1. Download structured.tgz from the site provided by your instructor and extract the files:

1 lpyeatt@rp01 $ tar xfz structured.tgz

  1. Edit colortogray.S and finish the function color_to_gray. Refer to the file named netpbm.h and the C source files for additional information.