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

  1. /* imgtoppm.c - read an Img-whatnot file and produce a portable pixmap
  2. **
  3. ** Based on a simple conversion program posted to comp.graphics by Ed Falk.
  4. **
  5. ** Copyright (C) 1989 by Jef Poskanzer.
  6. **
  7. ** Permission to use, copy, modify, and distribute this software and its
  8. ** documentation for any purpose and without fee is hereby granted, provided
  9. ** that the above copyright notice appear in all copies and that both that
  10. ** copyright notice and this permission notice appear in supporting
  11. ** documentation.  This software is provided "as is" without express or
  12. ** implied warranty.
  13. */
  14.  
  15. #include "ppm.h"
  16.  
  17. int
  18. main( argc, argv )
  19.     int argc;
  20.     char* argv[];
  21.     {
  22.     FILE* ifp;
  23.     pixel* pixelrow;
  24.     pixel colormap[256];
  25.     register pixel* pP;
  26.     int argn, rows, cols, row, i;
  27.     register int col;
  28.     pixval maxval;
  29.     int len, cmaplen, gotAT, gotCM;
  30.     unsigned char buf[4096];
  31.     register unsigned char* bP;
  32.  
  33.  
  34.     ppm_init( &argc, argv );
  35.  
  36.     argn = 1;
  37.  
  38.     if ( argn < argc )
  39.     {
  40.     ifp = pm_openr( argv[argn] );
  41.     argn++;
  42.     }
  43.     else
  44.     ifp = stdin;
  45.  
  46.     if ( argn != argc )
  47.     pm_usage( "[imgfile]" );
  48.  
  49.     /* Get signature. */
  50.     fread( buf, 8, 1, ifp );
  51.     buf[8] = '\0';
  52.  
  53.     /* Get entries. */
  54.     gotAT = 0;
  55.     gotCM = 0;
  56.     while ( fread( buf, 2, 1, ifp ) == 1 )
  57.     {
  58.     if ( strncmp( (char*) buf, "AT", 2 ) == 0 )
  59.         {
  60.         if ( fread( buf, 8, 1, ifp ) != 1 )
  61.         pm_error( "bad attributes header" );
  62.         buf[8] = '\0';
  63.         len = atoi( buf );
  64.         if ( fread( buf, len, 1, ifp ) != 1 )
  65.         pm_error( "bad attributes buf" );
  66.         buf[len] = '\0';
  67.         sscanf( (char*) buf, "%4u%4u%4u", &cols, &rows, &cmaplen );
  68.         maxval = 255;
  69.         gotAT = 1;
  70.         }
  71.  
  72.     else if ( strncmp( (char*) buf, "CM", 2 ) == 0 )
  73.         {
  74.         if ( ! gotAT )
  75.         pm_error( "missing attributes header" );
  76.         if ( fread( buf, 8, 1, ifp ) != 1 )
  77.         pm_error( "bad colormap header" );
  78.         buf[8] = '\0';
  79.         len = atoi( buf );
  80.         if ( fread( buf, len, 1, ifp ) != 1 )
  81.         pm_error( "bad colormap buf" );
  82.         if ( cmaplen * 3 != len )
  83.         {
  84.         pm_message(
  85.             "cmaplen (%d) and colormap buf length (%d) do not match",
  86.             cmaplen, len, 0, 0, 0 );
  87.         if ( cmaplen * 3 < len )
  88.             len = cmaplen * 3;
  89.         else if ( cmaplen * 3 > len )
  90.             cmaplen = len / 3;
  91.         }
  92.         for ( i = 0; i < len; i += 3 )
  93.         PPM_ASSIGN( colormap[i / 3], buf[i], buf[i + 1], buf[i + 2] );
  94.         gotCM = 1;
  95.         }
  96.  
  97.     else if ( strncmp( (char*) buf, "PD", 2 ) == 0 )
  98.         {
  99.         if ( fread( buf, 8, 1, ifp ) != 1 )
  100.         pm_error( "bad pixel data header" );
  101.         buf[8] = '\0';
  102.         len = atoi( buf );
  103.         if ( len != cols * rows )
  104.         pm_message(
  105.             "pixel data length (%d) does not match image size (%d)",
  106.             len, cols * rows, 0, 0, 0 );
  107.  
  108.         ppm_writeppminit( stdout, cols, rows, maxval, 0 );
  109.         pixelrow = ppm_allocrow( cols );
  110.  
  111.         for ( row = 0; row < rows; row++ )
  112.         {
  113.         if ( fread( buf, 1, cols, ifp ) != cols )
  114.             pm_error( "EOF / read error" );
  115.         for ( col = 0, pP = pixelrow, bP = buf;
  116.               col < cols; col++, pP++, bP++ )
  117.             {
  118.             if ( gotCM )
  119.             *pP = colormap[*bP];
  120.             else
  121.             PPM_ASSIGN( *pP, *bP, *bP, *bP );
  122.             }
  123.         ppm_writeppmrow( stdout, pixelrow, cols, maxval, 0 );
  124.         }
  125.         pm_close( ifp );
  126.         pm_close( stdout );
  127.         exit( 0 );
  128.         }
  129.     }
  130.     }
  131.