logo

Desklib - Online Library for Study Material

This assignment involves performing different operations on images saved as Bitmaps, such as changing luminosity, cropping, flipping horizontally, inverting, and quantizing the image.

11 Pages2075 Words276 Views
   

Added on  2023-03-30

About This Document

Desklib is an online library that provides study material including solved assignments, essays, dissertations, and more. Access a wide range of resources for your academic needs.

Desklib - Online Library for Study Material

This assignment involves performing different operations on images saved as Bitmaps, such as changing luminosity, cropping, flipping horizontally, inverting, and quantizing the image.

   Added on 2023-03-30

ShareRelated Documents
AS REQUESTED THIS IS THE EXPLANATION OF THE PROGRAM, SOURCE CODE;
the comments I WILL WRITE THEM COLORED GREEN
//
***********************************************************************************
******** ALL THIS SECTION WAS GIVEN BY THE LECTURE, SO IT WAS A COPY PASTE
#define MAX_FILE_NAME_SIZE 100
1. #include<stdio.h>
2. #include<stdlib.h>
3. #include<string.h>
4.
5.
6. struct Pixel
7. {
8. unsigned char red;
9. unsigned char green;
10. unsigned char blue;
11. };
12.
13. struct RGB_Image
14. {
15. char file_name[MAX_FILE_NAME_SIZE];
16. long height;
17. long width;
18. long size;
19. struct Pixel** pixels;
20. };
21.
22. //FILE FUNCTIONS AND DYNAMIC MEMORY ALLOCATION
23. int load_image(struct RGB_Image* image);
24. int save_image(struct RGB_Image image);
25.
26. //FREE FUNCTION
27. void free_pixels(struct RGB_Image image);
28.
29. //REALLOC FUNCTION
30. void re_allocate_pixels(struct RGB_Image image, int new_height, int new_width);
31.
32. //IMAGE FUNCTIONS
33. void save_copy_image();
34. void remove_channel_image();
35. void invert_image();
36. void quantize_image();
37. void flip_horizontal_image();
38. void flip_horizontal_image();
39. void change_luminosity_image();
40. void print_information_image();
41. void crop_image();
42.
43.
44. //PIXEL FUNCTIONS
45. void invert_pixels(struct Pixel** pixels, int height, int width);
46. void flip_horizontal_pixels(struct Pixel** pixels, int height, int width);
47. void quantize_pixels(struct Pixel** pixels, int height, int width, int quantizat
ion_level);
Desklib - Online Library for Study Material_1
48. void remove_red_pixels(struct Pixel** pixels, int height, int width);
49. void remove_green_pixels(struct Pixel** pixels, int height, int width);
50. void remove_blue_pixels(struct Pixel** pixels, int height, int width);
51. void change_luminosity_pixels(struct Pixel** pixels, int height, int width, int
luminosity_level);
***************************END OF COPY PASTE.... ALL CODE ABOVE WERE GIVEN BY
LECTURER*******
52. void interface(); // THIS IS MY OWN CODE interface() which displays the
information on the screen.
53.
54.
MAIN IS THE START OF THE PROGRAM, this will display the options to select and request
a user to enter an option.
55. int main(int argc, char *argv[]) {
56. char password[55] ="TYPE PASSWORD HERE";
57. printf("******************* TYPE STUDENT NAME AND ID HERE*******************
**\n\n");
58. printf("PASSWORD: %s \n",password);
59. int choice;
60. // Display the user interface in an infinite loop
61. for(;;) {
62. interface();
63. scanf("%d",&choice);
64. switch(choice)
65. {
66. case 0:
67. print_information_image();
68. break;
69. case 1:
70.
71. save_copy_image();
72. break;
73. case 2:
74. change_luminosity_image();
75.
76. break;
77. case 3:
78. remove_channel_image();
79.
80. break;
81. case 4:
82. invert_image();
83.
84. break;
85. case 5:
86. quantize_image();
87. break;
88. case 6:
89. flip_horizontal_image();
90. break;
91. case 7:
Desklib - Online Library for Study Material_2
92. crop_image();
93. break;
94. case -1:
95. exit(0);
96. break;
97. default:
98. printf("Invalid Option. Enter options 0 - 6\n");
99. }
100.
101. }
102.
103. return 0;
104. }
105. /**
106. * This function loads the image into a pointer
107. the function requests the user to type in the filename of the image to
load
108. the function then appends an extensions .bmp TO THE name that the user
has entered
109. */
110. int load_image(struct RGB_Image* image_pointer)
111. {
112.
113. char file_name[MAX_FILE_NAME_SIZE];
114. FILE *file_pointer;
115. long i,j;
116. printf("\n Enter the filename of the image to load : ");
117. scanf("%s", image_pointer->file_name);
118. strcpy(file_name, image_pointer->file_name);
119. // append bmp extension
120. strcat(file_name, ".bmp");
121. file_pointer = fopen(file_name, "rb");
122. if(file_pointer == NULL)
123. {
124. printf("\n File can not be opened");
125. return 1;
126. }
127.
128. fseek(file_pointer, 2, SEEK_SET);
129. fread(&(image_pointer->size), 4, 1, file_pointer);
130. fseek(file_pointer, 12, SEEK_CUR);
131. fread(&(image_pointer->width), 4, 1, file_pointer);
132. fread(&(image_pointer->height), 4, 1, file_pointer);
133. fseek(file_pointer, 28, SEEK_CUR);
134.
135. //allocate memory
136. image_pointer->pixels = (struct Pixel**) malloc(sizeof(*image_pointer-
>pixels) * image_pointer->height);
137. i = 0;
138. for(;i<image_pointer->height;i++)
139. {
140. image_pointer->pixels[i] = (struct Pixel*) malloc(sizeof(struct Pixel*)
* image_pointer->width);
141. }
142.
143.
144. for(i=0;i<image_pointer->height;i++)
145. {
146. for(j=0;j<image_pointer->width;j++)
147. {
148. fread(&(image_pointer->pixels[i][j].red), 1, 1, file_pointer);
Desklib - Online Library for Study Material_3
149. fread(&(image_pointer->pixels[i][j].green), 1, 1, file_pointer);
150. fread(&(image_pointer->pixels[i][j].blue), 1, 1, file_pointer);
151.
152. }
153. }
154.
155. fclose(file_pointer);
156. printf("\nImage loaded... \n \n");
157.
158. return 0;
159. }
160.
161. /*
162. * The function receives an RGB_Image
163. it adds a bmp extension to the file and saves it
164. it then prints out a message confirming that it has saved
165. */
166. int save_image(struct RGB_Image image)
167. {
168. char filename[MAX_FILE_NAME_SIZE];
169.
170. strcpy(filename,image.file_name);
171. strcat(filename,".bmp");
172. FILE *file_ptr;
173. file_ptr= fopen(filename,"w");
174. if(file_ptr==NULL)
175. {
176. printf("File can not be saved");
177. return 1;
178. }
THE SECTION BELOW WAS GIVEN BY THE TEACHER; IT DOES THE ACTUAL SAVING
OF THE IMAGE.
179. {
180. int image_datasize = image.size-40;
181. unsigned char bmp_header[] = {
182. 0x42,0x4D,
183. image.size, image.size>>8, image.si
ze>>16, image.size>>24,
184. 0x00, 0x00, 0x00, 0x00,
185. 0x36, 0x00, 0x00, 0x00, //No pale
tte
186. 0x28, 0x00, 0x00, 0x00,
187. image.width, image.width>>8, image.
width>>16, image.width>>24,
188. image.height, image.height>>8, imag
e.height>>16, image.height>>24,
189. 0x01,0x00,0x18,0x00,
190. 0x00, 0x00, 0x00, 0x00,
191. image_datasize, image_datasize>>8,
image_datasize>>16,image_datasize>>24,
192. 0x00, 0x00, 0x00, 0x00,
193. 0x00, 0x00, 0x00, 0x00,
194. 0x00, 0x00, 0x00, 0x00,
195. 0x00, 0x00, 0x00, 0x00,
196. };
197.
Desklib - Online Library for Study Material_4

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
For this Application I followed the step-by-step guide that was
|4
|343
|265

Foundations of Cyber Security
|26
|5206
|328

Hostel Management System
|25
|1266
|447

Questions on Stack Program
|11
|1305
|31

CLDA Loan Management Application: View, Request, Sort and Accept Loans
|14
|3975
|327

User and Technical Documentation for Health Club Membership Calculator Program
|26
|5023
|395