Desklib - Online Library for Study Material

Verified

Added on  2023/03/30

|11
|2075
|276
AI Summary
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.

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
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);

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
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:
Document Page
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);
Document Page
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.

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
198.
199. fwrite(bmp_header, 1, 54 ,file_ptr);
200. }
201. int i;
202. int j;
203. for( i=0;i<image.height;i++)
204. {
205. for( j=0;j<image.width;j++)
206. {
207. fwrite(&(image.pixels[i][j].red), 1,1,file_ptr);
208. fwrite(&(image.pixels[i][j].green), 1,1,file_ptr);
209. fwrite(&(image.pixels[i][j].blue), 1,1,file_ptr);
210.
211. }
212. }
213.
214. int padding = 0;
215. fwrite(&padding, 2, 1, file_ptr);
216.
217. fclose(file_ptr);
218. printf("Image Saved\n\n");
219. return 0;
220. }
221.
222. /*
223. * frees the dynamically allocated memory
224. */
225. void free_pixels(struct RGB_Image image)
226. {
227. long i=0;
228. for(;i<image.height;i++)
229. {
230. free(image.pixels[i]);
231. }
232. }
233.
234.
235. void re_allocate_pixels(struct RGB_Image image, int new_height, int new_w
idth){
236. //malloc, free and realloc
237.
238. //allocate memory
239. image.pixels = (struct Pixel**) malloc(sizeof(*image.pixels) * image.hei
ght);
240. int i ;
241. for(i =0 ;i<image.height;i++)
242. {
243. image.pixels[i] = (struct Pixel*) malloc(sizeof(struct Pixel*) * image.
width);
244. }
245.
246. //Free all rows above the new height
247. for(i = new_height;i<image.height;i++)
248. {
249. free(image.pixels[i]);
250.
251. }
252.
253. //realloc to new width
254. for(i = 0;i<new_height;i++)
255. {
Document Page
256. realloc(image.pixels[i],new_width);
257.
258. }
259.
260.
261. }
262.
263.
264. //DISPLAY OPTIONS
265. void interface(){
266.
267. printf("\n\n IMAGE MENU \n\n");
268. printf(" 0. Print Image Information\n");
269. printf(" 1. Save Copy of Image\n");
270. printf(" 2. Change Luminosity\n");
271. printf(" 3. Remove Image Channel\n");
272. printf(" 4. Invert Image Clours\n");
273. printf(" 5. Quantize Image\n");
274. printf(" 6. Flip Image Horizntally\n");
275. printf(" 7. Crop Image\n");
276. printf(" -1. Quit\n");
277. printf("\nChoice>> : ");
278.
279. }
280.
281.
282.
283. //this section prints out the information about the image
284. void print_information_image()
285. {
286. struct RGB_Image image;
287.
288. int loaded = load_image(&image);
289. if(loaded == 0)
290. {
291. printf("File name : %s\n", image.file_name);
292. printf("Height : %ld\n", image.height);
293. printf("Width : %ld\n", image.width);
294. printf("Size : %ld\n", image.size);
295. free_pixels(image);
296. }
297. }
298.
299.
300. void save_copy_image()
301. {
302. struct RGB_Image image;
303. int loaded = load_image(&image);
304. if(loaded ==0)
305. {
306. strcat(image.file_name,"_copy");
307. printf("Image Copied\n\n");
308. save_image(image);
309. free_pixels(image);
310. }
311. }
312.
313.
314. void remove_channel_image()
315. {
316.
Document Page
317. struct RGB_Image image;
318. char filename[MAX_FILE_NAME_SIZE];
319. char *colors[] = {"red","green","blue"};
320. int status = load_image(&image);
321. if(status==0)
322. {
323. int choice = 0 ;
324.
325. while(choice > 3 || choice < 1) {
326. printf("Enter the channel to remove \n");
327. printf("1.Red\n");
328. printf("2.Green\n");
329. printf("3.Red\n");
330. printf("Choice:>> ");
331. scanf("%d",&choice);
332.
333. switch(choice)
334. {
335. case 1:
336. remove_red_pixels(image.pixels,image.height,image
.width);
337. break;
338. case 2:
339. remove_green_pixels(image.pixels,image.height,ima
ge.width);
340. break;
341. case 3:
342. remove_blue_pixels(image.pixels,image.height,imag
e.width);
343. break;
344. default:
345. break;
346. }
347.
348. }
349.
350.
351. strcat(image.file_name,"_");
352. strcat(image.file_name,colors[choice-1]);
353. strcat(image.file_name,"_channel_removed");
354. printf("\n\n %s channel removed\n",colors[choice-1]);
355. save_image(image);
356. free_pixels(image);
357. }
358. }
359.
360. void remove_red_pixels(struct Pixel** pixels , int height , int width)
361. {
362. int i;
363. int j;
364. for( i=0;i<height;i++)
365. {
366. for( j=0;j<width;j++)
367. {
368. pixels[i][j].red=0b00000000;
369.
370. }
371. }
372. }
373.

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
374. void remove_green_pixels(struct Pixel** pixels , int height , int width )
375. {
376. int i;
377. int j;
378. for( i=0;i<height;i++)
379. {
380. for( j=0;j<width;j++)
381. {
382. pixels[i][j].green=0b00000000;
383. }
384. }
385. }
386.
387. void remove_blue_pixels(struct Pixel** pixels , int height , int width )
388. {
389. int i;
390. int j;
391. for( i=0;i<height;i++)
392. {
393. for( j=0;j<width;j++)
394. {
395. pixels[i][j].blue=0b00000000;
396. }
397. }
398. }
399.
400. void invert_image()
401. {
402. struct RGB_Image image;
403. char filename[MAX_FILE_NAME_SIZE];
404. int status = load_image(&image);
405. if(status==0)
406. {
407. invert_pixels(image.pixels,image.height,image.width);
408. strcat(image.file_name,"_inverted");
409. printf("Image Inverted");
410. save_image(image);
411. free_pixels(image);
412. }
413. }
414.
415. void invert_pixels(struct Pixel** pixels , int height , int width )
416. {
417. int i;
418. int j;
419. for( i=0;i<height;i++)
420. {
421. for( j=0;j<width;j++)
422. {
423. pixels[i][j].red = pixels[i][j].red^0b11111111;
424. pixels[i][j].green = pixels[i][j].green^0b11111111;
425. pixels[i][j].blue = pixels[i][j].blue^0b11111111;
426. }
427. }
428. }
429.
430. void quantize_image()
431. {
432. struct RGB_Image image;
433. char filename[MAX_FILE_NAME_SIZE];
Document Page
434. int status = load_image(&image);
435. if(status == 0)
436. {
437. int choice = 9;
438.
439. while(choice > 7 || choice < 0){
440. printf("Enter the quantization level <0 to 7>:\n");
441. scanf("%d",&choice);
442.
443. }
444. quantize_pixels(image.pixels,image.height,image.width,choice);
445. printf("Image Quantized by a level of %d",choice);
446. char quantize[MAX_FILE_NAME_SIZE] = "_quantize_";
447. sprintf(quantize, "%s%d",quantize , choice);
448. strcat(image.file_name,quantize);
449. save_image(image);
450. free_pixels(image);
451. }
452. }
453.
454. void quantize_pixels(struct Pixel** pixels , int height , int width , int
quantization_level)
455. {
456. int i,j;
457. for(i=0;i<height;i++)
458. {
459. for(j=0;j<width;j++)
460. {
461. pixels[i][j].red = pixels[i][j].red - quantization_level;
462. pixels[i][j].green = pixels[i][j].green - quantization_level;
463. pixels[i][j].blue = pixels[i][j].blue - quantization_level;
464. }
465. }
466. }
467. /**
468. * loads an image, flips the image
469. * horizontally and then saves the image
470. */
471. void flip_horizontal_image(){
472. struct RGB_Image image;
473. int img_load = load_image(&image);
474. if(img_load == 0){
475. flip_horizontal_pixels( image.pixels, image.width, image.height )
;
476. strcat(image.file_name,"_flipped_horizontally");
477. printf("Image Flipped Horizontally\n\n");
478. save_image(image);
479. free_pixels(image);
480. }
481.
482. }
483.
484.
485.
486. void flip_horizontal_pixels(struct Pixel** pixels, int height, int width)
{
487. int i,j;
488. int right = width - 1;
Document Page
489. //Height
490. for(i =0 ;i<height;i++){
491. //width
492. for(j =0 ;j<width/2;j++){
493. struct Pixel leftmost = pixels[i][j];
494. struct Pixel rightmost = pixels[i][right];
495. pixels[i][j] = rightmost;
496. pixels[i][right] = leftmost;
497. right -=1;
498. }
499. right = width - 1;
500. }
501.
502.
503. }
504.
505. void crop_image(){
506. struct RGB_Image image;
507. int loading_status = load_image(&image);
508.
509. int array[4]={100,200,300,400};
510. if (loading_status == 0){
511. printf("\nEnter the size to crop: \n");
512. printf(" 1. 100x100\n");
513. printf(" 2. 200x200\n");
514. printf(" 3. 300x300\n");
515. printf(" 4. 400x400\n");
516.
517. int choices;
518.
519. for(;;){
520. printf("Choice>> ");
521.
522. scanf("%d",&choices);
523.
524. if(!(choices < 1 || choices >4)){
525.
526. int h = array[choices-1];
527. int w = array[choices-1];
528. char newfilename[MAX_FILE_NAME_SIZE] = "_image_cropped_";
529. sprintf(newfilename, "%s%d",newfilename , h);
530.
531. strcat(image.file_name,newfilename);
532. re_allocate_pixels(image,h, w);
533.
534. printf("\nImage Cropped\n");
535. save_image(image);
536. free_pixels(image);
537.
538.
539. break;
540. }
541.
542. }
543. }
544.
545. }
546.
547.
548.
549. void change_luminosity_image()

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
550. {
551. struct RGB_Image image;
552. int luminosity_level, file_status;
553. file_status = load_image(&image);
554. if (file_status == 0)
555. {
556. printf(" Enter the luminosity: ");
557. scanf("%d", &luminosity_level);
558. change_luminosity_pixels(image.pixels, image.height, image.width,
luminosity_level);
559. strcat(image.file_name, "_luminosity_");
560. //strcat(image.file_name, luminosity_level);
561. save_image(image);
562. printf("Image luminosity changed by a level of %d", luminosity_le
vel);
563. free_pixels(image);
564. }
565. }
566.
567.
568. void change_luminosity_pixels(struct Pixel** pixels, int height, int widt
h, int luminosity_level)
569. {
570. int i, j, value_red = 0, value_green = 0, value_blue = 0;
571. for (i = 0; i < height; i++) {
572. for (j = 0; j < width; j++) {
573. value_red = pixels[i][j].red + luminosity_level;
574. if (value_red > 255)
575. value_red = 255;
576. else if (value_red < 0)
577. value_red = 0;
578.
579. pixels[i][j].red = value_red;
580. value_green = pixels[i][j].green + luminosity_level;
581. if (value_green > 255)
582. value_green = 255;
583. else if (value_green < 0)
584. value_green = 0;
585. pixels[i][j].green = value_green;
586. value_blue = pixels[i][j].blue + luminosity_level;
587. if (value_blue > 255)
588. value_blue = 255;
589. else if (value_blue < 0)
590. value_blue = 0;
591. pixels[i][j].blue = value_blue;
592. }
593. }
594. }
1 out of 11
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]