-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmypgm_omp.h
204 lines (189 loc) · 5.9 KB
/
mypgm_omp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* pgm file IO headerfile ------ mypgm.h */
/* Constant declaration */
#define MAX_IMAGESIZE 4000
#define MAX_BRIGHTNESS 255 /* Maximum gray level */
#define GRAYLEVEL 256 /* No. of gray levels */
#define MAX_FILENAME 256 /* Filename length limit */
#define MAX_BUFFERSIZE 256
/* Global constant declaration */
/* Image storage arrays */
unsigned char image1[MAX_IMAGESIZE][MAX_IMAGESIZE],
image2[MAX_IMAGESIZE][MAX_IMAGESIZE];
int x_size1, y_size1, /* width & height of image1*/
x_size2, y_size2; /* width & height of image2 */
/* Prototype declaration of functions */
void load_image_data( ); /* image input */
void save_image_data( ); /* image output*/
void load_image_file(char *); /* image input */
void save_image_file(char *); /* image output*/
/* Main body of functions */
void load_image_data( )
/* Input of header & body information of pgm file */
/* for image1[ ][ ]Cx_size1Cy_size1 */
{
char file_name[MAX_FILENAME];
char buffer[MAX_BUFFERSIZE];
FILE *fp; /* File pointer */
int max_gray; /* Maximum gray level */
int x, y; /* Loop variable */
/* Input file open */
printf("\n-----------------------------------------------------\n");
printf("Monochromatic image file input routine \n");
printf("-----------------------------------------------------\n\n");
printf(" Only pgm binary file is acceptable\n\n");
printf("Name of input image file? (*.pgm) : ");
scanf("%s", file_name);
fp = fopen(file_name, "rb");
if (NULL == fp) {
printf(" The file doesn't exist!\n\n");
exit(1);
}
/* Check of file-type ---P5 */
fgets(buffer, MAX_BUFFERSIZE, fp);
if (buffer[0] != 'P' || buffer[1] != '5') {
printf(" Mistaken file format, not P5!\n\n");
exit(1);
}
/* input of x_size1, y_size1 */
x_size1 = 0;
y_size1 = 0;
while (x_size1 == 0 || y_size1 == 0) {
fgets(buffer, MAX_BUFFERSIZE, fp);
if (buffer[0] != '#') {
sscanf(buffer, "%d %d", &x_size1, &y_size1);
}
}
/* input of max_gray */
max_gray = 0;
while (max_gray == 0) {
fgets(buffer, MAX_BUFFERSIZE, fp);
if (buffer[0] != '#') {
sscanf(buffer, "%d", &max_gray);
}
}
/* Display of parameters */
printf("\n Image width = %d, Image height = %d\n", x_size1, y_size1);
printf(" Maximum gray level = %d\n\n",max_gray);
if (x_size1 > MAX_IMAGESIZE || y_size1 > MAX_IMAGESIZE) {
printf(" Image size exceeds %d x %d\n\n",
MAX_IMAGESIZE, MAX_IMAGESIZE);
printf(" Please use smaller images!\n\n");
exit(1);
}
if (max_gray != MAX_BRIGHTNESS) {
printf(" Invalid value of maximum gray level!\n\n");
exit(1);
}
/* Input of image data*/
for (y = 0; y < y_size1; y++) {
for (x = 0; x < x_size1; x++) {
image1[y][x] = (unsigned char)fgetc(fp);
}
}
printf("-----Image data input OK-----\n\n");
printf("-----------------------------------------------------\n\n");
fclose(fp);
}
void save_image_data( )
/* Output of image2[ ][ ], x_size2, y_size2 in pgm format*/
{
char file_name[MAX_FILENAME];
FILE *fp; /* File pointer */
int x, y; /* Loop variable */
/* Output file open */
printf("-----------------------------------------------------\n");
printf("Monochromatic image file output routine\n");
printf("-----------------------------------------------------\n\n");
//printf("Name of output image file? (*.pgm) : ");
//scanf("%s",file_name);
fp = fopen("out.pgm", "wb");
/* output of pgm file header information */
fputs("P5\n", fp);
fputs("# Created by Image Processing\n", fp);
fprintf(fp, "%d %d\n", x_size2, y_size2);
fprintf(fp, "%d\n", MAX_BRIGHTNESS);
/* Output of image data */
for (y = 0; y < y_size2; y++) {
for (x = 0; x < x_size2; x++) {
fputc(image2[y][x], fp);
}
}
printf("\n-----Image data output OK-----\n\n");
printf("-----------------------------------------------------\n\n");
fclose(fp);
}
void load_image_file(char *filename)
/* Input of header & body information of pgm file */
/* for image1[ ][ ]Cx_size1Cy_size1 */
{
char buffer[MAX_BUFFERSIZE];
FILE *fp; /* File pointer */
int max_gray; /* Maximum gray level */
int x, y; /* Loop variable */
/* Input file open */
fp = fopen(filename, "rb");
if (NULL == fp) {
printf(" The file doesn't exist!\n\n");
exit(1);
}
/* Check of file-type ---P5 */
fgets(buffer, MAX_BUFFERSIZE, fp);
if (buffer[0] != 'P' || buffer[1] != '5') {
printf(" Mistaken file format, not P5!\n\n");
exit(1);
}
/* input of x_size1, y_size1 */
x_size1 = 0;
y_size1 = 0;
while (x_size1 == 0 || y_size1 == 0) {
fgets(buffer, MAX_BUFFERSIZE, fp);
if (buffer[0] != '#') {
sscanf(buffer, "%d %d", &x_size1, &y_size1);
}
}
/* input of max_gray */
max_gray = 0;
while (max_gray == 0) {
fgets(buffer, MAX_BUFFERSIZE, fp);
if (buffer[0] != '#') {
sscanf(buffer, "%d", &max_gray);
}
}
if (x_size1 > MAX_IMAGESIZE || y_size1 > MAX_IMAGESIZE) {
printf(" Image size exceeds %d x %d\n\n",
MAX_IMAGESIZE, MAX_IMAGESIZE);
printf(" Please use smaller images!\n\n");
exit(1);
}
if (max_gray != MAX_BRIGHTNESS) {
printf(" Invalid value of maximum gray level!\n\n");
exit(1);
}
/* Input of image data*/
for (y = 0; y < y_size1; y++) {
for (x = 0; x < x_size1; x++) {
image1[y][x] = (unsigned char)fgetc(fp);
}
}
fclose(fp);
}
void save_image_file(char *filename)
/* Output of image2[ ][ ], x_size2, y_size2 */
/* into pgm file with header & body information */
{
FILE *fp; /* File pointer */
int x, y; /* Loop variable */
fp = fopen(filename, "wb");
/* output of pgm file header information */
fputs("P5\n", fp);
fputs("# Created by Image Processing\n", fp);
fprintf(fp, "%d %d\n", x_size2, y_size2);
fprintf(fp, "%d\n", MAX_BRIGHTNESS);
/* Output of image data */
for (y = 0; y < y_size2; y++) {
for (x = 0; x < x_size2; x++) {
fputc(image2[y][x], fp);
}
}
fclose(fp);
}