This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgm_io.cu
executable file
·247 lines (227 loc) · 10 KB
/
pgm_io.cu
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
* FILE: pgm_io.c
* This code was written by Mike Heath. heath@csee.usf.edu (in 1995).
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/******************************************************************************
* Function: read_pgm_image
* Purpose: This function reads in an image in PGM format. The image can be
* read in from either a file or from standard input. The image is only read
* from standard input when infilename = NULL. Because the PGM format includes
* the number of columns and the number of rows in the image, these are read
* from the file. Memory to store the image is allocated in this function.
* All comments in the header are discarded in the process of reading the
* image. Upon failure, this function returns 0, upon sucess it returns 1.
******************************************************************************/
int read_pgm_image(char *infilename, unsigned char **image, int *rows,
int *cols)
{
errno_t err;
FILE *fp;
char buf[71];
/***************************************************************************
* Open the input image file for reading if a filename was given. If no
* filename was provided, set fp to read from standard input.
***************************************************************************/
if (infilename == NULL) fp = stdin;
else{
if ((err = fopen_s(&fp, infilename, "rb")) != 0){
fprintf(stderr, "Error reading the file '%s' in read_pgm_image(): %d \n",
infilename, err);
return(0);
}
}
/***************************************************************************
* Verify that the image is in PGM format, read in the number of columns
* and rows in the image and scan past all of the header information.
***************************************************************************/
fgets(buf, 70, fp);
if (strncmp(buf, "P5", 2) != 0){
fprintf(stderr, "The file %s is not in PGM format in ", infilename);
fprintf(stderr, "read_pgm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
do{
fgets(buf, 70, fp);
} while (buf[0] == '#' || buf[0] == '\n'); /* skip all comment lines */
sscanf_s(buf, "%d %d", cols, rows);
do{
fgets(buf, 70, fp);
} while (buf[0] == '#'); /* skip all comment lines */
/***************************************************************************
* Allocate memory to store the image then read the image from the file.
***************************************************************************/
if (((*image) = (unsigned char *)malloc((*rows)*(*cols))) == NULL){
fprintf(stderr, "Memory allocation failure in read_pgm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
int tmp = fread((*image), (*cols), (*rows), fp);
if ((*rows) != tmp){
fprintf(stderr, "Error reading the image data in read_pgm_image().\n");
if (fp != stdin) fclose(fp);
free((*image));
return(0);
}
if (fp != stdin) fclose(fp);
return(1);
}
/******************************************************************************
* Function: write_pgm_image
* Purpose: This function writes an image in PGM format. The file is either
* written to the file specified by outfilename or to standard output if
* outfilename = NULL. A comment can be written to the header if coment != NULL.
******************************************************************************/
int write_pgm_image(char *outfilename, unsigned char *image, int rows,
int cols, char *comment, int maxval)
{
FILE *fp;
errno_t err;
/***************************************************************************
* Open the output image file for writing if a filename was given. If no
* filename was provided, set fp to write to standard output.
***************************************************************************/
if (outfilename == NULL) fp = stdout;
else{
if ((err = fopen_s(&fp, outfilename, "wb")) != 0){
fprintf(stderr, "Error writing the file %s in write_pgm_image(): %d \n",
outfilename, err);
return(0);
}
}
/***************************************************************************
* Write the header information to the PGM file.
***************************************************************************/
fprintf(fp, "P5\n%d %d\n", cols, rows);
if (comment != NULL)
if (strlen(comment) <= 70) fprintf(fp, "# %s\n", comment);
fprintf(fp, "%d\n", maxval);
/***************************************************************************
* Write the image data to the file.
***************************************************************************/
if (rows != fwrite(image, cols, rows, fp)){
fprintf(stderr, "Error writing the image data in write_pgm_image().\n");
if (fp != stdout) fclose(fp);
return(0);
}
if (fp != stdout) fclose(fp);
return(1);
}
/******************************************************************************
* Function: read_ppm_image
* Purpose: This function reads in an image in PPM format. The image can be
* read in from either a file or from standard input. The image is only read
* from standard input when infilename = NULL. Because the PPM format includes
* the number of columns and the number of rows in the image, these are read
* from the file. Memory to store the image is allocated in this function.
* All comments in the header are discarded in the process of reading the
* image. Upon failure, this function returns 0, upon sucess it returns 1.
******************************************************************************/
int read_ppm_image(char *infilename, unsigned char **image_red,
unsigned char **image_grn, unsigned char **image_blu, int *rows,
int *cols)
{
FILE *fp;
errno_t err;
char buf[71];
int p, size;
/***************************************************************************
* Open the input image file for reading if a filename was given. If no
* filename was provided, set fp to read from standard input.
***************************************************************************/
if (infilename == NULL) fp = stdin;
else{
if ((err = fopen_s(&fp, infilename, "rb")) != 0){
fprintf(stderr, "Error reading the file %s in read_ppm_image(): %d \n",
infilename, err);
return(0);
}
}
/***************************************************************************
* Verify that the image is in PPM format, read in the number of columns
* and rows in the image and scan past all of the header information.
***************************************************************************/
fgets(buf, 70, fp);
if (strncmp(buf, "P6", 2) != 0){
fprintf(stderr, "The file %s is not in PPM format in ", infilename);
fprintf(stderr, "read_ppm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
do{ fgets(buf, 70, fp); } while (buf[0] == '#'); /* skip all comment lines */
sscanf_s(buf, "%d %d", cols, rows);
do{ fgets(buf, 70, fp); } while (buf[0] == '#'); /* skip all comment lines */
/***************************************************************************
* Allocate memory to store the image then read the image from the file.
***************************************************************************/
if (((*image_red) = (unsigned char *)malloc((*rows)*(*cols))) == NULL){
fprintf(stderr, "Memory allocation failure in read_ppm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
if (((*image_grn) = (unsigned char *)malloc((*rows)*(*cols))) == NULL){
fprintf(stderr, "Memory allocation failure in read_ppm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
if (((*image_blu) = (unsigned char *)malloc((*rows)*(*cols))) == NULL){
fprintf(stderr, "Memory allocation failure in read_ppm_image().\n");
if (fp != stdin) fclose(fp);
return(0);
}
size = (*rows)*(*cols);
for (p = 0; p<size; p++){
(*image_red)[p] = (unsigned char)fgetc(fp);
(*image_grn)[p] = (unsigned char)fgetc(fp);
(*image_blu)[p] = (unsigned char)fgetc(fp);
}
if (fp != stdin) fclose(fp);
return(1);
}
/******************************************************************************
* Function: write_ppm_image
* Purpose: This function writes an image in PPM format. The file is either
* written to the file specified by outfilename or to standard output if
* outfilename = NULL. A comment can be written to the header if coment != NULL.
******************************************************************************/
int write_ppm_image(char *outfilename, unsigned char *image_red,
unsigned char *image_grn, unsigned char *image_blu, int rows,
int cols, char *comment, int maxval)
{
FILE *fp;
errno_t err;
long size, p;
/***************************************************************************
* Open the output image file for writing if a filename was given. If no
* filename was provided, set fp to write to standard output.
***************************************************************************/
if (outfilename == NULL) fp = stdout;
else{
if ((err = fopen_s(&fp, outfilename, "wb")) != 0){
fprintf(stderr, "Error writing the file %s in write_pgm_image(): %d \n",
outfilename, err);
return(0);
}
}
/***************************************************************************
* Write the header information to the PGM file.
***************************************************************************/
fprintf(fp, "P6\n%d %d\n", cols, rows);
if (comment != NULL)
if (strlen(comment) <= 70) fprintf(fp, "# %s\n", comment);
fprintf(fp, "%d\n", maxval);
/***************************************************************************
* Write the image data to the file.
***************************************************************************/
size = (long)rows * (long)cols;
for (p = 0; p<size; p++){ /* Write the image in pixel interleaved format. */
fputc(image_red[p], fp);
fputc(image_grn[p], fp);
fputc(image_blu[p], fp);
}
if (fp != stdout) fclose(fp);
return(1);
}