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

  1. /* mtvtoppm.c - read an MTV 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.     pixel* pixelrow;
  22.     register pixel* pP;
  23.     int rows, cols, row, col;
  24.     pixval maxval;
  25. #define MAXLINE 500
  26.     char line[MAXLINE];
  27.     unsigned char buf[3];
  28.  
  29.  
  30.     ppm_init( &argc, argv );
  31.  
  32.     if ( argc > 2 )
  33.     pm_usage( "[mtvfile]" );
  34.  
  35.     if ( argc == 2 )
  36.     ifp = pm_openr( argv[1] );
  37.     else
  38.     ifp = stdin;
  39.  
  40.     /* Read in the MTV file.  First the header. */
  41.     if ( fgets( line, MAXLINE, ifp ) == NULL )
  42.     pm_error( "unable to read MTV file header" );
  43.     if ( sscanf( line, "%d%d", &cols, &rows ) != 2 )
  44.     pm_error( "unable to parse MTV file header" );
  45.  
  46.     if ( cols <= 0 || rows <= 0 )
  47.     pm_error( "invalid size: %d %d", cols, rows );
  48.     maxval = 255;
  49.  
  50.     ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  51.     pixelrow = ppm_allocrow( cols );
  52.  
  53.     for ( row = 0; row < rows; row++ )
  54.     {
  55.     for ( col = 0, pP = pixelrow; col < cols; col++, pP++ )
  56.         {
  57.         if ( fread( buf, sizeof(buf), 1, ifp ) != 1 )
  58.         pm_error( "EOF / read error" );
  59.         PPM_ASSIGN( *pP, buf[0], buf[1], buf[2] );
  60.         }
  61.     ppm_writeppmrow( stdout, pixelrow, cols, maxval, 0 );
  62.     }
  63.  
  64.     pm_close( ifp );
  65.     pm_close( stdout );
  66.  
  67.     exit( 0 );
  68.     }
  69.