Description

In this assignment, you will implement a number of small programs. Here, you will:

  • use dynamic memory allocation, pointers, and references.
  • use files.
  • Input validation should be performed. Hints on how to set the limits for validation are inside the text of the question.

Implementation Instructions

The HW2.tar.gz archives contains the file HW2.pdf (this file!) and the initial source code of the project. In the directory src/ you will find the following files:

  • the image_difference.cpp and image.h files that contains some function prototypes and a struct you need to implement.
  • two .pgm files to use as sample input.

You will submit:

1. only the source files (i.e., .cpp and .h files). Do not submit .o files, executables, or your test datasets.

2. files at point 1 (i.e. .cpp and .h files must be in the src directory compressed in the src.tar.gz file.

To compress src directory, move to its parent directory (i.e., if you are inside src, type cd ..), then type the following command on the linux terminal:

tar zcvf src . tar . gz src

Questions

Image Difference This program will calculate the image difference of two pictures of the same subject. The difference between two images is calculated by finding the difference between each pixel in each image, and generating an image based on the result.

A black & white image of width w and a height h can be represented as a matrix I hw , where each element of the matrix represents the corresponding value of a pixel. Pixels assume values in the range [0,255], where a value of 0 represents black, and a value of 255 represents white. Every other value in that range represents a different shade of gray (yes, there are more than 50 shades of gray :).

Images are in PGM format. Plain PGM images are saved in plain ascii format (http://netpbm. sourceforge.net/doc/pgm.html). This is a very simple image format. The format consists of a three-line header field followed by the image data (i.e., pixel values). An example of a small image is given below:

P2
24 7
7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 3 3 3 3 0 0 7 7 7 7 0 0 1 1 1 1 0 0 5 5 5 5 0
0 3 0 0 0 0 0 7 0 0 0 0 0 1 0 0 0 0 0 5 0 0 5 0
0 3 3 3 0 0 0 7 7 7 0 0 0 1 1 1 0 0 0 5 5 5 5 0
0 3 0 0 0 0 0 7 0 0 0 0 0 1 0 0 0 0 0 5 0 0 0 0
0 3 0 0 0 0 0 7 7 7 7 0 0 1 1 1 1 0 0 5 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

where:

  • First line: Magic number P2 that identifies the type of the image; P2 is for pgm images (when you read the .pgm file test that the magic number value is correct. You should also add it to the image you will save to disk).
  • Second line: The width and height of the image.
  • Third line: The maximum pixel value in the image (it might be less than 255!).
  • Each pixel in the raster has white spaces before and after it (new lines are considered white spaces). There must be at least one white space character between any two pixels.
  • No line should be longer than 70 characters (white spaces count as characters!).
  • There is a newline character at the end of each of these lines (you must go to a new line after 70 characters!).

Your program will be organized as follows:

  • An Image struct is to be defined in image.h. You must use this struct as data type for the image.
  • load_image function accepts a filename, and an Image reference. This function read a pgm image from a file and load it into memory. It returns 0 in case of failure, non zero otherwise.
  • save_image function accepts a filename, and an Image reference. This function save the image into a file with the correct format. It returns 0 in case of failure, non zero otherwise.
  • diff function accepts three references, respectively of the first image, second image, and the difference image. It stores the difference between images im1 and im2 in imDiff. It returns the Sum of Absolute Differences (SAD). To compute SAD sum the absolute difference between each pixel in the original image and the corresponding pixel in the image being used for comparison.
  • load_and_save function accepts two filenames as parameters. It loads the images im1 and im2 by using load_image function, compute the difference, and then save the difference image to a file named "difference.pgm" by using save_image function.
  • Note: your program must check that loading image and save image operations went fine. You must also ensure that the image files are in the correct format.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.