about summary refs log tree commit diff
path: root/converter/pgm
diff options
context:
space:
mode:
authorgiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2015-03-29 23:07:03 +0000
committergiraffedata <giraffedata@9d0c8265-081b-0410-96cb-a4ca84ce46f8>2015-03-29 23:07:03 +0000
commitdd2bedb00517b296115cc56a66300194ab9a25d9 (patch)
tree7a68e60481770e1d0dd7d03c229d96d42cd93e13 /converter/pgm
parent04c4c8a3ce3ccb391877c25039c52a1a9cc07bd4 (diff)
downloadnetpbm-mirror-dd2bedb00517b296115cc56a66300194ab9a25d9.tar.gz
netpbm-mirror-dd2bedb00517b296115cc56a66300194ab9a25d9.tar.xz
netpbm-mirror-dd2bedb00517b296115cc56a66300194ab9a25d9.zip
Release 10.70.00
git-svn-id: http://svn.code.sf.net/p/netpbm/code/advanced@2442 9d0c8265-081b-0410-96cb-a4ca84ce46f8
Diffstat (limited to 'converter/pgm')
-rw-r--r--converter/pgm/Makefile4
-rw-r--r--converter/pgm/pgmtosbig.c130
-rw-r--r--converter/pgm/pgmtost4.c104
-rw-r--r--converter/pgm/sbigtopgm.c304
-rw-r--r--converter/pgm/st4topgm.c260
5 files changed, 707 insertions, 95 deletions
diff --git a/converter/pgm/Makefile b/converter/pgm/Makefile
index b109683b..f7ff341e 100644
--- a/converter/pgm/Makefile
+++ b/converter/pgm/Makefile
@@ -8,8 +8,8 @@ VPATH=.:$(SRCDIR)/$(SUBDIR)
 include $(BUILDDIR)/config.mk
 
 PORTBINARIES =	asciitopgm bioradtopgm fstopgm hipstopgm \
-		lispmtopgm pgmtofs pgmtolispm pgmtopgm \
-	        psidtopgm spottopgm sbigtopgm
+		lispmtopgm pgmtofs pgmtolispm pgmtopgm pgmtosbig pgmtost4 \
+	        psidtopgm spottopgm sbigtopgm st4topgm
 MATHBINARIES =	rawtopgm
 BINARIES =	$(PORTBINARIES) $(MATHBINARIES)
 
