August 2000
CA208 : 'C' PROGRAMMING

QUESTION 4

Total Marks: 15 Marks

Click here to access other questions

SUGGESTED SOLUTIONS
Solutions and allocated marks are indicated in green.
Return to
Question 4

Modern computer monitors are capable of displaying over 16 million colours. Each pixel is represented using three bytes - one byte for each of the colours red, green and blue. Consider the following data type for a byte:
typedef unsigned char BYTE;

(a) Define a data structure, called Pixel, which contains variables which represent each of the three colours r, g and b. [3 marks]
(a) A sample definition follows:
typedef struct {
  BYTE b, g, r;
} RGB;
And the following marking scheme should be used:
• Including members of type BYTE; (1 mark)
• Correct application of typedef; (1 mark)
• Correctly designing the struct with name; (1 mark)
[3 marks]

(b) Implement a procedure, called Colour2Grey, the signature of which is given below, which takes a reference to a Pixel and sets the value of the pixel to grey. Conversion to grey is achieved by assigning to each of the three colours the average of the three colours.
void Colour2Grey(Pixel* p); [3 marks]
(b) A sample definition of Colour2Grey follows:
void Colour2Grey(RGB* pixel) {
  pixel->r = pixel->g = pixel->b=
  (pixel->r + pixel->g + pixel->b)/3;
}
And the following marking scheme should be used:
• Finding the average value; (1 mark)
• Correct pointer dereferencing; (1 mark)
• Correctly assigning the average value; (1 mark)
[3 marks]

(c) Implement a procedure, called Image2Grey, the signature of which is given below, which takes in a pointer to a rectangular shaped image of width pixels wide, and height pixels high, and converts each pixel to grey.
void Image2Grey(Pixel* img, int width, int height); [4 marks]
(c) A sample definition of greyImage follows:
void greyImage(RGB* img, int width, int height) {
  long int a;
  for(a= 0; a< height*width; a++)
  Colour2Grey(img+c);
}
And the following marking scheme should be used:
• Declaring a to be a long int; (1 mark)
• Initialising and incrementing the counter correctly; (1 mark)
• Terminating the loop correctly; (1 mark)
• Calling Colour2Grey with the correct parameter; (1 mark)

(d) Implement a procedure, called FlipImage, the signature of which is given below, which takes in an image represented as a 2-dimensional array. The procedure should ip the image about the vertical axis, such that the left+n pixel is interchanged with the right-n pixel.
void FlipImage(Pixel* img[height][width]); [5 marks]
(d) A sample definition of ipImage follows:
void FlipImage(RGB img[height][width]) {
  int w, h, mid=width/2;
  for(h= 0; h< height; h++)
  for(w= 0; w< mid; w++)
  swap(&img[h][w], &img[h][width-w];)
}
And the following marking scheme should be used:
• A correct outer loop; (1 mark)
• A correct inner loop; (1 mark)
• Correctly indexing the parameters in the swap function; (1 mark)
• Passing the values by reference; (1 mark)
[4 marks]