home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / pnm / sirtopnm.c < prev    next >
C/C++ Source or Header  |  1993-12-09  |  3KB  |  96 lines

  1. /* sirtopnm.c - read a Solitaire Image Recorder file and write a portable anymap
  2. **
  3. ** Copyright (C) 1991 by Marvin Landis.
  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 "pnm.h"
  14.  
  15. int main( argc, argv )
  16. int argc;
  17. char* argv[];
  18. {
  19.     FILE *ifp;
  20.     xel *xelrow, *xP;
  21.     unsigned char *sirarray;
  22.     int rows, cols, row, format, picsize, planesize;
  23.     register int col, i;
  24.     short info;
  25.  
  26.     pnm_init( &argc, argv );
  27.  
  28.     if ( argc > 2 )
  29.     pm_usage( "[sirfile]" );
  30.  
  31.     if ( argc == 2 )
  32.     ifp = pm_openr( argv[1] );
  33.     else
  34.     ifp = stdin;
  35.  
  36.     pm_readlittleshort( ifp, &info );
  37.     if ( info != 0x3a4f)
  38.     pm_error( "Input file is not a Solitaire file", 0,0,0,0,0 );
  39.     pm_readlittleshort( ifp, &info );
  40.     pm_readlittleshort( ifp, &info );
  41.     if ( info == 17 )
  42.     {
  43.     format = PGM_TYPE;
  44.     }
  45.     else if ( info == 11 )
  46.     {
  47.     format = PPM_TYPE;
  48.     }
  49.     else
  50.     pm_error( "Input is not MGI TYPE 11 or MGI TYPE 17", 0,0,0,0,0 );
  51.     pm_readlittleshort( ifp, &info );
  52.     cols = (int) ( info );
  53.     pm_readlittleshort( ifp, &info );
  54.     rows = (int) ( info );
  55.     for ( i = 1; i < 1531; i++ )
  56.     pm_readlittleshort( ifp, &info );
  57.  
  58.     pnm_writepnminit( stdout, cols, rows, 255, format, 0 );
  59.     xelrow = pnm_allocrow( cols );
  60.     switch ( PNM_FORMAT_TYPE(format) )
  61.     {
  62.     case PGM_TYPE:
  63.             pm_message( "Writing a PGM file", 0,0,0,0,0 );
  64.         for ( row = 0; row < rows; ++row )
  65.         {
  66.             for ( col = 0, xP = xelrow; col < cols; col++, xP++ )
  67.                 PNM_ASSIGN1( *xP, fgetc( ifp ) );
  68.             pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
  69.         }
  70.         break;
  71.     case PPM_TYPE:
  72.         picsize = cols * rows * 3;
  73.         planesize = cols * rows;
  74.             if ( !( sirarray = (unsigned char*) malloc( picsize ) ) ) 
  75.             pm_error( "Not enough memory to load SIR file", 0,0,0,0,0 );
  76.         if ( fread( sirarray, 1, picsize, ifp ) != picsize )
  77.             pm_error( "Error reading SIR file", 0,0,0,0,0 );
  78.             pm_message( "Writing a PPM file", 0,0,0,0,0 );
  79.             for ( row = 0; row < rows; row++ )
  80.         {
  81.             for ( col = 0, xP = xelrow; col < cols; col++, xP++ )
  82.                 PPM_ASSIGN( *xP, sirarray[row*cols+col],
  83.                  sirarray[planesize + (row*cols+col)],
  84.                  sirarray[2*planesize + (row*cols+col)] );
  85.                 pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
  86.         }
  87.         break;
  88.     default:
  89.         pm_error( "Shouldn't happen", 0,0,0,0,0 );
  90.     }
  91.  
  92.     pm_close( ifp );
  93.  
  94.     exit( 0 );
  95. }
  96.