# 15110 Spring 2012 [Touretzky/Kaynar]

## Lab 8 - Thursday, October 25, 2012

### CA Activities

1. CA Evaluation survey: do it at http://goo.gl/wZ8MH.
2. Review of tutoring session signups and Sunday evening help session times/locations.
3. Remind students about the Ruby drills.
4. Demonstrate loading and displaying the nautical flags image.

### Deliverables

1. flip_image.rb
2. c_flag.rb
3. swap_bg.rb
4. grayscale.rb
5. transpose.rb

### Activities

1. Create a lab8 directory. Download the files ppm.rb and nautical.ppm to this directory. cd to this directory. Run irb in your lab8 directory and display the nautical flag alphabet on the Ruby canvas by evaluating the following Ruby expressions:
 load "ppm.rb" b = load_ppm("nautical.ppm") plot(b) plot(b,2) 

2. The nautical alphabet image is 200 by 136 pixels. It is encoded as an array of arrays. The main array contains 200 rows. Each row is an array containing 136 pixels. Each pixel is an array of form [r,g,b], where r, g, and b are red, blue, and green intensity values from 0 to 255. Try the following expressions:
b.size
b[0].size
b[0][0]
b[100][100]

Knowing that an image is an array of rows, write a function flip_image! that flips an image upside-down and plots the result. (Hint: remember the reverse! method.) Try flipping the nautical alphabet image upside-down.

3. Write a function c_flag to create a little 5-by-6 pixel image encoding the flag for the letter C. Refer to the RGB Color Table to get the codes for red and navy blue. Since this image is very small, you will want to magnify it for display purposes by writing plot(c_flag,20).

4. Write a function swap_bg!(image) that swaps the blue and green values of every pixel in the image, and plots the result. Test it on the nautical flag image. What happens if you call swap_bg! repeatedly on the same image?

5. Write a function grayscale!(image) that converts an image to grayscale by setting each pixel's r, g, and b values to their average value (r+g+b)/3, and plots the resulting grayscale image. Test your function on the nautical flag image.

6. Challenge problem: write a function transpose that flips an image along the major diagonal, so that the rows become columns and the columns become rows, and plots the result.