home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Graphics / graphics-16000.iso / general / convrtrs / pbmplus / ntpbmsrc.lha / netpbm / pnm / sirtopnm.c < prev    next >
C/C++ Source or Header  |  1993-10-11  |  3KB  |  109 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
  16. main( argc, argv )
  17. int argc;
  18. char* argv[];
  19. {
  20.     FILE *ifp;
  21.     xel *xelrow, *xP;
  22.     unsigned char *sirarray;
  23.     int rows, cols, row, format, picsize, planesize;
  24.     register int col, i;
  25.     short info;
  26.  
  27.     pnm_init( &argc, argv );
  28.  
  29.     if ( argc > 2 )
  30.     pm_usage( "[sirfile]" );
  31.  
  32.     if ( argc == 2 )
  33.     ifp = pm_openr( argv[1] );
  34.     else
  35.     ifp = stdin;
  36.  
  37.     pm_readlittleshort( ifp, &info );
  38.     if ( info != 0x3a4f)
  39.     pm_error( "Input file is not a Solitaire file", 0,0,0,0,0 );
  40.     pm_readlittleshort( ifp, &info );
  41.     pm_readlittleshort( ifp, &info );
  42.     if ( info == 17 )
  43.     {
  44. #ifdef PGM
  45.     format = PGM_TYPE;
  46. #else /*PGM*/
  47.         pm_error( "Must have PGM defined to convert MGI TYPE 17", 0,0,0,0,0 );
  48. #endif /*PGM*/
  49.     }
  50.     else if ( info == 11 )
  51.     {
  52. #ifdef PPM
  53.     format = PPM_TYPE;
  54. #else /*PPM*/
  55.     pm_error( "Must have PPM defined to convert MGI TYPE 11", 0,0,0,0,0 );
  56. #endif /*PPM*/
  57.     }
  58.     else
  59.     pm_error( "Input is not MGI TYPE 11 or MGI TYPE 17", 0,0,0,0,0 );
  60.     pm_readlittleshort( ifp, &info );
  61.     cols = (int) ( info );
  62.     pm_readlittleshort( ifp, &info );
  63.     rows = (int) ( info );
  64.     for ( i = 1; i < 1531; i++ )
  65.     pm_readlittleshort( ifp, &info );
  66.  
  67.     pnm_writepnminit( stdout, cols, rows, 255, format, 0 );
  68.     xelrow = pnm_allocrow( cols );
  69.     switch ( PNM_FORMAT_TYPE(format) )
  70.     {
  71. #ifdef PGM
  72.     case PGM_TYPE:
  73.         pm_message( "Writing a PGM file", 0,0,0,0,0 );
  74.     for ( row = 0; row < rows; ++row )
  75.     {
  76.         for ( col = 0, xP = xelrow; col < cols; col++, xP++ )
  77.         PNM_ASSIGN1( *xP, fgetc( ifp ) );
  78.         pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
  79.     }
  80.     break;
  81. #endif /*PGM*/
  82. #ifdef PPM
  83.     case PPM_TYPE:
  84.     picsize = cols * rows * 3;
  85.     planesize = cols * rows;
  86.         if ( !( sirarray = (unsigned char*) malloc( picsize ) ) )
  87.         pm_error( "Not enough memory to load SIR file", 0,0,0,0,0 );
  88.     if ( fread( sirarray, 1, picsize, ifp ) != picsize )
  89.         pm_error( "Error reading SIR file", 0,0,0,0,0 );
  90.         pm_message( "Writing a PPM file", 0,0,0,0,0 );
  91.         for ( row = 0; row < rows; row++ )
  92.     {
  93.         for ( col = 0, xP = xelrow; col < cols; col++, xP++ )
  94.             PPM_ASSIGN( *xP, sirarray[row*cols+col],
  95.                  sirarray[planesize + (row*cols+col)],
  96.                  sirarray[2*planesize + (row*cols+col)] );
  97.             pnm_writepnmrow( stdout, xelrow, cols, 255, format, 0 );
  98.     }
  99.     break;
  100. #endif /*PPM*/
  101.     default:
  102.     pm_error( "Shouldn't happen", 0,0,0,0,0 );
  103.     }
  104.  
  105.     pm_close( ifp );
  106.  
  107.     exit( 0 );
  108. }
  109.