]> git.imager.perl.org - imager.git/blame - pnm.c
bump to 0.53
[imager.git] / pnm.c
CommitLineData
92bda632 1#include "imager.h"
02d1d628 2#include "log.h"
067d6bdc 3#include "iolayer.h"
92bda632 4#include "imageri.h"
02d1d628
AMH
5
6#include <stdlib.h>
f0e7b647 7#include <errno.h>
02d1d628
AMH
8
9
10/*
11=head1 NAME
12
13pnm.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, -1); // 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
25pnm.c implements the basic functions to read and write portable
26anymap files. It uses the iolayer and needs either a seekable source
27or an entire memory mapped buffer.
28
29=head1 FUNCTION REFERENCE
30
31Some of these functions are internal.
32
b8c2033e 33=over
02d1d628
AMH
34
35=cut
36*/
37
38
39#define BSIZ 1024
40#define misspace(x) (x==' ' || x=='\n' || x=='\r' || x=='\t' || x=='\f' || x=='\v')
41#define misnumber(x) (x <= '9' && x>='0')
42
43static char *typenames[]={"ascii pbm", "ascii pgm", "ascii ppm", "binary pbm", "binary pgm", "binary ppm"};
44
45/*
46 * Type to encapsulate the local buffer
47 * management skipping over in a file
48 */
49
50typedef struct {
51 io_glue *ig;
52 int len;
53 int cp;
54 char buf[BSIZ];
55} mbuf;
56
57
58static
59void init_buf(mbuf *mb, io_glue *ig) {
60 mb->len = 0;
61 mb->cp = 0;
62 mb->ig = ig;
63}
64
65
66
67/*
68=item gnext(mbuf *mb)
69
70Fetches a character and advances in stream by one character.
71Returns a pointer to the byte or NULL on failure (internal).
72
73 mb - buffer object
74
75=cut
76*/
77
78static
79char *
80gnext(mbuf *mb) {
81 io_glue *ig = mb->ig;
82 if (mb->cp == mb->len) {
83 mb->cp = 0;
84 mb->len = ig->readcb(ig, mb->buf, BSIZ);
85 if (mb->len == -1) {
fac8664e 86 i_push_error(errno, "file read error");
02d1d628
AMH
87 mm_log((1, "i_readpnm: read error\n"));
88 return NULL;
89 }
90 if (mb->len == 0) {
fac8664e 91 i_push_error(errno, "unexpected end of file");
02d1d628
AMH
92 mm_log((1, "i_readpnm: end of file\n"));
93 return NULL;
94 }
95 }
96 return &mb->buf[mb->cp++];
97}
98
99
100/*
fdd00d54 101=item gpeek(mbuf *mb)
02d1d628
AMH
102
103Fetches a character but does NOT advance. Returns a pointer to
104the byte or NULL on failure (internal).
105
106 mb - buffer object
107
108=cut
109*/
110
111static
112char *
113gpeek(mbuf *mb) {
114 io_glue *ig = mb->ig;
115 if (mb->cp == mb->len) {
116 mb->cp = 0;
117 mb->len = ig->readcb(ig, mb->buf, BSIZ);
118 if (mb->len == -1) {
fac8664e 119 i_push_error(errno, "read error");
02d1d628
AMH
120 mm_log((1, "i_readpnm: read error\n"));
121 return NULL;
122 }
123 if (mb->len == 0) {
fac8664e 124 i_push_error(0, "unexpected end of file");
02d1d628
AMH
125 mm_log((1, "i_readpnm: end of file\n"));
126 return NULL;
127 }
128 }
129 return &mb->buf[mb->cp];
130}
131
132
133
134
135/*
136=item skip_spaces(mb)
137
138Advances in stream until it is positioned at a
139non white space character. (internal)
140
141 mb - buffer object
142
143=cut
144*/
145
146static
147int
148skip_spaces(mbuf *mb) {
149 char *cp;
150 while( (cp = gpeek(mb)) && misspace(*cp) ) if ( !gnext(mb) ) break;
151 if (!cp) return 0;
152 return 1;
153}
154
155
156/*
fdd00d54 157=item skip_comment(mb)
02d1d628
AMH
158
159Advances in stream over whitespace and a comment if one is found. (internal)
160
161 mb - buffer object
162
163=cut
164*/
165
166static
167int
168skip_comment(mbuf *mb) {
169 char *cp;
170
171 if (!skip_spaces(mb)) return 0;
172
173 if (!(cp = gpeek(mb))) return 0;
174 if (*cp == '#') {
175 while( (cp = gpeek(mb)) && (*cp != '\n' && *cp != '\r') ) {
176 if ( !gnext(mb) ) break;
177 }
178 }
179 if (!cp) return 0;
180
181 return 1;
182}
183
184
185/*
186=item gnum(mb, i)
187
188Fetches the next number from stream and stores in i, returns true
189on success else false.
190
191 mb - buffer object
192 i - integer to store result in
193
194=cut
195*/
196
197static
198int
199gnum(mbuf *mb, int *i) {
200 char *cp;
201 *i = 0;
202
203 if (!skip_spaces(mb)) return 0;
204
205 while( (cp = gpeek(mb)) && misnumber(*cp) ) {
206 *i = *i*10+(*cp-'0');
207 cp = gnext(mb);
208 }
209 return 1;
210}
211
212
213/*
214=item i_readpnm_wiol(ig, length)
215
216Retrieve an image and stores in the iolayer object. Returns NULL on fatal error.
217
218 ig - io_glue object
219 length - maximum length to read from data source, before closing it -1
220 signifies no limit.
221
222=cut
223*/
224
225
226i_img *
227i_readpnm_wiol(io_glue *ig, int length) {
228 i_img* im;
229 int type;
230 int x, y, ch;
231 int width, height, maxval, channels, pcount;
8b695554 232 int rounder;
02d1d628
AMH
233 char *cp;
234 unsigned char *uc;
235 mbuf buf;
236 i_color val;
02d1d628 237
fac8664e
TC
238 i_clear_error();
239
02d1d628
AMH
240 mm_log((1,"i_readpnm(ig %p, length %d)\n", ig, length));
241
02d1d628
AMH
242 io_glue_commit_types(ig);
243 init_buf(&buf, ig);
244
245 cp = gnext(&buf);
246
247 if (!cp || *cp != 'P') {
fac8664e 248 i_push_error(0, "bad header magic, not a PNM file");
02d1d628
AMH
249 mm_log((1, "i_readpnm: Could not read header of file\n"));
250 return NULL;
251 }
252
253 if ( !(cp = gnext(&buf)) ) {
254 mm_log((1, "i_readpnm: Could not read header of file\n"));
255 return NULL;
256 }
257
258 type = *cp-'0';
259
260 if (type < 1 || type > 6) {
fac8664e 261 i_push_error(0, "unknown PNM file type, not a PNM file");
02d1d628
AMH
262 mm_log((1, "i_readpnm: Not a pnm file\n"));
263 return NULL;
264 }
265
266 if ( !(cp = gnext(&buf)) ) {
267 mm_log((1, "i_readpnm: Could not read header of file\n"));
268 return NULL;
269 }
270
271 if ( !misspace(*cp) ) {
fac8664e 272 i_push_error(0, "unexpected character, not a PNM file");
02d1d628
AMH
273 mm_log((1, "i_readpnm: Not a pnm file\n"));
274 return NULL;
275 }
276
277 mm_log((1, "i_readpnm: image is a %s\n", typenames[type-1] ));
278
279
280 /* Read sizes and such */
281
282 if (!skip_comment(&buf)) {
fac8664e 283 i_push_error(0, "while skipping to width");
02d1d628
AMH
284 mm_log((1, "i_readpnm: error reading before width\n"));
285 return NULL;
286 }
287
288 if (!gnum(&buf, &width)) {
fac8664e 289 i_push_error(0, "could not read image width");
02d1d628
AMH
290 mm_log((1, "i_readpnm: error reading width\n"));
291 return NULL;
292 }
293
294 if (!skip_comment(&buf)) {
fac8664e 295 i_push_error(0, "while skipping to height");
02d1d628
AMH
296 mm_log((1, "i_readpnm: error reading before height\n"));
297 return NULL;
298 }
299
300 if (!gnum(&buf, &height)) {
fac8664e 301 i_push_error(0, "could not read image height");
02d1d628
AMH
302 mm_log((1, "i_readpnm: error reading height\n"));
303 return NULL;
304 }
305
306 if (!(type == 1 || type == 4)) {
307 if (!skip_comment(&buf)) {
fac8664e 308 i_push_error(0, "while skipping to maxval");
02d1d628
AMH
309 mm_log((1, "i_readpnm: error reading before maxval\n"));
310 return NULL;
311 }
312
313 if (!gnum(&buf, &maxval)) {
fac8664e 314 i_push_error(0, "could not read maxval");
02d1d628
AMH
315 mm_log((1, "i_readpnm: error reading maxval\n"));
316 return NULL;
317 }
8b695554
TC
318
319 if (maxval == 0) {
320 i_push_error(0, "maxval is zero - invalid pnm file");
321 mm_log((1, "i_readpnm: maxval is zero, invalid pnm file\n"));
322 return NULL;
323 }
324 else if (maxval > 65535) {
325 i_push_errorf(0, "maxval of %d is over 65535 - invalid pnm file",
326 maxval);
327 mm_log((1, "i_readpnm: maxval of %d is over 65535 - invalid pnm file\n"));
328 return NULL;
329 }
330 else if (type >= 4 && maxval > 255) {
331 i_push_errorf(0, "maxval of %d is over 255 - not currently supported by Imager for binary pnm", maxval);
332 mm_log((1, "i_readpnm: maxval of %d is over 255 - not currently supported by Imager for binary pnm\n", maxval));
333 return NULL;
334 }
02d1d628 335 } else maxval=1;
8b695554 336 rounder = maxval / 2;
02d1d628
AMH
337
338 if (!(cp = gnext(&buf)) || !misspace(*cp)) {
fac8664e 339 i_push_error(0, "garbage in header, invalid PNM file");
02d1d628
AMH
340 mm_log((1, "i_readpnm: garbage in header\n"));
341 return NULL;
342 }
343
344 channels = (type == 3 || type == 6) ? 3:1;
345 pcount = width*height*channels;
346
77157728
TC
347 if (!i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))) {
348 mm_log((1, "i_readpnm: image size exceeds limits\n"));
349 return NULL;
350 }
351
02d1d628
AMH
352 mm_log((1, "i_readpnm: (%d x %d), channels = %d, maxval = %d\n", width, height, channels, maxval));
353
354 im = i_img_empty_ch(NULL, width, height, channels);
355
642a675b
TC
356 i_tags_add(&im->tags, "i_format", 0, "pnm", -1, 0);
357
02d1d628
AMH
358 switch (type) {
359 case 1: /* Ascii types */
360 case 2:
361 case 3:
02d1d628
AMH
362 for(y=0;y<height;y++) for(x=0; x<width; x++) {
363 for(ch=0; ch<channels; ch++) {
364 int t;
8b695554 365 if (gnum(&buf, &t)) val.channel[ch] = (t * 255 + rounder) / maxval;
02d1d628
AMH
366 else {
367 mm_log((1,"i_readpnm: gnum() returned false in data\n"));
368 return im;
369 }
370 }
371 i_ppix(im, x, y, &val);
372 }
373 break;
374
375 case 4: /* binary pbm */
376 for(y=0;y<height;y++) for(x=0; x<width; x+=8) {
a5eb593a 377 if ( (uc = (unsigned char*)gnext(&buf)) ) {
02d1d628
AMH
378 int xt;
379 int pc = width-x < 8 ? width-x : 8;
380 /* mm_log((1,"i_readpnm: y=%d x=%d pc=%d\n", y, x, pc)); */
381 for(xt = 0; xt<pc; xt++) {
382 val.channel[0] = (*uc & (128>>xt)) ? 0 : 255;
383 i_ppix(im, x+xt, y, &val);
384 }
385 } else {
386 mm_log((1,"i_readpnm: gnext() returned false in data\n"));
387 return im;
388 }
389 }
390 break;
391
392 case 5: /* binary pgm */
393 case 6: /* binary ppm */
394 for(y=0;y<height;y++) for(x=0; x<width; x++) {
395 for(ch=0; ch<channels; ch++) {
8b695554
TC
396 if ( (uc = (unsigned char*)gnext(&buf)) )
397 val.channel[ch] = (*uc * 255 + rounder) / maxval;
02d1d628
AMH
398 else {
399 mm_log((1,"i_readpnm: gnext() returned false in data\n"));
400 return im;
401 }
402 }
403 i_ppix(im, x, y, &val);
404 }
405 break;
406 default:
407 mm_log((1, "type %s [P%d] unsupported\n", typenames[type-1], type));
408 return NULL;
409 }
410 return im;
411}
412
02d1d628 413
067d6bdc
AMH
414undef_int
415i_writeppm_wiol(i_img *im, io_glue *ig) {
416 char header[255];
417 int rc;
02d1d628 418
067d6bdc
AMH
419 mm_log((1,"i_writeppm(im %p, ig %p)\n", im, ig));
420 i_clear_error();
02d1d628 421
067d6bdc
AMH
422 /* Add code to get the filename info from the iolayer */
423 /* Also add code to check for mmapped code */
02d1d628 424
067d6bdc 425 io_glue_commit_types(ig);
02d1d628 426
faa9b3e7 427 if (im->channels == 3) {
067d6bdc 428 sprintf(header,"P6\n#CREATOR: Imager\n%d %d\n255\n",im->xsize,im->ysize);
faa9b3e7 429 if (ig->writecb(ig,header,strlen(header))<0) {
067d6bdc
AMH
430 i_push_error(errno, "could not write ppm header");
431 mm_log((1,"i_writeppm: unable to write ppm header.\n"));
432 return(0);
433 }
faa9b3e7
TC
434
435 if (!im->virtual && im->bits == i_8_bits && im->type == i_direct_type) {
436 rc = ig->writecb(ig,im->idata,im->bytes);
437 }
438 else {
439 unsigned char *data = mymalloc(3 * im->xsize);
440 if (data != NULL) {
441 int y = 0;
faa9b3e7
TC
442 static int rgb_chan[3] = { 0, 1, 2 };
443
444 rc = 0;
445 while (y < im->ysize && rc >= 0) {
446 i_gsamp(im, 0, im->xsize, y, data, rgb_chan, 3);
447 rc = ig->writecb(ig, data, im->xsize * 3);
43c5dacb 448 ++y;
faa9b3e7
TC
449 }
450 myfree(data);
451 }
452 else {
453 i_push_error(0, "Out of memory");
454 return 0;
455 }
456 }
067d6bdc
AMH
457 if (rc<0) {
458 i_push_error(errno, "could not write ppm data");
459 mm_log((1,"i_writeppm: unable to write ppm data.\n"));
460 return(0);
461 }
462 }
463 else if (im->channels == 1) {
464 sprintf(header, "P5\n#CREATOR: Imager\n%d %d\n255\n",
465 im->xsize, im->ysize);
faa9b3e7 466 if (ig->writecb(ig,header, strlen(header)) < 0) {
067d6bdc
AMH
467 i_push_error(errno, "could not write pgm header");
468 mm_log((1,"i_writeppm: unable to write pgm header.\n"));
469 return(0);
470 }
faa9b3e7
TC
471
472 if (!im->virtual && im->bits == i_8_bits && im->type == i_direct_type) {
473 rc=ig->writecb(ig,im->idata,im->bytes);
474 }
475 else {
476 unsigned char *data = mymalloc(im->xsize);
477 if (data != NULL) {
478 int y = 0;
faa9b3e7 479 int chan = 0;
faa9b3e7
TC
480
481 rc = 0;
482 while (y < im->ysize && rc >= 0) {
483 i_gsamp(im, 0, im->xsize, y, data, &chan, 1);
484 rc = ig->writecb(ig, data, im->xsize);
43c5dacb 485 ++y;
faa9b3e7
TC
486 }
487 myfree(data);
488 }
489 else {
490 i_push_error(0, "Out of memory");
491 return 0;
492 }
493 }
067d6bdc
AMH
494 if (rc<0) {
495 i_push_error(errno, "could not write pgm data");
496 mm_log((1,"i_writeppm: unable to write pgm data.\n"));
497 return(0);
498 }
499 }
500 else {
501 i_push_error(0, "can only save 1 or 3 channel images to pnm");
502 mm_log((1,"i_writeppm: ppm/pgm is 1 or 3 channel only (current image is %d)\n",im->channels));
503 return(0);
504 }
10461f9a 505 ig->closecb(ig);
faa9b3e7 506
067d6bdc
AMH
507 return(1);
508}
b8c2033e
AMH
509
510/*
511=back
512
513=head1 AUTHOR
514
515Arnar M. Hrafnkelsson <addi@umich.edu>
516
517=head1 SEE ALSO
518
519Imager(3)
520
521=cut
522*/