CMPS-2240 Semester Project
Gordon Griesel
Spring 2019

You may begin on this project.
This is an individual project.
Do not accept code from other students or tutors.

This is not a trivial assignment.

Project details

Due date: to be announced.

Grading:
Your grade for the final project is based upon the highest phase that you complete according to the specifications. Keep your previous code in case you run into trouble. Feel free to complete your project in advance.

All project phases leading up to your highest phase must be completed.

The project due date is firm. Late projects cannot be accepted for any reason.

After testing your code I will also check your source for readability. Points marked off if your source is not well-documented and clean. Code should have a consistent and pleasing style.

Required header:
Your code should have a header at the top. An example follows.


# author: Your Name
# date: Fall 2019
# desc: 2240 semester project
#       This program displays an image on a text display. 
# usage: spim -f proj1.s 
#

Collaboration policy:
You can discuss the project with other students but the code must be written entirely by you. Please do not ask tutors to write your code. Tutors can show you how to debug your code and can explain algorithms only. Tutors cannot write code for you.

Where to write your project code?
I recommend doing your project development in your /2240/proj/ folder.
Your instructor wants to see your progress.

Example Code:
Example code for project: https://www.cs.csub.edu/~gordon/2240/project/

Project Grading Milestones:

Program   Grade  Milestone
-------   -----  ---------------------------------------------------------
proj60.s    60%   Read PPM 3 file and display image on screen 
proj70.s    70%   Read PPM 3 color image and display image on screen 
proj80.s    80%   Read PPM 6 file and display image on screen 
proj90.s    90%   Read PPM 6 file into memory and display image on screen 
proj100.s  100%   Menu system to quickly display images 
--------------------------------------------------------------------------

Project Technical Description:
Unless stated otherwise, all phases must be written using functions. Each function should build a stack frame when needed, according to MIPS programming conventions. Failing to use stack frames will result in a deduction in score. No spaghetti code.

This project is build around PPM images.
Read about PPM files here:
http://netpbm.sourceforge.net/doc/ppm.html

The project consists of specific milestones. You should complete one milestone before moving on to the next. The milestone you reach is the grade you will recieve (assuming you properly completed that milestone). Refer to Table above for the milestones.

Function Calls:
Your program should have functions that do the following tasks.

main
your main function

read until whitespace
read from the file until you find a whitespace character

read until non-whitespace
read from the file until non-whitespace is found

read until newline
read from the file until a newline is found

read comment
read from the file while displaying the characters read

show file descriptor
display the file handle number

atoi
the atoi function from other assignments

check for good P3
function to check for valid file

newline
function to do a newline

Program 60

Deliverable: proj60.s

Write a MIPS program to read a PPM type P3 file. Display the following information about the file.

You will be given program proj1.s to begin.

$ spim -f proj60.s csub3.ppm

2240 proj60.s
Read a PPM image file.

File descriptor: 3
Good P3 file found.
Found a comment: image file for 2240 project testing. 
Image width: 60
Image height: 30
Maximum color value: 255

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@*:   .=%@@@&-. .-%@@@@  @@@@@@  @@@@     .+@@@@@@@@
@@@@@@@#:        @@*       @@@@  @@@@@@  @@@@       =@@@@@@@
@@@@@@@: .*&@#&*:@@. :&##*:@@@@  @@@@@@  @@@@  @@#+  @@@@@@@
@@@@@@* .#@@@@@@@@@  #@@@@@@@@@  @@@@@@  @@@@  @@@#  @@@@@@@
@@@@@@: *@@@@@@@@@@. +#@@@@@@@@  @@@@@@  @@@@  @@#+ -@@@@@@@
@@@@@@  #@@@@@@@@@@*   .-*@@@@@  @@@@@@  @@@@      =#@@@@@@@
@@@@@@  @@@@@@@@@@@@%-    .#@@@  @@@@@@  @@@@      .*@@@@@@@
@@@@@@  #@@@@@@@@@@@@@@&+. =@@@  @@@@@@  @@@@  @@#&: *@@@@@@
@@@@@@: *@@@@@@@@@@@@@@@@& .@@@  #@@@@#  @@@@  @@@@# .@@@@@@
@@@@@@* .#@@@@@@@@@@@@@@@#  @@@: *@@@@* :@@@@  @@@@#  @@@@@@
@@@@@@#. .*&@#&*:@@:*&##%: .@@@+  *##*  +@@@@  @@#&: :@@@@@@
@@@@@@@#:        @@        *@@@#:      :@@@@@        %@@@@@@
@@@@@@@@@*:   .=%@@&=.  .-%@@@@@#+.  .+#@@@@@     .-&@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Your program should produce similar output for the following runs:
  $ spim -f proj60.s csub3.ppm
  $ spim -f proj60.s csub3_.ppm

