home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / qrttoppm.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  2KB  |  70 lines

  1. /* qrttoppm.c - read a QRT ray-tracer output file and produce a portable pixmap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14.  
  15. int
  16. main( argc, argv )
  17.     int argc;
  18.     char* argv[];
  19.     {
  20.     FILE* ifp;
  21.     register pixel* pixelrow;
  22.     int rows, cols, row, col;
  23.     pixval maxval;
  24.     unsigned char* buf;
  25.  
  26.  
  27.     ppm_init( &argc, argv );
  28.  
  29.     if ( argc > 2 )
  30.     pm_usage( "[qrtfile]" );
  31.  
  32.     if ( argc == 2 )
  33.     ifp = pm_openr( argv[1] );
  34.     else
  35.     ifp = stdin;
  36.  
  37.     /* Read in the QRT file.  First the header. */
  38.     cols = getc( ifp );
  39.     cols += getc( ifp ) << 8;
  40.     rows = getc( ifp );
  41.     rows += getc( ifp ) << 8;
  42.  
  43.     if ( cols <= 0 || rows <= 0 )
  44.     pm_error( "invalid size: %d %d", cols, rows );
  45.     maxval = 255;
  46.  
  47.     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  48.     pixelrow = ppm_allocrow( cols );
  49.     buf = (unsigned char *) malloc( 3 * cols );
  50.     if ( buf == (unsigned char *) 0 )
  51.     pm_error( "out of memory" );
  52.  
  53.     for ( row = 0; row < rows; row++ )
  54.     {
  55.         (void) getc( ifp );    /* discard */
  56.         (void) getc( ifp );    /* linenum */
  57.     if ( fread( buf, 3 * cols, 1, ifp ) != 1 )
  58.         pm_error( "EOF / read error" );
  59.     for ( col = 0; col < cols; col++ )
  60.         PPM_ASSIGN(
  61.         pixelrow[col], buf[col], buf[cols + col], buf[2 * cols + col] );
  62.     ppm_writeppmrow( stdout, pixelrow, cols, maxval, 0 );
  63.     }
  64.  
  65.     pm_close( ifp );
  66.     pm_close( stdout );
  67.  
  68.     exit( 0 );
  69.     }
  70.