diff --git a/converter/pgm/pgmtosbig.c b/converter/pgm/pgmtosbig.c
new file mode 100644
index 00000000..0a302dd8
--- /dev/null
+++ b/converter/pgm/pgmtosbig.c
@@ -0,0 +1,130 @@
+/*=============================================================================
+                                 pgmtosbig
+===============================================================================
+
+  This program converts from PGM to a simple subset of SBIG.
+
+  By Bryan Henderson January 19, 2015.
+
+  Contributed to the public domain by its author.
+=============================================================================*/
+#include <string.h>
+
+#include "pm.h"
+#include "nstring.h"
+#include "pgm.h"
+
+
+
+#define SBIG_HEADER_LENGTH  2048      /* File header length */
+
+#define CTLZ "\x1A"
+
+
+struct SbigHeader {
+/*----------------------------------------------------------------------------
+   The information in an SBIG file header.
+
+   This is only the information this program cares about; the header
+   may have much more information in it.
+-----------------------------------------------------------------------------*/
+    unsigned int height;
+    unsigned int width;
+    unsigned int saturationLevel;
+};
+
+
+
+static void
+addUintParm(char *       const buffer,
+            const char * const name,
+            unsigned int const value) {
+
+    const char * line;
+
+    pm_asprintf(&line, "%s=%u\n\r", name, value);
+
+    strcat(buffer, line);
+
+    pm_strfree(line);
+}
+
+
+
+static void
+writeSbigHeader(FILE *            const ofP,
+                struct SbigHeader const hdr) {
+
+    char buffer[SBIG_HEADER_LENGTH];
+
+    memset(&buffer[0], 0x00, sizeof(buffer));
+
+    buffer[0] = '\0';
+
+    /* N.B. LF-CR instead of CRLF.  That's what the spec says. */
+
+    strcat(buffer, "ST-6 Image\n\r" );
+
+    addUintParm(buffer, "Height", hdr.height);
+
+    addUintParm(buffer, "Width", hdr.width);
+
+    addUintParm(buffer, "Sat_level", hdr.saturationLevel);
+
+    strcat(buffer, "End\n\r" CTLZ);
+
+    fwrite(buffer, 1, sizeof(buffer), ofP);
+}
+
+
+
+int
+main(int argc, const char * argv[]) {
+
+    FILE * ifP;
+    gray * grayrow;
+    int rows;
+    int cols;
+    int format;
+    struct SbigHeader hdr;
+    unsigned int row;
+    gray maxval;
+    const char * inputFile;
+
+    pm_proginit(&argc, argv);
+
+    if (argc-1 < 1)
+        inputFile = "-";
+    else {
+        inputFile = argv[1];
+
+        if (argc-1 > 2)
+            pm_error("Too many arguments.  The only argument is the optional "
+                     "input file name");
+    }
+
+    ifP = pm_openr(inputFile);
+
+    pgm_readpgminit(ifP, &cols, &rows, &maxval, &format);
+
+    grayrow = pgm_allocrow(cols);
+
+    hdr.height = rows;
+    hdr.width = cols;
+    hdr.saturationLevel = maxval;
+
+    writeSbigHeader(stdout, hdr);
+
+    for (row = 0; row < rows; ++row) {
+        unsigned int col;
+
+        pgm_readpgmrow(ifP, grayrow, cols, maxval, format);
+
+        for (col = 0; col < cols; ++col)
+            pm_writelittleshort(stdout, grayrow[col]);
+    }
+
+    pm_close(ifP);
+
+    return 0;
+}
diff --git a/converter/pgm/pgmtost4.c b/converter/pgm/pgmtost4.c
new file mode 100644
index 00000000..fa101ac9
--- /dev/null
+++ b/converter/pgm/pgmtost4.c
@@ -0,0 +1,104 @@
+/*=============================================================================
+                                 pgmtost4
+===============================================================================
+
+  This program converts from PGM to a simple subset of SBIG ST-4.
+
+  By Bryan Henderson January 19, 2015.
+
+  Contributed to the public domain by its author.
+=============================================================================*/
+#include <string.h>
+
+#include "pm.h"
+#include "nstring.h"
+#include "pam.h"
+
+
+
+static unsigned int const st4Height = 165;
+static unsigned int const st4Width  = 192;
+static unsigned int const st4Maxval = 255;
+
+
+
+static void
+writeSt4Footer(FILE * const ofP) {
+
+    const char * const comment = "This was created by Pgmtost4";
+    char buffer[192];
+
+    memset(buffer, ' ', sizeof(buffer));  /* initial value */
+
+    buffer[0] = 'v';
+
+    memcpy(&buffer[  0], "v", 1);
+    memcpy(&buffer[  1], comment, strlen(comment));
+    memcpy(&buffer[ 79], "         7", 10);
+    memcpy(&buffer[ 89], "         8", 10);
+    memcpy(&buffer[ 99], "         9", 10);
+    memcpy(&buffer[109], "        10", 10);
+
+    fwrite(buffer, 1, sizeof(buffer), ofP);
+}
+
+
+
+int
+main(int argc, const char * argv[]) {
+
+    FILE * ifP;
+    tuple * tuplerow;
+    struct pam inpam;
+    unsigned int row;
+    const char * inputFile;
+
+    pm_proginit(&argc, argv);
+
+    if (argc-1 < 1)
+        inputFile = "-";
+    else {
+        inputFile = argv[1];
+
+        if (argc-1 > 2)
+            pm_error("Too many arguments.  The only argument is the optional "
+                     "input file name");
+    }
+
+    ifP = pm_openr(inputFile);
+
+    pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type));
+
+    if (inpam.height != st4Height)
+        pm_error("Image is wrong height for ST-4 SBIG: %u pixels.  "
+                 "Must be %u", inpam.height, st4Height);
+
+    if (inpam.width != st4Width)
+        pm_error("Image is wrong width for ST-4 SBIG: %u pixels.  "
+                 "Must be %u", inpam.width, st4Width);
+    
+    /* Really, we should just scale to maxval 255.  There are library routines
+       for that, but we're too lazy even for that, since nobody is really
+       going to use this program.
+    */
+    if (inpam.maxval != st4Maxval)
+        pm_error("Image is wrong maxval for ST-4 SBIG: %u.  "
+                 "Must be %u", (unsigned)inpam.maxval, st4Maxval);
+
+    tuplerow = pnm_allocpamrow(&inpam);
+
+    for (row = 0; row < inpam.height; ++row) {
+        unsigned int col;
+
+        pnm_readpamrow(&inpam, tuplerow);
+
+        for (col = 0; col < inpam.width; ++col)
+            pm_writechar(stdout, (char)tuplerow[col][0]);
+    }
+
+    writeSt4Footer(stdout);
+
+    pm_close(ifP);
+
+    return 0;
+}
diff --git a/converter/pgm/sbigtopgm.c b/converter/pgm/sbigtopgm.c
index c49a4165..2e8b4586 100644
--- a/converter/pgm/sbigtopgm.c
+++ b/converter/pgm/sbigtopgm.c
@@ -1,42 +1,102 @@
 /*
-
     sbigtopgm.c - read a Santa Barbara Instruments Group CCDOPS file
 
-    Note: All SBIG CCD astronomical cameras produce 14 bits or
-	  (the ST-4 and ST-5) or 16 bits (ST-6 and later) per pixel.
-
-		  Copyright (C) 1998 by John Walker
-		       http://www.fourmilab.ch/
+    Note: All SBIG CCD astronomical cameras produce 14 bits
+    (the ST-4 and ST-5) or 16 bits (ST-6 and later) per pixel.
 
     If you find yourself having to add functionality included subsequent
     to the implementation of this program, you can probably find
     documentation of any changes to the SBIG file format on their
     Web site: http://www.sbig.com/
 
+    Copyright (C) 1998 by John Walker
+    http://www.fourmilab.ch/
+
     Permission to use, copy, modify, and distribute this software and
     its documentation for any purpose and without fee is hereby
     granted, provided that the above copyright notice appear in all
     copies and that both that copyright notice and this permission
-    notice appear in supporting documentation.	This software is
+    notice appear in supporting documentation.  This software is
     provided "as is" without express or implied warranty.
-
 */
 
 #include <string.h>
 
