pnm.c

filter: Statementstmt 82.3% 365 / 443 Branchbran 71.9% 246 / 342 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 77.8% 611 / 785 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« tga.c ↑ 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 632 skip_spaces(io_glue *ig) {
58 int c;
59 951 ▶ while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) {
Branch: 8 branches
BranchTrueFalseTotal
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 943 8 951
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 943 8 951
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 152 791 943
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 167 624 791
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 624 624
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 624 624
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 624 624
while( (c = i_io_peekc(ig)) != EOF && misspace(c) ) { 0 624 624
60 319 ▶ if ( i_io_getc(ig) == EOF )
Branch: 2 branches
BranchTrueFalseTotal
if ( i_io_getc(ig) == EOF ) 319 0 319
if ( i_io_getc(ig) == EOF ) 0 319 319
61 0 break;
62 }
63 632 ▶ if (c == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (c == EOF) 8 624 632
64 8 return 0;
65  
66 624 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 265 skip_comment(io_glue *ig) {
83 int c;
84  
85 265 ▶ if (!skip_spaces(ig))
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_spaces(ig)) 1 264 265
86 1 return 0;
87  
88 264 ▶ if ((c = i_io_peekc(ig)) == EOF)
Branch: 2 branches
BranchTrueFalseTotal
if ((c = i_io_peekc(ig)) == EOF) 264 0 264
if ((c = i_io_peekc(ig)) == EOF) 0 264 264
89 0 return 0;
90  
91 264 ▶ if (c == '#') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '#') { 70 194 264
92 2354 ▶ 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') ) { 2354 0 2354
while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) { 2354 0 2354
while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) { 2284 70 2354
while( (c = i_io_peekc(ig)) != EOF && (c != '\n' && c != '\r') ) { 2284 0 2284
93 2284 ▶ if ( i_io_getc(ig) == EOF )
Branch: 2 branches
BranchTrueFalseTotal
if ( i_io_getc(ig) == EOF ) 2284 0 2284
if ( i_io_getc(ig) == EOF ) 0 2284 2284
94 0 break;
95 }
96 }
97 264 ▶ if (c == EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (c == EOF) 0 264 264
98 0 return 0;
99
100 264 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 340 gnum(io_glue *ig, unsigned *i) {
119 int c;
120 340 *i = 0;
121  
122 340 ▶ if (!skip_spaces(ig)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_spaces(ig)) return 0; 4 336 340
123  
124 336 ▶ if ((c = i_io_peekc(ig)) == EOF)
Branch: 2 branches
BranchTrueFalseTotal
if ((c = i_io_peekc(ig)) == EOF) 336 0 336
if ((c = i_io_peekc(ig)) == EOF) 0 336 336
125 0 return 0;
126 336 ▶ if (!misnumber(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!misnumber(c)) 332 4 336
if (!misnumber(c)) 0 332 332
127 4 return 0;
128 1185 ▶ while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) {
Branch: 4 branches
BranchTrueFalseTotal
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 1183 2 1185
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 1183 2 1185
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 1183 0 1183
while( (c = i_io_peekc(ig)) != EOF && misnumber(c) ) { 855 328 1183
129 855 unsigned work = *i * 10 + (c - '0');
130 855 ▶ if (work < *i) {
Branch: 1 branch
BranchTrueFalseTotal
if (work < *i) { 2 853 855
131 /* overflow */
132 2 i_push_error(0, "integer overflow");
133 2 return 0;
134 }
135 853 *i = work;
136 853 ▶ i_io_nextc(ig);
Branch: 1 branch
BranchTrueFalseTotal
i_io_nextc(ig); 853 0 853
137 }
138  
139 330 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 10 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 10 double maxvalf = maxval;
209  
210 10 line = mymalloc(width * sizeof(i_fcolor));
211 10 read_size = channels * width * 2;
212 10 read_buf = mymalloc(read_size);
213 498 ▶ for(y=0;y<height;y++) {
Branch: 1 branch
BranchTrueFalseTotal
for(y=0;y<height;y++) { 492 6 498
214 492 linep = line;
215 492 readp = read_buf;
216 492 ▶ 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 488 492
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 68510 ▶ for(x=0; x<width; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for(x=0; x<width; x++) { 68022 488 68510
231 227080 ▶ for(ch=0; ch<channels; ch++) {
Branch: 1 branch
BranchTrueFalseTotal
for(ch=0; ch<channels; ch++) { 159058 68022 227080
232 159058 unsigned sample = (readp[0] << 8) + readp[1];
233 159058 ▶ if (sample > maxval)
Branch: 1 branch
BranchTrueFalseTotal
if (sample > maxval) 0 159058 159058
234 0 sample = maxval;
235 159058 readp += 2;
236 159058 linep->channel[ch] = sample / maxvalf;
237 }
238 68022 ++linep;
239 }
240 488 i_plinf(im, 0, width, y, line);
241 }
242 6 myfree(read_buf);
243 6 myfree(line);
244  
245 6 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 95 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 95 i_clear_error();
442 95 mm_log((1,"i_readpnm(ig %p, allow_incomplete %d)\n", ig, allow_incomplete));
443  
444 95 ▶ c = i_io_getc(ig);
Branch: 1 branch
BranchTrueFalseTotal
c = i_io_getc(ig); 39 56 95
445  
446 95 ▶ if (c != 'P') {
Branch: 1 branch
BranchTrueFalseTotal
if (c != 'P') { 2 93 95
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 93 ▶ if ((c = i_io_getc(ig)) == EOF ) {
Branch: 2 branches
BranchTrueFalseTotal
if ((c = i_io_getc(ig)) == EOF ) { 93 0 93
if ((c = i_io_getc(ig)) == EOF ) { 0 93 93
453 0 mm_log((1, "i_readpnm: Could not read header of file\n"));
454 0 return NULL;
455 }
456
457 93 type = c - '0';
458  
459 93 ▶ if (type < 1 || type > 6) {
Branch: 2 branches
BranchTrueFalseTotal
if (type < 1 || type > 6) { 93 0 93
if (type < 1 || type > 6) { 0 93 93
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 93 ▶ if ( (c = i_io_getc(ig)) == EOF ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( (c = i_io_getc(ig)) == EOF ) { 93 0 93
if ( (c = i_io_getc(ig)) == EOF ) { 0 93 93
466 0 mm_log((1, "i_readpnm: Could not read header of file\n"));
467 0 return NULL;
468 }
469
470 93 ▶ if ( !misspace(c) ) {
Branch: 6 branches
BranchTrueFalseTotal
if ( !misspace(c) ) { 93 0 93
if ( !misspace(c) ) { 0 93 93
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 93 mm_log((1, "i_readpnm: image is a %s\n", typenames[type-1] ));
477  
478
479 /* Read sizes and such */
480  
481 93 ▶ if (!skip_comment(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_comment(ig)) { 0 93 93
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 93 ▶ if (!gnum(ig, &width)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &width)) { 1 92 93
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 92 ▶ if (!skip_comment(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_comment(ig)) { 1 91 92
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 91 ▶ if (!gnum(ig, &height)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &height)) { 1 90 91
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 90 ▶ if (!(type == 1 || type == 4)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(type == 1 || type == 4)) { 84 6 90
if (!(type == 1 || type == 4)) { 80 4 84
506 80 ▶ if (!skip_comment(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!skip_comment(ig)) { 0 80 80
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 80 ▶ if (!gnum(ig, &maxval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gnum(ig, &maxval)) { 0 80 80
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 80 ▶ if (maxval == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (maxval == 0) { 1 79 80
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 79 ▶ else if (maxval > 65535) {
Branch: 1 branch
BranchTrueFalseTotal
else if (maxval > 65535) { 1 78 79
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 88 ▶ if ((c = i_io_getc(ig)) == EOF || !misspace(c)) {
Branch: 8 branches
BranchTrueFalseTotal
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 88 0 88
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 88 0 88
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 88 0 88
if ((c = i_io_getc(ig)) == EOF || !misspace(c)) { 0 88 88
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 88 ▶ channels = (type == 3 || type == 6) ? 3:1;
Branch: 2 branches
BranchTrueFalseTotal
channels = (type == 3 || type == 6) ? 3:1; 82 6 88
channels = (type == 3 || type == 6) ? 3:1; 58 24 82
538 88 mm_log((1, "i_readpnm: (%d x %d), channels = %d, maxval = %d\n", width, height, channels, maxval));
539  
540 98 ▶ if (type == 1 || type == 4) {
Branch: 2 branches
BranchTrueFalseTotal
if (type == 1 || type == 4) { 82 6 88
if (type == 1 || type == 4) { 4 78 82
541 i_color pbm_pal[2];
542 10 pbm_pal[0].channel[0] = 255;
543 10 pbm_pal[1].channel[0] = 0;
544
545 10 im = i_img_pal_file_new(width, height, 1);
546 10 ▶ if (im)
Branch: 1 branch
BranchTrueFalseTotal
if (im) 10 0 10
547 10 ▶ i_addcolors(im, pbm_pal, 2);
Branch: 1 branch
BranchTrueFalseTotal
i_addcolors(im, pbm_pal, 2); 10 0 10
548 }
549 else {
550 78 ▶ if (maxval > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (maxval > 255) 13 65 78
551 13 im = i_img_16_file_new(width, height, channels);
552 else
553 65 im = i_img_8_file_new(width, height, channels);
554 }
555 88 ▶ if (!im)
Branch: 1 branch
BranchTrueFalseTotal
if (!im) 5 83 88
556 5 return NULL;
557  
558 83 switch (type) {
559 6 case 1: /* Ascii types */
560 6 im = read_pbm_ascii(ig, im, width, height, allow_incomplete);
561 6 break;
562  
563 11 case 2:
564 case 3:
565 11 ▶ if (maxval > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (maxval > 255) 1 10 11
566 1 im = read_pgm_ppm_ascii_16(ig, im, width, height, channels, maxval, allow_incomplete);
567 else
568 10 im = read_pgm_ppm_ascii(ig, im, width, height, channels, maxval, allow_incomplete);
569 11 break;
570
571 4 case 4: /* binary pbm */
572 4 im = read_pbm_bin(ig, im, width, height, allow_incomplete);
573 4 break;
574  
575 62 case 5: /* binary pgm */
576 case 6: /* binary ppm */
577 62 ▶ if (maxval > 255)
Branch: 1 branch
BranchTrueFalseTotal
if (maxval > 255) 10 52 62
578 10 im = read_pgm_ppm_bin16(ig, im, width, height, channels, maxval, allow_incomplete);
579 else
580 52 im = read_pgm_ppm_bin8(ig, im, width, height, channels, maxval, allow_incomplete);
581 62 break;
582  
583 0 default:
584 0 mm_log((1, "type %s [P%d] unsupported\n", typenames[type-1], type));
585 0 return NULL;
586 }
587  
588 83 ▶ if (!im)
Branch: 1 branch
BranchTrueFalseTotal
if (!im) 11 72 83
589 11 return NULL;
590  
591 72 i_tags_add(&im->tags, "i_format", 0, "pnm", -1, 0);
592 72 i_tags_setn(&im->tags, "pnm_maxval", maxval);
593 72 i_tags_setn(&im->tags, "pnm_type", type);
594  
595 72 return im;
596 }
597  
598 0 static void free_images(i_img **imgs, int count) {
599 int i;
600  
601 0 ▶ if (count) {
Branch: 1 branch
BranchTrueFalseTotal
if (count) { 0 0 0
602 0 ▶ for (i = 0; i < count; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; ++i) 0 0 0
603 0 i_img_destroy(imgs[i]);
604 0 myfree(imgs);
605 }
606 0 }
607  
608 1 i_img **i_readpnm_multi_wiol(io_glue *ig, int *count, int allow_incomplete) {
609 1 i_img **results = NULL;
610 1 i_img *img = NULL;
611 1 char c = EOF;
612 1 int result_alloc = 0,
613 1 value = 0,
614 1 eof = 0;
615 1 *count=0;
616  
617 do {
618 3 mm_log((1, "read image %i\n", 1+*count));
619 3 img = i_readpnm_wiol( ig, allow_incomplete );
620 3 ▶ if( !img ) {
Branch: 1 branch
BranchTrueFalseTotal
if( !img ) { 0 3 3
621 0 free_images( results, *count );
622 0 return NULL;
623 }
624 3 ++*count;
625 3 ▶ if (*count > result_alloc) {
Branch: 1 branch
BranchTrueFalseTotal
if (*count > result_alloc) { 1 2 3
626 1 ▶ if (result_alloc == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (result_alloc == 0) { 1 0 1
627 1 result_alloc = 5;
628 1 results = mymalloc(result_alloc * sizeof(i_img *));
629 }
630 else {
631 /* myrealloc never fails (it just dies if it can't allocate) */
632 0 result_alloc *= 2;
633 0 results = myrealloc(results, result_alloc * sizeof(i_img *));
634 }
635 }
636 3 results[*count-1] = img;
637  
638  
639 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
640 0 eof = 1;
641 }
642 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
643 2 eof = 0;
644 }
645 else {
646 1 eof = 1;
647 }
648 3 ▶ } while(!eof);
Branch: 1 branch
BranchTrueFalseTotal
} while(!eof); 2 1 3
649 1 return results;
650 }
651  
652  
653  
654 static
655 int
656 5 write_pbm(i_img *im, io_glue *ig, int zero_is_white) {
657 int x, y;
658 i_palidx *line;
659 i_img_dim write_size;
660 unsigned char *write_buf;
661 unsigned char *writep;
662 char header[255];
663 unsigned mask;
664  
665 5 sprintf(header, "P4\012# CREATOR: Imager\012%" i_DF " %" i_DF "\012",
666 5 i_DFc(im->xsize), i_DFc(im->ysize));
667 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
668 0 i_push_error(0, "could not write pbm header");
669 0 return 0;
670 }
671 5 write_size = (im->xsize + 7) / 8;
672 5 line = mymalloc(sizeof(i_palidx) * im->xsize);
673 5 write_buf = mymalloc(write_size);
674 475 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 470 5 475
675 470 ▶ i_gpal(im, 0, im->xsize, y, line);
Branch: 1 branch
BranchTrueFalseTotal
i_gpal(im, 0, im->xsize, y, line); 470 0 470
676 470 mask = 0x80;
677 470 writep = write_buf;
678 470 memset(write_buf, 0, write_size);
679 68170 ▶ for (x = 0; x < im->xsize; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < im->xsize; ++x) { 67700 470 68170
680 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
681 48649 *writep |= mask;
682 67700 mask >>= 1;
683 67700 ▶ if (!mask) {
Branch: 1 branch
BranchTrueFalseTotal
if (!mask) { 8120 59580 67700
684 8120 ++writep;
685 8120 mask = 0x80;
686 }
687 }
688 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
689 0 i_push_error(0, "write failure");
690 0 myfree(write_buf);
691 0 myfree(line);
692 0 return 0;
693 }
694 }
695 5 myfree(write_buf);
696 5 myfree(line);
697  
698 5 return 1;
699 }
700  
701 static
702 int
703 22 write_ppm_data_8(i_img *im, io_glue *ig, int want_channels) {
704 22 size_t write_size = im->xsize * want_channels;
705 22 size_t buf_size = im->xsize * im->channels;
706 22 unsigned char *data = mymalloc(buf_size);
707 22 i_img_dim y = 0;
708 22 int rc = 1;
709 i_color bg;
710  
711 22 i_get_file_background(im, &bg);
712 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
713 2257 i_gsamp_bg(im, 0, im->xsize, y, data, want_channels, &bg);
714 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
715 0 i_push_error(errno, "could not write ppm data");
716 0 rc = 0;
717 0 break;
718 }
719 2257 ++y;
720 }
721 22 myfree(data);
722  
723 22 return rc;
724 }
725  
726 static
727 int
728 14 write_ppm_data_16(i_img *im, io_glue *ig, int want_channels) {
729 14 size_t line_size = im->channels * im->xsize * sizeof(i_fsample_t);
730 14 size_t sample_count = want_channels * im->xsize;
731 14 size_t write_size = sample_count * 2;
732 14 i_fsample_t *line_buf = mymalloc(line_size);
733 i_fsample_t *samplep;
734 14 unsigned char *write_buf = mymalloc(write_size);
735 unsigned char *writep;
736 size_t sample_num;
737 14 i_img_dim y = 0;
738 14 int rc = 1;
739 i_fcolor bg;
740  
741 14 i_get_file_backgroundf(im, &bg);
742  
743 1306 ▶ while (y < im->ysize) {
Branch: 1 branch
BranchTrueFalseTotal
while (y < im->ysize) { 1292 14 1306
744 1292 i_gsampf_bg(im, 0, im->xsize, y, line_buf, want_channels, &bg);
745 1292 samplep = line_buf;
746 1292 writep = write_buf;
747 435458 ▶ for (sample_num = 0; sample_num < sample_count; ++sample_num) {
Branch: 1 branch
BranchTrueFalseTotal
for (sample_num = 0; sample_num < sample_count; ++sample_num) { 434166 1292 435458
748 434166 unsigned sample16 = SampleFTo16(*samplep++);
749 434166 *writep++ = sample16 >> 8;
750 434166 *writep++ = sample16 & 0xFF;
751 }
752 1292 ▶ 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 1292 1292
753 0 i_push_error(errno, "could not write ppm data");
754 0 rc = 0;
755 0 break;
756 }
757 1292 ++y;
758 }
759 14 myfree(line_buf);
760 14 myfree(write_buf);
761  
762 14 return rc;
763 }
764  
765 undef_int
766 138 i_writeppm_wiol(i_img *im, io_glue *ig) {
767 char header[255];
768 int zero_is_white;
769 int wide_data;
770  
771 138 mm_log((1,"i_writeppm(im %p, ig %p)\n", im, ig));
772 138 i_clear_error();
773  
774 /* Add code to get the filename info from the iolayer */
775 /* Also add code to check for mmapped code */
776  
777 138 ▶ if (i_img_is_monochrome(im, &zero_is_white)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_img_is_monochrome(im, &zero_is_white)) { 5 133 138
778 5 ▶ if (!write_pbm(im, ig, zero_is_white))
Branch: 1 branch
BranchTrueFalseTotal
if (!write_pbm(im, ig, zero_is_white)) 0 5 5
779 0 return 0;
780 }
781 else {
782 int type;
783 int maxval;
784 133 int want_channels = im->channels;
785  
786 133 ▶ if (want_channels == 2 || want_channels == 4)
Branch: 2 branches
BranchTrueFalseTotal
if (want_channels == 2 || want_channels == 4) 133 0 133
if (want_channels == 2 || want_channels == 4) 6 127 133
787 6 --want_channels;
788  
789 133 ▶ 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 19 133
790 114 wide_data = 0;
791  
792 133 ▶ if (want_channels == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (want_channels == 3) { 122 11 133
793 122 type = 6;
794 }
795 11 ▶ else if (want_channels == 1) {
Branch: 1 branch
BranchTrueFalseTotal
else if (want_channels == 1) { 11 0 11
796 11 type = 5;
797 }
798 else {
799 0 i_push_error(0, "can only save 1 or 3 channel images to pnm");
800 0 mm_log((1,"i_writeppm: ppm/pgm is 1 or 3 channel only (current image is %d)\n",im->channels));
801 0 return(0);
802 }
803 133 ▶ if (im->bits <= 8 || !wide_data)
Branch: 2 branches
BranchTrueFalseTotal
if (im->bits <= 8 || !wide_data) 29 104 133
if (im->bits <= 8 || !wide_data) 15 14 29
804 119 maxval = 255;
805 else
806 14 maxval = 65535;
807  
808 133 sprintf(header,"P%d\n#CREATOR: Imager\n%" i_DF " %" i_DF"\n%d\n",
809 133 type, i_DFc(im->xsize), i_DFc(im->ysize), maxval);
810  
811 133 ▶ 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 133 133
812 0 i_push_error(errno, "could not write ppm header");
813 0 mm_log((1,"i_writeppm: unable to write ppm header.\n"));
814 0 return(0);
815 }
816  
817 133 ▶ 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 133 0 133
if (!i_img_virtual(im) && im->bits == i_8_bits && im->type == i_direct_type 104 29 133
if (!i_img_virtual(im) && im->bits == i_8_bits && im->type == i_direct_type 100 4 104
818 100 ▶ && im->channels == want_channels) {
Branch: 1 branch
BranchTrueFalseTotal
&& im->channels == want_channels) { 97 3 100
819 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
820 0 i_push_error(errno, "could not write ppm data");
821 0 return 0;
822 }
823 }
824 36 ▶ else if (maxval == 255) {
Branch: 1 branch
BranchTrueFalseTotal
else if (maxval == 255) { 22 14 36
825 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
826 0 return 0;
827 }
828 else {
829 14 ▶ if (!write_ppm_data_16(im, ig, want_channels))
Branch: 1 branch
BranchTrueFalseTotal
if (!write_ppm_data_16(im, ig, want_channels)) 0 14 14
830 0 return 0;
831 }
832 }
833 138 ▶ if (i_io_close(ig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_close(ig)) { 5 133 138
834 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
835 5 return 0;
836 }
837  
838 133 return(1);
839 }
840  
841 /*
842 =back
843  
844 =head1 AUTHOR
845  
846 Arnar M. Hrafnkelsson <addi@umich.edu>, Tony Cook <tony@develop-help.com>,
847 Philip Gwyn <gwyn@cpan.org>.
848  
849 =head1 SEE ALSO
850  
851 Imager(3)
852  
853 =cut
854 */
« tga.c ↑ Summary fills.c »