home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 309.lha / PBM_PLUS / ppm / imgtoppm.c < prev    next >
C/C++ Source or Header  |  1980-12-04  |  3KB  |  133 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 <stdio.h>
  16. #ifdef    SYSV
  17. #include <string.h>
  18. #else    SYSV
  19. #include <strings.h>
  20. #endif    SYSV
  21. #include "ppm.h"
  22.  
  23. main( argc, argv )
  24. int argc;
  25. char *argv[];
  26.     {
  27.     FILE *ifd;
  28.     pixel *pixelrow, colormap[256];
  29.     register pixel *pP;
  30.     int argn, rows, cols, row, i;
  31.     register int col;
  32.     pixval maxval;
  33.     int len, cmaplen, gotAT, gotCM;
  34.     unsigned char buf[4096];
  35.     register unsigned char *bP;
  36.  
  37.     pm_progname = argv[0];
  38.  
  39.     argn = 1;
  40.  
  41.     if ( argn < argc )
  42.     {
  43.     ifd = pm_openr( argv[argn] );
  44.     argn++;
  45.     }
  46.     else
  47.     ifd = stdin;
  48.  
  49.     if ( argn != argc )
  50.     pm_usage( "[imgfile]" );
  51.  
  52.     /* Get signature. */
  53.     fread( buf, 8, 1, ifd );
  54.     buf[8] = '\0';
  55.  
  56.     /* Get entries. */
  57.     gotAT = 0;
  58.     gotCM = 0;
  59.     while ( fread( buf, 2, 1, ifd ) == 1 )
  60.     {
  61.     if ( strncmp( buf, "AT", 2 ) == 0 )
  62.         {
  63.         if ( fread( buf, 8, 1, ifd ) != 1 )
  64.         pm_error( "bad attributes header", 0,0,0,0,0 );
  65.         buf[8] = '\0';
  66.         len = atoi( buf );
  67.         if ( fread( buf, len, 1, ifd ) != 1 )
  68.         pm_error( "bad attributes buf", 0,0,0,0,0 );
  69.         buf[len] = '\0';
  70.         sscanf( buf, "%4u%4u%4u", &cols, &rows, &cmaplen );
  71.         maxval = 255;
  72.         gotAT = 1;
  73.         }
  74.  
  75.     else if ( strncmp( buf, "CM", 2 ) == 0 )
  76.         {
  77.         if ( ! gotAT )
  78.         pm_error( "missing attributes header", 0,0,0,0,0 );
  79.         if ( fread( buf, 8, 1, ifd ) != 1 )
  80.         pm_error( "bad colormap header", 0,0,0,0,0 );
  81.         buf[8] = '\0';
  82.         len = atoi( buf );
  83.         if ( fread( buf, len, 1, ifd ) != 1 )
  84.         pm_error( "bad colormap buf", 0,0,0,0,0 );
  85.         if ( cmaplen * 3 != len )
  86.         {
  87.         pm_message(
  88.             "cmaplen (%d) and colormap buf length (%d) do not match",
  89.             cmaplen, len, 0, 0, 0 );
  90.         if ( cmaplen * 3 < len )
  91.             len = cmaplen * 3;
  92.         else if ( cmaplen * 3 > len )
  93.             cmaplen = len / 3;
  94.         }
  95.         for ( i = 0; i < len; i += 3 )
  96.         PPM_ASSIGN( colormap[i / 3], buf[i], buf[i + 1], buf[i + 2] );
  97.         gotCM = 1;
  98.         }
  99.  
  100.     else if ( strncmp( buf, "PD", 2 ) == 0 )
  101.         {
  102.         if ( fread( buf, 8, 1, ifd ) != 1 )
  103.         pm_error( "bad pixel data header", 0,0,0,0,0 );
  104.         buf[8] = '\0';
  105.         len = atoi( buf );
  106.         if ( len != cols * rows )
  107.         pm_message(
  108.             "pixel data length (%d) does not match image size (%d)",
  109.             len, cols * rows, 0, 0, 0 );
  110.  
  111.         ppm_writeppminit( stdout, cols, rows, maxval );
  112.         pixelrow = ppm_allocrow( cols );
  113.  
  114.         for ( row = 0; row < rows; row++ )
  115.         {
  116.         if ( fread( buf, 1, cols, ifd ) != cols )
  117.             pm_error( "premature EOF", 0,0,0,0,0 );
  118.         for ( col = 0, pP = pixelrow, bP = buf;
  119.               col < cols; col++, pP++, bP++ )
  120.             {
  121.             if ( gotCM )
  122.             *pP = colormap[*bP];
  123.             else
  124.             PPM_ASSIGN( *pP, *bP, *bP, *bP );
  125.             }
  126.         ppm_writeppmrow( stdout, pixelrow, cols, maxval );
  127.         }
  128.         pm_close( ifd );
  129.         exit( 0 );
  130.         }
  131.     }
  132.     }
  133.