about summary refs log tree commit diff
path: root/converter/ppm/qrttoppm.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/ppm/qrttoppm.c')
-rw-r--r--converter/ppm/qrttoppm.c104
1 files changed, 60 insertions, 44 deletions
diff --git a/converter/ppm/qrttoppm.c b/converter/ppm/qrttoppm.c
index 935463e7..48a395e9 100644
--- a/converter/ppm/qrttoppm.c
+++ b/converter/ppm/qrttoppm.c
@@ -1,4 +1,4 @@
-/* qrttoppm.c - read a QRT ray-tracer output file and produce a portable pixmap
+/* qrttoppm.c - read a QRT ray-tracer output file and produce a PPM
 **
 ** Copyright (C) 1989 by Jef Poskanzer.
 **
@@ -10,60 +10,76 @@
 ** implied warranty.
 */
 
+#include "mallocvar.h"
 #include "ppm.h"
 
+
+
 int
-main( argc, argv )
-    int argc;
-    char* argv[];
-    {
-    FILE* ifp;
-    register pixel* pixelrow;
-    int rows, cols, row, col;
-    pixval maxval;
-    unsigned char* buf;
+main(int argc, const char ** argv) {
 
+    FILE * ifP;
+    pixel * pixelrow;
+    unsigned int rows, cols;
+    unsigned int row;
+    pixval maxval;
+    unsigned char * buf;
 
-    ppm_init( &argc, argv );
+    pm_proginit(&argc, argv);
 
-    if ( argc > 2 )
-	pm_usage( "[qrtfile]" );
+    if (argc-1 > 1)
+        pm_error("Too many arguments (%u).  The only possible argument "
+                 "is the input file name", argc-1);
 
-    if ( argc == 2 )
-	ifp = pm_openr( argv[1] );
+    if (argc-1 >= 1)
+        ifP = pm_openr(argv[1]);
     else
-	ifp = stdin;
+        ifP = stdin;
 
     /* Read in the QRT file.  First the header. */
-    cols = getc( ifp );
-    cols += getc( ifp ) << 8;
-    rows = getc( ifp );
-    rows += getc( ifp ) << 8;
+    cols = (unsigned char)getc(ifP);
+    cols += (unsigned char)getc(ifP) << 8;
+    rows = (unsigned char)getc(ifP);
+    rows += (unsigned char)getc(ifP) << 8;
+
+    if (cols <= 0 || rows <= 0)
+        pm_error("Invalid size: %u %u", cols, rows);
 
-    if ( cols <= 0 || rows <= 0 )
-	pm_error( "invalid size: %d %d", cols, rows );
     maxval = 255;
 
-    ppm_writeppminit( stdout, cols, rows, maxval, 0 );
-    pixelrow = ppm_allocrow( cols );
-    buf = (unsigned char *) malloc( 3 * cols );
-    if ( buf == (unsigned char *) 0 )
-	pm_error( "out of memory" );
-
-    for ( row = 0; row < rows; row++ )
-	{
-        (void) getc( ifp );	/* discard */
-        (void) getc( ifp );	/* linenum */
-	if ( fread( buf, 3 * cols, 1, ifp ) != 1 )
-	    pm_error( "EOF / read error" );
-	for ( col = 0; col < cols; col++ )
-	    PPM_ASSIGN(
-		pixelrow[col], buf[col], buf[cols + col], buf[2 * cols + col] );
-	ppm_writeppmrow( stdout, pixelrow, cols, maxval, 0 );
-	}
-
-    pm_close( ifp );
-    pm_close( stdout );
-
-    exit( 0 );
+    ppm_writeppminit(stdout, cols, rows, maxval, 0);
+
+    pixelrow = ppm_allocrow(cols);
+
+    MALLOCARRAY(buf, 3 * cols);
+
+    if (!buf)
+        pm_error("Failed to allocate buffer for %u columns", cols);
+
+    for (row = 0; row < rows; ++row) {
+        unsigned int col;
+
+        getc(ifP); /* discard */
+        getc(ifP); /* linenum */
+
+        if (fread(buf, 3 * cols, 1, ifP) != 1)
+            pm_error("EOF / read error");
+
+        for (col = 0; col < cols; ++col) {
+            PPM_ASSIGN(pixelrow[col],
+                       buf[col], buf[cols + col], buf[2 * cols + col]);
+        }
+        ppm_writeppmrow(stdout, pixelrow, cols, maxval, 0);
     }
+
+    free(buf);
+    ppm_freerow(pixelrow);
+
+    pm_close(ifP);
+    pm_close(stdout);
+
+    exit(0);
+}
+
+
+