For this exercise, you will first write a C program to convert a color image (in PPM format) to grayscale (PGM format). Once you have thoroughly tested your C code to verify it is working correctly, you will convert it into a MIPS assembly program to do the same.
For this exercise, we will use the ASCII file formats of type PPM (color) and PGM (grayscale). For a detailed description of these file formats, please see the Wikipedia entry on Netpbm format. A short description is given below.
The format of a PPM or PGM file is as follows:
Additional Formatting Specifications for This Exercise: Since the MIPS input/output routines (syscalls) available in MARS are somewhat limited, we will put further restrictions on the format of PPM/PGM files as follows:
There are a number of free/open-source viewers available for displaying PPM/PGM image files, including:
Write a C program that reads a PPM image (on its standard input, i.e., keyboard), and outputs its corresponding PGM image (on its standard output, i.e., console). Keep in mind the following:
Gray = (R*30% + G*59% + B*11%)
Gray_Value = ((R*30 + G*59 + B*11) / 100) * 255 / PPM_MAX --OR--
Gray_Value = ((R*30 + G*59 + B*11) * 255) / (100 * PPM_MAX)Your C code (and assembly code below) must compute the gray value exactly as described by the last expression above, and exactly in the order indicated by the parentheses. The division is a truncated integer division (i.e., no rounding, throw away remainder); therefore, for best accuracy, all of the multiply operations are performed first, and division last.
ex1 < ex1in1.ppm > ex1result1.pgm Copy the PGM output to your laptop and view it in GIMP Make sure the PGM output looks like a grayscale version of the PPM input Make sure the PGM output has the same overall brightness as the input; otherwise check PPM_MAX
Name the file containing your C program ex1.c, and test it on sample inputs provided. First view the output file using GIMP. If it looks good, then you may check it against the sample output provided using the diff command.
Once your C program is working correctly, write an equivalent MIPS assembly program that implements the same conversion from PPM to PGM images. Do not begin work on the assembly program until you are sure that the C program is working correctly. The best strategy would be to convert your C code (pretty much line-by-line) into its assembly equivalent. Run your program in MARS and verify that it is working correctly.
You can run your program in MARS and test it by typing (or cutting-and-pasting) sample inputs onto the console, and looking at the console output. For the larger examples, you will need to run it in command-line mode and use input/output redirection.
If you are developing your code on a Mac/Linux machine, you can use the following command on your computer within a terminal window without logging onto classroom.cs.unc.edu (replace /path/to with the actual path to Mars, e.g., /Users/yourid/Desktop/Mars/Mars4_5.jar):
% java -jar /path/to/Mars4_5.jar nc mc CompactDataAtZero ex1.asm < ex1in1.ppm > ex1result1.pgm
If you are using a Windows machine (or even Mac/Linux), you can login onto classroom.cs.unc.edu, copy your work there, and then run the following command:
% java -jar /home/montek/comp411/bin/Mars4_5.jar nc mc CompactDataAtZero ex1.asm < ex1in1.ppm > ex1result1.pgm
For more information on running MARS in command-line mode, please see http://courses.missouristate.edu/kenvollmar/mars/help/MarsHelpCommand.html.
Name the file containing your assembly program ex1.asm, and test it on sample inputs provided. First view the output file using GIMP. If it looks good, then you may check it against the sample output provided using the diff command.
For this exercise, you will a program in C first and then in assembly to crop a grayscale image (in PGM format). The program will read four integers from the standard input, one per line as follows:
x1 x2 y1 y2
These numbers represent the coordinates of the bounding box with which to crop the image. That is, the top-left corner will be (x1, y1), and the bottom right corner will be (x2, y2). You can assume that x2 >= x1 and y2 >= y1, and that the values will be within the bounds of the actual image (so no error checking needed). The cropped image will include all the columns from x1 to x2 (inclusive), and all the rows from y1 to y2 (inclusive). The numbering starts with the top-left corner of the image at (0,0).
The input will consist of the four integers as above, followed by the contents of the PGM file but without the "P2" line at the top (to make things easier) (see description in Exercise 1). The output should be the contents of the cropped grayscale image, in PGM format, just as in Exercise 1. See sample inputs for examples. First view the output using GIMP. If it looks good, then you may check it against the sample output provided using the diff command.
TIP: Your program does not (in fact, should not) need to store the entire image in an array. Cropping simply can be done by reading one pixel at a time, deciding whether it belongs in the cropped image or not (based on its rown and column number), and then printing it on the output if it belongs in the image. There is no need at all to create an array to store all the pixel values.
Name your C program ex2.c and the assembly ex2.asm. You can run the C program after compiling it as follows:
% ex2 < ex2in1 > ex2result1.pgm
And the assembly program as follows:
% java -jar /home/montek/comp411/bin/Mars4_5.jar nc mc CompactDataAtZero ex2.asm < ex2in1 > ex2result1.pgm
Sample inputs and corresponding outputs are provided under /home/montek/comp411/samples/lab6. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files.
Your assignment must be submitted electronically by 11:59pm on Friday, March 11.
First copy the files ex1.c, ex1.asm, ex2.c and ex2.asm to the CS Dept server (classroom.cs.unc.edu) under the appropriate folder in your home directory (e.g., comp411lab/lab6). You can use Secure FTP/scp or other equivalent commands to copy this file from your laptop to the server. Then, run the submit script:
% cd ~/comp411lab/lab6
% /home/montek/comp411/bin/submitlab6
You can also check your work without submitting it:
% cd ~/comp411lab/lab6
% /home/montek/comp411/bin/selfchecklab6
In case of any problems, please contact the instructor or the TAs.