-#include "pgm.h"
+#include "pm_c_util.h"
+#include "mallocvar.h"
 #include "nstring.h"
+#include "shhopt.h"
+#include "pm.h"
+#include "pgm.h"
+
+struct CmdlineInfo {
+    /* All the information the user supplied in the command line,
+       in a form easy for the program to use.
+    */
+    const char * inputFileName;
+};
+
+
+
+static void
+parseCommandLine(int argc, const char ** argv,
+                 struct CmdlineInfo * const cmdlineP) {
+/*----------------------------------------------------------------------------
+   Note that the file spec array we return is stored in the storage that
+   was passed to as as the argv array.
+-----------------------------------------------------------------------------*/
+    optEntry * option_def;
+        /* Instructions to pm_optParseOptions3 on how to parse our options.
+         */
+    optStruct3 opt;
+
+    unsigned int option_def_index;
+
+    MALLOCARRAY_NOFAIL(option_def, 100);
+    
+    option_def_index = 0;   /* incremented by OPTENT3 */
+    OPTENTINIT;
+
+    opt.opt_table     = option_def;
+    opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */
+    opt.allowNegNum   = FALSE; /* We have no parms that are negative numbers */
+    
+    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
+        /* Uses and sets argc, argv, and some of *cmdlineP and others */
+
+    if (argc-1 < 1)
+        cmdlineP->inputFileName = "-";
+    else {
+        cmdlineP->inputFileName = argv[1];
+
+        if (argc-1 > 1)
+            pm_error("Too many arguments.  The only possible argument is the "
+                     "optional input file name");
+    }
+}
+
+
 
 #define SBIG_HEADER_LENGTH  2048      /* File header length */
 
