-
Notifications
You must be signed in to change notification settings - Fork 2
/
pnm.c
310 lines (243 loc) · 10.7 KB
/
pnm.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* ############################################################################
Name : pnm.c
Company : ALCON 2009
Project : Finding Clones
Programmer : ALCON 2009
Revisor : S. Vagionitis
Description : PNM Functions
Procedure Description
============================= =================================================
read_pnm_header Read a PNM file header.
load_ppm Read PPM binary file (needs to free the return
pointer).
load_pgm Read PGM binary file (needs to free the return
pointer).
save_ppm Save into PPM binary file.
save_pgm Save into PGM binary file.
Globals Type Description
============== ============== =================================================
Programmer Date Action
============== ============== =================================================
S. Vagionitis 04/06/2010 Creation
############################################################################ */
#define __PNM_C__
#include "pnm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* buffer size of file input */
#define BUF_SIZE 256
/* ############################################################################
Name : read_pnm_header
Description : Read a PNM file header. "P5" is header for PGM (grayscale),
"P6" is header for PPM (color).
Arguments Type Description
============== ============== =================================================
f(IN) FILE * File descriptor of the image.
w(OUT) int * Width of the image.
h(OUT) int * Height of the image.
max(OUT) int * The maximum value of the colour components for
the pixels.
Return Values Description
============================= =================================================
FALSE If wrong "magic" ppm identifier.
FALSE If width or height is less than zero.
FALSE If the maximum value of colour components for
pixels is more than 255.
ret The "magic" ppm identifier, could be 6 or 5, from
P6 or P5 respectively.
Globals Type Description
============== ============== =================================================
Locals Type Description
============== ============== =================================================
buf char[] Buffer for image header.
ret int The "magic" ppm identifier, could be 6 or 5, from
P6 or P5 respectively.
############################################################################ */
static int read_pnm_header(FILE *f, int *w, int *h, int *max)
{
char buf[BUF_SIZE];
memset(buf, 0, sizeof(buf));
int ret;
fgets(buf, BUF_SIZE, f);
if (strcmp(buf, "P6\n") == 0) {
ret = 6;
} else if (strcmp(buf, "P5\n") == 0) {
ret = 5;
} else {
return FALSE;
}
do {
fgets(buf, BUF_SIZE, f);
} while (buf[0] == '#');
sscanf(buf, "%d %d", w, h);
if (*w < 0 || *h < 0) return FALSE;
fgets(buf, BUF_SIZE, f);
sscanf(buf, "%d", max);
if (*max > 255) return FALSE;
return ret;
}
/* ############################################################################
Name : load_ppm
Description : Read PPM binary file (needs to free the return pointer). Load
the image data in memory.
Arguments Type Description
============== =================== ===========================================
filename(IN) unsigned char * Filename of the image file.
width(OUT) int * Width of the image.
height(OUT) int * Height of the image.
Return Values Description
================================== ===========================================
data The image data.
Globals Type Description
============== =================== ===========================================
Locals Type Description
============== =================== ===========================================
data unsigned char * The image data.
max int The maximum value of the colour components
for the pixels.
w int The width of the image.
h int The height of the image.
c size_t The total number of elements successfully
read from image file.
############################################################################ */
unsigned char *load_ppm(const char *filename, int *width, int *height)
{
unsigned char *data = NULL;
int max, w, h;
size_t c;
FILE *f = fopen(filename, "rb");
if (!f) return NULL;
if (read_pnm_header(f, &w, &h, &max) != 6) {
fclose(f);
return NULL;
}
data = (unsigned char *)malloc(3*w*h);
if (data == NULL){
printf("Cannot allocate %d bytes for memory.\n", (3*w*h));
exit(-1);
}
c = fread(data, 1, 3*w*h, f);
if (c != w*h*3) {
free(data);
data = NULL;
}
*width = w;
*height = h;
return data;
}
/* ############################################################################
Name : load_pgm
Description : Read PGM binary file (needs to free the return pointer).Load
the image data in memory.
Arguments Type Description
============== =================== ===========================================
filename(IN) unsigned char * Filename of the image file.
width(OUT) int * Width of the image.
height(OUT) int * Height of the image.
Return Values Description
================================== ===========================================
data The image data.
Globals Type Description
============== =================== ===========================================
Locals Type Description
============== =================== ===========================================
data unsigned char * The image data.
max int The maximum value of the colour components
for the pixels.
w int The width of the image.
h int The height of the image.
c size_t The total number of elements successfully
read from image file.
############################################################################ */
unsigned char *load_pgm(const char *filename, int *width, int *height)
{
unsigned char *data = NULL;
int max, w, h;
size_t c;
FILE *f = fopen(filename, "rb");
if (!f) return NULL;
if (read_pnm_header(f, &w, &h, &max) != 5) {
fclose(f);
return NULL;
}
data = (unsigned char *)malloc(w*h);
if (data == NULL){
printf("Cannot allocate %d bytes for memory.\n", (w*h));
exit(-1);
}
c = fread(data, 1, w*h, f);
if (c != w*h) {
free(data);
data = NULL;
}
*width = w;
*height = h;
return data;
}
/* ############################################################################
Name : save_ppm
Description : Save into PPM binary file.
Arguments Type Description
============== =================== ===========================================
filename(IN) unsigned char * Filename of the image file.
width(IN) int Width of the image.
height(IN) int Height of the image.
data(IN) unsigned char * The image data.
Return Values Description
================================== ===========================================
TRUE If everything OK.
FALSE If file descriptor is not OK or could not
write to the file.
Globals Type Description
============== =================== ===========================================
Locals Type Description
============== =================== ===========================================
f FILE * The image file descriptor.
############################################################################ */
int save_ppm(const char *filename, int width, int height, unsigned char *data)
{
int bytes2write = 3*width*height;
FILE *f = fopen(filename, "wb");
if (!f) return FALSE;
fprintf(f, "P6\n%d %d\n255\n", width, height);
if (fwrite(data, 1, bytes2write, f) != bytes2write){
fclose(f);
return FALSE;
}
fclose(f);
return TRUE;
}
/* ############################################################################
Name : save_pgm
Description : Save into PGM binary file.
Arguments Type Description
============== =================== ===========================================
filename(IN) unsigned char * Filename of the image file.
width(IN) int Width of the image.
height(IN) int Height of the image.
data(IN) unsigned char * The image data.
Return Values Description
================================== ===========================================
TRUE If everything OK.
FALSE If file descriptor is not OK or could not
write to the file.
Globals Type Description
============== =================== ===========================================
Locals Type Description
============== =================== ===========================================
f FILE * The image file descriptor.
############################################################################ */
int save_pgm(const char *filename, int width, int height, unsigned char *data)
{
int bytes2write = width*height;
FILE *f = fopen(filename, "wb");
if (!f) return FALSE;
fprintf(f, "P5\n%d %d\n255\n", width, height);
if (fwrite(data, 1, bytes2write, f) != bytes2write){
fclose(f);
return FALSE;
}
fclose(f);
return TRUE;
}