Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
CS 341 : Programming Languages
Project 02 : Image processing in F# (doc v1.0)
Assignment: F# function library to perform image operations
Evaluation: Gradescope followed by manual execution & review
Policy: Individual work only
Complete By: Saturday, October 28th @ 11:59pm CDT
Late submissions: Not Allowed
Background
You are going to write a program to perform various operations on images stored in PPM format, such as
this lovely image of a piece of cake:
There are many image formats you are no doubt familiar with: JPG, PNG, etc. The advantage of PPM is that the
file format is human-readable, so you can open PPM files in a text editor. This makes it easier to write programs
that manipulate the images, and it also makes it easier to debug your output — you can simply open the image
file in a text editor and “see” what’s wrong. First some background on PPM files, and then the details of the
assignment…
PPM Image Format
The PPM (or Portable Pix Map) image format is encoded in human-readable ASCII text. For those of you
who enjoy reading documentation, the formal image specification can be found here 1
. Here is a sample ppm
file, representing a very small 4x4 image:
1 http://netpbm.sourceforge.net/doc/ppm.html
Page 2 of 11
P3
4 4
255
0 0 0 100 0 0 0 0 0 255 0 255
0 0 0 0 255 175 0 0 0 0 0 0
0 0 0 0 0 0 0 15 175 0 0 0
255 0 255 0 0 0 0 0 0 255 255 255
Here is what this image looks like, magnified 5,000%. Notice it consists of 16 pixels, laid out in 4 rows with 4
pixels per row:
You can think of an image as having two parts, a header and a body. The header consists of information about
the image such as width and height, GPS location, time, date, etc.. For PPM images, the header is very simple,
and has only 4 entries:
P3
4 4
255
P3 is a "magic number". It indicates what type of PPM image this is (full color, ASCII encoding). For this
assignment it will always be the string “P3”. The next two values, 4 4, represent the width and height of the
image — or more accurately from a programming perspective, the number of pixels in one row is the width
and the number of rows in the image is the height. The final value, 255, is the maximum color depth for the
image. For images in “P3” format, the depth can be any value in the range 0..255, inclusive.
The image body contains the pixel data — i.e. the color of each pixel in the image. For the image shown
above, which is a 4x4 image, we have 4 rows of pixel data:
0 0 0 100 0 0 0 0 0 255 0 255
0 0 0 0 255 175 0 0 0 0 0 0
0 0 0 0 0 0 0 15 175 0 0 0
255 0 255 0 0 0 0 0 0 255 255 255
Page 3 of 11
Look at this data closely… First, notice the values range from 0 .. maximum color depth (in this case 255).
Second, notice that each row contains exactly 12 values, with at least one space between each value. Why
12? Because each row contains 4 pixels, but each pixel in PPM format consists of 3 values: the amount of
RED, the amount of GREEN, and the amount of BLUE. This is more commonly known as the pixel’s RGB value.
Black, the absence of color, has an RGB value of 0 0 0 — the minimum amount of each color. White, the
presence of all colors, has an RGB value of depth depth depth — the maximum amount of each color. As
shown above, notice the first pixel in the 4x4 image is black, and the last pixel is white.
In general a pixel’s RGB value is the mix of red, green, and blue needed to make that color. For example,
here are some common RGB values, assuming a maximum color depth of 255:
Yellow: 255 255 0
Maroon: 128 0 0
Navy Blue: 0 0 128
Purple: 128 0 128
You can read more about RGB on the web2
. We will provide you with 5 PPM images to work with. The image
shown above is “tiny4by4.ppm”. The images will be made available in the replit repository “Project 02” for
this project.
● blocks.ppm
● cake.ppm
● square.ppm
● tiny4by4.ppm
● tinyred4by4.ppm
Viewing PPM Images
PPM files are an uncommon file format, so if you double-click on a “.ppm” image file you will be unable to
view it. Here are some options for viewing PPM files, which you’ll need to do for testing purposes…
Option #1 is to download a simple JavaScript program for viewing images on your local computer:
ppmReader.zip . Download, double-click to open, and extract the file ppmReader.html --- save this anywhere
on your local computer. When you want to view a PPM image file, download the PPM file to your local
computer, double-click on ppmReader.html, and select the PPM image file for viewing. This tool is also
available online.