From ac3f89b460944c4e911544a0d2bed9fa74ee8a89 Mon Sep 17 00:00:00 2001 From: Andrey Kiselev Date: Mon, 5 May 2003 10:40:04 +0000 Subject: [PATCH] PPM header parser improved: now able to skip comments. --- tools/ppm2tiff.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/tools/ppm2tiff.c b/tools/ppm2tiff.c index 481fccde..46e2b266 100644 --- a/tools/ppm2tiff.c +++ b/tools/ppm2tiff.c @@ -1,4 +1,4 @@ -/* $Header: /cvs/maptools/cvsroot/libtiff/tools/ppm2tiff.c,v 1.5 2003-03-12 14:05:06 dron Exp $ */ +/* $Header: /cvs/maptools/cvsroot/libtiff/tools/ppm2tiff.c,v 1.6 2003-05-05 10:40:04 dron Exp $ */ /* * Copyright (c) 1991-1997 Sam Leffler @@ -74,6 +74,10 @@ main(int argc, char* argv[]) extern int optind; extern char* optarg; + if ( argc < 2 ) { + fprintf(stderr, "%s: Too few arguments\n", argv[0]); + usage(); + } while ((c = getopt(argc, argv, "c:r:R:")) != -1) switch (c) { case 'c': /* compression scheme */ @@ -112,9 +116,9 @@ main(int argc, char* argv[]) in = stdin; } - if (getc(in) != 'P') + if (fgetc(in) != 'P') BadPPM(infile); - switch (getc(in)) { + switch (fgetc(in)) { case '5': /* it's a PGM file */ spp = 1; photometric = PHOTOMETRIC_MINISBLACK; @@ -129,9 +133,30 @@ main(int argc, char* argv[]) default: BadPPM(infile); } + + /* Parse header */ + while(1) { + if (feof(in)) + BadPPM(infile); + c = fgetc(in); + /* Skip whitespaces (blanks, TABs, CRs, LFs) */ + if (strchr(" \t\r\n", c)) + continue; + + /* Check fo comment line */ + if (c == '#') { + do { + c = fgetc(in); + } while(!strchr("\r\n", c) || feof(in)); + continue; + } + + ungetc(c, in); + break; + } if (fscanf(in, " %ld %ld %d", &w, &h, &prec) != 3) BadPPM(infile); - if (getc(in) != '\n' || w <= 0 || h <= 0 || prec != 255) + if (fgetc(in) != '\n' || w <= 0 || h <= 0 || prec != 255) BadPPM(infile); out = TIFFOpen(argv[optind], "w"); @@ -220,9 +245,8 @@ char* stuff[] = { "", " -c jpeg[:opts] compress output with JPEG encoding", " -c lzw[:opts] compress output with Lempel-Ziv & Welch encoding", -" (no longer supported by default due to Unisys patent enforcement)", " -c zip[:opts] compress output with deflate encoding", -" -c packbits compress output with packbits encoding", +" -c packbits compress output with packbits encoding (the default)", " -c none use no compression algorithm on output", "", "JPEG options:",