Refer to file-io.s for help reading a file.
Copy the project example files into your current directory like this:
$ cp /home/fac/gordon/public_html/2240/project/* .



Program 70

Deliverable: proj70.s

Extend your proj60.s program to read and display a P3 color image.

The image data is similar to a gray-scale image. (csub3.ppm)

The proj60.s program reads just the Red color. Modify your program to read the Red, Green, and Blue color values, and then use the average color to display your character.
A new color image will be posted soon.

Display an ASCII character on the screen for each 3 data values read.
Read width x height x 3 values.
Use nested loops to read through all the data.
Number of rows = height
Number of columns = width
After each row is read, print a newline character.


$ spim -f proj70.s csub3_.ppm

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@*:   .=%@@@&-. .-%@@@@  @@@@@@  @@@@     .+@@@@@@@@
@@@@@@@#:        @@*       @@@@  @@@@@@  @@@@       =@@@@@@@
@@@@@@@: .*&@#&*:@@. :&##*:@@@@  @@@@@@  @@@@  @@#+  @@@@@@@
@@@@@@* .#@@@@@@@@@  #@@@@@@@@@  @@@@@@  @@@@  @@@#  @@@@@@@
@@@@@@: *@@@@@@@@@@. +#@@@@@@@@  @@@@@@  @@@@  @@#+ -@@@@@@@
@@@@@@  #@@@@@@@@@@*   .-*@@@@@  @@@@@@  @@@@      =#@@@@@@@
@@@@@@  @@@@@@@@@@@@%-    .#@@@  @@@@@@  @@@@      .*@@@@@@@
@@@@@@  #@@@@@@@@@@@@@@&+. =@@@  @@@@@@  @@@@  @@#&: *@@@@@@
@@@@@@: *@@@@@@@@@@@@@@@@& .@@@  #@@@@#  @@@@  @@@@# .@@@@@@
@@@@@@* .#@@@@@@@@@@@@@@@#  @@@: *@@@@* :@@@@  @@@@#  @@@@@@
@@@@@@#. .*&@#&*:@@:*&##%: .@@@+  *##*  +@@@@  @@#&: :@@@@@@
@@@@@@@#:        @@        *@@@#:      :@@@@@        %@@@@@@
@@@@@@@@@*:   .=%@@&=.  .-%@@@@@#+.  .+#@@@@@     .-&@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

How to display the image characters?

Recommended method:

1. define an array in your data section that looks something like this.

   color_array: .space " .+:=&#@"

   Notice how each character from left-to-right has more substance.
   These characters will be used as color.
   Black is a space, and white is an @ symbol.
   This array has a size of 8 bytes.

2. Determining which character to print.

   After reading each 3-values of RGB from your image,
   Take just the 3rd value and scale it down.
   Read all 3 color values for each pixel (as described above).
   Add them together.
   Divide by 3 to get the average color.
   
   Use a color image such as csub_color.ppm
                             csub_color_60.ppm
   
You must start with the proj1.s file for this step. proj1.s is provided to you. Use it to display csub_color_60.ppm It is most-likely correct when you can clearly see CSUB in the image.
Use the average color to get a character from the "color" array. Our goal is a value between 0 and 7. 0,1,2,3,4,5,6,7 are valid indicies of our color array. Maximum value is 0 to 255. Scaled value must be 0 to 7. Divide your data value by 32. A low value will become 0. A medium value will become 3 or 4. A high value will become 7. Use this scaled value as an offset into the color_array. Your maximum data value will fit in 8-bits. Your scaled value will fit in 3-bits. Can you use a bit-shift to divide by a power of 2? Of course.

You may also display your proj60 output just prior to showing the image.



Program 80

Deliverable: proj80.s

Extend your proj70.s program to read P3 and P6 PPM files.

•Allow for multiple comments in the file.
Read the PPM file specifications to be sure you cover all the
possible places a comment might be found.

A P6 file is very similar to a P3 file.
The file header is the same, the image data is different.
Once you get to the image data, there is no whitespace!
Each pixel's color is stored in three consecutive bytes.

Your proj80.s program will know the difference between a P3 and P6
because of the first two characters in the file.


$ spim -f proj80.s p6test.ppm

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@ @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@     %@@@*   -@@@@@@@
@@@@@@ @@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@#: @@% =#%.@@@@@@@
@@@@@@ @@ @@ :  =:  +@@@@@@%.  .%@@@@@@ @@@& @@.-@@@@@@@@@@@
@@@@@@ @@@@@ .&- .&- @@@@@@ +&&. @@@@@@ @@#: @@ :  .#@@@@@@@
@@@@@@ @@@@@ #@& #@& @@@@@@@@@@& @@@@@@     %@@ .##..@@@@@@@
@@@@@@ @@@@@ @@@ @@@ @@@@@@*     @@@@@@ @@@@@@@ #@@# @@@@@@@
@@@@@@ @@@@@ @@@ @@@ @@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@@@@
@@@@@@@@@@@@@=     =@@@  @@@@@  @@&=  =&# #@=. =@@@@@@@@@@@@
@@@@@@@@@@@@= :#&#: =@@  &@@@&  @@@@@@@@:+@+ ++ +@@@@@@@@@@@
@@@@@@@@@@@@. &@@@& .@@. #@@@# .@@@@@@@*.@@. && .@@@@@@@@@@@
@@@@@@@@@@@@  &@@@&  @@- +@@@+ -@@@@@@&.#@@  @@  @@@@@@@@@@@
@@@@@@@@@@@@: :#&#: :@@%  %&%  %@@@@@@==@@@. && .@@@@@@@@@@@
@@@@@@@@@@@@%       %@@@-     -@@@@@@%.&@@@+ ++ +@@@@@@@@@@@
@@@@@@@@@@@@@#-.  -%@@@@&+. .+&@@@@@&.%@@@@&=  =&@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

The color section of a P6 file is much smaller than a P3 file, so the image can be read much faster than a P3. Also, the file requires less memory for storage.



Program 90

Deliverable: proj90.s

Details...

Extend your proj80.s program to read the image portion of a P6 file with one file read syscall command.

Setup a large memory buffer in your .data section. Make it large enough to store the largest file that you might want to read and display.

Then display the image by reading through the memory buffer holding the image colors. This will cause large P6 files to be read and displayed very quickly.

•When you get this working, remove the data buffer used to read the color data. Use syscall 9 to allocate just the correct amount of memory by taking into account the image dimensions.
This will allow your program to load and display any valid PPM file that will fit in your computer's memory.


$ spim -f proj90.s spock.ppm

:::::::-:--------------------------------------------------------.
::::::::::-------------------------------------------------------.    
:::::::---------------------------------------------------------:     
::::::::---------------------=-...----:------------------:------:.    
:::::::::-------------------:         ...------------------------.    
::::::-:-------------------  ..         ..:----------------::----:..  
:::-:::-------------------:   .:..       ..-----------::::---:--::.. 
.::::::::-----------------:. ....-....   .--------------::-::-::::::..
:........:::::::.......::- ::.   .-::-==-=..::-----:-----++=----::----
 .....  .....::::::.....:-.. .-==:          .-:-----:-:=++=-::....::::
..::.::::....=...&#.....:-.: :--**%&&=::=&%-.--:-:--::---:::::::::::::
.   ........=&+.:&&#  ..:=. .==-:-*&&#&&#&=-*::::::-:::-:..::::::.::::
......  ...-*&-.+&%&.  .:=:.-=-:...:=&=..++-=::::::::::..:.....:::::::
    .... .-#%&::&&%#  .::=.:==***%&+*#&%%**=:::::::::::-:......:::::..
       .:.*&*&.=#&#& .:::=::=:=*%%*-*#%&&&&-:::::::::::......    .....
          &&%#.##*@&::::::=:+::-*%=-%#%&&%%:::::::::::::.........     
         .%#*%+&&&#%::::::::-:::+=+:-++*%*=:::::::::::::::::::::::....
     .  .:&#&#&##@#+::::::::-:.:==++*%%&**::::::::::::::::::::::::::::
.        :&#&&#####:::::::::::.:=:--+=**%:::::::::::::::::::::::::::::
.    ....*&#&&@#&##.:::::.:-:.::+-----+*+:::::::::::::::::::::::::::::
.    .:::*##&#@@@@#=::::*&&+-. .=**&&&%*::::::::.:.:.::..........:::::
     .::-*&#&#@@@###-=&&&%    .  -==++++....:..............      .....
     .::-*&#&##########&=::       ::+.. -%%%=...........::............
     .:::+%&&####@@##&*+=:.::.         =**%%*+*% ........:.. .........
     .:::=%&&&&&&&%%+=+++*+==+-+*.   =*%********%%%    ..:...:.. .    
     .::==+*%**++++***+==++*++++==**********+********...:-:::.......  
     .:=+%**%%*+++=+****+====+*++*+*******+++********+ .:::....::...  
     .*=&&%%%*++**++++****+===+++**+*****+++++****+**%..............  
    .-*%%&%%*+++***+++++*++++=++++*******+++++*****+*%:...:.:....     
   .--*&&&%*-**++*+*+++=++++==+++++******+*+++****+**%%::::.          
  .-:+%%&%%==-+++**+++++==++++++++********************%.::::.....     
 ==:-+&%&%+**+:=++**+=+++++++*++++**********+*&+******%=-------:.     
=--:--**++++*+=:++=*++=+++++++++**+***********%********+-------:..    
+-=:-==++=++++-:=*==+*+==+++*****************+&*+*******:..::::....



Program 100

Deliverable: proj100.s

Elements of the 100% phase:
multiple image files.

Add a small menu to your program that lists several image file names. When a menu item is selected, the image is displayed on screen.

Please design a simple menu.
The menu should include files:
csub3.ppm
csub3_.ppm
p6test.ppm
spock.ppm

Your program output should look similar to below.

$ spim -f proj100.s

Choose an image.

1 csub3.ppm
2 csub3_.ppm
3 p6test.ppm
4 spock.ppm
0 exit

Enter image number: 

An image is displayed when a valid menu item is entered.