Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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...

1 answer below »
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
Answered Same Day May 20, 2020 CSE1PES La Trobe University

Solution

Snehil answered on May 28 2020
137 Votes
main.c
#include #include #include #define MAX_FILE_NAME_SIZE 100
struct Pixel
{
unsigned char red;
unsigned char green;
unsigned char blue;
};
struct RGB_Image
{
char file_name[MAX_FILE_NAME_SIZE];
int height;
int width;
int size;
struct Pixel** pixels;
};
void save_image_copy();
void remove_image_channel();
void invert_image_colours();
void quantize_image();
void flip_horizontal_image();
int load_image(struct RGB_Image* image_ptr);
int save_image(struct RGB_Image image);
void free_RGB_pixels(struct RGB_Image image);
void invert_pixels(struct Pixel** pixels, int height, int width);
void flip_horizontal_pixels(struct Pixel** pixels, int height, int width);
void quantize_pixels(struct Pixel** pixels, int height, int width, int quantization_level);
void remove_red_pixels(struct Pixel** pixels, int height, int width);
void remove_green_pixels(struct Pixel** pixels, int height, int width);
void remove_blue_pixels(struct Pixel** pixels, int height, int width);
void swap_pixel_colours(struct Pixel* pixel1,struct Pixel* pixel2);
int main(int argc, char** argv) {
int choice;
user choice
printf("**%d %s**", 3777777, "John Smith Assignment 2\n");
do
loop to get user choice, runs till user provides valid choice
{
printf("Menu\n1. Save Copy of Image\n2. Remove Image Channel\n3. Invert Image Colours\n4. Quantize Image\n5. Flip Image Horizontally\n-1.Quit\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
save_image_copy();

eak;
case 2:
remove_image_channel();

eak;
case 3:
invert_image_colours();

eak;
case 4:
quantize_image();

eak;
case 5:
flip_horizontal_image();

eak;
case -1:

eak;
default:
printf("\n");
}
}while(choice!=-1);
return (0);
}
function creates an exact copy of an image
void save_image_copy()
{
struct RGB_Image image;
int success = load_image(&image);
if(success==0)
{
strcat(image.file_name,"_copy");
printf("Image Copied\n\n");
save_image(image);
free_RGB_pixels(image);
}
}
function loads an image based on given file name
int load_image(struct RGB_Image* image_ptr)
{
int i,j;
char file_name[MAX_FILE_NAME_SIZE+4];
printf("Enter file name : ");
scanf("%s",image_ptr->file_name);
strcpy(file_name,image_ptr->file_name);
strcat(file_name,".bmp");
FILE *file_ptr = fopen(file_name,"
");
if(file_ptr==NULL)
{
printf("\nFile cannot be opened\n");
return (1);
}
fseek(file_ptr,2,SEEK_CUR);
fread(&(image_ptr->size),4,1,file_ptr);
fseek(file_ptr,12,SEEK_CUR);
fread(&(image_ptr->width),4,1,file_ptr);
fread(&(image_ptr->height),4,1,file_ptr);
fseek(file_ptr,28,SEEK_CUR);
image_ptr->pixels = malloc(image_ptr->height * sizeof(struct Pixel*));
for(i = 0;iheight;i++)
{
image_ptr->pixels[i] = malloc(image_ptr->width * sizeof(struct Pixel));
}
for(i =...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here