13 pnm.c - implements reading and writing ppm/pnm/pbm files, uses io layer.
17 io_glue *ig = io_new_fd( fd );
18 i_img *im = i_readpnm_wiol(ig, 0); // no limit on how much is read
20 io_glue *ig = io_new_fd( fd );
21 return_code = i_writepnm_wiol(im, ig);
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.
29 =head1 FUNCTION REFERENCE
31 Some of these functions are internal.
40 #define misspace(x) (x==' ' || x=='\n' || x=='\r' || x=='\t' || x=='\f' || x=='\v')
41 #define misnumber(x) (x <= '9' && x>='0')
43 static char *typenames[]={"ascii pbm", "ascii pgm", "ascii ppm", "binary pbm", "binary pgm", "binary ppm"};
46 * Type to encapsulate the local buffer
47 * management skipping over in a file
59 void init_buf(mbuf *mb, io_glue *ig) {
70 Fetches a character and advances in stream by one character.
71 Returns a pointer to the byte or NULL on failure (internal).
78 #define gnext(mb) (((mb)->cp == (mb)->len) ? gnextf(mb) : (mb)->buf + (mb)->cp++)
84 if (mb->cp == mb->len) {
86 mb->len = ig->readcb(ig, mb->buf, BSIZ);
88 i_push_error(errno, "file read error");
89 mm_log((1, "i_readpnm: read error\n"));
93 mm_log((1, "i_readpnm: end of file\n"));
97 return &mb->buf[mb->cp++];
102 =item gpeek(mbuf *mb)
104 Fetches a character but does NOT advance. Returns a pointer to
105 the byte or NULL on failure (internal).
112 #define gpeek(mb) ((mb)->cp == (mb)->len ? gpeekf(mb) : (mb)->buf + (mb)->cp)
117 io_glue *ig = mb->ig;
118 if (mb->cp == mb->len) {
120 mb->len = ig->readcb(ig, mb->buf, BSIZ);
122 i_push_error(errno, "read error");
123 mm_log((1, "i_readpnm: read error\n"));
127 mm_log((1, "i_readpnm: end of file\n"));
131 return &mb->buf[mb->cp];
135 gread(mbuf *mb, unsigned char *buf, size_t read_size) {
137 if (mb->cp != mb->len) {
138 int avail_size = mb->len - mb->cp;
139 int use_size = read_size > avail_size ? avail_size : read_size;
140 memcpy(buf, mb->buf+mb->cp, use_size);
142 total_read += use_size;
143 read_size -= use_size;
147 io_glue *ig = mb->ig;
148 int read_res = i_io_read(ig, buf, read_size);
150 total_read += read_res;
158 =item skip_spaces(mb)
160 Advances in stream until it is positioned at a
161 non white space character. (internal)
170 skip_spaces(mbuf *mb) {
172 while( (cp = gpeek(mb)) && misspace(*cp) ) if ( !gnext(mb) ) break;
179 =item skip_comment(mb)
181 Advances in stream over whitespace and a comment if one is found. (internal)
190 skip_comment(mbuf *mb) {
193 if (!skip_spaces(mb)) return 0;
195 if (!(cp = gpeek(mb))) return 0;
197 while( (cp = gpeek(mb)) && (*cp != '\n' && *cp != '\r') ) {
198 if ( !gnext(mb) ) break;
210 Fetches the next number from stream and stores in i, returns true
211 on success else false.
214 i - integer to store result in
221 gnum(mbuf *mb, int *i) {
225 if (!skip_spaces(mb)) return 0;
227 if (!(cp = gpeek(mb)))
231 while( (cp = gpeek(mb)) && misnumber(*cp) ) {
232 *i = *i*10+(*cp-'0');
240 read_pgm_ppm_bin8(mbuf *mb, i_img *im, int width, int height,
241 int channels, int maxval, int allow_incomplete) {
242 i_color *line, *linep;
244 unsigned char *read_buf, *readp;
246 int rounder = maxval / 2;
248 line = mymalloc(width * sizeof(i_color));
249 read_size = channels * width;
250 read_buf = mymalloc(read_size);
251 for(y=0;y<height;y++) {
254 if (gread(mb, read_buf, read_size) != read_size) {
257 if (allow_incomplete) {
258 i_tags_setn(&im->tags, "i_incomplete", 1);
259 i_tags_setn(&im->tags, "i_lines_read", y);
263 i_push_error(0, "short read - file truncated?");
269 for(x=0; x<width; x++) {
270 for(ch=0; ch<channels; ch++) {
271 linep->channel[ch] = *readp++;
277 for(x=0; x<width; x++) {
278 for(ch=0; ch<channels; ch++) {
279 /* we just clamp samples to the correct range */
280 unsigned sample = *readp++;
283 linep->channel[ch] = (sample * 255 + rounder) / maxval;
288 i_plin(im, 0, width, y, line);
298 read_pgm_ppm_bin16(mbuf *mb, i_img *im, int width, int height,
299 int channels, int maxval, int allow_incomplete) {
300 i_fcolor *line, *linep;
302 unsigned char *read_buf, *readp;
304 double maxvalf = maxval;
306 line = mymalloc(width * sizeof(i_fcolor));
307 read_size = channels * width * 2;
308 read_buf = mymalloc(read_size);
309 for(y=0;y<height;y++) {
312 if (gread(mb, read_buf, read_size) != read_size) {
315 if (allow_incomplete) {
316 i_tags_setn(&im->tags, "i_incomplete", 1);
317 i_tags_setn(&im->tags, "i_lines_read", y);
321 i_push_error(0, "short read - file truncated?");
326 for(x=0; x<width; x++) {
327 for(ch=0; ch<channels; ch++) {
328 unsigned sample = (readp[0] << 8) + readp[1];
332 linep->channel[ch] = sample / maxvalf;
336 i_plinf(im, 0, width, y, line);
346 read_pbm_bin(mbuf *mb, i_img *im, int width, int height, int allow_incomplete) {
347 i_palidx *line, *linep;
349 unsigned char *read_buf, *readp;
353 line = mymalloc(width * sizeof(i_palidx));
354 read_size = (width + 7) / 8;
355 read_buf = mymalloc(read_size);
356 for(y = 0; y < height; y++) {
357 if (gread(mb, read_buf, read_size) != read_size) {
360 if (allow_incomplete) {
361 i_tags_setn(&im->tags, "i_incomplete", 1);
362 i_tags_setn(&im->tags, "i_lines_read", y);
366 i_push_error(0, "short read - file truncated?");
374 for(x = 0; x < width; ++x) {
375 *linep++ = *readp & mask ? 1 : 0;
382 i_ppal(im, 0, width, y, line);
390 /* unlike pgm/ppm pbm:
391 - doesn't require spaces between samples (bits)
392 - 1 (maxval) is black instead of white
396 read_pbm_ascii(mbuf *mb, i_img *im, int width, int height, int allow_incomplete) {
397 i_palidx *line, *linep;
400 line = mymalloc(width * sizeof(i_palidx));
401 for(y = 0; y < height; y++) {
403 for(x = 0; x < width; ++x) {
406 if (!(cp = gnext(mb)) || (*cp != '0' && *cp != '1')) {
408 if (allow_incomplete) {
409 i_tags_setn(&im->tags, "i_incomplete", 1);
410 i_tags_setn(&im->tags, "i_lines_read", y);
415 i_push_error(0, "invalid data for ascii pnm");
417 i_push_error(0, "short read - file truncated?");
422 *linep++ = *cp == '0' ? 0 : 1;
424 i_ppal(im, 0, width, y, line);
433 read_pgm_ppm_ascii(mbuf *mb, i_img *im, int width, int height, int channels,
434 int maxval, int allow_incomplete) {
435 i_color *line, *linep;
437 int rounder = maxval / 2;
439 line = mymalloc(width * sizeof(i_color));
440 for(y=0;y<height;y++) {
442 for(x=0; x<width; x++) {
443 for(ch=0; ch<channels; ch++) {
446 if (!gnum(mb, &sample)) {
448 if (allow_incomplete) {
449 i_tags_setn(&im->tags, "i_incomplete", 1);
450 i_tags_setn(&im->tags, "i_lines_read", 1);
455 i_push_error(0, "invalid data for ascii pnm");
457 i_push_error(0, "short read - file truncated?");
464 linep->channel[ch] = (sample * 255 + rounder) / maxval;
468 i_plin(im, 0, width, y, line);
477 read_pgm_ppm_ascii_16(mbuf *mb, i_img *im, int width, int height,
478 int channels, int maxval, int allow_incomplete) {
479 i_fcolor *line, *linep;
481 double maxvalf = maxval;
483 line = mymalloc(width * sizeof(i_fcolor));
484 for(y=0;y<height;y++) {
486 for(x=0; x<width; x++) {
487 for(ch=0; ch<channels; ch++) {
490 if (!gnum(mb, &sample)) {
492 if (allow_incomplete) {
493 i_tags_setn(&im->tags, "i_incomplete", 1);
494 i_tags_setn(&im->tags, "i_lines_read", y);
499 i_push_error(0, "invalid data for ascii pnm");
501 i_push_error(0, "short read - file truncated?");
508 linep->channel[ch] = sample / maxvalf;
512 i_plinf(im, 0, width, y, line);
520 =item i_readpnm_wiol(ig, allow_incomplete)
522 Retrieve an image and stores in the iolayer object. Returns NULL on fatal error.
525 allow_incomplete - allows a partial file to be read successfully
532 i_readpnm_wiol(io_glue *ig, int allow_incomplete) {
535 int width, height, maxval, channels, pcount;
541 mm_log((1,"i_readpnm(ig %p, allow_incomplete %d)\n", ig, allow_incomplete));
543 io_glue_commit_types(ig);
548 if (!cp || *cp != 'P') {
549 i_push_error(0, "bad header magic, not a PNM file");
550 mm_log((1, "i_readpnm: Could not read header of file\n"));
554 if ( !(cp = gnext(&buf)) ) {
555 mm_log((1, "i_readpnm: Could not read header of file\n"));
561 if (type < 1 || type > 6) {
562 i_push_error(0, "unknown PNM file type, not a PNM file");
563 mm_log((1, "i_readpnm: Not a pnm file\n"));
567 if ( !(cp = gnext(&buf)) ) {
568 mm_log((1, "i_readpnm: Could not read header of file\n"));
572 if ( !misspace(*cp) ) {
573 i_push_error(0, "unexpected character, not a PNM file");
574 mm_log((1, "i_readpnm: Not a pnm file\n"));
578 mm_log((1, "i_readpnm: image is a %s\n", typenames[type-1] ));
581 /* Read sizes and such */
583 if (!skip_comment(&buf)) {
584 i_push_error(0, "while skipping to width");
585 mm_log((1, "i_readpnm: error reading before width\n"));
589 if (!gnum(&buf, &width)) {
590 i_push_error(0, "could not read image width");
591 mm_log((1, "i_readpnm: error reading width\n"));
595 if (!skip_comment(&buf)) {
596 i_push_error(0, "while skipping to height");
597 mm_log((1, "i_readpnm: error reading before height\n"));
601 if (!gnum(&buf, &height)) {
602 i_push_error(0, "could not read image height");
603 mm_log((1, "i_readpnm: error reading height\n"));
607 if (!(type == 1 || type == 4)) {
608 if (!skip_comment(&buf)) {
609 i_push_error(0, "while skipping to maxval");
610 mm_log((1, "i_readpnm: error reading before maxval\n"));
614 if (!gnum(&buf, &maxval)) {
615 i_push_error(0, "could not read maxval");
616 mm_log((1, "i_readpnm: error reading maxval\n"));
621 i_push_error(0, "maxval is zero - invalid pnm file");
622 mm_log((1, "i_readpnm: maxval is zero, invalid pnm file\n"));
625 else if (maxval > 65535) {
626 i_push_errorf(0, "maxval of %d is over 65535 - invalid pnm file",
628 mm_log((1, "i_readpnm: maxval of %d is over 65535 - invalid pnm file\n"));
632 rounder = maxval / 2;
634 if (!(cp = gnext(&buf)) || !misspace(*cp)) {
635 i_push_error(0, "garbage in header, invalid PNM file");
636 mm_log((1, "i_readpnm: garbage in header\n"));
640 channels = (type == 3 || type == 6) ? 3:1;
641 pcount = width*height*channels;
643 if (!i_int_check_image_file_limits(width, height, channels, sizeof(i_sample_t))) {
644 mm_log((1, "i_readpnm: image size exceeds limits\n"));
648 mm_log((1, "i_readpnm: (%d x %d), channels = %d, maxval = %d\n", width, height, channels, maxval));
650 if (type == 1 || type == 4) {
652 pbm_pal[0].channel[0] = 255;
653 pbm_pal[1].channel[0] = 0;
655 im = i_img_pal_new(width, height, 1, 256);
656 i_addcolors(im, pbm_pal, 2);
660 im = i_img_16_new(width, height, channels);
662 im = i_img_8_new(width, height, channels);
666 case 1: /* Ascii types */
667 im = read_pbm_ascii(&buf, im, width, height, allow_incomplete);
673 im = read_pgm_ppm_ascii_16(&buf, im, width, height, channels, maxval, allow_incomplete);
675 im = read_pgm_ppm_ascii(&buf, im, width, height, channels, maxval, allow_incomplete);
678 case 4: /* binary pbm */
679 im = read_pbm_bin(&buf, im, width, height, allow_incomplete);
682 case 5: /* binary pgm */
683 case 6: /* binary ppm */
685 im = read_pgm_ppm_bin16(&buf, im, width, height, channels, maxval, allow_incomplete);
687 im = read_pgm_ppm_bin8(&buf, im, width, height, channels, maxval, allow_incomplete);
691 mm_log((1, "type %s [P%d] unsupported\n", typenames[type-1], type));
698 i_tags_add(&im->tags, "i_format", 0, "pnm", -1, 0);
699 i_tags_setn(&im->tags, "pnm_maxval", maxval);
700 i_tags_setn(&im->tags, "pnm_type", type);
707 write_pbm(i_img *im, io_glue *ig, int zero_is_white) {
711 unsigned char *write_buf;
712 unsigned char *writep;
716 sprintf(header, "P4\012# CREATOR: Imager\012%d %d\012",
717 im->xsize, im->ysize);
718 if (i_io_write(ig, header, strlen(header)) < 0) {
719 i_push_error(0, "could not write pbm header");
722 write_size = (im->xsize + 7) / 8;
723 line = mymalloc(sizeof(i_palidx) * im->xsize);
724 write_buf = mymalloc(write_size);
725 for (y = 0; y < im->ysize; ++y) {
726 i_gpal(im, 0, im->xsize, y, line);
729 memset(write_buf, 0, write_size);
730 for (x = 0; x < im->xsize; ++x) {
731 if (zero_is_white ? line[x] : !line[x])
739 if (i_io_write(ig, write_buf, write_size) != write_size) {
740 i_push_error(0, "write failure");
754 write_ppm_data_8(i_img *im, io_glue *ig) {
755 int write_size = im->xsize * im->channels;
756 unsigned char *data = mymalloc(write_size);
760 while (y < im->ysize && rc >= 0) {
761 i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
762 if (i_io_write(ig, data, write_size) != write_size) {
763 i_push_error(errno, "could not write ppm data");
776 write_ppm_data_16(i_img *im, io_glue *ig) {
777 int sample_count = im->channels * im->xsize;
778 int write_size = sample_count * 2;
779 int line_size = sample_count * sizeof(i_fsample_t);
780 i_fsample_t *line_buf = mymalloc(line_size);
781 i_fsample_t *samplep;
782 unsigned char *write_buf = mymalloc(write_size);
783 unsigned char *writep;
788 while (y < im->ysize) {
789 i_gsampf(im, 0, im->xsize, y, line_buf, NULL, im->channels);
792 for (sample_num = 0; sample_num < sample_count; ++sample_num) {
793 unsigned sample16 = SampleFTo16(*samplep++);
794 *writep++ = sample16 >> 8;
795 *writep++ = sample16 & 0xFF;
797 if (i_io_write(ig, write_buf, write_size) != write_size) {
798 i_push_error(errno, "could not write ppm data");
811 i_writeppm_wiol(i_img *im, io_glue *ig) {
816 mm_log((1,"i_writeppm(im %p, ig %p)\n", im, ig));
819 /* Add code to get the filename info from the iolayer */
820 /* Also add code to check for mmapped code */
822 io_glue_commit_types(ig);
824 if (i_img_is_monochrome(im, &zero_is_white)) {
825 return write_pbm(im, ig, zero_is_white);
831 if (!i_tags_get_int(&im->tags, "pnm_write_wide_data", 0, &wide_data))
834 if (im->channels == 3) {
837 else if (im->channels == 1) {
841 i_push_error(0, "can only save 1 or 3 channel images to pnm");
842 mm_log((1,"i_writeppm: ppm/pgm is 1 or 3 channel only (current image is %d)\n",im->channels));
845 if (im->bits <= 8 || !wide_data)
850 sprintf(header,"P%d\n#CREATOR: Imager\n%d %d\n%d\n",
851 type, im->xsize, im->ysize, maxval);
853 if (ig->writecb(ig,header,strlen(header)) != strlen(header)) {
854 i_push_error(errno, "could not write ppm header");
855 mm_log((1,"i_writeppm: unable to write ppm header.\n"));
859 if (!im->virtual && im->bits == i_8_bits && im->type == i_direct_type) {
860 if (ig->writecb(ig,im->idata,im->bytes) != im->bytes) {
861 i_push_error(errno, "could not write ppm data");
865 else if (maxval == 255) {
866 if (!write_ppm_data_8(im, ig))
870 if (!write_ppm_data_16(im, ig))
884 Arnar M. Hrafnkelsson <addi@umich.edu>, Tony Cook<tony@imager.perl.org>