CSE1PES: PROGRAMMING FOR ENGINEERS AND SCIENTISTS
ASSIGNMENT 3
Assignment developed by Matthew Felicetti 2016, updated 2017(MF), updated 2018(MF & JM)
TOOLS
• Unix server through Putty for compiling and testing
• Notepad XXXXXXXXXXfor writing solutions
• LMS for submission
SUBMISSION
• The code
o Submitted as a .c file in the normal assignment submission portal. (Do not submit the
executable.)
ACADEMIC INTEGRITY
Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your
own. For individual assignments, plagiarism includes the case where two or more students work collaboratively on
the assignment. The School of Engineering and Mathematics treats plagiarism very seriously. When it is detected,
penalties are strictly imposed.
http:
www.latrobe.edu.au/students/academic-integrity
http:
www.latrobe.edu.au/students/academic-integrity
IMAGE PROCESSING
BACKGROUND
This program will perform different operations on images saved as Bitmaps. (extension .bmp)
The output files for this assignment are bmp, and hence you will need to open the results in windows or
on macOS using a suitable image editor
If you want to add your own images you can but they must be 24-bit Bitmaps, and the width of the
image*3 must be divisible by 4
PROBLEM
The program will perform the following different operations on images:
Save a copy of an image – Load and save a copy of an image
Remove channel - Remove either the red, blue or green components of an image
Quantize – Reduce the number of colours in an image
Invert – Invert all the colours in an image
Flip Horizontally – Flip the image horizontally
Please note this is not the ideal solution, but one designed to test your understanding of various topics.
FUNCTION PROTOTYPES AND INCLUDES
1. You must include the stdio.h, stdlib.h and string.h li
aries
2. Add the following macro
3. The following function prototypes are to be used:
a. All are described below
. You can change the naming
c. You cannot change the types
4. The following structs are to be used
FUNCTION - MAIN
Steps:
1. The program prints to the screen YOUR student number, student name and the assignment number. This
is enclosed by double asterisks. Ensure you follow the below format.
2. Create a menu as shown in the output below. The user enters the input value after the menu is printed. If
an invalid integer is entered, the menu appears again with NO prompt to the user that an invalid entry has
een made. You can assume the user always enters an intege
3. If the user selects a valid entry (1,2,3,4,5) then the appropriate function should be called (After the
function call is finished, the menu should be displayed again)
a. Save Copy of Image should call the save_image_copy function
. Remove Image Channel should call the remove_image_channel function
c. Invert Image Colours should call the invert_image_colours function
d. Quantize Image should call the quantize_image function
e. Flip Image Horizontally should call the flip_horizontal_image function
4. The program should end when the user enters -1
FUNCTION – LOAD_IMAGE
Steps:
When implementing this function, keep in mind that it accepts a pointer to a struct RGB_Image
The function returns a 0 on successful execution, else a 1 if the image can not be loaded
1. Ask the user to enter the file name of the image to load (This should not include the extension)
2. Store this file name in the struct RGB_Image that is pointed to by image_ptr
3. Create a copy of the file name and store appropriately using strcpy
4. Add the extension “.bmp” to the copy using strcat
5. Open the image, using the file name with the extension, for reading using fopen and store the File pointer
appropriately
a. If the file can not be opened for any reason print to the screen “File can not be opened”
. And return 1
6. Skip the first two bytes of the file using fseek
7. Read the size of the file by reading the next 4 bytes and store appropriately using fread
a. (Note: you will need to pass the address of where the size is to be stored in the struct
RGB_Image)
8. Skip the next twelve bytes of the file using fseek
9. Read the width of the image by reading the next 4 bytes and store appropriately using fread
10. Read the height of the image by reading the next 4 bytes and store appropriately using fread
11. Skip the next twenty-eight bytes of the file using fseek
12. Now dynamically allocate the memory for the pixels:
a. First we dynamically allocate memory for a pointer to each row in the image
*Example for an image with a height of 3 pixels (Note: the struct RGB_Image is omitted)
. Then we iteratively (hint. for loop) allocate memory for the pixels for each pixel in a row
*Example continued for an image with a height of 3 pixels and width of 4 pixels
13. Create iteration variables i and j (You do not need to create i if you used it in Step 12.)
14. Iterate from 0 to the height of the image (exclusive) using i :
a. Iterate from 0 to the width of the image (exclusive) using j :
i. Read the next byte in the file and store in the blue pixel at location i,j
(where i is the row, and j is the column)
(this is not a line of code to just put in, this needs to be used in the fread call)
ii. Read the next byte in the file and store in the green pixel at location i,j
iii. Read the next byte in the file and store in the red pixel at location i,j
15. Close the file
16. Print “Image Loaded” and then two newlines
17. Return 0
FUNCTION – SAVE_IMAGE
Steps:
1. Create a copy of the file name from image, and store appropriately using strcpy
2. Add the extension “.bmp” to the copy using strcat
3. Create a new file, using the file name with the extension, for writing using fopen and store the File pointer
appropriately
a. If the file can not be opened for any reason print to the screen “File can not be saved”
. And return 1
4. Insert the following code below your code from Step 3. (Note the
aces enclosing the code)
5. Create iteration variables i and j
6. Iterate from 0 to the height of the image (exclusive) using i :
a. Iterate from 0 to the width of the image (exclusive) using j :
i. Write the blue pixel at location i,j into the next byte in the file
(where i is the row, and j is the column)
(this is not a line of code to just put in, this needs to be used in the fwrite call)
ii. Write the green pixel at location i,j into the next byte in the file
iii. Write the red pixel at location i,j into the next byte in the file
7. Close the file
8. Print “Image Saved” and then two newlines
9. Return 0
FUNCTION – FREE_RGB_PIXELS
This function frees the dynamic memory allocated from when the image was loaded.
Steps:
1. Free all the rows dynamically allocated using a for loop
2. Free the memory dynamically allocated for storing the pointers to each row
FUNCTION – SAVE_IMAGE_COPY
This function loads an image and then saves the same image.
Eg. If you have an image “Hello.bmp”, then this function should create a copy named “Hello_copy.bmp”
Steps:
1. Create a struct RGB_Image named image
2. Call the load_image function and pass the address of image, store what is returned from the function
appropriately.
3. If the image was loaded successfully (use the return value of the call above to determine)
a. Add the text “_copy” to the file name from image using strcat
. Print “Image Copied” and then two newlines
c. Call the save_image function and pass the appropriate argument
(Note: You do not need to use/store the value returned here)
d. Free the dynamically allocated memory by calling the free_RGB_pixels function and passing the
appropriate argument
STOP AND MAKE SURE EVERYTHING WORKS BEFORE CONTINUING.
FUNCTION – REMOVE_IMAGE_CHANNEL
This function loads an image, ask the user which channel they would like to remove, removes the channel and then
saves the image.
Removing a channel involves removing all of one colour from the image (red, blue or green).
Steps:
1. Create a struct RGB_Image named image
2. Load the file and store the image information appropriately in image
3. If the image was loaded
a. Create a menu as shown in the output below. The user enters the value after the menu is
displayed. If an invalid integer is entered the menu appears again with NO prompt to the
user that an invalid entry has been made. You can assume the user always enters an integer
. If the user selects a valid entry (1,2,3) then the appropriate function should be called
i. Red should call remove_red_pixels function and pass the pointer to the pixels a
ay,
the height and the width of the image
ii. Green should call remove_green_pixels function and pass the pointer to the pixels
a
ay, the height and the width of the image
iii. Blue should call remove_blue_pixels function and pass the pointer to the pixels
a
ay, the height and the width of the image
c. Add “_X_channel_removed” to the end of the filename, where X is the colour removed
d. Print “X channel removed” to the screen, where X is the colour removed
e. Save the image using the appropriate function
f. Free the dynamically allocated memory using the appropriate function
FUNCTION – REMOVE_RED_PIXELS
Iterate through ALL pixels and set red to 0
Steps:
1. Create iteration variables i and j
2. Iterate from 0 to the height of the image (exclusive) using i :
a. Iterate from 0 to the width of the image (exclusive) using j :
i. Set the