pnm.c

filter: Statementstmt 82.2% 367 / 446 Branchbran 71.8% 247 / 344 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.7% 614 / 790 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« iminline.h ↑ Summary fills.c »
1 #include "imager.h"
2 #include "log.h"
3 #include "iolayer.h"
4 #include "imageri.h"
5  
6 #include <stdlib.h>
7 #include <errno.h>
8  
9  
10 /*
11 =head1 NAME
12  
13 pnm.c - implements reading and writing ppm/pnm/pbm files, uses io layer.
14  
15 =head1 SYNOPSIS
16  
17 io_glue *ig = io_new_fd( fd );
18 i_img *im = i_readpnm_wiol(ig, 0); // no limit on how much is read
19 // or
20 io_glue *ig = io_new_fd( fd );
21 return_code = i_writepnm_wiol(im, ig);
22  
23 =head1 DESCRIPTION
24  
25 pnm.c implements the basic functions to read and write portable
26 anymap files. It uses the iolayer and needs either a seekable source
27 or an entire memory mapped buffer.
28  
29 =head1 FUNCTION REFERENCE
30  
31 Some of these functions are internal.
32  
33 =over
34  
35 =cut
36 */
37  
38  
39 #define misspace(x) (x==' ' || x=='\n' || x=='\r' || x=='\t' || x=='\f' || x=='\v')
40 #define misnumber(x) (x <= '9' && x>='0')
41  
42 static char *typenames[]={"ascii pbm", "ascii pgm", "ascii ppm", "binary pbm", "binary pgm", "binary ppm"};
43  
44 /*
45 =item skip_spaces(ig)
46  
47 Advances in stream until it is positioned at a
48 non white space character. (internal)
49  
50 ig - io_glue
51  
52 =cut
53 */
54  
55 static
56 int
57 608 skip_spaces(io_glue *ig) {
58 int c;
59 915 ▶ while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) {
Branch: 8 branches
BranchTrueFalseTotal
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 907 8 915
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 907 8 915
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 148 759 907
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 159 600 759
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 600 600
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 600 600
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 600 600
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 600 600
60 307 ▶ if ( i_io_getc(ig) == EOF )
Branch: 2 branches
BranchTrueFalseTotal
if ( i_io_getc(ig) == EOF ) 307 0 307
if ( i_io_getc(ig) == EOF ) 0 307 307
61 0 break;
62 }
63 608 ▶ if (c == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (c == EOF) 8 600 608
64 8 return 0;
65  
66 600 return 1;
67 }
68  
69  
70 /*
71 =item skip_comment(ig)
72  
73 Advances in stream over whitespace and a comment if one is found. (internal)
74  
75 ig - io_glue object
76  
77 =cut
78 */
79  
80 static
81 int
82 253 skip_comment(io_glue *ig) {
83 int c;
84  
85 253 ▶ if (!skip_spaces(ig))
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_spaces(ig)) 1 252 253
86 1 return 0;
87  
88 252 ▶ if ((c = i_io_peekc(ig)) == EOF)
Branch: 2 branches
BranchTrueFalseTotal
if ((c = i_io_peekc(ig)) == EOF) 252 0 252
if ((c = i_io_peekc(ig)) == EOF) 0 252 252
89 0 return 0;
90  
91 252 ▶ if (c == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '#') { 66 186 252
92 2286 ▶ while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) {
Branch: 4 branches
BranchTrueFalseTotal
while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) { 2286 0 2286
while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) { 2286 0 2286
while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) { 2220 66 2286
while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) { 2220 0 2220
93 2220 ▶ if ( i_io_getc(ig) == EOF )
Branch: 2 branches
BranchTrueFalseTotal
if ( i_io_getc(ig) == EOF ) 2220 0 2220
if ( i_io_getc(ig) == EOF ) 0 2220 2220
94 0 break;
95 }
96 }
97 252 ▶ if (c == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (c == EOF) 0 252 252
98 0 return 0;
99
100 252 return 1;
101 }
102  
103  
104 /*
105 =item gnum(mb, i)
106  
107 Fetches the next number from stream and stores in i, returns true
108 on success else false.
109  
110 mb - buffer object
111 i - integer to store result in
112  
113 =cut
114 */
115  
116 static
117 int
118 328 gnum(io_glue *ig, unsigned *i) {
119 int c;
120 328 *i = 0;
121  
122 328 ▶ if (!skip_spaces(ig)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_spaces(ig)) return 0; 4 324 328
123  
124 324 ▶ if ((c = i_io_peekc(ig)) == EOF)
Branch: 2 branches
BranchTrueFalseTotal
if ((c = i_io_peekc(ig)) == EOF) 324 0 324
if ((c = i_io_peekc(ig)) == EOF) 0 324 324
125 0 return 0;
126 324 ▶ if (!misnumber(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!misnumber(c)) 320 4 324
if (!misnumber(c)) 0 320 320
127 4 return 0;
128 1129 ▶ while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) {
Branch: 4 branches
BranchTrueFalseTotal
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 1127 2 1129
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 1127 2 1129
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 1127 0 1127
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 811 316 1127
129 811 unsigned work = *i * 10 + (c - '0');
130 811 ▶ if (work < *i) {
Branch: 1 branch
BranchTrueFalseTotal
if (work < *i) { 2 809 811
131 /* overflow */
132 2 i_push_error(0, "integer overflow");
133 2 return 0;
134 }
135 809 *i = work;
136 809 ▶ i_io_nextc(ig);
Branch: 1 branch
BranchTrueFalseTotal
i_io_nextc(ig); 809 0 809
137 }
138  
139 318 return 1;
140 }
141  
142 static
143 i_img *
144 52 read_pgm_ppm_bin8(io_glue *ig, i_img *im, int width, int height,
145 int channels, unsigned maxval, int allow_incomplete) {
146 i_color *line, *linep;
147 int read_size;
148 unsigned char *read_buf, *readp;
149 int x, y, ch;
150 52 unsigned rounder = maxval / 2;
151  
152 52 line = mymalloc(width * sizeof(i_color));
153 52 read_size = channels * width;
154 52 read_buf = mymalloc(read_size);
155 5666 ▶ for(y=0;y<height;y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y=0;y<height;y++) { 5618 48 5666
156 5618 linep = line;
157 5618 readp = read_buf;
158 5618 ▶ if (i_io_read(ig, read_buf, read_size) != read_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_read(ig, read_buf, read_size) != read_size) { 4 5614 5618
159 4 myfree(line);
160 4 myfree(read_buf);
161 4 ▶ if (allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_incomplete) { 2 2 4
162 2 i_tags_setn(&im->tags, "i_incomplete", 1);
163 2 i_tags_setn(&im->tags, "i_lines_read", y);
164 2 return im;
165 }
166 else {
167 2 i_push_error(0, "short read - file truncated?");
168 2 i_img_destroy(im);
169 2 return NULL;
170 }
171 }
172 5614 ▶ if (maxval == 255) {
Branch: 1 branch
BranchTrueFalseTotal
if (maxval == 255) { 5612 2 5614
173 854844 ▶ for(x=0; x<width; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x=0; x<width; x++) { 849232 5612 854844
174 3351904 ▶ for(ch=0; ch<channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<channels; ch++) { 2502672 849232 3351904
175 2502672 linep->channel[ch] = *readp++;
176 }
177 849232 ++linep;
178 }
179 }
180 else {
181 8 ▶ for(x=0; x<width; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x=0; x<width; x++) { 6 2 8
182 24 ▶ for(ch=0; ch<channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<channels; ch++) { 18 6 24
183 /* we just clamp samples to the correct range */
184 18 unsigned sample = *readp++;
185 18 ▶ if (sample > maxval)
Branch: 1 branch
BranchTrueFalseTotal
if (sample > maxval) 0 18 18
186 0 sample = maxval;
187 18 linep->channel[ch] = (sample * 255 + rounder) / maxval;
188 }
189 6 ++linep;
190 }
191 }
192 5614 i_plin(im, 0, width, y, line);
193 }
194 48 myfree(read_buf);
195 48 myfree(line);
196  
197 48 return im;
198 }
199  
200 static
201 i_img *
202 8 read_pgm_ppm_bin16(io_glue *ig, i_img *im, int width, int height,
203 int channels, unsigned maxval, int allow_incomplete) {
204 i_fcolor *line, *linep;
205 int read_size;
206 unsigned char *read_buf, *readp;
207 int x, y, ch;
208 8 double maxvalf = maxval;
209  
210 8 line = mymalloc(width * sizeof(i_fcolor));
211 8 read_size = channels * width * 2;
212 8 read_buf = mymalloc(read_size);
213 196 ▶ for(y=0;y<height;y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y=0;y<height;y++) { 192 4 196
214 192 linep = line;
215 192 readp = read_buf;
216 192 ▶ if (i_io_read(ig, read_buf, read_size) != read_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_read(ig, read_buf, read_size) != read_size) { 4 188 192
217 4 myfree(line);
218 4 myfree(read_buf);
219 4 ▶ if (allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_incomplete) { 2 2 4
220 2 i_tags_setn(&im->tags, "i_incomplete", 1);
221 2 i_tags_setn(&im->tags, "i_lines_read", y);
222 2 return im;
223 }
224 else {
225 2 i_push_error(0, "short read - file truncated?");
226 2 i_img_destroy(im);
227 2 return NULL;
228 }
229 }
230 23210 ▶ for(x=0; x<width; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x=0; x<width; x++) { 23022 188 23210
231 92080 ▶ for(ch=0; ch<channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<channels; ch++) { 69058 23022 92080
232 69058 unsigned sample = (readp[0] << 8) + readp[1];
233 69058 ▶ if (sample > maxval)
Branch: 1 branch
BranchTrueFalseTotal
if (sample > maxval) 0 69058 69058
234 0 sample = maxval;
235 69058 readp += 2;
236 69058 linep->channel[ch] = sample / maxvalf;
237 }
238 23022 ++linep;
239 }
240 188 i_plinf(im, 0, width, y, line);
241 }
242 4 myfree(read_buf);
243 4 myfree(line);
244  
245 4 return im;
246 }
247  
248 static
249 i_img *
250 4 read_pbm_bin(io_glue *ig, i_img *im, int width, int height, int allow_incomplete) {
251 i_palidx *line, *linep;
252 int read_size;
253 unsigned char *read_buf, *readp;
254 int x, y;
255 unsigned mask;
256  
257 4 line = mymalloc(width * sizeof(i_palidx));
258 4 read_size = (width + 7) / 8;
259 4 read_buf = mymalloc(read_size);
260 26 ▶ for(y = 0; y < height; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < height; y++) { 24 2 26
261 24 ▶ if (i_io_read(ig, read_buf, read_size) != read_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_read(ig, read_buf, read_size) != read_size) { 2 22 24
262 2 myfree(line);
263 2 myfree(read_buf);
264 2 ▶ if (allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_incomplete) { 1 1 2
265 1 i_tags_setn(&im->tags, "i_incomplete", 1);
266 1 i_tags_setn(&im->tags, "i_lines_read", y);
267 1 return im;
268 }
269 else {
270 1 i_push_error(0, "short read - file truncated?");
271 1 i_img_destroy(im);
272 1 return NULL;
273 }
274 }
275 22 linep = line;
276 22 readp = read_buf;
277 22 mask = 0x80;
278 254 ▶ for(x = 0; x < width; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < width; ++x) { 232 22 254
279 232 *linep++ = *readp & mask ? 1 : 0;
280 232 mask >>= 1;
281 232 ▶ if (mask == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (mask == 0) { 24 208 232
282 24 ++readp;
283 24 mask = 0x80;
284 }
285 }
286 22 ▶ i_ppal(im, 0, width, y, line);
Branch: 1 branch
BranchTrueFalseTotal
i_ppal(im, 0, width, y, line); 22 0 22
287 }
288 2 myfree(read_buf);
289 2 myfree(line);
290  
291 2 return im;
292 }
293  
294 /* unlike pgm/ppm pbm:
295 - doesn't require spaces between samples (bits)
296 - 1 (maxval) is black instead of white
297 */
298 static
299 i_img *
300 6 read_pbm_ascii(io_glue *ig, i_img *im, int width, int height, int allow_incomplete) {
301 i_palidx *line, *linep;
302 int x, y;
303  
304 6 line = mymalloc(width * sizeof(i_palidx));
305 14 ▶ for(y = 0; y < height; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y = 0; y < height; y++) { 12 2 14
306 12 linep = line;
307 32 ▶ for(x = 0; x < width; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for(x = 0; x < width; ++x) { 24 8 32
308 int c;
309 24 skip_spaces(ig);
310 24 ▶ if ((c = i_io_getc(ig)) == EOF || (c != '0' && c != '1')) {
Branch: 4 branches
BranchTrueFalseTotal
if ((c = i_io_getc(ig)) == EOF || (c != '0' && c != '1')) { 22 2 24
if ((c = i_io_getc(ig)) == EOF || (c != '0' && c != '1')) { 22 2 24
if ((c = i_io_getc(ig)) == EOF || (c != '0' && c != '1')) { 12 10 22
if ((c = i_io_getc(ig)) == EOF || (c != '0' && c != '1')) { 2 10 12
311 4 myfree(line);
312 4 ▶ if (allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_incomplete) { 2 2 4
313 2 i_tags_setn(&im->tags, "i_incomplete", 1);
314 2 i_tags_setn(&im->tags, "i_lines_read", y);
315 2 return im;
316 }
317 else {
318 2 ▶ if (c != EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (c != EOF) 1 1 2
319 1 i_push_error(0, "invalid data for ascii pnm");
320 else
321 1 i_push_error(0, "short read - file truncated?");
322 2 i_img_destroy(im);
323 2 return NULL;
324 }
325 }
326 20 *linep++ = c == '0' ? 0 : 1;
327 }
328 8 ▶ i_ppal(im, 0, width, y, line);
Branch: 1 branch
BranchTrueFalseTotal
i_ppal(im, 0, width, y, line); 8 0 8
329 }
330 2 myfree(line);
331  
332 2 return im;
333 }
334  
335 static
336 i_img *
337 10 read_pgm_ppm_ascii(io_glue *ig, i_img *im, int width, int height, int channels,
338 unsigned maxval, int allow_incomplete) {
339 i_color *line, *linep;
340 int x, y, ch;
341 10 unsigned rounder = maxval / 2;
342  
343 10 line = mymalloc(width * sizeof(i_color));
344 21 ▶ for(y=0;y<height;y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y=0;y<height;y++) { 19 2 21
345 19 linep = line;
346 48 ▶ for(x=0; x<width; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x=0; x<width; x++) { 37 11 48
347 96 ▶ for(ch=0; ch<channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<channels; ch++) { 67 29 96
348 unsigned sample;
349
350 67 ▶ if (!gnum(ig, &sample)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &sample)) { 8 59 67
351 8 myfree(line);
352 8 ▶ if (allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_incomplete) { 4 4 8
353 4 i_tags_setn(&im->tags, "i_incomplete", 1);
354 4 i_tags_setn(&im->tags, "i_lines_read", 1);
355 8 return im;
356 }
357 else {
358 4 ▶ if (i_io_peekc(ig) != EOF)
Branch: 2 branches
BranchTrueFalseTotal
if (i_io_peekc(ig) != EOF) 2 2 4
if (i_io_peekc(ig) != EOF) 0 2 2
359 2 i_push_error(0, "invalid data for ascii pnm");
360 else
361 2 i_push_error(0, "short read - file truncated?");
362 4 i_img_destroy(im);
363 4 return NULL;
364 }
365 }
366 59 ▶ if (sample > maxval)
Branch: 1 branch
BranchTrueFalseTotal
if (sample > maxval) 0 59 59
367 0 sample = maxval;
368 59 linep->channel[ch] = (sample * 255 + rounder) / maxval;
369 }
370 29 ++linep;
371 }
372 11 i_plin(im, 0, width, y, line);
373 }
374 2 myfree(line);
375  
376 2 return im;
377 }
378  
379 static
380 i_img *
381 1 read_pgm_ppm_ascii_16(io_glue *ig, i_img *im, int width, int height,
382 int channels, unsigned maxval, int allow_incomplete) {
383 i_fcolor *line, *linep;
384 int x, y, ch;
385 1 double maxvalf = maxval;
386  
387 1 line = mymalloc(width * sizeof(i_fcolor));
388 2 ▶ for(y=0;y<height;y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y=0;y<height;y++) { 1 1 2
389 1 linep = line;
390 4 ▶ for(x=0; x<width; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x=0; x<width; x++) { 3 1 4
391 12 ▶ for(ch=0; ch<channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<channels; ch++) { 9 3 12
392 unsigned sample;
393
394 9 ▶ if (!gnum(ig, &sample)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &sample)) { 0 9 9
395 0 myfree(line);
396 0 ▶ if (allow_incomplete) {
Branch: 1 branch
BranchTrueFalseTotal
if (allow_incomplete) { 0 0 0
397 0 i_tags_setn(&im->tags, "i_incomplete", 1);
398 0 i_tags_setn(&im->tags, "i_lines_read", y);
399 0 return im;
400 }
401 else {
402 0 ▶ if (i_io_peekc(ig) != EOF)
Branch: 2 branches
BranchTrueFalseTotal
if (i_io_peekc(ig) != EOF) 0 0 0
if (i_io_peekc(ig) != EOF) 0 0 0
403 0 i_push_error(0, "invalid data for ascii pnm");
404 else
405 0 i_push_error(0, "short read - file truncated?");
406 0 i_img_destroy(im);
407 0 return NULL;
408 }
409 }
410 9 ▶ if (sample > maxval)
Branch: 1 branch
BranchTrueFalseTotal
if (sample > maxval) 0 9 9
411 0 sample = maxval;
412 9 linep->channel[ch] = sample / maxvalf;
413 }
414 3 ++linep;
415 }
416 1 i_plinf(im, 0, width, y, line);
417 }
418 1 myfree(line);
419  
420 1 return im;
421 }
422  
423 /*
424 =item i_readpnm_wiol(ig, allow_incomplete)
425  
426 Retrieve an image and stores in the iolayer object. Returns NULL on fatal error.
427  
428 ig - io_glue object
429 allow_incomplete - allows a partial file to be read successfully
430  
431 =cut
432 */
433  
434 i_img *
435 91 i_readpnm_wiol( io_glue *ig, int allow_incomplete) {
436 i_img* im;
437 int type;
438 unsigned width, height, maxval, channels;
439 int c;
440  
441 91 i_clear_error();
442 91 mm_log((1,"i_readpnm(ig %p, allow_incomplete %d)\n", ig, allow_incomplete));
443  
444 91 ▶ c = i_io_getc(ig);
Branch: 1 branch
BranchTrueFalseTotal
c = i_io_getc(ig); 39 52 91
445  
446 91 ▶ if (c != 'P') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != 'P') { 2 89 91
447 2 i_push_error(0, "bad header magic, not a PNM file");
448 2 mm_log((1, "i_readpnm: Could not read header of file\n"));
449 2 return NULL;
450 }
451  
452 89 ▶ if ((c = i_io_getc(ig)) == EOF ) {
Branch: 2 branches
BranchTrueFalseTotal
if ((c = i_io_getc(ig)) == EOF ) { 89 0 89
if ((c = i_io_getc(ig)) == EOF ) { 0 89 89
453 0 mm_log((1, "i_readpnm: Could not read header of file\n"));
454 0 return NULL;
455 }
456
457 89 type = c - '0';
458  
459 89 ▶ if (type < 1 || type > 6) {
Branch: 2 branches
BranchTrueFalseTotal
if (type < 1 || type > 6) { 89 0 89
if (type < 1 || type > 6) { 0 89 89
460 0 i_push_error(0, "unknown PNM file type, not a PNM file");
461 0 mm_log((1, "i_readpnm: Not a pnm file\n"));
462 0 return NULL;
463 }
464  
465 89 ▶ if ( (c = i_io_getc(ig)) == EOF ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( (c = i_io_getc(ig)) == EOF ) { 89 0 89
if ( (c = i_io_getc(ig)) == EOF ) { 0 89 89
466 0 mm_log((1, "i_readpnm: Could not read header of file\n"));
467 0 return NULL;
468 }
469
470 89 ▶ if ( !misspace(c) ) {
Branch: 6 branches
BranchTrueFalseTotal
if ( !misspace(c) ) { 89 0 89
if ( !misspace(c) ) { 0 89 89
if ( !misspace(c) ) { 0 0 0
if ( !misspace(c) ) { 0 0 0
if ( !misspace(c) ) { 0 0 0
if ( !misspace(c) ) { 0 0 0
471 0 i_push_error(0, "unexpected character, not a PNM file");
472 0 mm_log((1, "i_readpnm: Not a pnm file\n"));
473 0 return NULL;
474 }
475
476 89 mm_log((1, "i_readpnm: image is a %s\n", typenames[type-1] ));
477  
478
479 /* Read sizes and such */
480  
481 89 ▶ if (!skip_comment(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_comment(ig)) { 0 89 89
482 0 i_push_error(0, "while skipping to width");
483 0 mm_log((1, "i_readpnm: error reading before width\n"));
484 0 return NULL;
485 }
486
487 89 ▶ if (!gnum(ig, &width)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &width)) { 1 88 89
488 1 i_push_error(0, "could not read image width");
489 1 mm_log((1, "i_readpnm: error reading width\n"));
490 1 return NULL;
491 }
492  
493 88 ▶ if (!skip_comment(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_comment(ig)) { 1 87 88
494 1 i_push_error(0, "while skipping to height");
495 1 mm_log((1, "i_readpnm: error reading before height\n"));
496 1 return NULL;
497 }
498  
499 87 ▶ if (!gnum(ig, &height)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &height)) { 1 86 87
500 1 i_push_error(0, "could not read image height");
501 1 mm_log((1, "i_readpnm: error reading height\n"));
502 1 return NULL;
503 }
504
505 86 ▶ if (!(type == 1 || type == 4)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(type == 1 || type == 4)) { 80 6 86
if (!(type == 1 || type == 4)) { 76 4 80
506 76 ▶ if (!skip_comment(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_comment(ig)) { 0 76 76
507 0 i_push_error(0, "while skipping to maxval");
508 0 mm_log((1, "i_readpnm: error reading before maxval\n"));
509 0 return NULL;
510 }
511  
512 76 ▶ if (!gnum(ig, &maxval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &maxval)) { 0 76 76
513 0 i_push_error(0, "could not read maxval");
514 0 mm_log((1, "i_readpnm: error reading maxval\n"));
515 0 return NULL;
516 }
517  
518 76 ▶ if (maxval == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (maxval == 0) { 1 75 76
519 1 i_push_error(0, "maxval is zero - invalid pnm file");
520 1 mm_log((1, "i_readpnm: maxval is zero, invalid pnm file\n"));
521 1 return NULL;
522 }
523 75 ▶ else if (maxval > 65535) {
Branch: 1 branch
BranchTrueFalseTotal
else if (maxval > 65535) { 1 74 75
524 1 i_push_errorf(0, "maxval of %d is over 65535 - invalid pnm file",
525 maxval);
526 1 mm_log((1, "i_readpnm: maxval of %d is over 65535 - invalid pnm file\n", maxval));
527 1 return NULL;
528 }
529 10 } else maxval=1;
530  
531 84 ▶ if ((c = i_io_getc(ig)) == EOF || !misspace(c)) {
Branch: 8 branches
BranchTrueFalseTotal
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 84 0 84
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 84 0 84
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 84 0 84
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 0 84 84
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 0 0 0
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 0 0 0
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 0 0 0
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 0 0 0
532 0 i_push_error(0, "garbage in header, invalid PNM file");
533 0 mm_log((1, "i_readpnm: garbage in header\n"));
534 0 return NULL;
535 }
536  
537 84 ▶ channels = (type == 3 || type == 6) ? 3:1;
Branch: 2 branches
BranchTrueFalseTotal
channels = (type == 3 || type == 6) ? 3:1; 78 6 84
channels = (type == 3 || type == 6) ? 3:1; 56 22 78
538  
539 84 ▶ if (!i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))) { 3 81 84
540 3 mm_log((1, "i_readpnm: image size exceeds limits\n"));
541 3 return NULL;
542 }
543  
544 81 mm_log((1, "i_readpnm: (%d x %d), channels = %d, maxval = %d\n", width, height, channels, maxval));
545  
546 91 ▶ if (type == 1 || type == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (type == 1 || type == 4) { 75 6 81
if (type == 1 || type == 4) { 4 71 75
547 i_color pbm_pal[2];
548 10 pbm_pal[0].channel[0] = 255;
549 10 pbm_pal[1].channel[0] = 0;
550
551 10 im = i_img_pal_new(width, height, 1, 256);
552 10 ▶ if (im)
Branch: 1 branch
BranchTrueFalseTotal
if (im) 10 0 10
553 10 ▶ i_addcolors(im, pbm_pal, 2);
Branch: 1 branch
BranchTrueFalseTotal
i_addcolors(im, pbm_pal, 2); 10 0 10
554 }
555 else {
556 71 ▶ if (maxval > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (maxval > 255) 9 62 71
557 9 im = i_img_16_new(width, height, channels);
558 else
559 62 im = i_img_8_new(width, height, channels);
560 }
561 81 ▶ if (!im)
Branch: 1 branch
BranchTrueFalseTotal
if (!im) 0 81 81
562 0 return NULL;
563  
564 81 switch (type) {
565 6 case 1: /* Ascii types */
566 6 im = read_pbm_ascii(ig, im, width, height, allow_incomplete);
567 6 break;
568  
569 11 case 2:
570 case 3:
571 11 ▶ if (maxval > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (maxval > 255) 1 10 11
572 1 im = read_pgm_ppm_ascii_16(ig, im, width, height, channels, maxval, allow_incomplete);
573 else
574 10 im = read_pgm_ppm_ascii(ig, im, width, height, channels, maxval, allow_incomplete);
575 11 break;
576
577 4 case 4: /* binary pbm */
578 4 im = read_pbm_bin(ig, im, width, height, allow_incomplete);
579 4 break;
580  
581 60 case 5: /* binary pgm */
582 case 6: /* binary ppm */
583 60 ▶ if (maxval > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (maxval > 255) 8 52 60
584 8 im = read_pgm_ppm_bin16(ig, im, width, height, channels, maxval, allow_incomplete);
585 else
586 52 im = read_pgm_ppm_bin8(ig, im, width, height, channels, maxval, allow_incomplete);
587 60 break;
588  
589 0 default:
590 0 mm_log((1, "type %s [P%d] unsupported\n", typenames[type-1], type));
591 0 return NULL;
592 }
593  
594 81 ▶ if (!im)
Branch: 1 branch
BranchTrueFalseTotal
if (!im) 11 70 81
595 11 return NULL;
596  
597 70 i_tags_add(&im->tags, "i_format", 0, "pnm", -1, 0);
598 70 i_tags_setn(&im->tags, "pnm_maxval", maxval);
599 70 i_tags_setn(&im->tags, "pnm_type", type);
600  
601 70 return im;
602 }
603  
604 0 static void free_images(i_img **imgs, int count) {
605 int i;
606  
607 0 ▶ if (count) {
Branch: 1 branch
BranchTrueFalseTotal
if (count) { 0 0 0
608 0 ▶ for (i = 0; i < count; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) 0 0 0
609 0 i_img_destroy(imgs[i]);
610 0 myfree(imgs);
611 }
612 0 }
613  
614 1 i_img **i_readpnm_multi_wiol(io_glue *ig, int *count, int allow_incomplete) {
615 1 i_img **results = NULL;
616 1 i_img *img = NULL;
617 1 char c = EOF;
618 1 int result_alloc = 0,
619 1 value = 0,
620 1 eof = 0;
621 1 *count=0;
622  
623 do {
624 3 mm_log((1, "read image %i\n", 1+*count));
625 3 img = i_readpnm_wiol( ig, allow_incomplete );
626 3 ▶ if( !img ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !img ) { 0 3 3
627 0 free_images( results, *count );
628 0 return NULL;
629 }
630 3 ++*count;
631 3 ▶ if (*count > result_alloc) {
Branch: 1 branch
BranchTrueFalseTotal
if (*count > result_alloc) { 1 2 3
632 1 ▶ if (result_alloc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result_alloc == 0) { 1 0 1
633 1 result_alloc = 5;
634 1 results = mymalloc(result_alloc * sizeof(i_img *));
635 }
636 else {
637 /* myrealloc never fails (it just dies if it can't allocate) */
638 0 result_alloc *= 2;
639 0 results = myrealloc(results, result_alloc * sizeof(i_img *));
640 }
641 }
642 3 results[*count-1] = img;
643  
644  
645 3 ▶ if( i_tags_get_int(&img->tags, "i_incomplete", 0, &value ) && value) {
Branch: 2 branches
BranchTrueFalseTotal
if( i_tags_get_int(&img->tags, "i_incomplete", 0, &value ) && value) { 0 3 3
if( i_tags_get_int(&img->tags, "i_incomplete", 0, &value ) && value) { 0 0 0
646 0 eof = 1;
647 }
648 3 ▶ else if( skip_spaces( ig ) && ( c=i_io_peekc( ig ) ) != EOF && c == 'P' ) {
Branch: 4 branches
BranchTrueFalseTotal
else if( skip_spaces( ig ) && ( c=i_io_peekc( ig ) ) != EOF && c == 'P' ) { 2 1 3
else if( skip_spaces( ig ) && ( c=i_io_peekc( ig ) ) != EOF && c == 'P' ) { 2 0 2
else if( skip_spaces( ig ) && ( c=i_io_peekc( ig ) ) != EOF && c == 'P' ) { 2 0 2
else if( skip_spaces( ig ) && ( c=i_io_peekc( ig ) ) != EOF && c == 'P' ) { 2 0 2
649 2 eof = 0;
650 }
651 else {
652 1 eof = 1;
653 }
654 3 ▶ } while(!eof);
Branch: 1 branch
BranchTrueFalseTotal
} while(!eof); 2 1 3
655 1 return results;
656 }
657  
658  
659  
660 static
661 int
662 5 write_pbm(i_img *im, io_glue *ig, int zero_is_white) {
663 int x, y;
664 i_palidx *line;
665 i_img_dim write_size;
666 unsigned char *write_buf;
667 unsigned char *writep;
668 char header[255];
669 unsigned mask;
670  
671 5 sprintf(header, "P4\012# CREATOR: Imager\012%" i_DF " %" i_DF "\012",
672 5 i_DFc(im->xsize), i_DFc(im->ysize));
673 5 ▶ if (i_io_write(ig, header, strlen(header)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_write(ig, header, strlen(header)) < 0) { 0 5 5
674 0 i_push_error(0, "could not write pbm header");
675 0 return 0;
676 }
677 5 write_size = (im->xsize + 7) / 8;
678 5 line = mymalloc(sizeof(i_palidx) * im->xsize);
679 5 write_buf = mymalloc(write_size);
680 475 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 470 5 475
681 470 ▶ i_gpal(im, 0, im->xsize, y, line);
Branch: 1 branch
BranchTrueFalseTotal
i_gpal(im, 0, im->xsize, y, line); 470 0 470
682 470 mask = 0x80;
683 470 writep = write_buf;
684 470 memset(write_buf, 0, write_size);
685 68170 ▶ for (x = 0; x < im->xsize; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < im->xsize; ++x) { 67700 470 68170
686 67700 ▶ if (zero_is_white ? line[x] : !line[x])
Branch: 2 branches
BranchTrueFalseTotal
if (zero_is_white ? line[x] : !line[x]) 100 67600 67700
if (zero_is_white ? line[x] : !line[x]) 48649 19051 67700
687 48649 *writep |= mask;
688 67700 mask >>= 1;
689 67700 ▶ if (!mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mask) { 8120 59580 67700
690 8120 ++writep;
691 8120 mask = 0x80;
692 }
693 }
694 470 ▶ if (i_io_write(ig, write_buf, write_size) != write_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_write(ig, write_buf, write_size) != write_size) { 0 470 470
695 0 i_push_error(0, "write failure");
696 0 myfree(write_buf);
697 0 myfree(line);
698 0 return 0;
699 }
700 }
701 5 myfree(write_buf);
702 5 myfree(line);
703  
704 5 return 1;
705 }
706  
707 static
708 int
709 22 write_ppm_data_8(i_img *im, io_glue *ig, int want_channels) {
710 22 size_t write_size = im->xsize * want_channels;
711 22 size_t buf_size = im->xsize * im->channels;
712 22 unsigned char *data = mymalloc(buf_size);
713 22 i_img_dim y = 0;
714 22 int rc = 1;
715 i_color bg;
716  
717 22 i_get_file_background(im, &bg);
718 2279 ▶ while (y < im->ysize && rc >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
while (y < im->ysize && rc >= 0) { 2257 22 2279
while (y < im->ysize && rc >= 0) { 2257 0 2257
719 2257 i_gsamp_bg(im, 0, im->xsize, y, data, want_channels, &bg);
720 2257 ▶ if (i_io_write(ig, data, write_size) != (ssize_t)write_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_write(ig, data, write_size) != (ssize_t)write_size) { 0 2257 2257
721 0 i_push_error(errno, "could not write ppm data");
722 0 rc = 0;
723 0 break;
724 }
725 2257 ++y;
726 }
727 22 myfree(data);
728  
729 22 return rc;
730 }
731  
732 static
733 int
734 12 write_ppm_data_16(i_img *im, io_glue *ig, int want_channels) {
735 12 size_t line_size = im->channels * im->xsize * sizeof(i_fsample_t);
736 12 size_t sample_count = want_channels * im->xsize;
737 12 size_t write_size = sample_count * 2;
738 12 i_fsample_t *line_buf = mymalloc(line_size);
739 i_fsample_t *samplep;
740 12 unsigned char *write_buf = mymalloc(write_size);
741 unsigned char *writep;
742 size_t sample_num;
743 12 i_img_dim y = 0;
744 12 int rc = 1;
745 i_fcolor bg;
746  
747 12 i_get_file_backgroundf(im, &bg);
748  
749 1004 ▶ while (y < im->ysize) {
Branch: 1 branch
BranchTrueFalseTotal
while (y < im->ysize) { 992 12 1004
750 992 i_gsampf_bg(im, 0, im->xsize, y, line_buf, want_channels, &bg);
751 992 samplep = line_buf;
752 992 writep = write_buf;
753 345158 ▶ for (sample_num = 0; sample_num < sample_count; ++sample_num) {
Branch: 1 branch
BranchTrueFalseTotal
for (sample_num = 0; sample_num < sample_count; ++sample_num) { 344166 992 345158
754 344166 unsigned sample16 = SampleFTo16(*samplep++);
755 344166 *writep++ = sample16 >> 8;
756 344166 *writep++ = sample16 & 0xFF;
757 }
758 992 ▶ if (i_io_write(ig, write_buf, write_size) != (ssize_t)write_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_write(ig, write_buf, write_size) != (ssize_t)write_size) { 0 992 992
759 0 i_push_error(errno, "could not write ppm data");
760 0 rc = 0;
761 0 break;
762 }
763 992 ++y;
764 }
765 12 myfree(line_buf);
766 12 myfree(write_buf);
767  
768 12 return rc;
769 }
770  
771 undef_int
772 136 i_writeppm_wiol(i_img *im, io_glue *ig) {
773 char header[255];
774 int zero_is_white;
775 int wide_data;
776  
777 136 mm_log((1,"i_writeppm(im %p, ig %p)\n", im, ig));
778 136 i_clear_error();
779  
780 /* Add code to get the filename info from the iolayer */
781 /* Also add code to check for mmapped code */
782  
783 136 ▶ if (i_img_is_monochrome(im, &zero_is_white)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_img_is_monochrome(im, &zero_is_white)) { 5 131 136
784 5 ▶ if (!write_pbm(im, ig, zero_is_white))
Branch: 1 branch
BranchTrueFalseTotal
if (!write_pbm(im, ig, zero_is_white)) 0 5 5
785 0 return 0;
786 }
787 else {
788 int type;
789 int maxval;
790 131 int want_channels = im->channels;
791  
792 131 ▶ if (want_channels == 2 || want_channels == 4)
Branch: 2 branches
BranchTrueFalseTotal
if (want_channels == 2 || want_channels == 4) 131 0 131
if (want_channels == 2 || want_channels == 4) 6 125 131
793 6 --want_channels;
794  
795 131 ▶ if (!i_tags_get_int(&im->tags, "pnm_write_wide_data", 0, &wide_data))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_tags_get_int(&im->tags, "pnm_write_wide_data", 0, &wide_data)) 114 17 131
796 114 wide_data = 0;
797  
798 131 ▶ if (want_channels == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_channels == 3) { 121 10 131
799 121 type = 6;
800 }
801 10 ▶ else if (want_channels == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (want_channels == 1) { 10 0 10
802 10 type = 5;
803 }
804 else {
805 0 i_push_error(0, "can only save 1 or 3 channel images to pnm");
806 0 mm_log((1,"i_writeppm: ppm/pgm is 1 or 3 channel only (current image is %d)\n",im->channels));
807 0 return(0);
808 }
809 131 ▶ if (im->bits <= 8 || !wide_data)
Branch: 2 branches
BranchTrueFalseTotal
if (im->bits <= 8 || !wide_data) 27 104 131
if (im->bits <= 8 || !wide_data) 15 12 27
810 119 maxval = 255;
811 else
812 12 maxval = 65535;
813  
814 131 sprintf(header,"P%d\n#CREATOR: Imager\n%" i_DF " %" i_DF"\n%d\n",
815 131 type, i_DFc(im->xsize), i_DFc(im->ysize), maxval);
816  
817 131 ▶ if (i_io_write(ig,header,strlen(header)) != (ssize_t)strlen(header)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_write(ig,header,strlen(header)) != (ssize_t)strlen(header)) { 0 131 131
818 0 i_push_error(errno, "could not write ppm header");
819 0 mm_log((1,"i_writeppm: unable to write ppm header.\n"));
820 0 return(0);
821 }
822  
823 131 ▶ if (!i_img_virtual(im) && im->bits == i_8_bits && im->type == i_direct_type
Branch: 3 branches
BranchTrueFalseTotal
if (!i_img_virtual(im) && im->bits == i_8_bits && im->type == i_direct_type 131 0 131
if (!i_img_virtual(im) && im->bits == i_8_bits && im->type == i_direct_type 104 27 131
if (!i_img_virtual(im) && im->bits == i_8_bits && im->type == i_direct_type 100 4 104
824 100 ▶ && im->channels == want_channels) {
Branch: 1 branch
BranchTrueFalseTotal
&& im->channels == want_channels) { 97 3 100
825 97 ▶ if (i_io_write(ig,im->idata,im->bytes) != (ssize_t)im->bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_write(ig,im->idata,im->bytes) != (ssize_t)im->bytes) { 0 97 97
826 0 i_push_error(errno, "could not write ppm data");
827 0 return 0;
828 }
829 }
830 34 ▶ else if (maxval == 255) {
Branch: 1 branch
BranchTrueFalseTotal
else if (maxval == 255) { 22 12 34
831 22 ▶ if (!write_ppm_data_8(im, ig, want_channels))
Branch: 1 branch
BranchTrueFalseTotal
if (!write_ppm_data_8(im, ig, want_channels)) 0 22 22
832 0 return 0;
833 }
834 else {
835 12 ▶ if (!write_ppm_data_16(im, ig, want_channels))
Branch: 1 branch
BranchTrueFalseTotal
if (!write_ppm_data_16(im, ig, want_channels)) 0 12 12
836 0 return 0;
837 }
838 }
839 136 ▶ if (i_io_close(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_close(ig)) { 5 131 136
840 5 ▶ i_push_errorf(i_io_error(ig), "Error closing stream: %d", i_io_error(ig));
Branch: 4 branches
BranchTrueFalseTotal
i_push_errorf(i_io_error(ig), "Error closing stream: %d", i_io_error(ig)); 5 0 5
i_push_errorf(i_io_error(ig), "Error closing stream: %d", i_io_error(ig)); 0 5 5
i_push_errorf(i_io_error(ig), "Error closing stream: %d", i_io_error(ig)); 5 0 5
i_push_errorf(i_io_error(ig), "Error closing stream: %d", i_io_error(ig)); 0 5 5
841 5 return 0;
842 }
843  
844 131 return(1);
845 }
846  
847 /*
848 =back
849  
850 =head1 AUTHOR
851  
852 Arnar M. Hrafnkelsson <addi@umich.edu>, Tony Cook <tonyc@cpan.org>,
853 Philip Gwyn <gwyn@cpan.org>.
854  
855 =head1 SEE ALSO
856  
857 Imager(3)
858  
859 =cut
860 */
« iminline.h ↑ Summary fills.c »