In this lab, students will:
[ [ [255,0,0] , [0,255,0] ] [ [0,0,255] , [255,255,255] ] [ [0,0,0] , [0,0,0] ] ]
We have developed some Ruby functions to make it easier to work with graphics. These functions are contained in ppm.rb and include load_ppm(filename) (which takes a name of a file in the PPM format and converts it into a the 3-dimensional bitmap representation described above) and plot(image) (which uses the RubyLabs canvas to render the bitmap image). There is also a function save_ppm(image, filename) that will store a 3d bitmap to the specified file.
Activity:
For each problem in PA7, your job is to write a function that takes an image represented in this format, and returns a new image of the same size that has something about the image changed. As an example, suppose we want to remove the red component from every pixel of an image. We can do this by resetting the red part of each pixel array to 0.
This can be accomplished using the following algorithm:
A. Set num_rows to the number of rows in the image.
B. Set num_columns to the number of columns in the image.
C. For each row in the image, do the following:
1. For each column in the image, do the following:
(a) Set green to the green component of the pixel array
at the current row and column in the image.
(b) Set blue to the blue component of the pixel array
at the current row and column in the image.
(c) Store the pixel array [0, green, blue] at the
current row and column in the image. (typo fixed)
D. Return nil.
Activity:
The following MARS assembly code converts 80 degrees fahrenheit into celsius:
;redcode
;name Celsius
;strategy cels = (fahr - 32) * 5 / 9
fahr DAT #80 ; input temperature
cels DAT #0 ; store result here
ftmp DAT #0 ; save fahr-32 here
start MOV fahr, ftmp ; (1) subtract 32
SUB #32, ftmp
mult ADD ftmp, acc ; (2) multiply by 5
SUB #1, count
JMN mult, count
div SUB #9, acc ; (3) divide by 9
SLT #0, acc
DAT #0 ; stop here when division done
ADD #1, cels
JMP div
acc DAT #0 ; accumulator
count DAT #5 ; counter
end start
Activity:
As an example, here are the first few lines that celsius_trace.txt should hold:
PC fahr cels ftmp acc count *5 35 0 3 0 5 *5 35 0 3 3 4