PandA-2024.02
mypgm.c
Go to the documentation of this file.
1 #include "mypgm.h"
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 
6 
7 /* Main body of functions */
8 
9 void load_image_data(const char* file_name, unsigned char *image1, unsigned int * x_size1, unsigned int * y_size1 )
10 /* Input of header & body information of pgm file */
11 /* for image1[ ][ ],x_size1,y_size1 */
12 {
13  char buffer[MAX_BUFFERSIZE];
14  FILE *fp; /* File pointer */
15  int max_gray; /* Maximum gray level */
16  int x, y; /* Loop variable */
17 
18  /* Input file open */
19  printf("\n-----------------------------------------------------\n");
20  printf("Monochromatic image file input routine \n");
21  printf("-----------------------------------------------------\n\n");
22  printf(" Only pgm binary file is acceptable\n\n");
23  printf("Name of input image file: %s\n", file_name);
24  fp = fopen(file_name, "rb");
25  if (NULL == fp) {
26  printf(" The file doesn't exist!\n\n");
27  exit(1);
28  }
29  /* Check of file-type ---P5 */
30  fgets(buffer, MAX_BUFFERSIZE, fp);
31  if (buffer[0] != 'P' || buffer[1] != '5') {
32  printf(" Mistaken file format, not P5!\n\n");
33  exit(1);
34  }
35  /* input of x_size1, y_size1 */
36  *x_size1 = 0;
37  *y_size1 = 0;
38  while (*x_size1 == 0 || *y_size1 == 0) {
39  fgets(buffer, MAX_BUFFERSIZE, fp);
40  if (buffer[0] != '#') {
41  sscanf(buffer, "%d %d", x_size1, y_size1);
42  }
43  }
44  /* input of max_gray */
45  max_gray = 0;
46  while (max_gray == 0) {
47  fgets(buffer, MAX_BUFFERSIZE, fp);
48  if (buffer[0] != '#') {
49  sscanf(buffer, "%d", &max_gray);
50  }
51  }
52  /* Display of parameters */
53  printf("\n Image width = %d, Image height = %d\n", *x_size1, *y_size1);
54  printf(" Maximum gray level = %d\n\n",max_gray);
55  if (*x_size1 > MAX_IMAGESIZE || *y_size1 > MAX_IMAGESIZE) {
56  printf(" Image size exceeds %d x %d\n\n",
58  printf(" Please use smaller images!\n\n");
59  exit(1);
60  }
61  if (max_gray != MAX_BRIGHTNESS) {
62  printf(" Invalid value of maximum gray level!\n\n");
63  exit(1);
64  }
65  /* Input of image data*/
66  for (y = 0; y < *y_size1; y++) {
67  for (x = 0; x < *x_size1; x++) {
68  *(image1 + y + x * MAX_IMAGESIZE) = (unsigned char)fgetc(fp);
69  }
70  }
71  printf("-----Image data input OK-----\n\n");
72  printf("-----------------------------------------------------\n\n");
73  fclose(fp);
74 }
75 
76 void save_image_data(const char* file_name, unsigned char * image2, unsigned int x_size2, unsigned int y_size2)
77 /* Output of image2[ ][ ], x_size2, y_size2 in pgm format*/
78 
79 {
80  FILE *fp; /* File pointer */
81  int x, y; /* Loop variable */
82 
83  /* Output file open */
84  printf("-----------------------------------------------------\n");
85  printf("Monochromatic image file output routine\n");
86  printf("-----------------------------------------------------\n\n");
87  printf("Name of output image file: %s\n", file_name);
88  fp = fopen(file_name, "wb");
89  /* output of pgm file header information */
90  fputs("P5\n", fp);
91  fputs("# Created by Image Processing\n", fp);
92  fprintf(fp, "%d %d\n", x_size2, y_size2);
93  fprintf(fp, "%d\n", MAX_BRIGHTNESS);
94  /* Output of image data */
95  for (y = 0; y < y_size2; y++) {
96  for (x = 0; x < x_size2; x++) {
97  fputc(*(image2 + y + x * MAX_IMAGESIZE), fp);
98  }
99  }
100  printf("\n-----Image data output OK-----\n\n");
101  printf("-----------------------------------------------------\n\n");
102  fclose(fp);
103 }
#define NULL
#define MAX_IMAGESIZE
Definition: mypgm.h:7
#define MAX_BRIGHTNESS
Definition: mypgm.h:8
void save_image_data(const char *file_name, unsigned char *image2, unsigned int x_size2, unsigned int y_size2)
Definition: mypgm.c:76
void load_image_data(const char *file_name, unsigned char *image1, unsigned int *x_size1, unsigned int *y_size1)
Definition: mypgm.c:9
x
Return the smallest n such that 2^n >= _x.
#define MAX_BUFFERSIZE
Definition: mypgm.h:11

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13