-/*  looseCanon	--  Canonicalize a line from the file header so
-    items more sloppily formatted than those
-    written by CCDOPS are still accepted.
-*/
+
 
 static void
 looseCanon(char * const cpArg) {
+/*----------------------------------------------------------------------------
+  Canonicalize a line from the file header so items more sloppily formatted
+  than those written by CCDOPS are still accepted.
+
+  Remove all whitespace and make all letters lowercase.
 
+  Note that the SBIG Type 3 format specification at www.sbig.com in January
+  2015 says header parameter names are capitalized like 'Height'; we change
+  that to "height".
+
+  The spec also says the line ends with LF, then CR (yes, really).  Assuming
+  Caller separates lines at LF, that means we see CR at the beginning of all
+  lines but the first.  We remove that.
+-----------------------------------------------------------------------------*/
     char * cp;
     char * op;
     char c;
@@ -56,43 +116,41 @@ looseCanon(char * const cpArg) {
 
 
 
-int
-main(int argc, char ** argv) {
+struct SbigHeader {
+/*----------------------------------------------------------------------------
+   The information in an SBIG file header.
 
-    FILE * ifP;
-    gray * grayrow;
-    gray * gP;
-    int argn, row;
-    int col;
-    int maxval;
-    int comp, rows, cols;
-    char header[SBIG_HEADER_LENGTH];
-    char * hdr;
-    static char camera[80] = "ST-?";
-
-    pgm_init(&argc, argv);
+   This is only the information this program cares about; the header
+   may have much more information in it.
+-----------------------------------------------------------------------------*/
+    unsigned int rows;
+    unsigned int cols;
+    unsigned int maxval;
+    bool isCompressed;
+    bool haveCameraType;
+    char cameraType[80];
+};
 
-    argn = 1;
 
-    if (argn < argc) {
-        ifP = pm_openr(argv[argn]);
-        argn++;
-    } else
-        ifP = stdin;
 
-    if (argn != argc)
-        pm_usage( "[sbigfile]" );
+static void
+readSbigHeader(FILE *              const ifP,
+               struct SbigHeader * const sbigHeaderP) {
 
-    if (fread(header, SBIG_HEADER_LENGTH, 1, ifP) < 1)
-        pm_error("error reading SBIG file header");
+    size_t rc;
+    bool gotCompression;
+    bool gotWidth;
+    bool gotHeight;
+    char buffer[SBIG_HEADER_LENGTH];
+    char * cursor;
+    bool endOfHeader;
 
-    /*	Walk through the header and parse relevant parameters.	*/
+    rc = fread(buffer, SBIG_HEADER_LENGTH, 1, ifP);
 
-    comp = -1;
-    cols = -1;
-    rows = -1;
+    if (rc < 1)
+        pm_error("error reading SBIG file header");
 
-    /*	The SBIG header specification equivalent to maxval is
+    /*  The SBIG header specification equivalent to maxval is
         "Sat_level", the saturation level of the image.  This
         specification is optional, and was not included in files
         written by early versions of CCDOPS. It was introduced when it
@@ -107,106 +165,166 @@ main(int argc, char ** argv) {
         65535 as the default because the overwhelming majority of
         cameras in use today are 16 bit, and it's possible some
         non-SBIG software may omit the "optional" Sat_level
-        specification.	Also, no harm is done if a larger maxval is
+        specification.  Also, no harm is done if a larger maxval is
         specified than appears in the image--a simple contrast stretch
         will adjust pixels to use the full 0 to maxval range.  The
         converse, pixels having values greater than maxval, results in
         an invalid file which may cause problems in programs which
         attempt to process it.
-	*/
+    */
 
-    maxval = 65535;
+    gotCompression = false;  /* initial value */
+    gotWidth       = false;  /* initial value */
+    gotHeight      = false;  /* initial value */
 
-    hdr = header;
+    sbigHeaderP->maxval = 65535;  /* initial assumption */
+    sbigHeaderP->haveCameraType = false;  /* initial assumption */
 
-    for (;;) {
-        char *cp = strchr(hdr, '\n');
+    for (cursor = &buffer[0], endOfHeader = false; !endOfHeader;) {
+        char * const cp = strchr(cursor, '\n');
 
         if (cp == NULL) {
             pm_error("malformed SBIG file header at character %u",
-                     (unsigned)(hdr - header));
+                     (unsigned)(cursor - &buffer[0]));
         }
         *cp = '\0';
-        if (strncmp(hdr, "ST-", 3) == 0) {
-            char * const ep = strchr(hdr + 3, ' ');
+        if (strneq(cursor, "ST-", 3)) {
+            char * const ep = strchr(cursor + 3, ' ');
 
             if (ep != NULL) {
                 *ep = '\0';
-                strcpy(camera, hdr);
+                strcpy(sbigHeaderP->cameraType, cursor);
+                sbigHeaderP->haveCameraType = true;
                 *ep = ' ';
             }
         }
-        looseCanon(hdr);
-        if (strncmp(hdr, "st-", 3) == 0) {
-            comp = strstr(hdr, "compressed") != NULL;
-        } else if (strncmp(hdr, "height=", 7) == 0) {
-            rows = atoi(hdr + 7);
-        } else if (strncmp(hdr, "width=", 6) == 0) {
-            cols = atoi(hdr + 6);
-        } else if (strncmp(hdr, "sat_level=", 10) == 0) {
-            maxval = atoi(hdr + 10);
-        } else if (streq(hdr, "end")) {
-            break;
+        
+        looseCanon(cursor);
+            /* Convert from standard SBIG to an internal format */
+
+        if (strneq(cursor, "st-", 3)) {
+            sbigHeaderP->isCompressed = (strstr("compressed", cursor) != NULL);
+            gotCompression = true;
+        } else if (strneq(cursor, "height=", 7)) {
+            sbigHeaderP->rows = atoi(cursor + 7);
+            gotHeight = true;
+        } else if (strneq(cursor, "width=", 6)) {
+            sbigHeaderP->cols = atoi(cursor + 6);
+            gotWidth = true;
+        } else if (strneq(cursor, "sat_level=", 10)) {
+            sbigHeaderP->maxval = atoi(cursor + 10);
+        } else if (streq("end", cursor)) {
+            endOfHeader = true;
         }
-        hdr = cp + 1;
+        cursor = cp + 1;
     }
 
-    if (comp == -1 || rows == -1 || cols == -1)
-        pm_error("required specification missing from SBIG file header");
+    if (!gotCompression)
+        pm_error("Required 'ST-*' specification missing "
+                 "from SBIG file header");
+    if (!gotHeight)
+        pm_error("required 'height=' specification missing"
+                 "from SBIG file header");
+    if (!gotWidth)
+        pm_error("required 'width=' specification missing "
+                 "from SBIG file header");
+}
+
 
-    pm_message("SBIG %s %dx%d %s image, saturation level = %d",
-               camera, cols, rows, comp ? "compressed" : "uncompressed",
-               maxval);
 
-    if (maxval > PGM_OVERALLMAXVAL) {
-        pm_error("Saturation level (%d levels) is too large"
-                 "This program's limit is %d.", maxval, PGM_OVERALLMAXVAL);
-    }
+static void
+writeRaster(FILE *            const ifP,
+            struct SbigHeader const hdr,
+            FILE *            const ofP) {
 
-    pgm_writepgminit(stdout, cols, rows, maxval, 0);
-    grayrow = pgm_allocrow(cols);
+    gray * grayrow;
+    unsigned int row;
 
-#define DOSINT(fp) ((getc(fp) & 0xFF) | (getc(fp) << 8))
+    grayrow = pgm_allocrow(hdr.cols);
 
-    for (row = 0; row < rows; ++row) {
-        int compthis;
+    for (row = 0; row < hdr.rows; ++row) {
+        bool compthis;
+        unsigned int col;
 
-        compthis = comp;  /* initial value */
+        if (hdr.isCompressed) {
+            unsigned short rowlen;        /* Compressed row length */
 
-        if (comp) {
-            int const rowlen = DOSINT(ifP); /* Compressed row length */
+            pm_readlittleshortu(ifP, &rowlen);
             
-            /*	If compression results in a row length >= the uncompressed
+            /*  If compression results in a row length >= the uncompressed
                 row length, that row is output uncompressed.  We detect this
                 by observing that the compressed row length is equal to
                 that of an uncompressed row.
             */
 
-            if (rowlen == cols * 2)
-                compthis = 0;
-        }
-        for (col = 0, gP = grayrow; col < cols; ++col, ++gP) {
-            gray g;
+            if (rowlen == hdr.cols * 2)
+                compthis = false;
+            else
+                compthis = hdr.isCompressed;
+        } else
+            compthis = hdr.isCompressed;
+
+        for (col = 0; col < hdr.cols; ++col) {
+            unsigned short g;
 
             if (compthis) {
                 if (col == 0) {
-                    g = DOSINT(ifP);
+                    pm_readlittleshortu(ifP, &g);
                 } else {
-                    int delta = getc(ifP);
+                    int const delta = getc(ifP);
 
                     if (delta == 0x80)
-                        g = DOSINT(ifP);
+                        pm_readlittleshortu(ifP, &g);
                     else
                         g += ((signed char) delta);
                 }
             } else
-                g = DOSINT(ifP);
-            *gP = g;
+                pm_readlittleshortu(ifP, &g);
+            grayrow[col] = g;
         }
-        pgm_writepgmrow(stdout, grayrow, cols, (gray) maxval, 0);
+        pgm_writepgmrow(ofP, grayrow, hdr.cols, hdr.maxval, 0);
     }
+
+    pgm_freerow(grayrow);
+}
+
+
+
+int
+main(int argc, const char ** argv) {
+
+    FILE * ifP;
+    struct CmdlineInfo cmdline;
+    struct SbigHeader hdr;
+
+    pm_proginit(&argc, argv);
+
+    parseCommandLine(argc, argv, &cmdline);
+
+    ifP = pm_openr(cmdline.inputFileName);
+
+    readSbigHeader(ifP, &hdr);
+
+    pm_message("SBIG '%s' %ux%u %s image, saturation level = %u",
+               (hdr.haveCameraType ? hdr.cameraType : "ST-?"),
+               hdr.cols, hdr.rows,
+               hdr.isCompressed ? "compressed" : "uncompressed",
+               hdr.maxval);
+
+    if (hdr.maxval > PGM_OVERALLMAXVAL) {
+        pm_error("Saturation level (%u levels) is too large"
+                 "This program's limit is %u.", hdr.maxval, PGM_OVERALLMAXVAL);
+    }
+
+    pgm_writepgminit(stdout, hdr.cols, hdr.rows, hdr.maxval, 0);
+
+    writeRaster(ifP, hdr, stdout);
+
     pm_close(ifP);
     pm_close(stdout);
 
     return 0;
 }
+
+
+
diff --git a/converter/pgm/st4topgm.c b/converter/pgm/st4topgm.c
new file mode 100644
index 00000000..e763852c
--- /dev/null
+++ b/converter/pgm/st4topgm.c
@@ -0,0 +1,260 @@
+/*=============================================================================
+                               st4topgm
+===============================================================================
+
+  Convert an SBIG ST-4 image (not to be confused with the more sophisticated
+  SBIG format that every other SBIG camera produces) to PGM.
+
+  By Bryan Henderson January 2015.
+
+  Contributed to the public domain by its author.
+
+  This program was intended to substitute for the program of the same name in
+  the Debian version of Netpbm, by Justin Pryzby <justinpryzby@users.sf.net>
+  in December 2003.
+
+=============================================================================*/
+#include <string.h>
+
+#include "pm_config.h"
+#include "pm_c_util.h"
+#include "pm.h"
+#include "pam.h"
+
+
+
+static unsigned int const st4Height = 165;
+static unsigned int const st4Width  = 192;
+static unsigned int const st4Maxval = 255;
+
+
+
+static void
+validateFileSize(FILE * const ifP) {
+/*----------------------------------------------------------------------------
+   Abort program if *ifP is not the proper size for an ST-4 SBIG file.
+
+   Don't change file position.
+-----------------------------------------------------------------------------*/
+    pm_filepos const st4FileSize = (st4Height+1) * st4Width;
+
+    pm_filepos oldFilePos;
+    pm_filepos endFilePos;
+
+    pm_tell2(ifP, &oldFilePos, sizeof(endFilePos));
+
+    fseek(ifP, 0, SEEK_END);
+
+    pm_tell2(ifP, &endFilePos, sizeof(endFilePos));
+
+    pm_seek2(ifP, &oldFilePos, sizeof(oldFilePos));
+
+    if (endFilePos != st4FileSize)
+        pm_error("File is the wrong size for an ST-4 SBIG file.  "
+                 "It is %u bytes; it should be %u bytes",
+                 (unsigned)endFilePos, (unsigned)st4FileSize);
+}
+
+
+static void
+writeRaster(FILE *       const ifP,
+            struct pam * const pamP) {
+
+    tuple * tuplerow;
+    unsigned int row;
+
+    tuplerow = pnm_allocpamrow(pamP);
+
+    for (row = 0; row < st4Height; ++row) {
+        unsigned int col;
+
+        for (col = 0; col < st4Width; ++col) {
+            char c;
+
+            pm_readchar(ifP, &c);
+
+            tuplerow[col][0] = (unsigned char)c;
+        }
+        pnm_writepamrow(pamP, tuplerow);
+    }
+
+    pnm_freepamrow(tuplerow);
+}
+
+
+
+struct St4Footer {
+/*----------------------------------------------------------------------------
+   The information in an ST-4 SBIG footer.
+-----------------------------------------------------------------------------*/
+    /* Note that numerical information is in decimal text, because we're lazy.
+    */
+
+    char comment[78+1];
+    char exposureTime[10+1];
+    char focalLength[10+1];
+    char apertureArea[10+1];
+    char calibrationFactor[10+1];
+};
+
+
+
+static void
+stripTrailing(char * const arg) {
+
+    if (strlen(arg) > 0) {
+        char * p;
+        for (p = arg + strlen(arg); p > arg && *(p-1) == ' '; --p);
+
+        *p = '\0';
+    }
+}
+
+
+
+static void
+stripLeading(char * const arg) {
+
+    const char * p;
+
+    for (p = &arg[0]; *p == ' '; ++p);
+
+    if (p > arg)
+        memmove(arg, p, strlen(p) + 1);
+}
+
+
+
+static void
+readFooter(FILE *             const ifP,
+           struct St4Footer * const footerP) {
+/*----------------------------------------------------------------------------
+   Read the footer of the ST-4 image from *ifP, assuming *ifP is positioned
+   to the footer.
+
+   Return its contents as *footerP.
+-----------------------------------------------------------------------------*/
+    char buffer[192];
+    size_t bytesReadCt;
+
+    /* The footer is laid out as follows.
+
+       off len description
+       --- --- -----------
+       000   1 Signature: 'v'
+       001  78 Freeform comment
+       079  10 Exposure time in 1/100s of a second
+       089  10 Focal length in inches
+       099  10 Aperture area in square inches
+       109  10 Calibration factor
+       119  73 Reserved
+
+       Note tha the footer is the same length as a raster row.
+    */
+
+    bytesReadCt = fread(buffer, 1, sizeof(buffer), ifP);
+
+    if (bytesReadCt != 192)
+        pm_error("Failed to read footer of image");
+
+    if (buffer[0] != 'v')
+        pm_error("Input is not an ST-4 file.  We know because the "
+                 "signature byte (first byte of the footer) is not 'v'");
+
+    buffer[191] = '\0';
+    memmove(footerP->comment, &buffer[1], 78);
+    footerP->comment[78] = '\0';
+    stripTrailing(footerP->comment);
+
+    memmove(footerP->exposureTime, &buffer[79], 10);
+    footerP->exposureTime[10] = '\0';
+    stripLeading(footerP->exposureTime);
+
+    memmove(footerP->focalLength, &buffer[89], 10);
+    footerP->focalLength[10] = '\0';
+    stripLeading(footerP->focalLength);
+
+    memmove(footerP->apertureArea, &buffer[99], 10);
+    footerP->apertureArea[10] = '\0';
+    stripLeading(footerP->apertureArea);
+
+    memmove(footerP->calibrationFactor, &buffer[109], 10);
+    footerP->calibrationFactor[10] = '\0';
+    stripLeading(footerP->calibrationFactor);
+}
+
+
+
+static void
+reportFooter(struct St4Footer const footer) {
+
+	pm_message("Comment:                 %s", footer.comment);
+
+	pm_message("Exposure time (1/100 s): %s", footer.exposureTime);
+
+	pm_message("Focal length (in):       %s", footer.focalLength);
+
+	pm_message("Aperture area (sq in):   %s", footer.apertureArea);
+
+	pm_message("Calibration factor:      %s", footer.calibrationFactor);
+}
+
+
+
+int
+main(int argc, const char **argv) {
+
+    FILE * ifP;
+    const char * inputFileName;
+    struct pam outpam;
+    struct St4Footer footer;
+
+    pm_proginit(&argc, argv);
+
+    if (argc-1 < 1)
+        inputFileName = "'";
+    else {
+        inputFileName = argv[1];
+        if (argc-1 > 1)
+            pm_error("Too many arguments: %u.  "
+                     "The only possible argument is the "
+                     "optional input file name", argc-1);
+    }        
+
+    /* We check the file size to catch the common problem of the input not
+       being valid ST-4 SBIG input.  Unlike most formats, this one does not
+       have any signature at the head of the file.
+
+       More checks on the validity of the format happens when we process
+       the image footer.
+    */
+
+    ifP = pm_openr_seekable(inputFileName);
+
+    validateFileSize(ifP);
+
+    outpam.size = sizeof(outpam);
+    outpam.len = PAM_STRUCT_SIZE(maxval);
+    outpam.file = stdout;
+    outpam.format = PGM_FORMAT;
+    outpam.plainformat = false;
+    outpam.height = st4Height;
+    outpam.width = st4Width;
+    outpam.depth = 1;
+    outpam.maxval = st4Maxval;
+
+    pnm_writepaminit(&outpam);
+
+    writeRaster(ifP, &outpam);
+
+    readFooter(ifP, &footer);
+
+    reportFooter(footer);
+
+    pm_close(ifP);
+    pm_close(stdout);
+
+    return 0;
+}
+
+